forked from tangled.org/core
this repo has no description

improve branch-pr ux

Changed files
+149 -51
appview
+17
appview/pages/pages.go
···
return p.execute("repo/pulls/patch", w, params)
}
+
type PullPatchUploadParams struct {
+
RepoInfo RepoInfo
+
}
+
+
func (p *Pages) PullPatchUploadFragment(w io.Writer, params PullPatchUploadParams) error {
+
return p.executePlain("fragments/pullPatchUpload", w, params)
+
}
+
+
type PullCompareBranchesParams struct {
+
RepoInfo RepoInfo
+
Branches []types.Branch
+
}
+
+
func (p *Pages) PullCompareBranchesFragment(w io.Writer, params PullCompareBranchesParams) error {
+
return p.executePlain("fragments/pullCompareBranches", w, params)
+
}
+
type PullResubmitParams struct {
LoggedInUser *auth.User
RepoInfo RepoInfo
+46
appview/pages/templates/fragments/pullCompareBranches.html
···
+
{{ define "fragments/pullCompareBranches" }}
+
<div id="patch-upload">
+
<label for="targetBranch" class="dark:text-white">configure branches</label>
+
<div class="flex flex-wrap gap-2 items-center">
+
<select
+
required
+
name="targetBranch"
+
class="p-1 border border-gray-200 bg-white dark:bg-gray-700 dark:text-white dark:border-gray-600"
+
>
+
<option disabled selected>target branch</option>
+
{{ range .Branches }}
+
<option value="{{ .Reference.Name }}" class="py-1">
+
{{ .Reference.Name }}
+
</option>
+
{{ end }}
+
</select>
+
+
{{ i "move-left" "w-5 h-5" }}
+
+
<select
+
name="sourceBranch"
+
class="p-1 border border-gray-200 bg-white dark:bg-gray-700 dark:text-white dark:border-gray-600"
+
>
+
<option disabled selected>source branch</option>
+
{{ range .Branches }}
+
<option value="{{ .Reference.Name }}" class="py-1">
+
{{ .Reference.Name }}
+
</option>
+
{{ end }}
+
</select>
+
+
<span class="text-sm">
+
... or upload a patch
+
<button
+
class="btn text-sm"
+
hx-get="/{{ .RepoInfo.FullName }}/pulls/new/patch-upload"
+
hx-swap="outerHTML"
+
hx-target="#patch-upload"
+
>
+
upload patch
+
</button>
+
</span>
+
</div>
+
+
</div>
+
{{ end }}
+26
appview/pages/templates/fragments/pullPatchUpload.html
···
+
{{ define "fragments/pullPatchUpload" }}
+
<div id="patch-upload">
+
<label for="patch" class="dark:text-white">paste your patch here</label>
+
<textarea
+
name="patch"
+
id="patch"
+
rows="10"
+
class="w-full resize-y font-mono dark:bg-gray-700 dark:text-white dark:border-gray-600"
+
placeholder="Paste your git diff output here."
+
></textarea>
+
+
{{ if .RepoInfo.Roles.IsPushAllowed }}
+
<div class="mt-4 text-sm">
+
you can also submit a pull request from a branch
+
<button
+
class="btn text-sm"
+
hx-get="/{{ .RepoInfo.FullName }}/pulls/new/compare-branches"
+
hx-swap="outerHTML"
+
hx-target="#patch-upload"
+
>
+
compare branches
+
</button>
+
</div>
+
{{ end }}
+
</div>
+
{{ end }}
+5 -51
appview/pages/templates/repo/pulls/new.html
···
></textarea>
</div>
-
<div>
-
<label for="targetBranch" class="dark:text-white">configure branches</label>
-
<div class="flex flex-wrap gap-2 items-center">
-
<select
-
required
-
name="targetBranch"
-
class="p-1 border border-gray-200 bg-white dark:bg-gray-700 dark:text-white dark:border-gray-600"
-
>
-
<option disabled selected>target branch</option>
-
{{ range .Branches }}
-
<option value="{{ .Reference.Name }}" class="py-1">
-
{{ .Reference.Name }}
-
</option>
-
{{ end }}
-
</select>
-
-
{{ if .RepoInfo.Roles.IsPushAllowed }}
-
{{ i "move-left" "w-5 h-5" }}
-
<select
-
name="sourceBranch"
-
class="p-1 border border-gray-200 bg-white dark:bg-gray-700 dark:text-white dark:border-gray-600"
-
>
-
<option disabled selected>source branch</option>
-
{{ range .Branches }}
-
<option value="{{ .Reference.Name }}" class="py-1">
-
{{ .Reference.Name }}
-
</option>
-
{{ end }}
-
</select>
-
{{ end }}
-
-
</div>
-
</div>
-
-
<div class="mt-4">
-
{{ $label := "paste your patch here" }}
-
{{ $rows := 10 }}
-
{{ if .RepoInfo.Roles.IsPushAllowed }}
-
{{ $label = "or paste your patch here" }}
-
{{ $rows = 4 }}
-
{{ end }}
-
-
<label for="patch" class="dark:text-white">{{ $label }}</label>
-
<textarea
-
name="patch"
-
id="patch"
-
rows="{{$rows}}"
-
class="w-full resize-y font-mono dark:bg-gray-700 dark:text-white dark:border-gray-600"
-
placeholder="Paste your git diff output here."
-
></textarea>
-
</div>
+
{{ if not .RepoInfo.Roles.IsPushAllowed }}
+
{{ template "fragments/pullPatchUpload" . }}
+
{{ else }}
+
{{ template "fragments/pullCompareBranches" . }}
+
{{ end }}
<div class="flex justify-end items-center gap-2">
<button type="submit" class="btn">create</button>
+53
appview/state/pull.go
···
}
}
+
func (s *State) PatchUploadFragment(w http.ResponseWriter, r *http.Request) {
+
user := s.auth.GetUser(r)
+
f, err := fullyResolvedRepo(r)
+
if err != nil {
+
log.Println("failed to get repo and knot", err)
+
return
+
}
+
+
s.pages.PullPatchUploadFragment(w, pages.PullPatchUploadParams{
+
RepoInfo: f.RepoInfo(s, user),
+
})
+
}
+
+
func (s *State) CompareBranchesFragment(w http.ResponseWriter, r *http.Request) {
+
user := s.auth.GetUser(r)
+
f, err := fullyResolvedRepo(r)
+
if err != nil {
+
log.Println("failed to get repo and knot", err)
+
return
+
}
+
+
us, err := NewUnsignedClient(f.Knot, s.config.Dev)
+
if err != nil {
+
log.Printf("failed to create unsigned client for %s", f.Knot)
+
s.pages.Error503(w)
+
return
+
}
+
+
resp, err := us.Branches(f.OwnerDid(), f.RepoName)
+
if err != nil {
+
log.Println("failed to reach knotserver", err)
+
return
+
}
+
+
body, err := io.ReadAll(resp.Body)
+
if err != nil {
+
log.Printf("Error reading response body: %v", err)
+
return
+
}
+
+
var result types.RepoBranchesResponse
+
err = json.Unmarshal(body, &result)
+
if err != nil {
+
log.Println("failed to parse response:", err)
+
return
+
}
+
+
s.pages.PullCompareBranchesFragment(w, pages.PullCompareBranchesParams{
+
RepoInfo: f.RepoInfo(s, user),
+
Branches: result.Branches,
+
})
+
}
+
func (s *State) ResubmitPull(w http.ResponseWriter, r *http.Request) {
user := s.auth.GetUser(r)
f, err := fullyResolvedRepo(r)
+2
appview/state/router.go
···
r.Get("/", s.RepoPulls)
r.With(AuthMiddleware(s)).Route("/new", func(r chi.Router) {
r.Get("/", s.NewPull)
+
r.Get("/patch-upload", s.PatchUploadFragment)
+
r.Get("/compare-branches", s.CompareBranchesFragment)
r.Post("/", s.NewPull)
})