Skip to content

Commit

Permalink
Merge pull request #9 from z0rc/main
Browse files Browse the repository at this point in the history
Allow `pull_request_target` event to run action
  • Loading branch information
desbo authored Mar 24, 2021
2 parents b22b099 + 36e8e17 commit e5cd901
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ commits.message = "Upgrade ${artifactName} from ${currentVersion} to ${nextVersi
```

## Caveats and future work
At the moment the action only runs in response to [`pull_request` events](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request). This means that, for the merge to take place, the check suite triggered by the PR being opened must pass and if this isn't the case (for example if a test fails), the merge action won't run, and the PR will need to be merged manually.
At the moment the action only runs in response to [`pull_request`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request) and [`pull_request_target`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request_target) events. This means that, for the merge to take place, the check suite triggered by the PR being opened must pass and if this isn't the case (for example if a test fails), the merge action won't run, and the PR will need to be merged manually.

It would be possible to update the action to also run on successful [`check_suite` events](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#check_suite); this would require some changes to access the PR title, which is not included in the event payload for non-`pull_request` events.

Expand Down
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const (
mergeMethodVariable = "INPUT_MERGE_METHOD"
)

var (
allowedEvents = []string{"pull_request", "pull_request_target"}
)

type pullRequestEvent struct {
PullRequest github.PullRequest `json:"pull_request"`
}
Expand All @@ -31,9 +35,19 @@ func getRequiredEnvVar(name string) string {
return value
}

func checkAllowedEvent(event string) bool {
for _, i := range allowedEvents {
if i == event {
return true
}
}
return false
}

func main() {
if eventName := getRequiredEnvVar(eventNameVariable); eventName != "pull_request" {
log.Println("event is not `pull_request`, exiting")
eventName := getRequiredEnvVar(eventNameVariable)
if checkAllowedEvent(eventName) == false {
log.Printf("event `%v` is not one of %v, exiting", eventName, allowedEvents)
os.Exit(0)
return
}
Expand Down

0 comments on commit e5cd901

Please sign in to comment.