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

@@ -8,6 +8,8 @@ import (
giteaSDK "code.gitea.io/sdk/gitea"
)
const prMarker = "<!-- stamp-release-pr -->"
// Client wraps the Gitea API.
type Client struct {
client *giteaSDK.Client
@@ -74,6 +76,51 @@ func (c *Client) UploadAsset(releaseID int64, filePath string) error {
return nil
}
// UpsertPR creates or updates a stamp release PR on the given base branch.
// It identifies the existing PR by scanning open PRs for the hidden marker in the body.
func (c *Client) UpsertPR(head, base, title, body string) (int64, string, error) {
markedBody := prMarker + "\n" + body
// Search open PRs for an existing stamp release PR (Gitea SDK does not support
// filtering by head branch, so we filter client-side).
prs, _, err := c.client.ListRepoPullRequests(c.owner, c.repo, giteaSDK.ListPullRequestsOptions{
State: giteaSDK.StateOpen,
})
if err != nil {
return 0, "", fmt.Errorf("listing pull requests: %w", err)
}
for _, pr := range prs {
if pr.Head != nil && pr.Head.Name != head {
continue
}
if strings.Contains(pr.Body, prMarker) {
// Update existing PR.
updated, _, err := c.client.EditPullRequest(c.owner, c.repo, pr.Index,
giteaSDK.EditPullRequestOption{
Title: title,
Body: &markedBody,
})
if err != nil {
return 0, "", fmt.Errorf("updating pull request #%d: %w", pr.Index, err)
}
return updated.Index, updated.HTMLURL, nil
}
}
// Create a new PR.
pr, _, err := c.client.CreatePullRequest(c.owner, c.repo, giteaSDK.CreatePullRequestOption{
Head: head,
Base: base,
Title: title,
Body: markedBody,
})
if err != nil {
return 0, "", fmt.Errorf("creating pull request: %w", err)
}
return pr.Index, pr.HTMLURL, nil
}
// UpsertPRComment posts or updates a stamped PR comment.
func (c *Client) UpsertPRComment(prNumber int, body string) error {
markedBody := commentMarker + "\n" + body