From d36d47fe66d80e38ee5a8eaba76a0de503fd2267 Mon Sep 17 00:00:00 2001 From: Callum Date: Sat, 9 Oct 2021 10:07:07 +0100 Subject: [PATCH] feat: adding workflow_dispatch webhook event --- .../horizontalrunnerautoscaler_types.go | 14 ++++++-- ...rwind.dev_horizontalrunnerautoscalers.yaml | 12 +++++++ .../horizontal_runner_autoscaler_webhook.go | 9 +++++ ...autoscaler_webhook_on_workflow_dispatch.go | 35 +++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 controllers/horizontal_runner_autoscaler_webhook_on_workflow_dispatch.go diff --git a/api/v1alpha1/horizontalrunnerautoscaler_types.go b/api/v1alpha1/horizontalrunnerautoscaler_types.go index 89a23584..74a37e90 100644 --- a/api/v1alpha1/horizontalrunnerautoscaler_types.go +++ b/api/v1alpha1/horizontalrunnerautoscaler_types.go @@ -69,9 +69,10 @@ type ScaleUpTrigger struct { } type GitHubEventScaleUpTriggerSpec struct { - CheckRun *CheckRunSpec `json:"checkRun,omitempty"` - PullRequest *PullRequestSpec `json:"pullRequest,omitempty"` - Push *PushSpec `json:"push,omitempty"` + CheckRun *CheckRunSpec `json:"checkRun,omitempty"` + PullRequest *PullRequestSpec `json:"pullRequest,omitempty"` + Push *PushSpec `json:"push,omitempty"` + WorkflowDispatch *WorkflowDispatchSpec `json:"push,omitempty"` } // https://docs.github.com/en/actions/reference/events-that-trigger-workflows#check_run @@ -101,6 +102,13 @@ type PullRequestSpec struct { type PushSpec struct { } +// WorkflowDispatchSpec is the condition for triggering scale-up on push event +// Also see https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch +type WorkflowDispatchSpec struct { + Branches []string `json:"branches,omitempty"` + BranchesIgnore []string `json:"branchesIgnore,omitempty"` +} + // CapacityReservation specifies the number of replicas temporarily added // to the scale target until ExpirationTime. type CapacityReservation struct { diff --git a/config/crd/bases/actions.summerwind.dev_horizontalrunnerautoscalers.yaml b/config/crd/bases/actions.summerwind.dev_horizontalrunnerautoscalers.yaml index 3b82566a..f8167513 100644 --- a/config/crd/bases/actions.summerwind.dev_horizontalrunnerautoscalers.yaml +++ b/config/crd/bases/actions.summerwind.dev_horizontalrunnerautoscalers.yaml @@ -157,6 +157,18 @@ spec: push: description: PushSpec is the condition for triggering scale-up on push event Also see https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push type: object + workflowDispatch: + description: WorkflowDispatchSpec is the condition for triggering scale-up on workflow_dispatch event Also see https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch + properties: + branches: + items: + type: string + type: array + branchesIgnore: + items: + type: string + type: array + type: object type: object type: object type: array diff --git a/controllers/horizontal_runner_autoscaler_webhook.go b/controllers/horizontal_runner_autoscaler_webhook.go index b522906e..d8e1ea84 100644 --- a/controllers/horizontal_runner_autoscaler_webhook.go +++ b/controllers/horizontal_runner_autoscaler_webhook.go @@ -183,6 +183,15 @@ func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) Handle(w http.Respons "action", e.GetAction(), ) } + case *gogithub.WorkflowDispatchEvent: + target, err = autoscaler.getScaleUpTarget( + context.TODO(), + log, + e.Repo.GetName(), + e.Repo.Owner.GetLogin(), + e.Repo.Owner.GetType(), + autoscaler.WorkflowDispatchEvent(e), + ) case *gogithub.WorkflowJobEvent: if workflowJob := e.GetWorkflowJob(); workflowJob != nil { log = log.WithValues( diff --git a/controllers/horizontal_runner_autoscaler_webhook_on_workflow_dispatch.go b/controllers/horizontal_runner_autoscaler_webhook_on_workflow_dispatch.go new file mode 100644 index 00000000..33b123fd --- /dev/null +++ b/controllers/horizontal_runner_autoscaler_webhook_on_workflow_dispatch.go @@ -0,0 +1,35 @@ +package controllers + +import ( + "github.com/actions-runner-controller/actions-runner-controller/api/v1alpha1" + "github.com/google/go-github/v37/github" +) + +// MatchPushEvent() +func (autoscaler *HorizontalRunnerAutoscalerGitHubWebhook) MatchWorkflowDispatchEvent(event *github.PushEvent) func(scaleUpTrigger v1alpha1.ScaleUpTrigger) bool { + return func(scaleUpTrigger v1alpha1.ScaleUpTrigger) bool { + g := scaleUpTrigger.GitHubEvent + + if g == nil { + return false + } + + WorkflowDispatch := g.WorkflowDispatch + + if WorkflowDispatch == nil { + return false + } + + // event.Ref = Branch that received dispatch + // event.Ref = refs/heads/branch-name + if !matchTriggerConditionAgainstEvent(WorkflowDispatch.Branches, event.Ref) { + return false + } + + if matchTriggerConditionAgainstEvent(WorkflowDispatch.BranchesIgnore, event.Ref) { + return false + } + + return true + } +}