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

Commit 9a1f3d9

Browse files
authored
Merge pull request #40 from thepwagner/updater-name
Updater.Name()
2 parents 100e9f8 + 33f7199 commit 9a1f3d9

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

actions/updateaction/release.go

+8-4
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

+26
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

+5
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/mockupdater_test.go

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

updater/updater.go

+8-7
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
}
@@ -39,6 +39,7 @@ type Repo interface {
3939
}
4040

4141
type Updater interface {
42+
Name() string
4243
Dependencies(context.Context) ([]Dependency, error)
4344
Check(ctx context.Context, dep Dependency, filter func(string) bool) (*Update, error)
4445
ApplyUpdate(context.Context, Update) error
@@ -53,7 +54,7 @@ type Factory interface {
5354
func NewRepoUpdater(repo Repo, updater Updater, opts ...RepoUpdaterOpt) *RepoUpdater {
5455
u := &RepoUpdater{
5556
repo: repo,
56-
updater: updater,
57+
Updater: updater,
5758
branchNamer: DefaultUpdateBranchNamer{},
5859
}
5960
for _, opt := range opts {
@@ -82,7 +83,7 @@ func (u *RepoUpdater) Update(ctx context.Context, baseBranch, branchName string,
8283
return fmt.Errorf("switching to target branch: %w", err)
8384
}
8485
for _, update := range updates.Updates {
85-
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
86+
if err := u.Updater.ApplyUpdate(ctx, update); err != nil {
8687
return fmt.Errorf("applying update: %w", err)
8788
}
8889
}
@@ -117,7 +118,7 @@ func (u *RepoUpdater) updateBranch(ctx context.Context, log logrus.FieldLogger,
117118
}
118119

119120
// List dependencies while on this branch:
120-
deps, err := u.updater.Dependencies(ctx)
121+
deps, err := u.Updater.Dependencies(ctx)
121122
if err != nil {
122123
return fmt.Errorf("getting dependencies: %w", err)
123124
}
@@ -183,7 +184,7 @@ func (u *RepoUpdater) updateBranch(ctx context.Context, log logrus.FieldLogger,
183184
}
184185

185186
func (u *RepoUpdater) checkForUpdate(ctx context.Context, log logrus.FieldLogger, dep Dependency, filter func(string) bool) *Update {
186-
update, err := u.updater.Check(ctx, dep, filter)
187+
update, err := u.Updater.Check(ctx, dep, filter)
187188
if err != nil {
188189
log.WithError(err).Warn("error checking for updates")
189190
return nil
@@ -209,7 +210,7 @@ func (u *RepoUpdater) singleUpdate(ctx context.Context, log logrus.FieldLogger,
209210
if err := u.repo.NewBranch(baseBranch, branch); err != nil {
210211
return false, fmt.Errorf("switching to target branch: %w", err)
211212
}
212-
if err := u.updater.ApplyUpdate(ctx, *update); err != nil {
213+
if err := u.Updater.ApplyUpdate(ctx, *update); err != nil {
213214
return false, fmt.Errorf("applying batched update: %w", err)
214215
}
215216

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

256257
for _, update := range updates {
257-
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
258+
if err := u.Updater.ApplyUpdate(ctx, update); err != nil {
258259
return 0, fmt.Errorf("applying batched update: %w", err)
259260
}
260261
}

0 commit comments

Comments
 (0)