Fixes?
This commit is contained in:
+32
-3
@@ -8,6 +8,17 @@ import (
|
||||
giteaSDK "code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
// isPRsDisabledError returns true when the Gitea server rejects a PR operation
|
||||
// because the Pull Requests unit is disabled on the repository.
|
||||
func isPRsDisabledError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "Can't read pulls") ||
|
||||
strings.Contains(msg, "UnitTypeCode")
|
||||
}
|
||||
|
||||
const prMarker = "<!-- stamp-release-pr -->"
|
||||
|
||||
// Client wraps the Gitea API.
|
||||
@@ -19,12 +30,18 @@ type Client struct {
|
||||
|
||||
const commentMarker = "<!-- stamp-pr-comment -->"
|
||||
|
||||
// NewClient creates a Gitea client from GITEA_TOKEN and GITEA_BASE_URL environment variables.
|
||||
// NewClient creates a Gitea client from GITEA_BASE_URL and a token environment variable.
|
||||
// The token is read from STAMP_GITEA_TOKEN first, falling back to GITEA_TOKEN.
|
||||
// STAMP_GITEA_TOKEN should be used in CI to avoid the reserved GITEA_TOKEN name that
|
||||
// Gitea Actions injects automatically (with limited scopes).
|
||||
// The repoSlug must be in "owner/repo" format.
|
||||
func NewClient(repoSlug string) (*Client, error) {
|
||||
token := os.Getenv("GITEA_TOKEN")
|
||||
token := os.Getenv("STAMP_GITEA_TOKEN")
|
||||
if token == "" {
|
||||
return nil, fmt.Errorf("GITEA_TOKEN environment variable is not set")
|
||||
token = os.Getenv("GITEA_TOKEN")
|
||||
}
|
||||
if token == "" {
|
||||
return nil, fmt.Errorf("neither STAMP_GITEA_TOKEN nor GITEA_TOKEN environment variable is set")
|
||||
}
|
||||
baseURL := os.Getenv("GITEA_BASE_URL")
|
||||
if baseURL == "" {
|
||||
@@ -87,6 +104,12 @@ func (c *Client) UpsertPR(head, base, title, body string) (int64, string, error)
|
||||
State: giteaSDK.StateOpen,
|
||||
})
|
||||
if err != nil {
|
||||
if isPRsDisabledError(err) {
|
||||
return 0, "", fmt.Errorf(
|
||||
"pull requests are disabled on %s/%s — enable them under Settings → General → Features → Pull Requests",
|
||||
c.owner, c.repo,
|
||||
)
|
||||
}
|
||||
return 0, "", fmt.Errorf("listing pull requests: %w", err)
|
||||
}
|
||||
|
||||
@@ -116,6 +139,12 @@ func (c *Client) UpsertPR(head, base, title, body string) (int64, string, error)
|
||||
Body: markedBody,
|
||||
})
|
||||
if err != nil {
|
||||
if isPRsDisabledError(err) {
|
||||
return 0, "", fmt.Errorf(
|
||||
"pull requests are disabled on %s/%s — enable them under Settings → General → Features → Pull Requests",
|
||||
c.owner, c.repo,
|
||||
)
|
||||
}
|
||||
return 0, "", fmt.Errorf("creating pull request: %w", err)
|
||||
}
|
||||
return pr.Index, pr.HTMLURL, nil
|
||||
|
||||
Reference in New Issue
Block a user