Monorepo for Tangled tangled.org

appview: repo/compare: conditionally show a create pull button

anirudh.fi c200b4ec c83c2259

verified
Changed files
+83 -3
appview
pages
templates
state
+12
appview/pages/pages.go
··· 865 865 Tags []*types.TagReference 866 866 Base string 867 867 Head string 868 + AllowPull bool 868 869 869 870 Active string 870 871 } ··· 872 873 func (p *Pages) RepoCompare(w io.Writer, params RepoCompareParams) error { 873 874 params.Active = "overview" 874 875 return p.executeRepo("repo/compare", w, params) 876 + } 877 + 878 + type RepoCompareAllowPullParams struct { 879 + LoggedInUser *oauth.User 880 + RepoInfo repoinfo.RepoInfo 881 + Base string 882 + Head string 883 + } 884 + 885 + func (p *Pages) RepoCompareAllowPullFragment(w io.Writer, params RepoCompareAllowPullParams) error { 886 + return p.executePlain("repo/fragments/compareAllowPull", w, params) 875 887 } 876 888 877 889 type RepoCompareDiffParams struct {
+18 -2
appview/pages/templates/repo/compare.html
··· 1 - {{ define "title" }}new comparison{{ end }} 1 + {{ define "title" }} 2 + {{ if and .Head .Base }} 3 + comparing {{ .Base }} and 4 + {{ .Head }} 5 + {{ else }} 6 + new comparison 7 + {{ end }} 8 + {{ end }} 2 9 3 10 {{ define "repoContent" }} 4 11 <section> ··· 104 111 </div> 105 112 </form> 106 113 </section> 107 - <section class="hidden"></section> 108 114 109 115 <script> 110 116 var templatedBase = `{{ .Base }}`; ··· 147 153 if (baseToUse && headToUse) { 148 154 const url = `/{{ .RepoInfo.FullName }}/compare/diff/${baseToUse}/${headToUse}`; 149 155 htmx.ajax('GET', url, { target: '#compare-diff' }); 156 + document.title = `comparing ${baseToUse} and ${headToUse}`; 157 + 158 + const allowPull = `{{ .AllowPull }}` 159 + if (allowPull) { 160 + htmx.ajax('GET', 161 + `/{{ .RepoInfo.FullName }}/compare/allow-pull/${baseToUse}/${headToUse}`, 162 + { target: '#allow-pull'}, 163 + ) 164 + } 150 165 } 151 166 } 152 167 </script> 168 + <section id="allow-pull" class="pt-4"></section> 153 169 {{ end }} 154 170 155 171 {{ define "repoAfter" }}
+24
appview/pages/templates/repo/fragments/compareAllowPull.html
··· 1 + {{ define "repo/fragments/compareAllowPull" }} 2 + <div class="flex items-baseline justify-normal gap-4"> 3 + <p> 4 + This comparison can be turned into a pull request to be reviewed and 5 + discussed. 6 + </p> 7 + 8 + {{ $newPullUrl := printf "/%s/pulls/new?strategy=branch&targetBranch=%s&sourceBranch=%s" .RepoInfo.FullName .Base .Head }} 9 + 10 + 11 + <div class="flex justify-start items-center gap-2 mt-2"> 12 + <a 13 + href="{{ $newPullUrl }}" 14 + class="btn flex items-center gap-2 no-underline hover:no-underline" 15 + > 16 + {{ i "git-pull-request-create" "w-4 h-4" }} 17 + create pull 18 + <span id="create-pull-spinner" class="group"> 19 + {{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }} 20 + </span> 21 + </a> 22 + </div> 23 + </div> 24 + {{ end }}
+27
appview/state/repo.go
··· 2110 2110 } 2111 2111 } 2112 2112 2113 + var allowPull bool = false 2114 + if user != nil { 2115 + if slices.ContainsFunc(branches.Branches, func(branch types.Branch) bool { 2116 + return branch.Name == head || branch.Name == base 2117 + }) { 2118 + allowPull = true 2119 + } 2120 + } 2121 + 2113 2122 s.pages.RepoCompare(w, pages.RepoCompareParams{ 2114 2123 LoggedInUser: user, 2115 2124 RepoInfo: f.RepoInfo(s, user), ··· 2118 2127 Tags: tags.Tags, 2119 2128 Base: base, 2120 2129 Head: head, 2130 + AllowPull: allowPull, 2131 + }) 2132 + 2133 + } 2134 + 2135 + func (s *State) RepoCompareAllowPullFragment(w http.ResponseWriter, r *http.Request) { 2136 + user := s.oauth.GetUser(r) 2137 + f, err := s.fullyResolvedRepo(r) 2138 + if err != nil { 2139 + log.Println("failed to get repo and knot", err) 2140 + return 2141 + } 2142 + 2143 + s.pages.RepoCompareAllowPullFragment(w, pages.RepoCompareAllowPullParams{ 2144 + Head: chi.URLParam(r, "head"), 2145 + Base: chi.URLParam(r, "base"), 2146 + RepoInfo: f.RepoInfo(s, user), 2147 + LoggedInUser: user, 2121 2148 }) 2122 2149 } 2123 2150
+2 -1
appview/state/router.go
··· 127 127 // /compare/master...some/feature 128 128 // /compare/master...example.com:another/feature <- this is a fork 129 129 r.Get("/{base}/{head}", s.RepoCompare) 130 + r.Get("/diff/{base}/{head}", s.RepoCompareDiffFragment) 131 + r.Get("/allow-pull/{base}/{head}", s.RepoCompareAllowPullFragment) 130 132 r.Get("/*", s.RepoCompare) 131 - r.Get("/diff/{base}/{head}", s.RepoCompareDiffFragment) 132 133 }) 133 134 134 135 r.Route("/pulls", func(r chi.Router) {