Skip to content
This repository was archived by the owner on Aug 6, 2021. It is now read-only.

Commit 33f7199

Browse files
committed
release->dispatch filter by updater
1 parent 6c0fa9b commit 33f7199

File tree

4 files changed

+46
-11
lines changed

4 files changed

+46
-11
lines changed

actions/updateaction/release.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"github.com/thepwagner/action-update/repo"
1212
)
1313

14+
// Release emits a `repository_dispatch` events to a list of repositories when a release is published.
15+
// The dispatched event should be handled by RepositoryDispatch(), to update the triggered repositories
16+
// to the release that was just made.
1417
func (h *handler) Release(ctx context.Context, evt *github.ReleaseEvent) error {
1518
if evt.GetAction() != "released" {
1619
logrus.WithField("action", evt.GetAction()).Info("ignoring release event")
@@ -29,7 +32,7 @@ func (h *handler) Release(ctx context.Context, evt *github.ReleaseEvent) error {
2932
}
3033
logrus.WithField("issue_number", feedbackIssue.Number).Debug("created feedback issue")
3134

32-
dispatchOpts, err := releaseDispatchOptions(evt, feedbackIssue)
35+
dispatchOpts, err := h.releaseDispatchOptions(evt, feedbackIssue)
3336
if err != nil {
3437
return err
3538
}
@@ -66,10 +69,11 @@ func releaseFeedbackIssue(ctx context.Context, gh *github.Client, evt *github.Re
6669
return issue, err
6770
}
6871

69-
func releaseDispatchOptions(evt *github.ReleaseEvent, feedbackIssue *github.Issue) (github.DispatchRequestOptions, error) {
72+
func (h *handler) releaseDispatchOptions(evt *github.ReleaseEvent, feedbackIssue *github.Issue) (github.DispatchRequestOptions, error) {
7073
payload, err := json.Marshal(&RepoDispatchActionUpdatePayload{
71-
Path: fmt.Sprintf("github.com/%s", evt.GetRepo().GetFullName()),
72-
Next: evt.GetRelease().GetTagName(),
74+
Updater: h.updaterFactory.NewUpdater("").Name(),
75+
Path: fmt.Sprintf("github.com/%s", evt.GetRepo().GetFullName()),
76+
Next: evt.GetRelease().GetTagName(),
7377
Feedback: RepoDispatchActionUpdatePayloadFeedback{
7478
Owner: evt.GetRepo().GetOwner().GetLogin(),
7579
Name: evt.GetRepo().GetName(),

actions/updateaction/release_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package updateaction_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/go-github/v33/github"
8+
"github.com/stretchr/testify/require"
9+
"github.com/thepwagner/action-update/actions/updateaction"
10+
)
11+
12+
func TestHandler_Release_WrongAction(t *testing.T) {
13+
handlers := updateaction.NewHandlers(&testEnvironment{})
14+
err := handlers.Release(context.Background(), &github.ReleaseEvent{
15+
Action: github.String("prereleased"),
16+
})
17+
require.NoError(t, err)
18+
}
19+
20+
func TestHandler_Release_NoRepos(t *testing.T) {
21+
handlers := updateaction.NewHandlers(&testEnvironment{})
22+
err := handlers.Release(context.Background(), &github.ReleaseEvent{
23+
Action: github.String("released"),
24+
})
25+
require.NoError(t, err)
26+
}

actions/updateaction/repositorydispatch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ func (h *handler) repoDispatchActionUpdate(ctx context.Context, evt *github.Repo
8888
if err != nil {
8989
return fmt.Errorf("getting RepoUpdater: %w", err)
9090
}
91+
if payload.Updater != "" && repoUpdater.Updater.Name() != payload.Updater {
92+
logrus.WithField("updater", payload.Updater).Info("skipping event for other updaters")
93+
return nil
94+
}
9195

9296
ug := updater.NewUpdateGroup("", update)
9397
if err := repoUpdater.Update(ctx, baseBranch, branchName, ug); err != nil {
@@ -98,6 +102,7 @@ func (h *handler) repoDispatchActionUpdate(ctx context.Context, evt *github.Repo
98102
}
99103

100104
type RepoDispatchActionUpdatePayload struct {
105+
Updater string `json:"updater"`
101106
Path string `json:"path"`
102107
Next string `json:"next"`
103108
Feedback RepoDispatchActionUpdatePayloadFeedback `json:"feedback"`

updater/updater.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
// RepoUpdater creates branches proposing all available updates for a Go module.
1414
type RepoUpdater struct {
1515
repo Repo
16-
updater Updater
16+
Updater Updater
1717
groups Groups
1818
branchNamer UpdateBranchNamer
1919
}
@@ -54,7 +54,7 @@ type Factory interface {
5454
func NewRepoUpdater(repo Repo, updater Updater, opts ...RepoUpdaterOpt) *RepoUpdater {
5555
u := &RepoUpdater{
5656
repo: repo,
57-
updater: updater,
57+
Updater: updater,
5858
branchNamer: DefaultUpdateBranchNamer{},
5959
}
6060
for _, opt := range opts {
@@ -83,7 +83,7 @@ func (u *RepoUpdater) Update(ctx context.Context, baseBranch, branchName string,
8383
return fmt.Errorf("switching to target branch: %w", err)
8484
}
8585
for _, update := range updates.Updates {
86-
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
86+
if err := u.Updater.ApplyUpdate(ctx, update); err != nil {
8787
return fmt.Errorf("applying update: %w", err)
8888
}
8989
}
@@ -118,7 +118,7 @@ func (u *RepoUpdater) updateBranch(ctx context.Context, log logrus.FieldLogger,
118118
}
119119

120120
// List dependencies while on this branch:
121-
deps, err := u.updater.Dependencies(ctx)
121+
deps, err := u.Updater.Dependencies(ctx)
122122
if err != nil {
123123
return fmt.Errorf("getting dependencies: %w", err)
124124
}
@@ -184,7 +184,7 @@ func (u *RepoUpdater) updateBranch(ctx context.Context, log logrus.FieldLogger,
184184
}
185185

186186
func (u *RepoUpdater) checkForUpdate(ctx context.Context, log logrus.FieldLogger, dep Dependency, filter func(string) bool) *Update {
187-
update, err := u.updater.Check(ctx, dep, filter)
187+
update, err := u.Updater.Check(ctx, dep, filter)
188188
if err != nil {
189189
log.WithError(err).Warn("error checking for updates")
190190
return nil
@@ -210,7 +210,7 @@ func (u *RepoUpdater) singleUpdate(ctx context.Context, log logrus.FieldLogger,
210210
if err := u.repo.NewBranch(baseBranch, branch); err != nil {
211211
return false, fmt.Errorf("switching to target branch: %w", err)
212212
}
213-
if err := u.updater.ApplyUpdate(ctx, *update); err != nil {
213+
if err := u.Updater.ApplyUpdate(ctx, *update); err != nil {
214214
return false, fmt.Errorf("applying batched update: %w", err)
215215
}
216216

@@ -255,7 +255,7 @@ func (u *RepoUpdater) groupedUpdate(ctx context.Context, log logrus.FieldLogger,
255255
}
256256

257257
for _, update := range updates {
258-
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
258+
if err := u.Updater.ApplyUpdate(ctx, update); err != nil {
259259
return 0, fmt.Errorf("applying batched update: %w", err)
260260
}
261261
}

0 commit comments

Comments
 (0)