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
4 changes: 3 additions & 1 deletion github/actions_workflow_runs.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo s

// ListWorkflowRunsByFileName lists all workflow runs by workflow file name.
//
// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape .
//
// GitHub API docs: https://docs.github.com/rest/actions/workflow-runs?apiVersion=2022-11-28#list-workflow-runs-for-a-workflow
//
//meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs
func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/runs", owner, repo, workflowFileName)
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/runs", owner, repo, url.PathEscape(workflowFileName))
return s.listWorkflowRuns(ctx, u, opts)
}

Expand Down
57 changes: 57 additions & 0 deletions github/actions_workflow_runs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package github

import (
"context"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -1107,3 +1108,59 @@ func TestActionsService_GetPendingDeployments(t *testing.T) {
return resp, err
})
}

func TestActionsService_ListWorkflowRunsByFileName_isEscaped(t *testing.T) {
t.Parallel()
for _, workflow := range []struct {
name string
escapedName string
}{
{name: "main.yml", escapedName: "main.yml"},
{name: "ci#1.yml", escapedName: "ci%231.yml"},
{name: "build test.yml", escapedName: "build%20test.yml"},
{name: "ci%231.yml", escapedName: "ci%25231.yml"},
} {
for _, tt := range []struct {
method string
wantPrefix string
wantSuffix string
wantMethod string
body string
call func(ctx context.Context, client *Client) error
}{
{
method: "ListWorkflowRunsByFileName",
wantPrefix: "/repos/o/r/actions/workflows/",
wantSuffix: "/runs",
wantMethod: "GET",
body: `{}`,
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Actions.ListWorkflowRunsByFileName(ctx, "o", "r", workflow.name, nil)
return err
},
},
} {
t.Run(tt.method+"/"+workflow.name, func(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, tt.wantMethod)
if got, want := r.URL.EscapedPath(), tt.wantPrefix+workflow.escapedName+tt.wantSuffix; got != want {
t.Errorf("Request path = %q, want %q", got, want)
}
if got, want := r.URL.Path, tt.wantPrefix+workflow.name+tt.wantSuffix; got != want {
t.Errorf("Decoded request path = %q, want %q", got, want)
}
if tt.body != "" {
fmt.Fprint(w, tt.body)
}
})

if err := tt.call(t.Context(), client); err != nil {
t.Fatalf("Actions.%v returned error: %v", tt.method, err)
}
})
}
}
}
21 changes: 16 additions & 5 deletions github/actions_workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package github
import (
"context"
"fmt"
"net/url"
)

// Workflow represents a repository action workflow.
Expand Down Expand Up @@ -137,11 +138,13 @@ func (s *ActionsService) GetWorkflowByID(ctx context.Context, owner, repo string

// GetWorkflowByFileName gets a specific workflow by file name.
//
// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape .
//
// GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
//
//meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}
func (s *ActionsService) GetWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Workflow, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowFileName)
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, url.PathEscape(workflowFileName))

return s.getWorkflow(ctx, u)
}
Expand Down Expand Up @@ -174,11 +177,13 @@ func (s *ActionsService) GetWorkflowUsageByID(ctx context.Context, owner, repo s

// GetWorkflowUsageByFileName gets a specific workflow usage by file name in the unit of billable milliseconds.
//
// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape .
//
// GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#get-workflow-usage
//
//meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/timing
func (s *ActionsService) GetWorkflowUsageByFileName(ctx context.Context, owner, repo, workflowFileName string) (*WorkflowUsage, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowFileName)
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, url.PathEscape(workflowFileName))

return s.getWorkflowUsage(ctx, u)
}
Expand Down Expand Up @@ -211,11 +216,13 @@ func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, ow

// CreateWorkflowDispatchEventByFileName manually triggers a GitHub Actions workflow run.
//
// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape .
//
// GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
//
//meta:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, body CreateWorkflowDispatchEventRequest) (*WorkflowDispatchRunDetails, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName)
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, url.PathEscape(workflowFileName))

return s.createWorkflowDispatchEvent(ctx, u, &body)
}
Expand Down Expand Up @@ -247,11 +254,13 @@ func (s *ActionsService) EnableWorkflowByID(ctx context.Context, owner, repo str

// EnableWorkflowByFileName enables a workflow and sets the state of the workflow to "active".
//
// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape .
//
// GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#enable-a-workflow
//
//meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable
func (s *ActionsService) EnableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowFileName)
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, url.PathEscape(workflowFileName))
return s.doNewPutRequest(ctx, u)
}

Expand All @@ -267,11 +276,13 @@ func (s *ActionsService) DisableWorkflowByID(ctx context.Context, owner, repo st

// DisableWorkflowByFileName disables a workflow and sets the state of the workflow to "disabled_manually".
//
// Note: the workflow file name is URL path escaped for you. See: https://pkg.go.dev/net/url#PathEscape .
//
// GitHub API docs: https://docs.github.com/rest/actions/workflows?apiVersion=2022-11-28#disable-a-workflow
//
//meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable
func (s *ActionsService) DisableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowFileName)
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, url.PathEscape(workflowFileName))
return s.doNewPutRequest(ctx, u)
}

Expand Down
101 changes: 101 additions & 0 deletions github/actions_workflows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package github

import (
"context"
"fmt"
"net/http"
"testing"
Expand Down Expand Up @@ -531,3 +532,103 @@ func TestActionsService_DisableWorkflowByFileName(t *testing.T) {
return client.Actions.DisableWorkflowByFileName(ctx, "o", "r", "main.yml")
})
}

func TestActionsService_workflowFileName_isEscaped(t *testing.T) {
t.Parallel()
for _, workflow := range []struct {
name string
escapedName string
}{
{name: "main.yml", escapedName: "main.yml"},
{name: "ci#1.yml", escapedName: "ci%231.yml"},
{name: "build test.yml", escapedName: "build%20test.yml"},
{name: "ci%231.yml", escapedName: "ci%25231.yml"},
} {
for _, tt := range []struct {
method string
wantPrefix string
wantSuffix string
wantMethod string
body string
call func(ctx context.Context, client *Client) error
}{
{
method: "GetWorkflowByFileName",
wantPrefix: "/repos/o/r/actions/workflows/",
wantSuffix: "",
wantMethod: "GET",
body: `{}`,
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Actions.GetWorkflowByFileName(ctx, "o", "r", workflow.name)
return err
},
},
{
method: "GetWorkflowUsageByFileName",
wantPrefix: "/repos/o/r/actions/workflows/",
wantSuffix: "/timing",
wantMethod: "GET",
body: `{}`,
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Actions.GetWorkflowUsageByFileName(ctx, "o", "r", workflow.name)
return err
},
},
{
method: "CreateWorkflowDispatchEventByFileName",
wantPrefix: "/repos/o/r/actions/workflows/",
wantSuffix: "/dispatches",
wantMethod: "POST",
body: `{}`,
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Actions.CreateWorkflowDispatchEventByFileName(ctx, "o", "r", workflow.name, CreateWorkflowDispatchEventRequest{Ref: "main"})
return err
},
},
{
method: "EnableWorkflowByFileName",
wantPrefix: "/repos/o/r/actions/workflows/",
wantSuffix: "/enable",
wantMethod: "PUT",
body: ``,
call: func(ctx context.Context, client *Client) error {
_, err := client.Actions.EnableWorkflowByFileName(ctx, "o", "r", workflow.name)
return err
},
},
{
method: "DisableWorkflowByFileName",
wantPrefix: "/repos/o/r/actions/workflows/",
wantSuffix: "/disable",
wantMethod: "PUT",
body: ``,
call: func(ctx context.Context, client *Client) error {
_, err := client.Actions.DisableWorkflowByFileName(ctx, "o", "r", workflow.name)
return err
},
},
} {
t.Run(tt.method+"/"+workflow.name, func(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, tt.wantMethod)
if got, want := r.URL.EscapedPath(), tt.wantPrefix+workflow.escapedName+tt.wantSuffix; got != want {
t.Errorf("Request path = %q, want %q", got, want)
}
if got, want := r.URL.Path, tt.wantPrefix+workflow.name+tt.wantSuffix; got != want {
t.Errorf("Decoded request path = %q, want %q", got, want)
}
if tt.body != "" {
fmt.Fprint(w, tt.body)
}
})

if err := tt.call(t.Context(), client); err != nil {
t.Fatalf("Actions.%v returned error: %v", tt.method, err)
}
})
}
}
}
Loading