mirror of
https://github.com/goreleaser/goreleaser.git
synced 2025-02-07 13:31:37 +02:00
feat(winget): sync fork before opening PR
closes https://github.com/goreleaser/goreleaser/issues/4720 Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
This commit is contained in:
parent
10a4a20122
commit
3687c097cd
@ -90,6 +90,11 @@ type ReleaseNotesGenerator interface {
|
||||
GenerateReleaseNotes(ctx *context.Context, repo Repo, prev, current string) (string, error)
|
||||
}
|
||||
|
||||
// ForkSyncer can sync forks.
|
||||
type ForkSyncer interface {
|
||||
SyncFork(ctx *context.Context, head, base Repo) error
|
||||
}
|
||||
|
||||
// PullRequestOpener can open pull requests.
|
||||
type PullRequestOpener interface {
|
||||
OpenPullRequest(ctx *context.Context, base, head Repo, title string, draft bool) error
|
||||
|
@ -28,6 +28,7 @@ var (
|
||||
_ Client = &githubClient{}
|
||||
_ ReleaseNotesGenerator = &githubClient{}
|
||||
_ PullRequestOpener = &githubClient{}
|
||||
_ ForkSyncer = &githubClient{}
|
||||
)
|
||||
|
||||
type githubClient struct {
|
||||
@ -217,6 +218,7 @@ func (c *githubClient) OpenPullRequest(
|
||||
if len(tpl) > 0 {
|
||||
log.Info("got a pr template")
|
||||
}
|
||||
|
||||
log := log.
|
||||
WithField("base", headString(base, Repo{})).
|
||||
WithField("head", headString(base, head)).
|
||||
@ -245,6 +247,31 @@ func (c *githubClient) OpenPullRequest(
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *githubClient) SyncFork(ctx *context.Context, head, base Repo) error {
|
||||
branch := base.Branch
|
||||
if branch == "" {
|
||||
def, err := c.getDefaultBranch(ctx, base)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
branch = def
|
||||
}
|
||||
res, _, err := c.client.Repositories.MergeUpstream(
|
||||
ctx,
|
||||
head.Owner,
|
||||
head.Name,
|
||||
&github.RepoMergeUpstreamRequest{
|
||||
Branch: github.String(branch),
|
||||
},
|
||||
)
|
||||
if res != nil {
|
||||
log.WithField("merge_type", res.GetMergeType()).
|
||||
WithField("base_branch", res.GetBaseBranch()).
|
||||
Info(res.GetMessage())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *githubClient) CreateFile(
|
||||
ctx *context.Context,
|
||||
commitAuthor config.CommitAuthor,
|
||||
|
@ -16,6 +16,7 @@ var (
|
||||
_ Client = &Mock{}
|
||||
_ ReleaseNotesGenerator = &Mock{}
|
||||
_ PullRequestOpener = &Mock{}
|
||||
_ ForkSyncer = &Mock{}
|
||||
)
|
||||
|
||||
func NewMock() *Mock {
|
||||
@ -42,6 +43,12 @@ type Mock struct {
|
||||
ReleaseNotes string
|
||||
ReleaseNotesParams []string
|
||||
OpenedPullRequest bool
|
||||
SyncedFork bool
|
||||
}
|
||||
|
||||
func (c *Mock) SyncFork(ctx *context.Context, head Repo, base Repo) error {
|
||||
c.SyncedFork = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Mock) OpenPullRequest(_ *context.Context, _, _ Repo, _ string, _ bool) error {
|
||||
|
@ -315,6 +315,20 @@ func doPublish(ctx *context.Context, cl client.Client, wingets []*artifact.Artif
|
||||
return err
|
||||
}
|
||||
|
||||
base := client.Repo{
|
||||
Name: winget.Repository.PullRequest.Base.Name,
|
||||
Owner: winget.Repository.PullRequest.Base.Owner,
|
||||
Branch: winget.Repository.PullRequest.Base.Branch,
|
||||
}
|
||||
|
||||
// try to sync branch
|
||||
fscli, ok := cl.(client.ForkSyncer)
|
||||
if ok && winget.Repository.PullRequest.Enabled {
|
||||
if err := fscli.SyncFork(ctx, repo, base); err != nil {
|
||||
log.WithError(err).Warn("could not sync fork")
|
||||
}
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if err := cl.CreateFile(
|
||||
ctx,
|
||||
@ -339,11 +353,7 @@ func doPublish(ctx *context.Context, cl client.Client, wingets []*artifact.Artif
|
||||
return fmt.Errorf("client does not support pull requests")
|
||||
}
|
||||
|
||||
return pcl.OpenPullRequest(ctx, client.Repo{
|
||||
Name: winget.Repository.PullRequest.Base.Name,
|
||||
Owner: winget.Repository.PullRequest.Base.Owner,
|
||||
Branch: winget.Repository.PullRequest.Base.Branch,
|
||||
}, repo, msg, winget.Repository.PullRequest.Draft)
|
||||
return pcl.OpenPullRequest(ctx, base, repo, msg, winget.Repository.PullRequest.Draft)
|
||||
}
|
||||
|
||||
func langserverLineFor(tp artifact.Type) string {
|
||||
|
@ -127,6 +127,10 @@ func TestRunPipe(t *testing.T) {
|
||||
Branch: "update-{{.Version}}",
|
||||
PullRequest: config.PullRequest{
|
||||
Enabled: true,
|
||||
Base: config.PullRequestBase{
|
||||
Owner: "ms",
|
||||
Name: "winget",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@ -753,6 +757,7 @@ func TestRunPipe(t *testing.T) {
|
||||
}
|
||||
|
||||
if tt.winget.Repository.PullRequest.Enabled {
|
||||
require.True(t, client.SyncedFork)
|
||||
require.True(t, client.OpenedPullRequest)
|
||||
}
|
||||
})
|
||||
|
@ -41,6 +41,7 @@ something: # can be nix, brews, etc...
|
||||
|
||||
This will:
|
||||
|
||||
- Try to sync the `john/repo` fork with `mike/repo:main` (if on GitHub).
|
||||
- Create the files into `john/repo`, in the branch `foo-1.2.3` (assuming
|
||||
`ProjectName=foo` and `Version=1.2.3`). [^head]
|
||||
- Open a pull request from `john/repo` into `mike/repo`, with the branch `main`
|
||||
@ -51,8 +52,6 @@ This will:
|
||||
|
||||
### Things that don't work
|
||||
|
||||
- **GoReleaser will not keep your fork in sync!!!** It might or might not be a
|
||||
problem in your case, in which case you'll have to sync it manually.
|
||||
- Opening pull requests to a forked repository (`go-github` does not have the
|
||||
required fields to do it).
|
||||
- Since this can fail for a myriad of reasons, if an error happen, it'll log it
|
||||
|
Loading…
x
Reference in New Issue
Block a user