Skip to content

Commit a37dd2d

Browse files
Add support for workflow usage and workflow run usage API endpoints (#1527)
Fixes: #1526.
1 parent f093974 commit a37dd2d

File tree

5 files changed

+286
-0
lines changed

5 files changed

+286
-0
lines changed

github/actions_workflow_runs.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ type ListWorkflowRunsOptions struct {
5454
ListOptions
5555
}
5656

57+
// WorkflowRunUsage represents a usage of a specific workflow run.
58+
type WorkflowRunUsage struct {
59+
Billable *WorkflowRunEnvironment `json:"billable,omitempty"`
60+
RunDurationMS *int64 `json:"run_duration_ms,omitempty"`
61+
}
62+
63+
// WorkflowRunEnvironment represents different runner environments available for a workflow run.
64+
type WorkflowRunEnvironment struct {
65+
Ubuntu *WorkflowRunBill `json:"UBUNTU,omitempty"`
66+
MacOS *WorkflowRunBill `json:"MACOS,omitempty"`
67+
Windows *WorkflowRunBill `json:"WINDOWS,omitempty"`
68+
}
69+
70+
// WorkflowRunBill specifies billable time for a specific environment in a workflow run.
71+
type WorkflowRunBill struct {
72+
TotalMS *int64 `json:"total_ms,omitempty"`
73+
Jobs *int `json:"jobs,omitempty"`
74+
}
75+
5776
func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
5877
u, err := addOptions(endpoint, opts)
5978
if err != nil {
@@ -193,3 +212,23 @@ func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo
193212

194213
return s.client.Do(ctx, req, nil)
195214
}
215+
216+
// GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
217+
//
218+
// GitHub API docs: https://developer.github.com/v3/actions/workflow-runs/#get-workflow-run-usage
219+
func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
220+
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
221+
222+
req, err := s.client.NewRequest("GET", u, nil)
223+
if err != nil {
224+
return nil, nil, err
225+
}
226+
227+
workflowRunUsage := new(WorkflowRunUsage)
228+
resp, err := s.client.Do(ctx, req, workflowRunUsage)
229+
if err != nil {
230+
return nil, resp, err
231+
}
232+
233+
return workflowRunUsage, resp, nil
234+
}

github/actions_workflow_runs_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,40 @@ func TestActionService_DeleteWorkflowRunLogs(t *testing.T) {
250250
t.Errorf("DeleteWorkflowRunLogs returned error: %v", err)
251251
}
252252
}
253+
254+
func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) {
255+
client, mux, _, teardown := setup()
256+
defer teardown()
257+
258+
mux.HandleFunc("/repos/o/r/actions/runs/29679449/timing", func(w http.ResponseWriter, r *http.Request) {
259+
testMethod(t, r, "GET")
260+
fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000,"jobs":1},"MACOS":{"total_ms":240000,"jobs":4},"WINDOWS":{"total_ms":300000,"jobs":2}},"run_duration_ms":500000}`)
261+
})
262+
263+
workflowRunUsage, _, err := client.Actions.GetWorkflowRunUsageByID(context.Background(), "o", "r", 29679449)
264+
if err != nil {
265+
t.Errorf("Actions.GetWorkflowRunUsageByID returned error: %v", err)
266+
}
267+
268+
want := &WorkflowRunUsage{
269+
Billable: &WorkflowRunEnvironment{
270+
Ubuntu: &WorkflowRunBill{
271+
TotalMS: Int64(180000),
272+
Jobs: Int(1),
273+
},
274+
MacOS: &WorkflowRunBill{
275+
TotalMS: Int64(240000),
276+
Jobs: Int(4),
277+
},
278+
Windows: &WorkflowRunBill{
279+
TotalMS: Int64(300000),
280+
Jobs: Int(2),
281+
},
282+
},
283+
RunDurationMS: Int64(500000),
284+
}
285+
286+
if !reflect.DeepEqual(workflowRunUsage, want) {
287+
t.Errorf("Actions.GetWorkflowRunUsageByID returned %+v, want %+v", workflowRunUsage, want)
288+
}
289+
}

github/actions_workflows.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ type Workflows struct {
3030
Workflows []*Workflow `json:"workflows,omitempty"`
3131
}
3232

33+
// WorkflowUsage represents a usage of a specific workflow.
34+
type WorkflowUsage struct {
35+
Billable *WorkflowEnvironment `json:"billable,omitempty"`
36+
}
37+
38+
// WorkflowEnvironment represents different runner environments available for a workflow.
39+
type WorkflowEnvironment struct {
40+
Ubuntu *WorkflowBill `json:"UBUNTU,omitempty"`
41+
MacOS *WorkflowBill `json:"MACOS,omitempty"`
42+
Windows *WorkflowBill `json:"WINDOWS,omitempty"`
43+
}
44+
45+
// WorkflowBill specifies billable time for a specific environment in a workflow.
46+
type WorkflowBill struct {
47+
TotalMS *int64 `json:"total_ms,omitempty"`
48+
}
49+
3350
// ListWorkflows lists all workflows in a repository.
3451
//
3552
// GitHub API docs: https://developer.github.com/v3/actions/workflows/#list-repository-workflows
@@ -86,3 +103,36 @@ func (s *ActionsService) getWorkflow(ctx context.Context, url string) (*Workflow
86103

87104
return workflow, resp, nil
88105
}
106+
107+
// GetWorkflowUsageByID gets a specific workflow usage by ID in the unit of billable milliseconds.
108+
//
109+
// GitHub API docs: https://developer.github.com/v3/actions/workflows/#get-workflow-usage
110+
func (s *ActionsService) GetWorkflowUsageByID(ctx context.Context, owner, repo string, workflowID int64) (*WorkflowUsage, *Response, error) {
111+
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowID)
112+
113+
return s.getWorkflowUsage(ctx, u)
114+
}
115+
116+
// GetWorkflowUsageByFileName gets a specific workflow usage by file name in the unit of billable milliseconds.
117+
//
118+
// GitHub API docs: https://developer.github.com/v3/actions/workflows/#get-workflow-usage
119+
func (s *ActionsService) GetWorkflowUsageByFileName(ctx context.Context, owner, repo, workflowFileName string) (*WorkflowUsage, *Response, error) {
120+
u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowFileName)
121+
122+
return s.getWorkflowUsage(ctx, u)
123+
}
124+
125+
func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*WorkflowUsage, *Response, error) {
126+
req, err := s.client.NewRequest("GET", url, nil)
127+
if err != nil {
128+
return nil, nil, err
129+
}
130+
131+
workflowUsage := new(WorkflowUsage)
132+
resp, err := s.client.Do(ctx, req, workflowUsage)
133+
if err != nil {
134+
return nil, resp, err
135+
}
136+
137+
return workflowUsage, resp, nil
138+
}

github/actions_workflows_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,67 @@ func TestActionsService_GetWorkflowByFileName(t *testing.T) {
8989
t.Errorf("Actions.GetWorkflowByFileName returned %+v, want %+v", workflow, want)
9090
}
9191
}
92+
93+
func TestActionsService_GetWorkflowUsageByID(t *testing.T) {
94+
client, mux, _, teardown := setup()
95+
defer teardown()
96+
97+
mux.HandleFunc("/repos/o/r/actions/workflows/72844/timing", func(w http.ResponseWriter, r *http.Request) {
98+
testMethod(t, r, "GET")
99+
fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000},"MACOS":{"total_ms":240000},"WINDOWS":{"total_ms":300000}}}`)
100+
})
101+
102+
workflowUsage, _, err := client.Actions.GetWorkflowUsageByID(context.Background(), "o", "r", 72844)
103+
if err != nil {
104+
t.Errorf("Actions.GetWorkflowUsageByID returned error: %v", err)
105+
}
106+
107+
want := &WorkflowUsage{
108+
Billable: &WorkflowEnvironment{
109+
Ubuntu: &WorkflowBill{
110+
TotalMS: Int64(180000),
111+
},
112+
MacOS: &WorkflowBill{
113+
TotalMS: Int64(240000),
114+
},
115+
Windows: &WorkflowBill{
116+
TotalMS: Int64(300000),
117+
},
118+
},
119+
}
120+
if !reflect.DeepEqual(workflowUsage, want) {
121+
t.Errorf("Actions.GetWorkflowUsageByID returned %+v, want %+v", workflowUsage, want)
122+
}
123+
}
124+
125+
func TestActionsService_GetWorkflowUsageByFileName(t *testing.T) {
126+
client, mux, _, teardown := setup()
127+
defer teardown()
128+
129+
mux.HandleFunc("/repos/o/r/actions/workflows/main.yml/timing", func(w http.ResponseWriter, r *http.Request) {
130+
testMethod(t, r, "GET")
131+
fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000},"MACOS":{"total_ms":240000},"WINDOWS":{"total_ms":300000}}}`)
132+
})
133+
134+
workflowUsage, _, err := client.Actions.GetWorkflowUsageByFileName(context.Background(), "o", "r", "main.yml")
135+
if err != nil {
136+
t.Errorf("Actions.GetWorkflowUsageByFileName returned error: %v", err)
137+
}
138+
139+
want := &WorkflowUsage{
140+
Billable: &WorkflowEnvironment{
141+
Ubuntu: &WorkflowBill{
142+
TotalMS: Int64(180000),
143+
},
144+
MacOS: &WorkflowBill{
145+
TotalMS: Int64(240000),
146+
},
147+
Windows: &WorkflowBill{
148+
TotalMS: Int64(300000),
149+
},
150+
},
151+
}
152+
if !reflect.DeepEqual(workflowUsage, want) {
153+
t.Errorf("Actions.GetWorkflowUsageByFileName returned %+v, want %+v", workflowUsage, want)
154+
}
155+
}

github/github-accessors.go

Lines changed: 96 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)