Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions github/actions_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
43 changes: 38 additions & 5 deletions github/actions_workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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")
}
}

Expand Down