Skip to content

Update Create/Update methods to return the workflow #2759

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 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 20 additions & 6 deletions github/actions_required_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ func (s *ActionsService) ListOrgRequiredWorkflows(ctx context.Context, org strin
// CreateRequiredWorkflow creates the required workflow in an org.
//
// GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#create-a-required-workflow
func (s *ActionsService) CreateRequiredWorkflow(ctx context.Context, org string, createRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*Response, error) {
func (s *ActionsService) CreateRequiredWorkflow(ctx context.Context, org string, createRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) {
url := fmt.Sprintf("orgs/%v/actions/required_workflows", org)
req, err := s.client.NewRequest("PUT", url, createRequiredWorkflowOptions)
if err != nil {
return nil, err
return nil, nil, err
}
return s.client.Do(ctx, req, nil)

orgRequiredWorkflow := new(OrgRequiredWorkflow)
resp, err := s.client.Do(ctx, req, orgRequiredWorkflow)
if err != nil {
return nil, resp, err
}

return orgRequiredWorkflow, resp, nil
}

// GetRequiredWorkflowByID get the RequiredWorkflows for an org by its ID.
Expand All @@ -124,13 +131,20 @@ func (s *ActionsService) GetRequiredWorkflowByID(ctx context.Context, owner stri
// UpdateRequiredWorkflow updates a required workflow in an org.
//
// GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#update-a-required-workflow
func (s *ActionsService) UpdateRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64, updateRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*Response, error) {
func (s *ActionsService) UpdateRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64, updateRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) {
url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", org, requiredWorkflowID)
req, err := s.client.NewRequest("PATCH", url, updateRequiredWorkflowOptions)
if err != nil {
return nil, err
return nil, nil, err
}
return s.client.Do(ctx, req, nil)

orgRequiredWorkflow := new(OrgRequiredWorkflow)
resp, err := s.client.Do(ctx, req, orgRequiredWorkflow)
if err != nil {
return nil, resp, err
}

return orgRequiredWorkflow, resp, nil
}

// DeleteRequiredWorkflow deletes a required workflow in an org.
Expand Down
84 changes: 75 additions & 9 deletions github/actions_required_workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,20 @@ func TestActionsService_CreateRequiredWorkflow(t *testing.T) {
testMethod(t, r, "PUT")
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, `{"workflow_file_path":".github/workflows/ci.yaml","repository_id":53,"scope":"selected","selected_repository_ids":[32,91]}`+"\n")
w.WriteHeader(http.StatusCreated)
fmt.Fprint(w, `{
"id": 2,
"name": "Required CI",
"path": ".github/workflows/ci.yml",
"scope": "selected",
"ref": "refs/head/main",
"state": "active",
"selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/required_workflows/2/repositories",
"created_at": "2020-01-22T19:33:08Z",
"updated_at": "2020-01-22T19:33:08Z",
"repository": {
"id": 53,
"name": "Hello-World",
"url": "https://api.github.com/repos/o/Hello-World"}}`)
})
input := &CreateUpdateRequiredWorkflowOptions{
WorkflowFilePath: String(".github/workflows/ci.yaml"),
Expand All @@ -95,20 +108,39 @@ func TestActionsService_CreateRequiredWorkflow(t *testing.T) {
SelectedRepositoryIDs: &SelectedRepoIDs{32, 91},
}
ctx := context.Background()
_, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input)

requiredWokflow, _, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input)
if err != nil {
t.Errorf("Actions.CreateRequiredWorkflow returned error: %v", err)
}
want := &OrgRequiredWorkflow{
ID: Int64(2),
Name: String("Required CI"),
Path: String(".github/workflows/ci.yml"),
Scope: String("selected"),
Ref: String("refs/head/main"),
State: String("active"),
SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/required_workflows/2/repositories"),
CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
Repository: &Repository{ID: Int64(53), URL: String("https://api.github.com/repos/o/Hello-World"), Name: String("Hello-World")},
}

if !cmp.Equal(requiredWokflow, want) {
t.Errorf("Actions.CreateRequiredWorkflow returned %+v, want %+v", requiredWokflow, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.CreateRequiredWorkflow(ctx, "o", input)
got, resp, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

Expand Down Expand Up @@ -169,7 +201,20 @@ func TestActionsService_UpdateRequiredWorkflow(t *testing.T) {
testMethod(t, r, "PATCH")
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, `{"workflow_file_path":".github/workflows/ci.yaml","repository_id":53,"scope":"selected","selected_repository_ids":[32,91]}`+"\n")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, `{
"id": 12345,
"name": "Required CI",
"path": ".github/workflows/ci.yml",
"scope": "selected",
"ref": "refs/head/main",
"state": "active",
"selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/required_workflows/12345/repositories",
"created_at": "2020-01-22T19:33:08Z",
"updated_at": "2020-01-22T19:33:08Z",
"repository": {
"id": 53,
"name": "Hello-World",
"url": "https://api.github.com/repos/o/Hello-World"}}`)
})
input := &CreateUpdateRequiredWorkflowOptions{
WorkflowFilePath: String(".github/workflows/ci.yaml"),
Expand All @@ -178,20 +223,41 @@ func TestActionsService_UpdateRequiredWorkflow(t *testing.T) {
SelectedRepositoryIDs: &SelectedRepoIDs{32, 91},
}
ctx := context.Background()
_, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)

requiredWokflow, _, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)

if err != nil {
t.Errorf("Actions.UpdateRequiredWorkflow returned error: %v", err)
}
want := &OrgRequiredWorkflow{
ID: Int64(12345),
Name: String("Required CI"),
Path: String(".github/workflows/ci.yml"),
Scope: String("selected"),
Ref: String("refs/head/main"),
State: String("active"),
SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/required_workflows/12345/repositories"),
CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
Repository: &Repository{ID: Int64(53), URL: String("https://api.github.com/repos/o/Hello-World"), Name: String("Hello-World")},
}

if !cmp.Equal(requiredWokflow, want) {
t.Errorf("Actions.UpdateRequiredWorkflow returned %+v, want %+v", requiredWokflow, want)
}

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

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)
got, resp, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

Expand Down