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

more progress on patches

- fix up filters
- fix missing icons bug
- fix missing close button

Changed files
+111 -56
appview
db
pages
templates
state
+1 -1
appview/db/db.go
···
pull_at text,
rkey text not null,
target_branch text not null,
-
open integer not null default 1,
+
state integer not null default 0 check (state in (0, 1, 2)), -- open, merged, closed
created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
unique(repo_at, pull_id),
foreign key (repo_at) references repos(at_uri) on delete cascade
+70 -15
appview/db/pulls.go
···
"github.com/bluesky-social/indigo/atproto/syntax"
)
+
type PullState int
+
+
const (
+
PullOpen PullState = iota
+
PullMerged
+
PullClosed
+
)
+
+
func (p PullState) String() string {
+
switch p {
+
case PullOpen:
+
return "open"
+
case PullMerged:
+
return "merged"
+
case PullClosed:
+
return "closed"
+
default:
+
return "closed"
+
}
+
}
+
+
func (p PullState) IsOpen() bool {
+
return p == PullOpen
+
}
+
func (p PullState) IsMerged() bool {
+
return p == PullMerged
+
}
+
func (p PullState) IsClosed() bool {
+
return p == PullClosed
+
}
+
type Pull struct {
ID int
OwnerDid string
···
PullId int
Title string
Body string
-
Open int
+
State PullState
Created time.Time
Rkey string
}
···
return pullId - 1, err
}
-
func GetPulls(e Execer, repoAt syntax.ATURI) ([]Pull, error) {
+
func GetPulls(e Execer, repoAt syntax.ATURI, state PullState) ([]Pull, error) {
var pulls []Pull
-
rows, err := e.Query(`select owner_did, pull_id, created, title, open, target_branch, pull_at, body, patch, rkey from pulls where repo_at = ? order by created desc`, repoAt)
+
rows, err := e.Query(`
+
select
+
owner_did,
+
pull_id,
+
created,
+
title,
+
state,
+
target_branch,
+
pull_at,
+
body,
+
patch,
+
rkey
+
from
+
pulls
+
where
+
repo_at = ? and state = ?
+
order by
+
created desc`, repoAt, state)
if err != nil {
return nil, err
}
···
for rows.Next() {
var pull Pull
var createdAt string
-
err := rows.Scan(&pull.OwnerDid, &pull.PullId, &createdAt, &pull.Title, &pull.Open, &pull.TargetBranch, &pull.PullAt, &pull.Body, &pull.Patch, &pull.Rkey)
+
err := rows.Scan(&pull.OwnerDid, &pull.PullId, &createdAt, &pull.Title, &pull.State, &pull.TargetBranch, &pull.PullAt, &pull.Body, &pull.Patch, &pull.Rkey)
if err != nil {
return nil, err
}
···
}
func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*Pull, error) {
-
query := `select owner_did, created, title, open, target_branch, pull_at, body, patch, rkey from pulls where repo_at = ? and pull_id = ?`
+
query := `select owner_did, created, title, state, target_branch, pull_at, body, patch, rkey from pulls where repo_at = ? and pull_id = ?`
row := e.QueryRow(query, repoAt, pullId)
var pull Pull
var createdAt string
-
err := row.Scan(&pull.OwnerDid, &createdAt, &pull.Title, &pull.Open, &pull.TargetBranch, &pull.PullAt, &pull.Body, &pull.Patch, &pull.Rkey)
+
err := row.Scan(&pull.OwnerDid, &createdAt, &pull.Title, &pull.State, &pull.TargetBranch, &pull.PullAt, &pull.Body, &pull.Patch, &pull.Rkey)
if err != nil {
return nil, err
}
···
}
func GetPullWithComments(e Execer, repoAt syntax.ATURI, pullId int) (*Pull, []PullComment, error) {
-
query := `select owner_did, pull_id, created, title, open, target_branch, pull_at, body, patch, rkey from pulls where repo_at = ? and pull_id = ?`
+
query := `select owner_did, pull_id, created, title, state, target_branch, pull_at, body, patch, rkey from pulls where repo_at = ? and pull_id = ?`
row := e.QueryRow(query, repoAt, pullId)
var pull Pull
var createdAt string
-
err := row.Scan(&pull.OwnerDid, &pull.PullId, &createdAt, &pull.Title, &pull.Open, &pull.TargetBranch, &pull.PullAt, &pull.Body, &pull.Patch, &pull.Rkey)
+
err := row.Scan(&pull.OwnerDid, &pull.PullId, &createdAt, &pull.Title, &pull.State, &pull.TargetBranch, &pull.PullAt, &pull.Body, &pull.Patch, &pull.Rkey)
if err != nil {
return nil, nil, err
}
···
return comments, nil
}
+
func SetPullState(e Execer, repoAt syntax.ATURI, pullId int, pullState PullState) error {
+
_, err := e.Exec(`update pulls set state = ? where repo_at = ? and pull_id = ?`, pullState, repoAt, pullId)
+
return err
+
}
+
func ClosePull(e Execer, repoAt syntax.ATURI, pullId int) error {
-
_, err := e.Exec(`update pulls set open = 0 where repo_at = ? and pull_id = ?`, repoAt, pullId)
+
err := SetPullState(e, repoAt, pullId, PullClosed)
return err
}
func ReopenPull(e Execer, repoAt syntax.ATURI, pullId int) error {
-
_, err := e.Exec(`update pulls set open = 1 where repo_at = ? and pull_id = ?`, repoAt, pullId)
+
err := SetPullState(e, repoAt, pullId, PullOpen)
return err
}
func MergePull(e Execer, repoAt syntax.ATURI, pullId int) error {
-
_, err := e.Exec(`update pulls set open = 2 where repo_at = ? and pull_id = ?`, repoAt, pullId)
+
err := SetPullState(e, repoAt, pullId, PullMerged)
return err
}
type PullCount struct {
Open int
+
Merged int
Closed int
}
func GetPullCount(e Execer, repoAt syntax.ATURI) (PullCount, error) {
row := e.QueryRow(`
select
-
count(case when open = 1 then 1 end) as open_count,
-
count(case when open = 0 then 1 end) as closed_count
+
count(case when state = 0 then 1 end) as open_count,
+
count(case when state = 1 then 1 end) as merged_count,
+
count(case when state = 2 then 1 end) as closed_count
from pulls
where repo_at = ?`,
repoAt,
)
var count PullCount
-
if err := row.Scan(&count.Open, &count.Closed); err != nil {
-
return PullCount{0, 0}, err
+
if err := row.Scan(&count.Open, &count.Merged, &count.Closed); err != nil {
+
return PullCount{0, 0, 0}, err
}
return count, nil
+1
appview/db/repos.go
···
type RepoStats struct {
StarCount int
IssueCount IssueCount
+
PullCount PullCount
}
func scanRepo(rows *sql.Rows, did, name, knot, rkey, description *string, created *time.Time) error {
+2 -9
appview/pages/pages.go
···
meta := make(map[string]any)
meta["issues"] = r.Stats.IssueCount.Open
+
meta["pulls"] = r.Stats.PullCount.Open
// more stuff?
···
Pulls []db.Pull
Active string
DidHandleMap map[string]string
+
FilteringBy db.PullState
}
func (p *Pages) RepoPulls(w io.Writer, params RepoPullsParams) error {
···
RepoInfo RepoInfo
DidHandleMap map[string]string
Pull db.Pull
-
State string
PullOwnerHandle string
Comments []db.PullComment
Active string
···
}
func (p *Pages) RepoSinglePull(w io.Writer, params RepoSinglePullParams) error {
-
switch params.Pull.Open {
-
case 0:
-
params.State = "close"
-
case 1:
-
params.State = "open"
-
case 2:
-
params.State = "merged"
-
}
params.Active = "pulls"
return p.executeRepo("repo/pulls/pull", w, params)
}
+1 -1
appview/pages/templates/repo/pulls/new.html
···
<p class="text-gray-500">
The branch you want to make your change against.
</p>
-
<select class="p-1 border border-gray-200 bg-white">
+
<select name="targetBranch" class="p-1 border border-gray-200 bg-white">
<option disabled selected>Select a branch</option>
{{ range .Branches }}
<option
+9 -7
appview/pages/templates/repo/pulls/pull.html
···
{{ $bgColor := "bg-gray-800" }}
{{ $icon := "ban" }}
-
{{ if eq .State "open" }}
+
+
{{ if .Pull.State.IsOpen }}
{{ $bgColor = "bg-green-600" }}
{{ $icon = "circle-dot" }}
-
{{ else if eq .State "merged" }}
+
{{ else if .Pull.State.IsMerged }}
{{ $bgColor = "bg-purple-600" }}
{{ $icon = "git-merge" }}
{{ end }}
···
data-lucide="{{ $icon }}"
class="w-4 h-4 mr-1.5 text-white"
></i>
-
<span class="text-white">{{ .State }}</span>
+
<span class="text-white">{{ .Pull.State.String }}</span>
</div>
<span class="text-gray-400 text-sm">
opened by
···
<a href="/{{ $owner }}" class="no-underline hover:underline"
>{{ $owner }}</a
>
-
<span class="px-1 select-none before:content-['\00B7']"></span>
+
<span class="select-none before:content-['\00B7']"></span>
<time>{{ .Pull.Created | timeFmt }}</time>
+
<span class="select-none before:content-['\00B7']"></span>
+
<time>targeting branch {{ .Pull.TargetBranch }}</time>
</span>
</div>
···
{{ $action := "close" }}
{{ $icon := "circle-x" }}
{{ $hoverColor := "red" }}
-
{{ if eq .State "closed" }}
+
{{ if .Pull.State.IsClosed }}
{{ $action = "reopen" }}
{{ $icon = "circle-dot" }}
{{ $hoverColor = "green" }}
···
<form
hx-post="/{{ .RepoInfo.FullName }}/pulls/{{ .Pull.PullId }}/{{ $action }}"
hx-swap="none"
-
class="mt-8"
-
>
+
class="mt-8">
<button type="submit" class="btn hover:bg-{{ $hoverColor }}-300">
<i
data-lucide="{{ $icon }}"
+9 -21
appview/pages/templates/repo/pulls/pulls.html
···
class="border px-1 bg-white border-gray-200"
onchange="window.location.href = '/{{ .RepoInfo.FullName }}/pulls?state=' + this.value"
>
-
<option value="open" {{ if .FilteringByOpen }}selected{{ end }}>
+
<option value="open" {{ if .FilteringBy.IsOpen }}selected{{ end }}>
open
</option>
-
<option
-
value="closed"
-
{{ if eq .FilteringState "closed" }}selected{{ end }}
-
>
-
closed
-
</option>
-
<option
-
value="merged"
-
{{ if eq .FilteringState "merged" }}selected{{ end }}
-
>
+
<option value="merged" {{ if .FilteringBy.IsMerged }}selected{{ end }}>
merged
</option>
+
<option value="closed" {{ if .FilteringBy.IsClosed }}selected{{ end }}>
+
closed
+
</option>
</select>
pull requests
</p>
···
{{ range .Pulls }}
<div class="rounded drop-shadow-sm bg-white px-6 py-4">
<div class="pb-2">
-
<a
-
href="/{{ $.RepoInfo.FullName }}/pulls/{{ .PullId }}"
-
class="no-underline hover:underline"
-
>
+
<a href="/{{ $.RepoInfo.FullName }}/pulls/{{ .PullId }}">
{{ .Title }}
<span class="text-gray-500">#{{ .PullId }}</span>
</a>
···
<p class="text-sm text-gray-500">
{{ $bgColor := "bg-gray-800" }}
{{ $icon := "ban" }}
-
{{ $state := "closed" }}
-
{{ if eq .Open 1 }}
+
{{ if .State.IsOpen }}
{{ $bgColor = "bg-green-600" }}
{{ $icon = "git-pull-request" }}
-
{{ $state = "open" }}
-
{{ else if eq .Open 2 }}
+
{{ else if .State.IsMerged }}
{{ $bgColor = "bg-purple-600" }}
{{ $icon = "git-merge" }}
-
{{ $state = "merged" }}
{{ end }}
···
data-lucide="{{ $icon }}"
class="w-3 h-3 mr-1.5 text-white"
></i>
-
<span class="text-white">{{ $state }}</span>
+
<span class="text-white">{{ .State.String }}</span>
</span>
<span>
+18 -2
appview/state/repo.go
···
targetBranch := r.FormValue("targetBranch")
patch := r.FormValue("patch")
-
if title == "" || body == "" || patch == "" {
+
if title == "" || body == "" || patch == "" || targetBranch == "" {
s.pages.Notice(w, "pull", "Title, body and patch diff are required.")
return
}
···
if err != nil {
log.Println("failed to get issue count for ", f.RepoAt)
}
+
pullCount, err := db.GetPullCount(s.db, f.RepoAt)
+
if err != nil {
+
log.Println("failed to get issue count for ", f.RepoAt)
+
}
knot := f.Knot
if knot == "knot1.tangled.sh" {
···
Stats: db.RepoStats{
StarCount: starCount,
IssueCount: issueCount,
+
PullCount: pullCount,
},
···
func (s *State) RepoPulls(w http.ResponseWriter, r *http.Request) {
user := s.auth.GetUser(r)
+
params := r.URL.Query()
+
+
state := db.PullOpen
+
switch params.Get("state") {
+
case "closed":
+
state = db.PullClosed
+
case "merged":
+
state = db.PullMerged
+
}
+
f, err := fullyResolvedRepo(r)
if err != nil {
log.Println("failed to get repo and knot", err)
return
-
pulls, err := db.GetPulls(s.db, f.RepoAt)
+
pulls, err := db.GetPulls(s.db, f.RepoAt, state)
if err != nil {
log.Println("failed to get pulls", err)
s.pages.Notice(w, "pulls", "Failed to load pulls. Try again later.")
···
RepoInfo: f.RepoInfo(s, user),
Pulls: pulls,
DidHandleMap: didHandleMap,
+
FilteringBy: state,
})
return