From 3687c097cdd6123f315ebebd10ecf4c7e080b8f4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 22 Mar 2024 16:42:15 -0300 Subject: [PATCH] feat(winget): sync fork before opening PR closes https://github.com/goreleaser/goreleaser/issues/4720 Signed-off-by: Carlos Alexandro Becker --- internal/client/client.go | 5 +++++ internal/client/github.go | 27 +++++++++++++++++++++++++++ internal/client/mock.go | 7 +++++++ internal/pipe/winget/winget.go | 20 +++++++++++++++----- internal/pipe/winget/winget_test.go | 5 +++++ www/docs/includes/prs.md | 3 +-- 6 files changed, 60 insertions(+), 7 deletions(-) diff --git a/internal/client/client.go b/internal/client/client.go index 9b957fe53..13caec849 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -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 diff --git a/internal/client/github.go b/internal/client/github.go index 8deb31ad8..be81ce229 100644 --- a/internal/client/github.go +++ b/internal/client/github.go @@ -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, diff --git a/internal/client/mock.go b/internal/client/mock.go index b27ffb528..4d4b833ad 100644 --- a/internal/client/mock.go +++ b/internal/client/mock.go @@ -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 { diff --git a/internal/pipe/winget/winget.go b/internal/pipe/winget/winget.go index 831cae3a7..48d5aec54 100644 --- a/internal/pipe/winget/winget.go +++ b/internal/pipe/winget/winget.go @@ -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 { diff --git a/internal/pipe/winget/winget_test.go b/internal/pipe/winget/winget_test.go index 1e1ef2c5e..d509cf5dd 100644 --- a/internal/pipe/winget/winget_test.go +++ b/internal/pipe/winget/winget_test.go @@ -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) } }) diff --git a/www/docs/includes/prs.md b/www/docs/includes/prs.md index 4effaf9af..c4f2bf264 100644 --- a/www/docs/includes/prs.md +++ b/www/docs/includes/prs.md @@ -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