Skip to content

Commit

Permalink
Add RerunFailedJobsByID and RerunJobByID (#2345)
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-chris committed May 13, 2022
1 parent 4174796 commit 8947206
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
28 changes: 28 additions & 0 deletions github/actions_workflow_runs.go
Expand Up @@ -211,6 +211,34 @@ func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo stri
return s.client.Do(ctx, req, nil)
}

// RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run
func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID)

req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}

// RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run
func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID)

req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}

// CancelWorkflowRunByID cancels a workflow run by ID.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#cancel-a-workflow-run
Expand Down
58 changes: 58 additions & 0 deletions github/actions_workflow_runs_test.go
Expand Up @@ -216,6 +216,64 @@ func TestActionsService_RerunWorkflowRunByID(t *testing.T) {
})
}

func TestActionsService_RerunFailedJobsByID(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/actions/runs/3434/rerun-failed-jobs", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusCreated)
})

ctx := context.Background()
resp, err := client.Actions.RerunFailedJobsByID(ctx, "o", "r", 3434)
if err != nil {
t.Errorf("Actions.RerunFailedJobsByID returned error: %v", err)
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Actions.RerunFailedJobsByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
}

const methodName = "RerunFailedJobsByID"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.RerunFailedJobsByID(ctx, "\n", "\n", 3434)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.RerunFailedJobsByID(ctx, "o", "r", 3434)
})
}

func TestActionsService_RerunJobByID(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/actions/jobs/3434/rerun", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
w.WriteHeader(http.StatusCreated)
})

ctx := context.Background()
resp, err := client.Actions.RerunJobByID(ctx, "o", "r", 3434)
if err != nil {
t.Errorf("Actions.RerunJobByID returned error: %v", err)
}
if resp.StatusCode != http.StatusCreated {
t.Errorf("Actions.RerunJobByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated)
}

const methodName = "RerunJobByID"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.RerunJobByID(ctx, "\n", "\n", 3434)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.RerunJobByID(ctx, "o", "r", 3434)
})
}

func TestActionsService_CancelWorkflowRunByID(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down

0 comments on commit 8947206

Please sign in to comment.