From 99788a1f260630eb12aafc77b73ba055a1ed9b90 Mon Sep 17 00:00:00 2001 From: Anton Nguyen Date: Mon, 31 Aug 2020 18:54:33 -0400 Subject: [PATCH 1/3] Add support within Actions for creating a workflow dispatch event According to https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event, this endpoint is utilized for manually triggering a Github Actions workflow run. --- github/actions_workflows.go | 20 ++++++++++++++++++++ github/actions_workflows_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 6dd02feb67d..88336ea7325 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -47,6 +47,12 @@ type WorkflowBill struct { TotalMS *int64 `json:"total_ms,omitempty"` } +// CreateWorkflowDispatchEventRequest represents a request to create a workflow dispatch event +type CreateWorkflowDispatchEventRequest struct { + // Ref is required when creating a workflow dispatch event + Ref string `json:"ref,omitempty"` +} + // ListWorkflows lists all workflows in a repository. // // GitHub API docs: https://developer.github.com/v3/actions/workflows/#list-repository-workflows @@ -136,3 +142,17 @@ func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*Wor return workflowUsage, resp, nil } + +// CreateWorkflowDispatchEvent 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, createWorkflowDispatchEvent CreateWorkflowDispatchEventRequest) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowID) + + req, err := s.client.NewRequest("POST", u, &createWorkflowDispatchEvent) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 39b71491720..dfb0e94ba08 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -7,6 +7,7 @@ package github import ( "context" + "encoding/json" "fmt" "net/http" "reflect" @@ -153,3 +154,31 @@ func TestActionsService_GetWorkflowUsageByFileName(t *testing.T) { t.Errorf("Actions.GetWorkflowUsageByFileName returned %+v, want %+v", workflowUsage, want) } } + +func TestActionsService_CreateWorkflowDispatchEvent(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + input := CreateWorkflowDispatchEventRequest{Ref: "d4cfb6e7"} + mux.HandleFunc("/repos/o/r/actions/workflows/72844/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, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + }) + + _, err := client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, input) + if err != nil { + t.Errorf("Actions.CreateWorkflowDispatchEvent returned error: %v", err) + } + + // Test s.client.NewRequest failure + client.BaseURL.Path = "" + _, err = client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, input) + if err == nil { + t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEvent err = nil, want error") + } +} From 3cb00ec0579013a9d8d5fffb30d869431e7fd4a4 Mon Sep 17 00:00:00 2001 From: AntonNguyen Date: Mon, 31 Aug 2020 23:23:46 -0400 Subject: [PATCH 2/3] Improved the comments and grammar for the request body and method --- github/actions_workflows.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 88336ea7325..488a806759b 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -47,10 +47,12 @@ type WorkflowBill struct { TotalMS *int64 `json:"total_ms,omitempty"` } -// CreateWorkflowDispatchEventRequest represents a request to create a workflow dispatch event +// CreateWorkflowDispatchEventRequest represents a request to create a workflow dispatch event. type CreateWorkflowDispatchEventRequest struct { - // Ref is required when creating a workflow dispatch event - Ref string `json:"ref,omitempty"` + // Ref represents the reference of the workflow run. + // The reference can be a branch, tag, or a commit SHA. + // Ref is required when creating a workflow dispatch event. + Ref string `json:"ref"` } // ListWorkflows lists all workflows in a repository. @@ -143,13 +145,13 @@ func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*Wor return workflowUsage, resp, nil } -// CreateWorkflowDispatchEvent manually triggers a Github Actions workflow run +// CreateWorkflowDispatchEvent 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, createWorkflowDispatchEvent CreateWorkflowDispatchEventRequest) (*Response, error) { +func (s *ActionsService) CreateWorkflowDispatchEvent(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, &createWorkflowDispatchEvent) + req, err := s.client.NewRequest("POST", u, &event) if err != nil { return nil, err } From f02e20972064fbc1cc83569540c4f4ecbd43dce7 Mon Sep 17 00:00:00 2001 From: AntonNguyen Date: Mon, 31 Aug 2020 23:25:24 -0400 Subject: [PATCH 3/3] Added inputs support --- github/actions_workflows.go | 4 ++++ github/actions_workflows_test.go | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 488a806759b..7b39fabecbf 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -53,6 +53,10 @@ type CreateWorkflowDispatchEventRequest struct { // The reference can be a branch, tag, or a commit SHA. // Ref is required when creating a workflow dispatch event. Ref string `json:"ref"` + // Inputs represents input keys and values configured in the workflow file. + // The maximum number of properties is 10. + // Default: Any default properties configured in the workflow file will be used when `inputs` are omitted. + Inputs map[string]interface{} `json:"inputs,omitempty"` } // ListWorkflows lists all workflows in a repository. diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index dfb0e94ba08..0d7c5e20db5 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -159,25 +159,30 @@ func TestActionsService_CreateWorkflowDispatchEvent(t *testing.T) { client, mux, _, teardown := setup() defer teardown() - input := CreateWorkflowDispatchEventRequest{Ref: "d4cfb6e7"} + event := CreateWorkflowDispatchEventRequest{ + Ref: "d4cfb6e7", + Inputs: map[string]interface{}{ + "key": "value", + }, + } mux.HandleFunc("/repos/o/r/actions/workflows/72844/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, input) { - t.Errorf("Request body = %+v, want %+v", v, input) + if !reflect.DeepEqual(v, event) { + t.Errorf("Request body = %+v, want %+v", v, event) } }) - _, err := client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, input) + _, err := client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, event) if err != nil { t.Errorf("Actions.CreateWorkflowDispatchEvent returned error: %v", err) } // Test s.client.NewRequest failure client.BaseURL.Path = "" - _, err = client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, input) + _, err = client.Actions.CreateWorkflowDispatchEvent(context.Background(), "o", "r", 72844, event) if err == nil { t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEvent err = nil, want error") }