Skip to content

Commit

Permalink
Merge pull request #24 from Adirio/fix-handlers
Browse files Browse the repository at this point in the history
🐛 Fix open, edit and reopen handlers
  • Loading branch information
k8s-ci-robot committed Nov 20, 2020
2 parents f75088a + 3c71cbc commit 4ac3302
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
4 changes: 4 additions & 0 deletions verify/common.go
Expand Up @@ -91,6 +91,7 @@ func ActionsEntrypoint(cb ActionsCallback) {
if err != nil {
l.Fatalf(1, "%v", err)
}
l.Debugf("environment for %s/%s ready", env.Owner, env.Repo)

if err := cb(env); err != nil {
l.Fatalf(2, "%v", err)
Expand All @@ -99,11 +100,13 @@ func ActionsEntrypoint(cb ActionsCallback) {
}

func RunPlugins(plugins ...PRPlugin) ActionsCallback {
l.Debugf("creating cb for %d plugins", len(plugins))
return func(env *ActionsEnv) error {
res := make(chan error)
var done sync.WaitGroup

for _, plugin := range plugins {
l.Debugf("launching %q plugin", plugin.Name)
done.Add(1)
go func(plugin PRPlugin) {
defer done.Done()
Expand All @@ -117,6 +120,7 @@ func RunPlugins(plugins ...PRPlugin) ActionsCallback {
close(res)
}()

l.Debug("retrieving plugin results")
errCount := 0
for err := range res {
if err == nil {
Expand Down
25 changes: 19 additions & 6 deletions verify/plugin.go
Expand Up @@ -27,6 +27,13 @@ import (
"sigs.k8s.io/kubebuilder-release-tools/verify/pkg/log"
)

const (
actionOpen = "opened"
actionReopen = "reopened"
actionEdit = "edited"
actionSync = "synchronize"
)

// ErrorWithHelp allows PRPlugin.ProcessPR to provide extended descriptions
type ErrorWithHelp interface {
error
Expand All @@ -45,10 +52,12 @@ type PRPlugin struct {
// init initializes the PRPlugin
func (p *PRPlugin) init() {
p.Logger = log.NewFor(p.Name)
p.Debug("plugin initialized")
}

// processPR executes the provided ProcessPR and parses the result
func (p PRPlugin) processPR(pr *github.PullRequest) (conclusion, summary, text string, err error) {
p.Debug("execute the plugin checks")
text, err = p.ProcessPR(pr)

if err != nil {
Expand Down Expand Up @@ -173,7 +182,7 @@ func (p PRPlugin) resetCheckRun(client *github.Client, owner, repo string, headS
checkRun.GetID(),
github.UpdateCheckRunOptions{
Name: p.Name,
Status: github.String("in-progress"),
Status: Started.StringP(),
},
)

Expand Down Expand Up @@ -248,23 +257,24 @@ func (p PRPlugin) duplicateCheckRun(client *github.Client, owner, repo, headSHA
// entrypoint will call the corresponding handler
func (p PRPlugin) entrypoint(env *ActionsEnv) (err error) {
switch env.Event.GetAction() {
case "open":
case actionOpen:
err = p.onOpen(env)
case "reopen":
case actionReopen:
err = p.onReopen(env)
case "edit":
case actionEdit:
err = p.onEdit(env)
case "synchronize":
case actionSync:
err = p.onSync(env)
default:
// Do nothing
p.Warningf("action %q received with no defined procedure, skipping", env.Event.GetAction())
}

return
}

// onOpen handles "open" actions
func (p PRPlugin) onOpen(env *ActionsEnv) error {
p.Debugf("%q handler", actionOpen)
headSHA := env.Event.GetPullRequest().GetHead().GetSHA()

// Create the check run
Expand All @@ -279,6 +289,7 @@ func (p PRPlugin) onOpen(env *ActionsEnv) error {

// onReopen handles "reopen" actions
func (p PRPlugin) onReopen(env *ActionsEnv) error {
p.Debugf("%q handler", actionReopen)
headSHA := env.Event.GetPullRequest().GetHead().GetSHA()

// Get the check run
Expand All @@ -303,6 +314,7 @@ func (p PRPlugin) onReopen(env *ActionsEnv) error {

// onEdit handles "edit" actions
func (p PRPlugin) onEdit(env *ActionsEnv) error {
p.Debugf("%q handler", actionEdit)
headSHA := env.Event.GetPullRequest().GetHead().GetSHA()

// Reset the check run
Expand All @@ -317,6 +329,7 @@ func (p PRPlugin) onEdit(env *ActionsEnv) error {

// onSync handles "synchronize" actions
func (p PRPlugin) onSync(env *ActionsEnv) error {
p.Debugf("%q handler", actionSync)
before, after := env.Event.GetBefore(), env.Event.GetAfter()

// Get the check run
Expand Down

0 comments on commit 4ac3302

Please sign in to comment.