forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

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

Changed files
+83 -3
appview
pages
templates
state
+12
appview/pages/pages.go
···
Tags []*types.TagReference
Base string
Head string
+
AllowPull bool
Active string
}
···
func (p *Pages) RepoCompare(w io.Writer, params RepoCompareParams) error {
params.Active = "overview"
return p.executeRepo("repo/compare", w, params)
+
}
+
+
type RepoCompareAllowPullParams struct {
+
LoggedInUser *oauth.User
+
RepoInfo repoinfo.RepoInfo
+
Base string
+
Head string
+
}
+
+
func (p *Pages) RepoCompareAllowPullFragment(w io.Writer, params RepoCompareAllowPullParams) error {
+
return p.executePlain("repo/fragments/compareAllowPull", w, params)
}
type RepoCompareDiffParams struct {
+18 -2
appview/pages/templates/repo/compare.html
···
-
{{ define "title" }}new comparison{{ end }}
+
{{ define "title" }}
+
{{ if and .Head .Base }}
+
comparing {{ .Base }} and
+
{{ .Head }}
+
{{ else }}
+
new comparison
+
{{ end }}
+
{{ end }}
{{ define "repoContent" }}
<section>
···
</div>
</form>
</section>
-
<section class="hidden"></section>
<script>
var templatedBase = `{{ .Base }}`;
···
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'},
+
)
+
}
}
}
</script>
+
<section id="allow-pull" class="pt-4"></section>
{{ end }}
{{ define "repoAfter" }}
+24
appview/pages/templates/repo/fragments/compareAllowPull.html
···
+
{{ define "repo/fragments/compareAllowPull" }}
+
<div class="flex items-baseline justify-normal gap-4">
+
<p>
+
This comparison can be turned into a pull request to be reviewed and
+
discussed.
+
</p>
+
+
{{ $newPullUrl := printf "/%s/pulls/new?strategy=branch&targetBranch=%s&sourceBranch=%s" .RepoInfo.FullName .Base .Head }}
+
+
+
<div class="flex justify-start items-center gap-2 mt-2">
+
<a
+
href="{{ $newPullUrl }}"
+
class="btn flex items-center gap-2 no-underline hover:no-underline"
+
>
+
{{ i "git-pull-request-create" "w-4 h-4" }}
+
create pull
+
<span id="create-pull-spinner" class="group">
+
{{ i "loader-circle" "w-4 h-4 animate-spin hidden group-[.htmx-request]:inline" }}
+
</span>
+
</a>
+
</div>
+
</div>
+
{{ end }}
+27
appview/state/repo.go
···
+
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
+
}
+
}
+
s.pages.RepoCompare(w, pages.RepoCompareParams{
LoggedInUser: user,
RepoInfo: f.RepoInfo(s, user),
···
Tags: tags.Tags,
Base: base,
Head: head,
+
AllowPull: allowPull,
+
})
+
+
}
+
+
func (s *State) RepoCompareAllowPullFragment(w http.ResponseWriter, r *http.Request) {
+
user := s.oauth.GetUser(r)
+
f, err := s.fullyResolvedRepo(r)
+
if err != nil {
+
log.Println("failed to get repo and knot", err)
+
return
+
}
+
+
s.pages.RepoCompareAllowPullFragment(w, pages.RepoCompareAllowPullParams{
+
Head: chi.URLParam(r, "head"),
+
Base: chi.URLParam(r, "base"),
+
RepoInfo: f.RepoInfo(s, user),
+
LoggedInUser: user,
})
+2 -1
appview/state/router.go
···
// /compare/master...some/feature
// /compare/master...example.com:another/feature <- this is a fork
r.Get("/{base}/{head}", s.RepoCompare)
+
r.Get("/diff/{base}/{head}", s.RepoCompareDiffFragment)
+
r.Get("/allow-pull/{base}/{head}", s.RepoCompareAllowPullFragment)
r.Get("/*", s.RepoCompare)
-
r.Get("/diff/{base}/{head}", s.RepoCompareDiffFragment)
})
r.Route("/pulls", func(r chi.Router) {