From c988f50203e0092496b79b0d5261291036489a22 Mon Sep 17 00:00:00 2001 From: Nilesh Singh Date: Mon, 12 Oct 2020 23:52:07 +0530 Subject: [PATCH 1/2] Add Workflow Dispatch Event by ID and FileName --- github/actions_workflows.go | 21 +++++++++++++--- github/actions_workflows_test.go | 43 ++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 9 deletions(-) diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 961cf38e061..0aae42fe59a 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -149,13 +149,26 @@ func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*Wor return workflowUsage, resp, nil } -// CreateWorkflowDispatchEvent manually triggers a GitHub Actions workflow run. +// CreateWorkflowDispatchEventByID manually triggers a GitHub Actions workflow run. // -// GitHub API docs: https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event -func (s *ActionsService) CreateWorkflowDispatchEvent(ctx context.Context, owner, repo string, workflowID int64, event CreateWorkflowDispatchEventRequest) (*Response, error) { +// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event +func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, owner, repo string, workflowID int64, event *CreateWorkflowDispatchEventRequest) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowID) - req, err := s.client.NewRequest("POST", u, &event) + return s.createWorkflowDispatchEvent(ctx, u, event) +} + +// CreateWorkflowDispatchEventByFileName manually triggers a GitHub Actions workflow run. +// +// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event +func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, event *CreateWorkflowDispatchEventRequest) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName) + + return s.createWorkflowDispatchEvent(ctx, u, event) +} + +func (s *ActionsService) createWorkflowDispatchEvent(ctx context.Context, url string, event *CreateWorkflowDispatchEventRequest) (*Response, error) { + req, err := s.client.NewRequest("POST", url, event) if err != nil { return nil, err } diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 0d7c5e20db5..5b84943b955 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -155,7 +155,7 @@ func TestActionsService_GetWorkflowUsageByFileName(t *testing.T) { } } -func TestActionsService_CreateWorkflowDispatchEvent(t *testing.T) { +func TestActionsService_CreateWorkflowDispatchEventByID(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -175,15 +175,48 @@ func TestActionsService_CreateWorkflowDispatchEvent(t *testing.T) { } }) - _, err := client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, event) + _, err := client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, &event) if err != nil { - t.Errorf("Actions.CreateWorkflowDispatchEvent returned error: %v", err) + t.Errorf("Actions.CreateWorkflowDispatchEventByID returned error: %v", err) } // Test s.client.NewRequest failure client.BaseURL.Path = "" - _, err = client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, event) + _, err = client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, &event) if err == nil { - t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEvent err = nil, want error") + t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByID err = nil, want error") + } +} + +func TestActionsService_CreateWorkflowDispatchEventByFileName(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + event := CreateWorkflowDispatchEventRequest{ + Ref: "d4cfb6e7", + Inputs: map[string]interface{}{ + "key": "value", + }, + } + mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/dispatches", func(w http.ResponseWriter, r *http.Request) { + var v CreateWorkflowDispatchEventRequest + json.NewDecoder(r.Body).Decode(&v) + + testMethod(t, r, "POST") + if !reflect.DeepEqual(v, event) { + t.Errorf("Request body = %+v, want %+v", v, event) + } + }) + + _, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", &event) + if err != nil { + t.Errorf("Actions.CreateWorkflowDispatchEventByFileName returned error: %v", err) + } + + // Test s.client.NewRequest failure + client.BaseURL.Path = "" + _, err = client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", &event) + if err == nil { + t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByFileName err = nil, want error") } } From 242ffc70fa8c31eb83b8c7fb93cc077f7a73d271 Mon Sep 17 00:00:00 2001 From: Nilesh Singh Date: Tue, 13 Oct 2020 01:00:53 +0530 Subject: [PATCH 2/2] Replace struct pointer with value --- github/actions_workflows.go | 8 ++++---- github/actions_workflows_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 0aae42fe59a..0dde0d4108b 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -152,19 +152,19 @@ func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*Wor // CreateWorkflowDispatchEventByID manually triggers a GitHub Actions workflow run. // // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event -func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, owner, repo string, workflowID int64, event *CreateWorkflowDispatchEventRequest) (*Response, error) { +func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, owner, repo string, workflowID int64, event CreateWorkflowDispatchEventRequest) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowID) - return s.createWorkflowDispatchEvent(ctx, u, event) + return s.createWorkflowDispatchEvent(ctx, u, &event) } // CreateWorkflowDispatchEventByFileName manually triggers a GitHub Actions workflow run. // // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event -func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, event *CreateWorkflowDispatchEventRequest) (*Response, error) { +func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, event CreateWorkflowDispatchEventRequest) (*Response, error) { u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName) - return s.createWorkflowDispatchEvent(ctx, u, event) + return s.createWorkflowDispatchEvent(ctx, u, &event) } func (s *ActionsService) createWorkflowDispatchEvent(ctx context.Context, url string, event *CreateWorkflowDispatchEventRequest) (*Response, error) { diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 5b84943b955..cb0ba9c0ce3 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -175,14 +175,14 @@ func TestActionsService_CreateWorkflowDispatchEventByID(t *testing.T) { } }) - _, err := client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, &event) + _, err := client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, event) if err != nil { t.Errorf("Actions.CreateWorkflowDispatchEventByID returned error: %v", err) } // Test s.client.NewRequest failure client.BaseURL.Path = "" - _, err = client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, &event) + _, err = client.Actions.CreateWorkflowDispatchEventByID(context.Background(), "o", "r", 72844, event) if err == nil { t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByID err = nil, want error") } @@ -208,14 +208,14 @@ func TestActionsService_CreateWorkflowDispatchEventByFileName(t *testing.T) { } }) - _, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", &event) + _, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", event) if err != nil { t.Errorf("Actions.CreateWorkflowDispatchEventByFileName returned error: %v", err) } // Test s.client.NewRequest failure client.BaseURL.Path = "" - _, err = client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", &event) + _, err = client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), "o", "r", "main.yml", event) if err == nil { t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEventByFileName err = nil, want error") }