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
26 changes: 26 additions & 0 deletions github/actions_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ type WorkflowBill struct {
TotalMS *int64 `json:"total_ms,omitempty"`
}

// CreateWorkflowDispatchEventRequest represents a request to create a workflow dispatch event.
type CreateWorkflowDispatchEventRequest struct {
// 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"`
// 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.
//
// GitHub API docs: https://developer.github.com/v3/actions/workflows/#list-repository-workflows
Expand Down Expand Up @@ -136,3 +148,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, 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)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
34 changes: 34 additions & 0 deletions github/actions_workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
"reflect"
Expand Down Expand Up @@ -153,3 +154,36 @@ 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()

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, event) {
t.Errorf("Request body = %+v, want %+v", v, event)
}
})

_, 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, event)
if err == nil {
t.Error("client.BaseURL.Path='' CreateWorkflowDispatchEvent err = nil, want error")
}
}