Skip to content

Commit 6c46d71

Browse files
Add helper to get runID from Custom Deployment Protection Rule Event (#3476)
1 parent f867d00 commit 6c46d71

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

github/actions_workflow_runs.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner,
204204
}
205205

206206
// GetWorkflowRunByID gets a specific workflow run by ID.
207+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
207208
//
208209
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run
209210
//
@@ -226,6 +227,7 @@ func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo str
226227
}
227228

228229
// GetWorkflowRunAttempt gets a specific workflow run attempt.
230+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
229231
//
230232
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run-attempt
231233
//
@@ -252,6 +254,7 @@ func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo
252254
}
253255

254256
// GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number.
257+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve a workflow run ID from the DeploymentProtectionRuleEvent.
255258
//
256259
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-attempt-logs
257260
//
@@ -302,6 +305,7 @@ func (s *ActionsService) getWorkflowRunAttemptLogsWithRateLimit(ctx context.Cont
302305
}
303306

304307
// RerunWorkflowByID re-runs a workflow by ID.
308+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID a the DeploymentProtectionRuleEvent.
305309
//
306310
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-workflow
307311
//
@@ -318,6 +322,7 @@ func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo stri
318322
}
319323

320324
// RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID.
325+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
321326
//
322327
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run
323328
//
@@ -335,6 +340,8 @@ func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo st
335340

336341
// RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID.
337342
//
343+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
344+
//
338345
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run
339346
//
340347
//meta:operation POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun
@@ -350,6 +357,7 @@ func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, j
350357
}
351358

352359
// CancelWorkflowRunByID cancels a workflow run by ID.
360+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
353361
//
354362
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#cancel-a-workflow-run
355363
//
@@ -366,6 +374,7 @@ func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo
366374
}
367375

368376
// GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
377+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
369378
//
370379
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-logs
371380
//
@@ -416,6 +425,7 @@ func (s *ActionsService) getWorkflowRunLogsWithRateLimit(ctx context.Context, u
416425
}
417426

418427
// DeleteWorkflowRun deletes a workflow run by ID.
428+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
419429
//
420430
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-a-workflow-run
421431
//
@@ -432,6 +442,7 @@ func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo stri
432442
}
433443

434444
// DeleteWorkflowRunLogs deletes all logs for a workflow run.
445+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
435446
//
436447
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-workflow-run-logs
437448
//
@@ -448,6 +459,7 @@ func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo
448459
}
449460

450461
// GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
462+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
451463
//
452464
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-workflow-run-usage
453465
//
@@ -470,6 +482,7 @@ func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, rep
470482
}
471483

472484
// GetPendingDeployments get all deployment environments for a workflow run that are waiting for protection rules to pass.
485+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
473486
//
474487
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-pending-deployments-for-a-workflow-run
475488
//
@@ -492,6 +505,7 @@ func (s *ActionsService) GetPendingDeployments(ctx context.Context, owner, repo
492505
}
493506

494507
// PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer.
508+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
495509
//
496510
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run
497511
//
@@ -514,6 +528,7 @@ func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo str
514528
}
515529

516530
// ReviewCustomDeploymentProtectionRule approves or rejects custom deployment protection rules provided by a GitHub App for a workflow run.
531+
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
517532
//
518533
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-custom-deployment-protection-rules-for-a-workflow-run
519534
//

github/github.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net/http"
2020
"net/url"
2121
"reflect"
22+
"regexp"
2223
"strconv"
2324
"strings"
2425
"sync"
@@ -1770,3 +1771,18 @@ type roundTripperFunc func(*http.Request) (*http.Response, error)
17701771
func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
17711772
return fn(r)
17721773
}
1774+
1775+
var runIDFromURLRE = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`)
1776+
1777+
// GetRunID is a Helper Function used to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL.
1778+
func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) {
1779+
match := runIDFromURLRE.FindStringSubmatch(*e.DeploymentCallbackURL)
1780+
if len(match) != 2 {
1781+
return -1, errors.New("no match")
1782+
}
1783+
runID, err := strconv.ParseInt(match[1], 10, 64)
1784+
if err != nil {
1785+
return -1, err
1786+
}
1787+
return runID, nil
1788+
}

github/github_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,3 +3101,31 @@ func TestPtr(t *testing.T) {
31013101
equal(t, int64(-10), *Ptr(int64(-10)))
31023102
equal(t, "str", *Ptr("str"))
31033103
}
3104+
3105+
func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) {
3106+
t.Parallel()
3107+
3108+
var want int64 = 123456789
3109+
url := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule"
3110+
3111+
e := DeploymentProtectionRuleEvent{
3112+
DeploymentCallbackURL: &url,
3113+
}
3114+
3115+
got, _ := e.GetRunID()
3116+
if got != want {
3117+
t.Errorf("want %#v, got %#v", want, got)
3118+
}
3119+
3120+
want = -1
3121+
url = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule"
3122+
3123+
got, err := e.GetRunID()
3124+
if err == nil {
3125+
t.Errorf("Expected error to be returned")
3126+
}
3127+
3128+
if got != want {
3129+
t.Errorf("want %#v, got %#v", want, got)
3130+
}
3131+
}

0 commit comments

Comments
 (0)