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

Commit

Permalink
Merge pull request #40 from thepwagner/updater-name
Browse files Browse the repository at this point in the history
Updater.Name()
  • Loading branch information
thepwagner authored Jan 9, 2021
2 parents 100e9f8 + 33f7199 commit 9a1f3d9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
12 changes: 8 additions & 4 deletions actions/updateaction/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/thepwagner/action-update/repo"
)

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

dispatchOpts, err := releaseDispatchOptions(evt, feedbackIssue)
dispatchOpts, err := h.releaseDispatchOptions(evt, feedbackIssue)
if err != nil {
return err
}
Expand Down Expand Up @@ -66,10 +69,11 @@ func releaseFeedbackIssue(ctx context.Context, gh *github.Client, evt *github.Re
return issue, err
}

func releaseDispatchOptions(evt *github.ReleaseEvent, feedbackIssue *github.Issue) (github.DispatchRequestOptions, error) {
func (h *handler) releaseDispatchOptions(evt *github.ReleaseEvent, feedbackIssue *github.Issue) (github.DispatchRequestOptions, error) {
payload, err := json.Marshal(&RepoDispatchActionUpdatePayload{
Path: fmt.Sprintf("github.com/%s", evt.GetRepo().GetFullName()),
Next: evt.GetRelease().GetTagName(),
Updater: h.updaterFactory.NewUpdater("").Name(),
Path: fmt.Sprintf("github.com/%s", evt.GetRepo().GetFullName()),
Next: evt.GetRelease().GetTagName(),
Feedback: RepoDispatchActionUpdatePayloadFeedback{
Owner: evt.GetRepo().GetOwner().GetLogin(),
Name: evt.GetRepo().GetName(),
Expand Down
26 changes: 26 additions & 0 deletions actions/updateaction/release_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package updateaction_test

import (
"context"
"testing"

"github.com/google/go-github/v33/github"
"github.com/stretchr/testify/require"
"github.com/thepwagner/action-update/actions/updateaction"
)

func TestHandler_Release_WrongAction(t *testing.T) {
handlers := updateaction.NewHandlers(&testEnvironment{})
err := handlers.Release(context.Background(), &github.ReleaseEvent{
Action: github.String("prereleased"),
})
require.NoError(t, err)
}

func TestHandler_Release_NoRepos(t *testing.T) {
handlers := updateaction.NewHandlers(&testEnvironment{})
err := handlers.Release(context.Background(), &github.ReleaseEvent{
Action: github.String("released"),
})
require.NoError(t, err)
}
5 changes: 5 additions & 0 deletions actions/updateaction/repositorydispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ func (h *handler) repoDispatchActionUpdate(ctx context.Context, evt *github.Repo
if err != nil {
return fmt.Errorf("getting RepoUpdater: %w", err)
}
if payload.Updater != "" && repoUpdater.Updater.Name() != payload.Updater {
logrus.WithField("updater", payload.Updater).Info("skipping event for other updaters")
return nil
}

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

type RepoDispatchActionUpdatePayload struct {
Updater string `json:"updater"`
Path string `json:"path"`
Next string `json:"next"`
Feedback RepoDispatchActionUpdatePayloadFeedback `json:"feedback"`
Expand Down
14 changes: 14 additions & 0 deletions updater/mockupdater_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// RepoUpdater creates branches proposing all available updates for a Go module.
type RepoUpdater struct {
repo Repo
updater Updater
Updater Updater
groups Groups
branchNamer UpdateBranchNamer
}
Expand All @@ -39,6 +39,7 @@ type Repo interface {
}

type Updater interface {
Name() string
Dependencies(context.Context) ([]Dependency, error)
Check(ctx context.Context, dep Dependency, filter func(string) bool) (*Update, error)
ApplyUpdate(context.Context, Update) error
Expand All @@ -53,7 +54,7 @@ type Factory interface {
func NewRepoUpdater(repo Repo, updater Updater, opts ...RepoUpdaterOpt) *RepoUpdater {
u := &RepoUpdater{
repo: repo,
updater: updater,
Updater: updater,
branchNamer: DefaultUpdateBranchNamer{},
}
for _, opt := range opts {
Expand Down Expand Up @@ -82,7 +83,7 @@ func (u *RepoUpdater) Update(ctx context.Context, baseBranch, branchName string,
return fmt.Errorf("switching to target branch: %w", err)
}
for _, update := range updates.Updates {
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
if err := u.Updater.ApplyUpdate(ctx, update); err != nil {
return fmt.Errorf("applying update: %w", err)
}
}
Expand Down Expand Up @@ -117,7 +118,7 @@ func (u *RepoUpdater) updateBranch(ctx context.Context, log logrus.FieldLogger,
}

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

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

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

for _, update := range updates {
if err := u.updater.ApplyUpdate(ctx, update); err != nil {
if err := u.Updater.ApplyUpdate(ctx, update); err != nil {
return 0, fmt.Errorf("applying batched update: %w", err)
}
}
Expand Down

0 comments on commit 9a1f3d9

Please sign in to comment.