Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace existing draft releases on github #3318

Merged
merged 5 commits into from Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions internal/client/github.go
Expand Up @@ -207,6 +207,12 @@ func (c *githubClient) CreateRelease(ctx *context.Context, body string) (string,
return "", err
}

if ctx.Config.Release.Draft && ctx.Config.Release.ReplaceExistingDraft {
if err := c.deleteExistedDraftRelease(ctx, title); err != nil {
return "", err
}
}

// Truncate the release notes if it's too long (github doesn't allow more than 125000 characters)
body = truncateReleaseBody(body)

Expand Down Expand Up @@ -357,3 +363,34 @@ func overrideGitHubClientAPI(ctx *context.Context, client *github.Client) error

return nil
}

func (c *githubClient) deleteExistedDraftRelease(ctx *context.Context, name string) error {
opt := github.ListOptions{PerPage: 50}
for {
releases, resp, err := c.client.Repositories.ListReleases(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
&opt,
)
if err != nil {
return fmt.Errorf("could not delete existing drafts: %w", err)
}
for _, r := range releases {
if r.GetDraft() && r.GetName() == name {
if _, err := c.client.Repositories.DeleteRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
r.GetID(),
); err != nil {
return fmt.Errorf("could not delete previous draft release: %w", err)
}
}
}
if resp.NextPage == 0 {
return nil
}
opt.Page = resp.NextPage
}
}
1 change: 1 addition & 0 deletions pkg/config/config.go
Expand Up @@ -500,6 +500,7 @@ type Release struct {
GitLab Repo `yaml:"gitlab,omitempty" json:"gitlab,omitempty"`
Gitea Repo `yaml:"gitea,omitempty" json:"gitea,omitempty"`
Draft bool `yaml:"draft,omitempty" json:"draft,omitempty"`
ReplaceExistingDraft bool `yaml:"replace_existing_draft,omitempty" json:"replace_existing_draft,omitempty"`
Disable bool `yaml:"disable,omitempty" json:"disable,omitempty"`
SkipUpload bool `yaml:"skip_upload,omitempty" json:"skip_upload,omitempty"`
Prerelease string `yaml:"prerelease,omitempty" json:"prerelease,omitempty"`
Expand Down
13 changes: 10 additions & 3 deletions www/docs/customization/release.md
Expand Up @@ -24,9 +24,16 @@ release:
- bar

# If set to true, will not auto-publish the release.
# Available only for GitHub and Gitea.
# Default is false.
draft: true

# Whether to remove existing draft releases with the same name before creating a new one.
# Only effective if `draft` is set to true.
# Available only for GitHub.
# Default is false.
replace_existing_draft: true

# If set, will create a release discussion in the category specified.
#
# Warning: do not use categories in the 'Announcement' format.
Expand Down Expand Up @@ -97,7 +104,7 @@ release:
```

!!! tip
[Learn how to setup an API token, GitHub Enterprise and etc](/scm/github/).
[Learn how to set up an API token, GitHub Enterprise, etc](/scm/github/).

## GitLab

Expand Down Expand Up @@ -153,7 +160,7 @@ release:
```

!!! tip
[Learn how to setup an API token, self-hosted GitLab and etc](/scm/gitlab/).
[Learn how to set up an API token, self-hosted GitLab, etc](/scm/gitlab/).

!!! tip
If you use GitLab subgroups, you need to specify it in the `owner` field, e.g. `mygroup/mysubgroup`.
Expand Down Expand Up @@ -220,7 +227,7 @@ ALLOWED_TYPES = application/gzip|application/x-gzip|application/x-gtar|applicati
```

!!! tip
[Learn how to setup an API token](/scm/gitea/).
[Learn how to set up an API token](/scm/gitea/).

!!! tip
Learn more about the [name template engine](/customization/templates/).
Expand Down