Repository: google/go-github
Pull request #2759 (2023-04-14 14:00:20)

Update Create/Update methods to return the workflow
Updated create and update org required workflow methods to return the required workflow object returned by the github api.
Merging this Fixes https://github.com/google/go-github/issues/2756

codecov[bot]: ## [Codecov](https://codecov.io/gh/google/go-github/pull/2759?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2759](https://codecov.io/gh/google/go-github/pull/2759?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (446c5e3) into [master](https://codecov.io/gh/google/go-github/commit/6d92e3022b4732e77c0ca1424974f384f0537c32?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (6d92e30) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master    #2759   +/-   ##
=======================================
  Coverage   98.05%   98.05%           
=======================================
  Files         132      132           
  Lines       11628    11638   +10     
=======================================
+ Hits        11402    11412   +10     
  Misses        154      154           
  Partials       72       72           
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/2759?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/actions\_required\_workflows.go](https://codecov.io/gh/google/go-github/pull/2759?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL2FjdGlvbnNfcmVxdWlyZWRfd29ya2Zsb3dzLmdv) | `100.00% <100.00%> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)

avivek: Thanks for your quick review @gmlewis .
I have made the changes, please take a look now.
gmlewis: Thank you, @valbeat !
Merging.

Patch:
diff --git a/github/actions_required_workflows.go b/github/actions_required_workflows.go
index 73a4c77f..11b96f60 100644
--- a/github/actions_required_workflows.go
+++ b/github/actions_required_workflows.go
@@ -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.
@@ -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.


Test patch:
diff --git a/github/actions_required_workflows_test.go b/github/actions_required_workflows_test.go
index a560d7e9..4298caa0 100644
--- a/github/actions_required_workflows_test.go
+++ b/github/actions_required_workflows_test.go
@@ -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"),
@@ -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
 	})
 }
 
@@ -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"),
@@ -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
 	})
 }