Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bduffany committed Feb 28, 2024
1 parent a405616 commit f82c20a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
30 changes: 25 additions & 5 deletions enterprise/server/githubapp/githubapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const (

// Max amount of time that a runner is allowed to run for until it is
// killed. This is just a safeguard for now; we eventually should remove it.
runnerTimeout = 8 * time.Hour
runnerTimeout = 1 * time.Hour
)

//go:embed runner.sh
Expand Down Expand Up @@ -209,13 +209,21 @@ func (a *GitHubApp) handleWebhookRequest(w http.ResponseWriter, req *http.Reques
}

func (a *GitHubApp) handleWebhookEvent(ctx context.Context, eventType string, event any) error {
// Delegate to the appropriate handler func based on event type.
switch event := event.(type) {
case *github.InstallationEvent:
return a.handleInstallationEvent(ctx, eventType, event)
case *github.WorkflowJobEvent:
return a.handleWorkflowJobEvent(ctx, eventType, event)
case *github.PushEvent:
return a.handlePushEvent(ctx, eventType, event)
case *github.PullRequestEvent:
return a.handlePullRequestEvent(ctx, eventType, event)
case *github.PullRequestReviewEvent:
return a.handlePullRequestReviewEvent(ctx, eventType, event)
default:
return a.handleBuildBuddyWorkflowEvent(ctx, eventType, event)
// Event type not yet handled
return nil
}
}

Expand Down Expand Up @@ -257,12 +265,24 @@ func (a *GitHubApp) handleWorkflowJobEvent(ctx context.Context, eventType string
labels = event.WorkflowJob.Labels
}
if a.matchesRunnerLabels(labels) {
return a.startWorkflowJob(ctx, event)
return a.startGitHubActionsRunnerTask(ctx, event)
}
}
return nil
}

func (a *GitHubApp) handlePushEvent(ctx context.Context, eventType string, event *github.PushEvent) error {
return a.maybeTriggerBuildBuddyWorkflow(ctx, eventType, event)
}

func (a *GitHubApp) handlePullRequestEvent(ctx context.Context, eventType string, event *github.PullRequestEvent) error {
return a.maybeTriggerBuildBuddyWorkflow(ctx, eventType, event)
}

func (a *GitHubApp) handlePullRequestReviewEvent(ctx context.Context, eventType string, event *github.PullRequestReviewEvent) error {
return a.maybeTriggerBuildBuddyWorkflow(ctx, eventType, event)
}

func (a *GitHubApp) matchesRunnerLabels(labels []string) bool {
for _, label := range labels {
if !slices.Contains(a.runnerLabels, label) {
Expand All @@ -272,7 +292,7 @@ func (a *GitHubApp) matchesRunnerLabels(labels []string) bool {
return true
}

func (a *GitHubApp) startWorkflowJob(ctx context.Context, event *github.WorkflowJobEvent) error {
func (a *GitHubApp) startGitHubActionsRunnerTask(ctx context.Context, event *github.WorkflowJobEvent) error {
if event.WorkflowJob == nil {
return status.FailedPreconditionError("workflow job cannot be nil")
}
Expand Down Expand Up @@ -359,7 +379,7 @@ func (a *GitHubApp) startWorkflowJob(ctx context.Context, event *github.Workflow
return nil
}

func (a *GitHubApp) handleBuildBuddyWorkflowEvent(ctx context.Context, eventType string, event any) error {
func (a *GitHubApp) maybeTriggerBuildBuddyWorkflow(ctx context.Context, eventType string, event any) error {
wd, err := gh_webhooks.ParseWebhookData(event)
if err != nil {
return err
Expand Down
5 changes: 4 additions & 1 deletion enterprise/server/githubapp/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ set -e
# This script wraps GitHub's runner, starting it with the one-time-use config
# provided as the environment variable $RUNNER_ENCODED_JITCONFIG. It also
# handles killing the runner if it does not accept a job within the configured
# idle timeout, passed as $RUNNER_IDLE_TIMEOUT.
# idle timeout, passed as $RUNNER_IDLE_TIMEOUT. This prevents runner tasks from
# waiting around forever in case the number of runners exceeds the number of
# available jobs, for example if a workflow job is canceled.

# TODO(bduffany): rewrite this in Go

# Turn on job control so that each background job starts in its own process
Expand Down

0 comments on commit f82c20a

Please sign in to comment.