From 233dc04c72ce81ba2ef93349c922ea3e3e0c3fee Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Fri, 23 May 2025 00:26:07 +0300 Subject: [PATCH] appview: pages: make allow-pull update as oob-swap Signed-off-by: Anirudh Oppiliappan --- appview/pages/pages.go | 1 - appview/pages/templates/repo/compare.html | 15 ++----- .../repo/fragments/compareAllowPull.html | 6 ++- appview/pages/templates/repo/index.html | 11 ++++- appview/state/repo.go | 41 ++++++++++++++----- 5 files changed, 49 insertions(+), 25 deletions(-) diff --git a/appview/pages/pages.go b/appview/pages/pages.go index bc6aeeba..a2dfef70 100644 --- a/appview/pages/pages.go +++ b/appview/pages/pages.go @@ -871,7 +871,6 @@ type RepoCompareParams struct { Tags []*types.TagReference Base string Head string - AllowPull bool Active string } diff --git a/appview/pages/templates/repo/compare.html b/appview/pages/templates/repo/compare.html index e6e1e30e..6621b1a2 100644 --- a/appview/pages/templates/repo/compare.html +++ b/appview/pages/templates/repo/compare.html @@ -8,7 +8,7 @@ {{ end }} {{ define "repoContent" }} -
+

Compare changes

@@ -152,22 +152,15 @@ if (baseToUse && headToUse) { const url = `/{{ .RepoInfo.FullName }}/compare/diff/${baseToUse}/${headToUse}`; - htmx.ajax('GET', url, { target: '#compare-diff' }); - document.title = `comparing ${baseToUse} and ${headToUse}`; - const allowPull = `{{ .AllowPull }}` - if (allowPull) { - htmx.ajax('GET', - `/{{ .RepoInfo.FullName }}/compare/allow-pull/${baseToUse}/${headToUse}`, - { target: '#allow-pull'}, - ) - } + // htmx.ajax('GET', url, { target: '#compare-diff' }) + document.title = `comparing ${baseToUse} and ${headToUse}`; } } -
{{ end }} {{ define "repoAfter" }} +
{{ end }} diff --git a/appview/pages/templates/repo/fragments/compareAllowPull.html b/appview/pages/templates/repo/fragments/compareAllowPull.html index ef3f1df7..870a3a9e 100644 --- a/appview/pages/templates/repo/fragments/compareAllowPull.html +++ b/appview/pages/templates/repo/fragments/compareAllowPull.html @@ -1,5 +1,9 @@ {{ define "repo/fragments/compareAllowPull" }} -
+

This comparison can be turned into a pull request to be reviewed and discussed. diff --git a/appview/pages/templates/repo/index.html b/appview/pages/templates/repo/index.html index ff5403a6..02e4cb6f 100644 --- a/appview/pages/templates/repo/index.html +++ b/appview/pages/templates/repo/index.html @@ -66,6 +66,7 @@ {{ end }} +

{{ $isOwner := and .LoggedInUser .RepoInfo.Roles.IsOwner }} {{ $isCollaborator := and .LoggedInUser .RepoInfo.Roles.IsCollaborator }} {{ if and (or $isOwner $isCollaborator) .ForkInfo .ForkInfo.IsFork }} @@ -99,7 +100,15 @@ sync {{ end }} -
+ + {{ i "git-compare" "w-4 h-4" }} + +
+
{{ end }} {{ define "fileTree" }} diff --git a/appview/state/repo.go b/appview/state/repo.go index 5274d61e..fa77b512 100644 --- a/appview/state/repo.go +++ b/appview/state/repo.go @@ -2110,24 +2110,16 @@ func (s *State) RepoCompare(w http.ResponseWriter, r *http.Request) { } } - var allowPull bool = false - if user != nil { - if slices.ContainsFunc(branches.Branches, func(branch types.Branch) bool { - return branch.Name == head || branch.Name == base - }) { - allowPull = true - } - } + repoinfo := f.RepoInfo(s, user) s.pages.RepoCompare(w, pages.RepoCompareParams{ LoggedInUser: user, - RepoInfo: f.RepoInfo(s, user), + RepoInfo: repoinfo, Forks: forks, Branches: branches.Branches, Tags: tags.Tags, Base: base, Head: head, - AllowPull: allowPull, }) } @@ -2179,10 +2171,37 @@ func (s *State) RepoCompareDiffFragment(w http.ResponseWriter, r *http.Request) } diff := patchutil.AsNiceDiff(formatPatch.Patch, base) + branches, err := us.Branches(f.OwnerDid(), f.RepoName) + if err != nil { + s.pages.Notice(w, "compare-error", "Failed to produce comparison. Try again later.") + log.Println("failed to fetch branches", err) + return + } + + repoinfo := f.RepoInfo(s, user) + w.Header().Add("Hx-Push-Url", fmt.Sprintf("/%s/compare/%s...%s", f.OwnerSlashRepo(), base, head)) + w.Header().Add("Content-Type", "text/html") s.pages.RepoCompareDiff(w, pages.RepoCompareDiffParams{ LoggedInUser: user, - RepoInfo: f.RepoInfo(s, user), + RepoInfo: repoinfo, Diff: diff, }) + + // checks if pull is allowed and performs an htmx oob-swap + // by writing to the same http.ResponseWriter + if user != nil { + if slices.ContainsFunc(branches.Branches, func(branch types.Branch) bool { + return branch.Name == head || branch.Name == base + }) { + if repoinfo.Roles.IsPushAllowed() { + s.pages.RepoCompareAllowPullFragment(w, pages.RepoCompareAllowPullParams{ + LoggedInUser: user, + RepoInfo: repoinfo, + Base: base, + Head: head, + }) + } + } + } } -- 2.51.2