diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 672b34947a9..aa680246e30 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/free-pro-team@latest/rest/reference/actions/#create-a-workflow-dispatch-event -func (s *ActionsService) CreateWorkflowDispatchEvent(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) - 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 77c4a49f0d0..04b7eaa1435 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,16 +175,49 @@ 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") } }