Skip to content

Commit ff89f86

Browse files
authored
Update Create/Update methods to return the workflow (#2759)
Fixes: #2756.
1 parent 6d92e30 commit ff89f86

File tree

2 files changed

+95
-15
lines changed

2 files changed

+95
-15
lines changed

github/actions_required_workflows.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,20 @@ func (s *ActionsService) ListOrgRequiredWorkflows(ctx context.Context, org strin
9292
// CreateRequiredWorkflow creates the required workflow in an org.
9393
//
9494
// GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#create-a-required-workflow
95-
func (s *ActionsService) CreateRequiredWorkflow(ctx context.Context, org string, createRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*Response, error) {
95+
func (s *ActionsService) CreateRequiredWorkflow(ctx context.Context, org string, createRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) {
9696
url := fmt.Sprintf("orgs/%v/actions/required_workflows", org)
9797
req, err := s.client.NewRequest("PUT", url, createRequiredWorkflowOptions)
9898
if err != nil {
99-
return nil, err
99+
return nil, nil, err
100100
}
101-
return s.client.Do(ctx, req, nil)
101+
102+
orgRequiredWorkflow := new(OrgRequiredWorkflow)
103+
resp, err := s.client.Do(ctx, req, orgRequiredWorkflow)
104+
if err != nil {
105+
return nil, resp, err
106+
}
107+
108+
return orgRequiredWorkflow, resp, nil
102109
}
103110

104111
// GetRequiredWorkflowByID get the RequiredWorkflows for an org by its ID.
@@ -124,13 +131,20 @@ func (s *ActionsService) GetRequiredWorkflowByID(ctx context.Context, owner stri
124131
// UpdateRequiredWorkflow updates a required workflow in an org.
125132
//
126133
// GitHub API docs: https://docs.github.com/en/rest/actions/required-workflows?apiVersion=2022-11-28#update-a-required-workflow
127-
func (s *ActionsService) UpdateRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64, updateRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*Response, error) {
134+
func (s *ActionsService) UpdateRequiredWorkflow(ctx context.Context, org string, requiredWorkflowID int64, updateRequiredWorkflowOptions *CreateUpdateRequiredWorkflowOptions) (*OrgRequiredWorkflow, *Response, error) {
128135
url := fmt.Sprintf("orgs/%v/actions/required_workflows/%v", org, requiredWorkflowID)
129136
req, err := s.client.NewRequest("PATCH", url, updateRequiredWorkflowOptions)
130137
if err != nil {
131-
return nil, err
138+
return nil, nil, err
132139
}
133-
return s.client.Do(ctx, req, nil)
140+
141+
orgRequiredWorkflow := new(OrgRequiredWorkflow)
142+
resp, err := s.client.Do(ctx, req, orgRequiredWorkflow)
143+
if err != nil {
144+
return nil, resp, err
145+
}
146+
147+
return orgRequiredWorkflow, resp, nil
134148
}
135149

136150
// DeleteRequiredWorkflow deletes a required workflow in an org.

github/actions_required_workflows_test.go

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,20 @@ func TestActionsService_CreateRequiredWorkflow(t *testing.T) {
8686
testMethod(t, r, "PUT")
8787
testHeader(t, r, "Content-Type", "application/json")
8888
testBody(t, r, `{"workflow_file_path":".github/workflows/ci.yaml","repository_id":53,"scope":"selected","selected_repository_ids":[32,91]}`+"\n")
89-
w.WriteHeader(http.StatusCreated)
89+
fmt.Fprint(w, `{
90+
"id": 2,
91+
"name": "Required CI",
92+
"path": ".github/workflows/ci.yml",
93+
"scope": "selected",
94+
"ref": "refs/head/main",
95+
"state": "active",
96+
"selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/required_workflows/2/repositories",
97+
"created_at": "2020-01-22T19:33:08Z",
98+
"updated_at": "2020-01-22T19:33:08Z",
99+
"repository": {
100+
"id": 53,
101+
"name": "Hello-World",
102+
"url": "https://api.github.com/repos/o/Hello-World"}}`)
90103
})
91104
input := &CreateUpdateRequiredWorkflowOptions{
92105
WorkflowFilePath: String(".github/workflows/ci.yaml"),
@@ -95,20 +108,39 @@ func TestActionsService_CreateRequiredWorkflow(t *testing.T) {
95108
SelectedRepositoryIDs: &SelectedRepoIDs{32, 91},
96109
}
97110
ctx := context.Background()
98-
_, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input)
99-
111+
requiredWokflow, _, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input)
100112
if err != nil {
101113
t.Errorf("Actions.CreateRequiredWorkflow returned error: %v", err)
102114
}
115+
want := &OrgRequiredWorkflow{
116+
ID: Int64(2),
117+
Name: String("Required CI"),
118+
Path: String(".github/workflows/ci.yml"),
119+
Scope: String("selected"),
120+
Ref: String("refs/head/main"),
121+
State: String("active"),
122+
SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/required_workflows/2/repositories"),
123+
CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
124+
UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
125+
Repository: &Repository{ID: Int64(53), URL: String("https://api.github.com/repos/o/Hello-World"), Name: String("Hello-World")},
126+
}
127+
128+
if !cmp.Equal(requiredWokflow, want) {
129+
t.Errorf("Actions.CreateRequiredWorkflow returned %+v, want %+v", requiredWokflow, want)
130+
}
103131

104132
const methodName = "CreateRequiredWorkflow"
105133
testBadOptions(t, methodName, func() (err error) {
106-
_, err = client.Actions.CreateRequiredWorkflow(ctx, "\n", input)
134+
_, _, err = client.Actions.CreateRequiredWorkflow(ctx, "\n", input)
107135
return err
108136
})
109137

110138
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
111-
return client.Actions.CreateRequiredWorkflow(ctx, "o", input)
139+
got, resp, err := client.Actions.CreateRequiredWorkflow(ctx, "o", input)
140+
if got != nil {
141+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
142+
}
143+
return resp, err
112144
})
113145
}
114146

@@ -169,7 +201,20 @@ func TestActionsService_UpdateRequiredWorkflow(t *testing.T) {
169201
testMethod(t, r, "PATCH")
170202
testHeader(t, r, "Content-Type", "application/json")
171203
testBody(t, r, `{"workflow_file_path":".github/workflows/ci.yaml","repository_id":53,"scope":"selected","selected_repository_ids":[32,91]}`+"\n")
172-
w.WriteHeader(http.StatusOK)
204+
fmt.Fprint(w, `{
205+
"id": 12345,
206+
"name": "Required CI",
207+
"path": ".github/workflows/ci.yml",
208+
"scope": "selected",
209+
"ref": "refs/head/main",
210+
"state": "active",
211+
"selected_repositories_url": "https://api.github.com/orgs/octo-org/actions/required_workflows/12345/repositories",
212+
"created_at": "2020-01-22T19:33:08Z",
213+
"updated_at": "2020-01-22T19:33:08Z",
214+
"repository": {
215+
"id": 53,
216+
"name": "Hello-World",
217+
"url": "https://api.github.com/repos/o/Hello-World"}}`)
173218
})
174219
input := &CreateUpdateRequiredWorkflowOptions{
175220
WorkflowFilePath: String(".github/workflows/ci.yaml"),
@@ -178,20 +223,41 @@ func TestActionsService_UpdateRequiredWorkflow(t *testing.T) {
178223
SelectedRepositoryIDs: &SelectedRepoIDs{32, 91},
179224
}
180225
ctx := context.Background()
181-
_, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)
226+
227+
requiredWokflow, _, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)
182228

183229
if err != nil {
184230
t.Errorf("Actions.UpdateRequiredWorkflow returned error: %v", err)
185231
}
232+
want := &OrgRequiredWorkflow{
233+
ID: Int64(12345),
234+
Name: String("Required CI"),
235+
Path: String(".github/workflows/ci.yml"),
236+
Scope: String("selected"),
237+
Ref: String("refs/head/main"),
238+
State: String("active"),
239+
SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/required_workflows/12345/repositories"),
240+
CreatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
241+
UpdatedAt: &Timestamp{time.Date(2020, time.January, 22, 19, 33, 8, 0, time.UTC)},
242+
Repository: &Repository{ID: Int64(53), URL: String("https://api.github.com/repos/o/Hello-World"), Name: String("Hello-World")},
243+
}
244+
245+
if !cmp.Equal(requiredWokflow, want) {
246+
t.Errorf("Actions.UpdateRequiredWorkflow returned %+v, want %+v", requiredWokflow, want)
247+
}
186248

187249
const methodName = "UpdateRequiredWorkflow"
188250
testBadOptions(t, methodName, func() (err error) {
189-
_, err = client.Actions.UpdateRequiredWorkflow(ctx, "\n", 12345, input)
251+
_, _, err = client.Actions.UpdateRequiredWorkflow(ctx, "\n", 12345, input)
190252
return err
191253
})
192254

193255
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
194-
return client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)
256+
got, resp, err := client.Actions.UpdateRequiredWorkflow(ctx, "o", 12345, input)
257+
if got != nil {
258+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
259+
}
260+
return resp, err
195261
})
196262
}
197263

0 commit comments

Comments
 (0)