Phase 6 part 5, step 3: theme updates by pull request
The theme carries a MANIFEST of its files' SHA-256s. A site's Theme card shows its copy's version against Supermarket's and any files changed by hand; Update the theme opens a pull request that writes new and changed files, removes retired ones, rebuilds, and lists what it replaces. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,9 +14,11 @@ import (
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/hugorepo"
|
||||
"supermarket/internal/hugorepo/hugotest"
|
||||
"supermarket/internal/profile"
|
||||
"supermarket/internal/secrets"
|
||||
"supermarket/internal/sites"
|
||||
"supermarket/internal/theme"
|
||||
)
|
||||
|
||||
func TestNewSite(t *testing.T) {
|
||||
@@ -127,3 +129,80 @@ func TestNewSite(t *testing.T) {
|
||||
t.Errorf("build check: %s", opened[0]["body"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestThemeUpdate(t *testing.T) {
|
||||
if hugorepo.HugoVersion(context.Background()) == "" {
|
||||
t.Skip("no hugo")
|
||||
}
|
||||
// A site with an older copy of the theme: version 0, a file the theme
|
||||
// no longer has, and the header changed by hand.
|
||||
files := map[string]string{"hugo.toml": "baseURL = 'https://acme.example/'\ntitle = 'Acme'\ntheme = 'supermarket'\ndisableKinds = ['taxonomy', 'term', 'RSS', 'sitemap']\n",
|
||||
"content/_index.md": "---\ntitle: Home\nsections:\n - type: hero\n title: Hi\n---\n"}
|
||||
old := theme.Files()
|
||||
old["VERSION"] = "0\n"
|
||||
old["layouts/partials/retired.html"] = "<p>old</p>"
|
||||
delete(old, "layouts/404.html")
|
||||
old["MANIFEST"] = theme.Manifest(old)
|
||||
old["layouts/partials/header.html"] += "<!-- changed by hand -->"
|
||||
for p, b := range old {
|
||||
files[theme.Dir+"/"+p] = b
|
||||
}
|
||||
clone := hugotest.Site(t, files)
|
||||
var opened []map[string]string
|
||||
g := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.URL.Path == "/api/v1/repos/Us/acme":
|
||||
fmt.Fprintf(w, `{"full_name":"Us/acme","default_branch":"main","clone_url":%q,"permissions":{"push":true}}`, clone)
|
||||
case r.URL.Path == "/api/v1/repos/Us/acme/pulls" && r.Method == http.MethodPost:
|
||||
var body map[string]string
|
||||
json.NewDecoder(r.Body).Decode(&body)
|
||||
opened = append(opened, body)
|
||||
fmt.Fprint(w, `{"number":4,"state":"open","html_url":"https://gitea.example/pulls/4"}`)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer g.Close()
|
||||
s, h := newTestServer(t, nil)
|
||||
profile.Save(s.DB, &profile.Profile{Name: "Acme", Websites: []profile.Website{{URL: "https://acme.example", Repo: "Us/acme"}}})
|
||||
postForm(h, "/settings/search", url.Values{"gitea_url": {g.URL}, secrets.GiteaToken: {"tok"}})
|
||||
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
|
||||
|
||||
body := do(h, http.MethodGet, "/sites", nil, nil).Body.String()
|
||||
if !strings.Contains(body, "theme version 0; Supermarket has version 1") || !strings.Contains(body, "Changed by hand in the site's copy of the theme: layouts/partials/header.html.") {
|
||||
t.Errorf("before: %s", body)
|
||||
}
|
||||
if rec := postForm(h, "/sites/theme/update", url.Values{"site": {"0"}}); rec.Code != http.StatusSeeOther {
|
||||
t.Fatalf("update: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if len(opened) != 1 {
|
||||
t.Fatalf("pull requests: %v", opened)
|
||||
}
|
||||
pr := opened[0]
|
||||
for _, want := range []string{"from version 0 to 1", "**Changed:**\n- `layouts/partials/header.html`", "**Added:**\n- `layouts/404.html`",
|
||||
"**Removed:**\n- `layouts/partials/retired.html`", "replaced by this update", "Checked with a build"} {
|
||||
if !strings.Contains(pr["body"], want) {
|
||||
t.Errorf("pull request lacks %q:\n%s", want, pr["body"])
|
||||
}
|
||||
}
|
||||
if pr["head"] != "supermarket/theme-1" || pr["title"] != "Sites: update the theme to version 1" {
|
||||
t.Errorf("pull request: %v", pr)
|
||||
}
|
||||
bare := strings.TrimPrefix(clone, "file://")
|
||||
if out, _ := exec.Command("git", "--git-dir", bare, "show", "supermarket/theme-1:"+theme.Dir+"/VERSION").CombinedOutput(); string(out) != "1\n" {
|
||||
t.Errorf("VERSION on the branch: %q", out)
|
||||
}
|
||||
if out, err := exec.Command("git", "--git-dir", bare, "show", "supermarket/theme-1:"+theme.Dir+"/layouts/partials/retired.html").CombinedOutput(); err == nil {
|
||||
t.Errorf("the retired file is still there: %s", out)
|
||||
}
|
||||
// Once merged, the site is up to date; updating again is refused.
|
||||
exec.Command("git", "--git-dir", bare, "update-ref", "refs/heads/main", "refs/heads/supermarket/theme-1").Run()
|
||||
postForm(h, "/sites/sync", url.Values{"site": {"0"}})
|
||||
body = do(h, http.MethodGet, "/sites", nil, nil).Body.String()
|
||||
if !strings.Contains(body, "version 1: the latest") || strings.Contains(body, "Changed by hand") || !strings.Contains(body, "https://gitea.example/pulls/4") {
|
||||
t.Errorf("after: %s", body)
|
||||
}
|
||||
if body := postForm(h, "/sites/theme/update", url.Values{"site": {"0"}}).Body.String(); !strings.Contains(body, "already has the theme's version 1") {
|
||||
t.Errorf("again: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,6 +186,7 @@ func (s *Server) Handler() (http.Handler, error) {
|
||||
mux.HandleFunc("GET /sites/pages/{id}", s.sitePagePage)
|
||||
mux.HandleFunc("POST /sites/pages/{id}/{action}", s.sitePageAction)
|
||||
mux.HandleFunc("GET /sites/publishing", s.sitesPublishingPage)
|
||||
mux.HandleFunc("POST /sites/theme/update", s.sitesThemeUpdate)
|
||||
mux.HandleFunc("GET /sites/new", s.newSitePage)
|
||||
mux.HandleFunc("POST /sites/new", s.newSiteCreate)
|
||||
mux.HandleFunc("GET /sites/forms", s.sitesFormsPage)
|
||||
|
||||
+39
-3
@@ -15,7 +15,6 @@ import (
|
||||
"supermarket/internal/hugorepo"
|
||||
"supermarket/internal/profile"
|
||||
"supermarket/internal/sites"
|
||||
"supermarket/internal/theme"
|
||||
)
|
||||
|
||||
// siteRepo is a website with a Hugo repository, from the profile.
|
||||
@@ -101,8 +100,12 @@ func (s *Server) renderSites(w http.ResponseWriter, r *http.Request, errMsg stri
|
||||
cancel()
|
||||
}
|
||||
wc.Release()
|
||||
if raw, rerr := os.ReadFile(filepath.Join(wc.Dir, theme.Dir, "VERSION")); rerr == nil {
|
||||
data["Theme"] = strings.TrimSpace(string(raw))
|
||||
if ts := sites.ReadTheme(wc.Dir); ts.Has {
|
||||
data["Theme"] = ts
|
||||
var pr formsPR
|
||||
if ok, _ := s.DB.GetJSON(themePRKey(cur.Repo), &pr); ok {
|
||||
data["ThemePR"] = pr
|
||||
}
|
||||
data["Kinds"] = []string{"home", "about", "contact"}
|
||||
if p, _ := profile.Load(s.DB); p != nil {
|
||||
data["Products"] = p.Products
|
||||
@@ -399,3 +402,36 @@ func (n noListing) Open(name string) (http.File, error) {
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func themePRKey(repo string) string { return "sites:theme-pr:" + repo }
|
||||
|
||||
// sitesThemeUpdate opens the pull request that brings a site's copy of the
|
||||
// theme up to date.
|
||||
func (s *Server) sitesThemeUpdate(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, cur := s.currentSite(r)
|
||||
if cur == nil || cur.Repo == "" {
|
||||
s.renderSites(w, r, "Choose a website with a Hugo repository.")
|
||||
return
|
||||
}
|
||||
if s.gitea() == nil {
|
||||
s.renderSites(w, r, "Save a Gitea token under Settings → Search engines first.")
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
||||
defer cancel()
|
||||
pull, d, err := sites.UpdateTheme(ctx, s.applier(seoSite{Index: cur.Index, URL: cur.URL}))
|
||||
if err != nil {
|
||||
s.renderSites(w, r, err.Error())
|
||||
return
|
||||
}
|
||||
pr := formsPR{Number: pull.Number, URL: pull.HTMLURL, At: time.Now(), State: "open", Capture: strconv.Itoa(d.To)}
|
||||
if err := s.DB.PutJSON(themePRKey(cur.Repo), pr); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/sites?site="+strconv.Itoa(cur.Index), http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@@ -26,10 +26,21 @@
|
||||
{{end}}
|
||||
|
||||
{{with .Catalog}}
|
||||
{{if $d.Theme}}
|
||||
{{with $d.Theme}}
|
||||
<section class="card">
|
||||
<h2>The theme</h2>
|
||||
{{if .Outdated}}
|
||||
<p>This site has Supermarket's theme version {{.Version}}; Supermarket has version {{.Latest}}.</p>
|
||||
<form method="post" action="/sites/theme/update"><input type="hidden" name="site" value="{{$d.Current.Index}}">
|
||||
<button type="submit" data-busy="Opening…" {{if not $d.Gitea}}disabled{{end}}>Update the theme</button>
|
||||
<span class="small muted">Opens a pull request that replaces the site's copy, checked with a build.</span></form>
|
||||
{{else}}<p class="ok">This site has Supermarket's theme, version {{.Version}}: the latest.</p>{{end}}
|
||||
{{with .Edited}}<p class="warn-text small">Changed by hand in the site's copy of the theme: {{join . ", "}}. An update replaces them; such changes belong in the site's own <code>layouts/</code> or <code>static/</code>, which win over the theme.</p>{{end}}
|
||||
{{with $d.ThemePR}}<p class="small">Last update: <a href="{{.URL}}" target="_blank" rel="noopener noreferrer">the pull request ↗</a>, {{ago .At}}.</p>{{end}}
|
||||
</section>
|
||||
<section class="card">
|
||||
<h2>First pages</h2>
|
||||
<p>This site uses Supermarket's theme (version {{$d.Theme}}). Build its pages: each is written from your profile, opens for you to edit and preview, and goes to the site as a pull request.</p>
|
||||
<p>This site uses Supermarket's theme. Build its pages: each is written from your profile, opens for you to edit and preview, and goes to the site as a pull request.</p>
|
||||
<div class="row-actions">
|
||||
{{range $d.Kinds}}
|
||||
<form method="post" action="/sites/pages"><input type="hidden" name="site" value="{{$d.Current.Index}}"><input type="hidden" name="kind" value="{{.}}">
|
||||
|
||||
Reference in New Issue
Block a user