Skip to content

Commit d926b93

Browse files
committed
Retrieve pull requests for workflo_run events from forks
This change causes workflow_run events from forks to be properly handled. Github doesn't provide PR information for these events if they originate from a PR from a fork, so we need to fetch this before processing. This will fix issues with dependency handling in fork-based PRs. Signed-off-by: Maciej Kwiek
1 parent 4dc2406 commit d926b93

File tree

2 files changed

+146
-84
lines changed

2 files changed

+146
-84
lines changed

internal/handlers/workflow_run.go

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,41 +43,53 @@ func (w *WorkflowRunHandler) Handle(ctx context.Context, eventType, deliveryID s
4343

4444
workflowRun := event.GetWorkflowRun()
4545
conclusion := workflowRun.GetConclusion()
46+
if conclusion == "cancelled" {
47+
logger.Debug().Msg("Workflow run was cancelled, skipping")
48+
return nil
49+
}
4650

4751
// Get the associated pull requests
48-
pullRequests := workflowRun.PullRequests
52+
pullRequestsFromWorkflowRun := workflowRun.PullRequests
4953

50-
if len(pullRequests) == 0 {
54+
client, err := w.NewInstallationClient(installationID)
55+
if err != nil {
56+
logger.Error().Err(err).Msg("Failed to create GitHub client")
57+
return err
58+
}
59+
60+
repositoryOwner := repository.GetOwner().GetLogin()
61+
repositoryName := repository.GetName()
62+
63+
if len(pullRequestsFromWorkflowRun) == 0 && !workflowRun.GetHeadRepository().GetFork() {
5164
logger.Debug().Msg("No pull requests associated with this workflow run")
5265
return nil
5366
}
5467

55-
client, err := w.NewInstallationClient(installationID)
68+
prHead := workflowRun.GetActor().GetLogin() + ":" + workflowRun.GetHeadBranch()
69+
// get PR details for all PRs associated with head branch of this workflow run
70+
fullPullRequests, _, err := client.PullRequests.List(ctx, repositoryOwner, repositoryName, &github.PullRequestListOptions{
71+
Head: prHead,
72+
})
5673
if err != nil {
74+
logger.Error().Err(err).Msgf("Failed to get PRs for head %s", prHead)
5775
return err
5876
}
5977

60-
repositoryOwner := repository.GetOwner().GetLogin()
61-
repositoryName := repository.GetName()
78+
if len(fullPullRequests) == 0 {
79+
logger.Debug().Msgf("No pull requests associated with this workflow run head: %s", prHead)
80+
return nil
81+
}
6282

6383
var fullPR *github.PullRequest
6484

6585
var arianeConfig *config.ArianeConfig = nil
6686

6787
// Check if PR creator matches required pattern (starts with repo owner and ends with [bot])
6888
// If not, check if they are an allowed team member in the config
69-
// We are getting pull request details in order to get the creator's login, which is not included in the workflow run payload.
70-
for _, pr := range pullRequests {
71-
prNumber := pr.GetNumber()
72-
fullPR, _, err = client.PullRequests.Get(ctx, repositoryOwner, repositoryName, prNumber)
73-
if err != nil {
74-
logger.Error().Err(err).Msgf("Failed to get PR #%d details", prNumber)
75-
continue
76-
}
77-
89+
for _, pr := range fullPullRequests {
7890
if arianeConfig == nil {
7991
// Retrieve Ariane configuration from repository based on first PR
80-
contextRef, _, _ := determineContextRef(fullPR, repositoryOwner, repositoryName, logger)
92+
contextRef, _, _ := determineContextRef(pr, repositoryOwner, repositoryName, logger)
8193

8294
// retrieve Ariane configuration (triggers, etc.) from repository based on chosen context
8395
arianeConfig, err = configGetArianeConfigFromRepository(client, ctx, repositoryOwner, repositoryName, contextRef)
@@ -87,16 +99,18 @@ func (w *WorkflowRunHandler) Handle(ctx context.Context, eventType, deliveryID s
8799
}
88100
}
89101

90-
prCreator := fullPR.GetUser().GetLogin()
102+
prCreator := pr.GetUser().GetLogin()
91103
if !strings.HasPrefix(prCreator, repositoryOwner) || !strings.HasSuffix(prCreator, "[bot]") {
92-
logger.Debug().Msgf("PR #%d creator '%s' does not match required pattern (prefix: '%s', suffix: '[bot]'), checking config", prNumber, prCreator, repositoryOwner)
104+
logger.Debug().Msgf("PR #%d creator '%s' does not match required pattern (prefix: '%s', suffix: '[bot]'), checking config", pr.GetNumber(), prCreator, repositoryOwner)
93105

94106
if !isAllowedTeamMember(ctx, client, arianeConfig, repositoryOwner, prCreator, logger) {
95-
logger.Debug().Msgf("PR #%d creator '%s' is not an allowed team member, skipping", prNumber, prCreator)
96-
fullPR = nil
107+
logger.Debug().Msgf("PR #%d creator '%s' is not an allowed team member, skipping", pr.GetNumber(), prCreator)
97108
continue
98109
}
99110
}
111+
// found a PR with an allowed creator, we can proceed with handling this workflow run
112+
fullPR = pr
113+
break
100114
}
101115

102116
if fullPR == nil {

0 commit comments

Comments
 (0)