Skip to content

Add helper to get runID from Custom Deployment Protection Rule Event #3476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
309da8b
add helper function to parse DeploymentCallBackURL to get RunID
pputman-clabs Feb 12, 2025
19449c0
added a test to make sure it got the runID properly
pputman-clabs Feb 12, 2025
52886c4
Add check for regex match
pputman-clabs Feb 12, 2025
4d2ad31
fix runID regex to only match on integers
pputman-clabs Feb 12, 2025
fa18394
add test for incorrect runID string
pputman-clabs Feb 12, 2025
918be15
revert go.mod and go.sum
pputman-clabs Feb 13, 2025
20afbb0
go format
pputman-clabs Feb 13, 2025
6cf202a
Merge branch 'master' into pputman/add-helper-function
pputman-clabs Feb 13, 2025
8564c96
remove error type checking as we only need to see if an error is retu…
pputman-clabs Feb 13, 2025
fbb37e4
Merge branch 'pputman/add-helper-function' of github.com:pputman-clab…
pputman-clabs Feb 13, 2025
84cdc28
go format
pputman-clabs Feb 13, 2025
3e68ecc
touch up the regex
pputman-clabs Feb 13, 2025
421a652
remove new custom error type since it provides no real benefit
pputman-clabs Feb 13, 2025
4fb5ed1
add comments to actions_worklow_runs.go methods that use RunID about …
pputman-clabs Feb 13, 2025
ffea876
Merge branch 'master' into pputman/add-helper-function
pputman-clabs Feb 13, 2025
d0396e5
fix linting issues
pputman-clabs Feb 13, 2025
5c6ddb2
Merge branch 'pputman/add-helper-function' of github.com:pputman-clab…
pputman-clabs Feb 13, 2025
5211c38
Rename regex to something more descriptive
pputman-clabs Feb 13, 2025
af36bac
run script/generate.sh for linter
pputman-clabs Feb 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion github/actions_workflow_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner,
return runs, resp, nil
}

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

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

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

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

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

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

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

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

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

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

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

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

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

// ReviewCustomDeploymentProtectionRule approves or rejects custom deployment protection rules provided by a GitHub App for a workflow run.
// You can use the helper function *DeploymentProtectionRuleEvent.GetRunID() to easily retrieve the workflow run ID from a DeploymentProtectionRuleEvent.
//
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-custom-deployment-protection-rules-for-a-workflow-run
//
Expand Down
16 changes: 16 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"net/http"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -1770,3 +1771,18 @@
func (fn roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return fn(r)
}

var r = regexp.MustCompile(`^repos/.*/actions/runs/(\d+)/deployment_protection_rule$`)

// GetRunID is a Helper Function used to extract the workflow RunID from the *DeploymentProtectionRuleEvent.DeploymentCallBackURL.
func (e *DeploymentProtectionRuleEvent) GetRunID() (int64, error) {
match := r.FindStringSubmatch(*e.DeploymentCallbackURL)
if len(match) != 2 {
return -1, errors.New("no match")
}
runID, err := strconv.ParseInt(match[1], 10, 64)
if err != nil {
return -1, err
}

Check warning on line 1786 in github/github.go

View check run for this annotation

Codecov / codecov/patch

github/github.go#L1785-L1786

Added lines #L1785 - L1786 were not covered by tests
return runID, nil
}
28 changes: 28 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3101,3 +3101,31 @@ func TestPtr(t *testing.T) {
equal(t, int64(-10), *Ptr(int64(-10)))
equal(t, "str", *Ptr("str"))
}

func TestDeploymentProtectionRuleEvent_GetRunID(t *testing.T) {
t.Parallel()

var want int64 = 123456789
url := "repos/dummy-org/dummy-repo/actions/runs/123456789/deployment_protection_rule"

e := DeploymentProtectionRuleEvent{
DeploymentCallbackURL: &url,
}

got, _ := e.GetRunID()
if got != want {
t.Errorf("want %#v, got %#v", want, got)
}

want = -1
url = "repos/dummy-org/dummy-repo/actions/runs/abc123/deployment_protection_rule"

got, err := e.GetRunID()
if err == nil {
t.Errorf("Expected error to be returned")
}

if got != want {
t.Errorf("want %#v, got %#v", want, got)
}
}
Loading