From 5a2d0e258251818cd6be99251d38ac053f10da04 Mon Sep 17 00:00:00 2001
From: Akshay
Date: Fri, 14 Mar 2025 00:00:37 +0000
Subject: [PATCH] more progress on patches
- fix up filters
- fix missing icons bug
- fix missing close button
---
appview/db/db.go | 2 +-
appview/db/pulls.go | 85 +++++++++++++++----
appview/db/repos.go | 1 +
appview/pages/pages.go | 11 +--
appview/pages/templates/repo/pulls/new.html | 2 +-
appview/pages/templates/repo/pulls/pull.html | 16 ++--
appview/pages/templates/repo/pulls/pulls.html | 30 ++-----
appview/state/repo.go | 20 ++++-
8 files changed, 111 insertions(+), 56 deletions(-)
diff --git a/appview/db/db.go b/appview/db/db.go
index 605798c3..8c78a560 100644
--- a/appview/db/db.go
+++ b/appview/db/db.go
@@ -114,7 +114,7 @@ func Make(dbPath string) (*DB, error) {
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
diff --git a/appview/db/pulls.go b/appview/db/pulls.go
index 82d3a50b..64defaa1 100644
--- a/appview/db/pulls.go
+++ b/appview/db/pulls.go
@@ -7,6 +7,37 @@ import (
"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
@@ -17,7 +48,7 @@ type Pull struct {
PullId int
Title string
Body string
- Open int
+ State PullState
Created time.Time
Rkey string
}
@@ -89,10 +120,27 @@ func NextPullId(e Execer, repoAt syntax.ATURI) (int, error) {
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
}
@@ -101,7 +149,7 @@ func GetPulls(e Execer, repoAt syntax.ATURI) ([]Pull, error) {
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
}
@@ -123,12 +171,12 @@ func GetPulls(e Execer, repoAt syntax.ATURI) ([]Pull, error) {
}
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
}
@@ -143,12 +191,12 @@ func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*Pull, error) {
}
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
}
@@ -217,39 +265,46 @@ func GetPullComments(e Execer, repoAt syntax.ATURI, pullId int) ([]PullComment,
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
diff --git a/appview/db/repos.go b/appview/db/repos.go
index b0f9202d..c2492771 100644
--- a/appview/db/repos.go
+++ b/appview/db/repos.go
@@ -246,6 +246,7 @@ func CollaboratingIn(e Execer, collaborator string) ([]Repo, error) {
type RepoStats struct {
StarCount int
IssueCount IssueCount
+ PullCount PullCount
}
func scanRepo(rows *sql.Rows, did, name, knot, rkey, description *string, created *time.Time) error {
diff --git a/appview/pages/pages.go b/appview/pages/pages.go
index 3ebe58a6..980a93c2 100644
--- a/appview/pages/pages.go
+++ b/appview/pages/pages.go
@@ -272,6 +272,7 @@ func (r RepoInfo) TabMetadata() map[string]any {
meta := make(map[string]any)
meta["issues"] = r.Stats.IssueCount.Open
+ meta["pulls"] = r.Stats.PullCount.Open
// more stuff?
@@ -524,6 +525,7 @@ type RepoPullsParams struct {
Pulls []db.Pull
Active string
DidHandleMap map[string]string
+ FilteringBy db.PullState
}
func (p *Pages) RepoPulls(w io.Writer, params RepoPullsParams) error {
@@ -536,7 +538,6 @@ type RepoSinglePullParams struct {
RepoInfo RepoInfo
DidHandleMap map[string]string
Pull db.Pull
- State string
PullOwnerHandle string
Comments []db.PullComment
Active string
@@ -544,14 +545,6 @@ type RepoSinglePullParams struct {
}
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)
}
diff --git a/appview/pages/templates/repo/pulls/new.html b/appview/pages/templates/repo/pulls/new.html
index 80302279..0be063f2 100644
--- a/appview/pages/templates/repo/pulls/new.html
+++ b/appview/pages/templates/repo/pulls/new.html
@@ -15,7 +15,7 @@
The branch you want to make your change against.
-
@@ -42,10 +36,7 @@
{{ range .Pulls }}
-
+
{{ .Title }}
#{{ .PullId }}
@@ -53,16 +44,13 @@
{{ $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 }}
@@ -73,7 +61,7 @@
data-lucide="{{ $icon }}"
class="w-3 h-3 mr-1.5 text-white"
>
- {{ $state }}
+ {{ .State.String }}
diff --git a/appview/state/repo.go b/appview/state/repo.go
index d9eedbb0..fe75eef4 100644
--- a/appview/state/repo.go
+++ b/appview/state/repo.go
@@ -413,7 +413,7 @@ func (s *State) NewPull(w http.ResponseWriter, r *http.Request) {
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
}
@@ -990,6 +990,10 @@ func (f *FullyResolvedRepo) RepoInfo(s *State, u *auth.User) pages.RepoInfo {
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" {
@@ -1008,6 +1012,7 @@ func (f *FullyResolvedRepo) RepoInfo(s *State, u *auth.User) pages.RepoInfo {
Stats: db.RepoStats{
StarCount: starCount,
IssueCount: issueCount,
+ PullCount: pullCount,
},
}
}
@@ -1399,13 +1404,23 @@ func (s *State) NewIssue(w http.ResponseWriter, r *http.Request) {
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.")
@@ -1431,6 +1446,7 @@ func (s *State) RepoPulls(w http.ResponseWriter, r *http.Request) {
RepoInfo: f.RepoInfo(s, user),
Pulls: pulls,
DidHandleMap: didHandleMap,
+ FilteringBy: state,
})
return
}
--
2.51.2