1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-10 03:47:03 +02:00
goreleaser/internal/pipe/milestone/milestone.go
Brian Flad 01fd3e8c7b
feat: support closing milestones (#1657)
* feat: support closing milestones

Reference: https://github.com/goreleaser/goreleaser/issues/1415

* refactor: Adjust milestone handling for code simplification, add ErrNoMilestoneFound, and fix milestone documentation close default

Reference: https://github.com/goreleaser/goreleaser/pull/1657#pullrequestreview-445025743

* refactor: Use single repo config in milestones instead of each VCS

* fix: Ensure milestone Pipe is included in Defaulters

* feat: Add fail_on_error configuration to milestone configuration

Co-authored-by: Radek Simko <radek.simko@gmail.com>
2020-07-09 21:40:37 +01:00

99 lines
2.0 KiB
Go

package milestone
import (
"github.com/apex/log"
"github.com/goreleaser/goreleaser/internal/client"
"github.com/goreleaser/goreleaser/internal/git"
"github.com/goreleaser/goreleaser/internal/pipe"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
)
const defaultNameTemplate = "{{ .Tag }}"
// Pipe for milestone.
type Pipe struct{}
func (Pipe) String() string {
return "milestones"
}
// Default sets the pipe defaults.
func (Pipe) Default(ctx *context.Context) error {
if len(ctx.Config.Milestones) == 0 {
ctx.Config.Milestones = append(ctx.Config.Milestones, config.Milestone{})
}
for i := range ctx.Config.Milestones {
milestone := &ctx.Config.Milestones[i]
if milestone.NameTemplate == "" {
milestone.NameTemplate = defaultNameTemplate
}
if milestone.Repo.Name == "" {
repo, err := git.ExtractRepoFromConfig()
if err != nil && !ctx.Snapshot {
return err
}
milestone.Repo = repo
}
}
return nil
}
// Publish the release.
func (Pipe) Publish(ctx *context.Context) error {
if ctx.SkipPublish {
return pipe.ErrSkipPublishEnabled
}
c, err := client.New(ctx)
if err != nil {
return err
}
return doPublish(ctx, c)
}
func doPublish(ctx *context.Context, vcsClient client.Client) error {
for i := range ctx.Config.Milestones {
milestone := &ctx.Config.Milestones[i]
if !milestone.Close {
return pipe.Skip("milestone pipe is disabled")
}
name, err := tmpl.New(ctx).Apply(milestone.NameTemplate)
if err != nil {
return err
}
repo := client.Repo{
Name: milestone.Repo.Name,
Owner: milestone.Repo.Owner,
}
log.WithField("milestone", name).
WithField("repo", repo.String()).
Info("closing milestone")
err = vcsClient.CloseMilestone(ctx, repo, name)
if err != nil {
if milestone.FailOnError {
return err
}
log.WithField("milestone", name).
WithField("repo", repo.String()).
Warnf("error closing milestone: %s", err)
}
}
return nil
}