This commit is contained in:
Thomas
2026-03-12 23:41:12 +01:00
parent 8049c505a0
commit 7c1a20465a
10 changed files with 1057 additions and 120 deletions

View File

@@ -94,3 +94,49 @@ func (c *Client) UpsertPRComment(ctx context.Context, prNumber int, body string)
&gh.IssueComment{Body: gh.Ptr(markedBody)})
return err
}
const prMarker = "<!-- stamp-release-pr -->"
// UpsertPR creates or updates a release PR from head into base.
// It identifies a previously-created PR by the hidden marker in the body.
// Returns the PR number and the URL.
func (c *Client) UpsertPR(ctx context.Context, head, base, title, body string) (int, string, error) {
markedBody := prMarker + "\n" + body
// Search open PRs from this head branch.
prs, _, err := c.client.PullRequests.List(ctx, c.owner, c.repo, &gh.PullRequestListOptions{
State: "open",
Head: c.owner + ":" + head,
Base: base,
})
if err != nil {
return 0, "", fmt.Errorf("listing pull requests: %w", err)
}
for _, pr := range prs {
if strings.Contains(pr.GetBody(), prMarker) {
// Update existing PR.
updated, _, err := c.client.PullRequests.Edit(ctx, c.owner, c.repo, pr.GetNumber(),
&gh.PullRequest{
Title: gh.Ptr(title),
Body: gh.Ptr(markedBody),
})
if err != nil {
return 0, "", fmt.Errorf("updating pull request: %w", err)
}
return updated.GetNumber(), updated.GetHTMLURL(), nil
}
}
// Create new PR.
pr, _, err := c.client.PullRequests.Create(ctx, c.owner, c.repo, &gh.NewPullRequest{
Title: gh.Ptr(title),
Head: gh.Ptr(head),
Base: gh.Ptr(base),
Body: gh.Ptr(markedBody),
})
if err != nil {
return 0, "", fmt.Errorf("creating pull request: %w", err)
}
return pr.GetNumber(), pr.GetHTMLURL(), nil
}