From 7f1d928eac3e842c781f0d7ad2043968b97bc454 Mon Sep 17 00:00:00 2001 From: oppiliappan Date: Wed, 9 Jul 2025 18:14:51 +0100 Subject: [PATCH] appview,lexicons: add SHA field to sh.tangled.repo.pull Change-Id: vquoltwpkunyvkoqsuzosnmostqltnqt refers to the tip of the branch that this PR was opened from. this addition permits us to associate a CI run with a branch or fork based pull request. Signed-off-by: oppiliappan --- api/tangled/cbor_gen.go | 38 ++++++++++++++++++++++++++++++++++++-- api/tangled/repopull.go | 1 + appview/db/artifact.go | 2 +- appview/db/pulls.go | 6 ++++++ appview/pulls/pulls.go | 32 +++++++++++++++++++++----------- appview/repo/index.go | 2 +- appview/repo/repo.go | 4 ++-- appview/repo/repo_util.go | 5 +++-- lexicons/pulls/pull.json | 8 +++++++- 9 files changed, 78 insertions(+), 20 deletions(-) diff --git a/api/tangled/cbor_gen.go b/api/tangled/cbor_gen.go index 9b2240f..00cd563 100644 --- a/api/tangled/cbor_gen.go +++ b/api/tangled/cbor_gen.go @@ -7008,7 +7008,7 @@ func (t *RepoPull_Source) MarshalCBOR(w io.Writer) error { } cw := cbg.NewCborWriter(w) - fieldCount := 2 + fieldCount := 3 if t.Repo == nil { fieldCount-- @@ -7018,6 +7018,29 @@ func (t *RepoPull_Source) MarshalCBOR(w io.Writer) error { return err } + // t.Sha (string) (string) + if len("sha") > 1000000 { + return xerrors.Errorf("Value in field \"sha\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("sha"))); err != nil { + return err + } + if _, err := cw.WriteString(string("sha")); err != nil { + return err + } + + if len(t.Sha) > 1000000 { + return xerrors.Errorf("Value in field t.Sha was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Sha))); err != nil { + return err + } + if _, err := cw.WriteString(string(t.Sha)); err != nil { + return err + } + // t.Repo (string) (string) if t.Repo != nil { @@ -7116,7 +7139,18 @@ func (t *RepoPull_Source) UnmarshalCBOR(r io.Reader) (err error) { } switch string(nameBuf[:nameLen]) { - // t.Repo (string) (string) + // t.Sha (string) (string) + case "sha": + + { + sval, err := cbg.ReadStringWithMax(cr, 1000000) + if err != nil { + return err + } + + t.Sha = string(sval) + } + // t.Repo (string) (string) case "repo": { diff --git a/api/tangled/repopull.go b/api/tangled/repopull.go index f9ecf48..e5ad781 100644 --- a/api/tangled/repopull.go +++ b/api/tangled/repopull.go @@ -32,4 +32,5 @@ type RepoPull struct { type RepoPull_Source struct { Branch string `json:"branch" cborgen:"branch"` Repo *string `json:"repo,omitempty" cborgen:"repo,omitempty"` + Sha string `json:"sha" cborgen:"sha"` } diff --git a/appview/db/artifact.go b/appview/db/artifact.go index 8f8d5d1..e021d9c 100644 --- a/appview/db/artifact.go +++ b/appview/db/artifact.go @@ -27,7 +27,7 @@ type Artifact struct { } func (a *Artifact) ArtifactAt() syntax.ATURI { - return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", a.Did, tangled.RepoPullNSID, a.Rkey)) + return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", a.Did, tangled.RepoArtifactNSID, a.Rkey)) } func AddArtifact(e Execer, artifact Artifact) error { diff --git a/appview/db/pulls.go b/appview/db/pulls.go index feb4196..e993791 100644 --- a/appview/db/pulls.go +++ b/appview/db/pulls.go @@ -87,6 +87,7 @@ func (p Pull) AsRecord() tangled.RepoPull { if p.PullSource != nil { s := p.PullSource.AsRecord() source = &s + source.Sha = p.LatestSha() } record := tangled.RepoPull{ @@ -164,6 +165,11 @@ func (p *Pull) LatestPatch() string { return latestSubmission.Patch } +func (p *Pull) LatestSha() string { + latestSubmission := p.Submissions[p.LastRoundNumber()] + return latestSubmission.SourceRev +} + func (p *Pull) PullAt() syntax.ATURI { return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", p.OwnerDid, tangled.RepoPullNSID, p.Rkey)) } diff --git a/appview/pulls/pulls.go b/appview/pulls/pulls.go index a626312..a587560 100644 --- a/appview/pulls/pulls.go +++ b/appview/pulls/pulls.go @@ -798,13 +798,6 @@ func (s *Pulls) handleBranchBasedPull( sourceBranch string, isStacked bool, ) { - pullSource := &db.PullSource{ - Branch: sourceBranch, - } - recordPullSource := &tangled.RepoPull_Source{ - Branch: sourceBranch, - } - // Generate a patch using /compare ksClient, err := knotclient.NewUnsignedClient(f.Knot, s.config.Core.Dev) if err != nil { @@ -828,6 +821,14 @@ func (s *Pulls) handleBranchBasedPull( return } + pullSource := &db.PullSource{ + Branch: sourceBranch, + } + recordPullSource := &tangled.RepoPull_Source{ + Branch: sourceBranch, + Sha: comparison.Rev2, + } + s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, sourceRev, pullSource, recordPullSource, isStacked) } @@ -914,10 +915,17 @@ func (s *Pulls) handleForkBasedPull(w http.ResponseWriter, r *http.Request, f *r return } - s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, sourceRev, &db.PullSource{ + pullSource := &db.PullSource{ Branch: sourceBranch, RepoAt: &forkAtUri, - }, &tangled.RepoPull_Source{Branch: sourceBranch, Repo: &fork.AtUri}, isStacked) + } + recordPullSource := &tangled.RepoPull_Source{ + Branch: sourceBranch, + Repo: &fork.AtUri, + Sha: sourceRev, + } + + s.createPullRequest(w, r, f, user, title, body, targetBranch, patch, sourceRev, pullSource, recordPullSource, isStacked) } func (s *Pulls) createPullRequest( @@ -934,7 +942,7 @@ func (s *Pulls) createPullRequest( ) { if isStacked { // creates a series of PRs, each linking to the previous, identified by jj's change-id - s.createStackedPulLRequest( + s.createStackedPullRequest( w, r, f, @@ -1049,7 +1057,7 @@ func (s *Pulls) createPullRequest( s.pages.HxLocation(w, fmt.Sprintf("/%s/pulls/%d", f.OwnerSlashRepo(), pullId)) } -func (s *Pulls) createStackedPulLRequest( +func (s *Pulls) createStackedPullRequest( w http.ResponseWriter, r *http.Request, f *reporesolver.ResolvedRepo, @@ -1566,6 +1574,7 @@ func (s *Pulls) resubmitPullHelper( if pull.IsBranchBased() { recordPullSource = &tangled.RepoPull_Source{ Branch: pull.PullSource.Branch, + Sha: sourceRev, } } if pull.IsForkBased() { @@ -1573,6 +1582,7 @@ func (s *Pulls) resubmitPullHelper( recordPullSource = &tangled.RepoPull_Source{ Branch: pull.PullSource.Branch, Repo: &repoAt, + Sha: sourceRev, } } diff --git a/appview/repo/index.go b/appview/repo/index.go index 9ad8219..b9ea13e 100644 --- a/appview/repo/index.go +++ b/appview/repo/index.go @@ -133,7 +133,7 @@ func (rp *Repo) RepoIndex(w http.ResponseWriter, r *http.Request) { for _, c := range commitsTrunc { shas = append(shas, c.Hash.String()) } - pipelines, err := rp.getPipelineStatuses(repoInfo, shas) + pipelines, err := getPipelineStatuses(rp.db, repoInfo, shas) if err != nil { log.Printf("failed to fetch pipeline statuses: %s", err) // non-fatal diff --git a/appview/repo/repo.go b/appview/repo/repo.go index f1df864..c2587b5 100644 --- a/appview/repo/repo.go +++ b/appview/repo/repo.go @@ -139,7 +139,7 @@ func (rp *Repo) RepoLog(w http.ResponseWriter, r *http.Request) { for _, c := range repolog.Commits { shas = append(shas, c.Hash.String()) } - pipelines, err := rp.getPipelineStatuses(repoInfo, shas) + pipelines, err := getPipelineStatuses(rp.db, repoInfo, shas) if err != nil { log.Println(err) // non-fatal @@ -304,7 +304,7 @@ func (rp *Repo) RepoCommit(w http.ResponseWriter, r *http.Request) { user := rp.oauth.GetUser(r) repoInfo := f.RepoInfo(user) - pipelines, err := rp.getPipelineStatuses(repoInfo, []string{result.Diff.Commit.This}) + pipelines, err := getPipelineStatuses(rp.db, repoInfo, []string{result.Diff.Commit.This}) if err != nil { log.Println(err) // non-fatal diff --git a/appview/repo/repo_util.go b/appview/repo/repo_util.go index c342bb5..50c614b 100644 --- a/appview/repo/repo_util.go +++ b/appview/repo/repo_util.go @@ -105,7 +105,8 @@ func randomString(n int) string { // grab pipelines from DB and munge that into a hashmap with commit sha as key // // golang is so blessed that it requires 35 lines of imperative code for this -func (rp *Repo) getPipelineStatuses( +func getPipelineStatuses( + d *db.DB, repoInfo repoinfo.RepoInfo, shas []string, ) (map[string]db.Pipeline, error) { @@ -116,7 +117,7 @@ func (rp *Repo) getPipelineStatuses( } ps, err := db.GetPipelineStatuses( - rp.db, + d, db.FilterEq("repo_owner", repoInfo.OwnerDid), db.FilterEq("repo_name", repoInfo.Name), db.FilterEq("knot", repoInfo.Knot), diff --git a/lexicons/pulls/pull.json b/lexicons/pulls/pull.json index bab1a1d..77f36aa 100644 --- a/lexicons/pulls/pull.json +++ b/lexicons/pulls/pull.json @@ -51,12 +51,18 @@ "source": { "type": "object", "required": [ - "branch" + "branch", + "sha" ], "properties": { "branch": { "type": "string" }, + "sha": { + "type": "string", + "minLength": 40, + "maxLength": 40 + }, "repo": { "type": "string", "format": "at-uri" -- 2.43.0