-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
branch submit: Heal from external PR submissions (#141)
If a PR was submitted without using `gs`, `branch submit` will detect that and heal state. Obviates need for manually associating PRs with branches after the fact. Resolves #62
- Loading branch information
Showing
6 changed files
with
245 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
kind: Changed | ||
body: 'branch submit: Auto-detect PRs created outside git-spice, e.g. using the GitHub UI.' | ||
time: 2024-06-01T16:21:28.52887-07:00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/google/go-github/v61/github" | ||
"go.abhg.dev/gs/internal/git" | ||
) | ||
|
||
// FindChangeItem is a single result from searching for changes in the | ||
// repository. | ||
type FindChangeItem struct { | ||
// ID is a unique identifier for the change. | ||
ID ChangeID | ||
|
||
// URL is the web URL at which the change can be viewed. | ||
URL string | ||
|
||
// Subject is the title of the change. | ||
Subject string | ||
|
||
// HeadHash is the hash of the commit at the top of the change. | ||
HeadHash git.Hash | ||
|
||
// BaseName is the name of the base branch | ||
// that this change is proposed against. | ||
BaseName string | ||
|
||
// Draft is true if the change is not yet ready to be reviewed. | ||
Draft bool | ||
} | ||
|
||
// TODO: Reduce filtering options in favor of explicit queries, | ||
// e.g. "FindChangesForBranch" or "ListOpenChanges", etc. | ||
|
||
// FindChangesByBranch searches for open changes with the given branch name. | ||
// Returns [ErrNotFound] if no changes are found. | ||
func (f *Forge) FindChangesByBranch(ctx context.Context, branch string) ([]FindChangeItem, error) { | ||
pulls, _, err := f.client.PullRequests.List(ctx, f.owner, f.repo, &github.PullRequestListOptions{ | ||
State: "open", | ||
Head: f.owner + ":" + branch, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("list pull requests: %w", err) | ||
} | ||
|
||
changes := make([]FindChangeItem, len(pulls)) | ||
for i, pull := range pulls { | ||
changes[i] = FindChangeItem{ | ||
ID: ChangeID(pull.GetNumber()), | ||
URL: pull.GetHTMLURL(), | ||
Subject: pull.GetTitle(), | ||
BaseName: pull.GetBase().GetRef(), | ||
HeadHash: git.Hash(pull.GetHead().GetSHA()), | ||
Draft: pull.GetDraft(), | ||
} | ||
} | ||
|
||
return changes, nil | ||
} | ||
|
||
// FindChangeByID searches for a change with the given ID. | ||
func (f *Forge) FindChangeByID(ctx context.Context, id ChangeID) (*FindChangeItem, error) { | ||
pull, _, err := f.client.PullRequests.Get(ctx, f.owner, f.repo, int(id)) | ||
if err != nil { | ||
return nil, fmt.Errorf("get pull request: %w", err) | ||
} | ||
|
||
return &FindChangeItem{ | ||
ID: ChangeID(pull.GetNumber()), | ||
URL: pull.GetHTMLURL(), | ||
Subject: pull.GetTitle(), | ||
BaseName: pull.GetBase().GetRef(), | ||
HeadHash: git.Hash(pull.GetHead().GetSHA()), | ||
Draft: pull.GetDraft(), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 'branch submit' should detect a PR created | ||
# outside of the branch submit command. | ||
|
||
as 'Test <[email protected]>' | ||
at '2024-05-18T13:57:12Z' | ||
|
||
# setup | ||
cd repo | ||
git init | ||
git commit --allow-empty -m 'Initial commit' | ||
|
||
# set up a fake GitHub remote | ||
gh-init | ||
gh-add-remote origin alice/example.git | ||
git push origin main | ||
|
||
# create a new branch and submit it | ||
git add feature1.txt | ||
gs bc -m 'Add feature1' feature1 | ||
gs branch submit --fill | ||
stderr 'Created #' | ||
|
||
# forget all state, and re-track the branch | ||
gs repo init --reset --trunk=main --remote=origin | ||
gs branch track --base=main feature1 | ||
|
||
# If we now commit to the branch and then submit, | ||
# the system should detect that a PR already exists, | ||
# and update that instead. | ||
cp $WORK/extra/feature1-update.txt feature1.txt | ||
git add feature1.txt | ||
git commit -m 'update feature1' | ||
|
||
gs branch submit | ||
stderr 'feature1: Found existing PR #' | ||
stderr 'Updated #' | ||
|
||
gh-dump-pull | ||
cmpenvJSON stdout $WORK/golden/update.json | ||
|
||
-- repo/feature1.txt -- | ||
Contents of feature1 | ||
|
||
-- extra/feature1-update.txt -- | ||
New contents of feature1 | ||
|
||
-- golden/update.json -- | ||
[ | ||
{ | ||
"number": 1, | ||
"state": "open", | ||
"title": "Add feature1", | ||
"body": "", | ||
"html_url": "$GITHUB_URL/alice/example/pull/1", | ||
"head": { | ||
"ref": "feature1", | ||
"sha": "b805a8b9545d71929cc128fc81b0d86bb2def9ed" | ||
}, | ||
"base": { | ||
"ref": "main", | ||
"sha": "9df31764fb4252f719c92d53fae05a766f019a17" | ||
} | ||
} | ||
] |