Repository: google/go-github
Pull request #2206 (2021-11-18 21:47:08)

Add missing attribute job_runs in WorkflowRunBill
### What type of PR is this?

Update `WorkflowRunBill` to include a missing attribute

### What this PR does / why we need it

Brings the implementation inline with the expected API response ([docs here](https://docs.github.com/en/rest/reference/actions#get-workflow-run-usage)).

`WorkflowRunBill` current implementation is missing the JobRuns (`job_runs`) attribute. This is important when calculating the actual cost and time spent in a given workflow run as the `total_ms` doesn't include the rounding ups or multipliers that GitHub applied to each job run.

codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2206](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (b046070) into [master](https://codecov.io/gh/google/go-github/commit/a4ce81018405bb023d54af1ec643414df39c08b4?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (a4ce810) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/google/go-github/pull/2206/graphs/tree.svg?width=650&height=150&src=pr&token=EgYjGjZlI0&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)

```diff
@@           Coverage Diff           @@
##           master    #2206   +/-   ##
=======================================
  Coverage   97.79%   97.79%           
=======================================
  Files         112      112           
  Lines       10036    10036           
=======================================
  Hits         9815     9815           
  Misses        154      154           
  Partials       67       67           
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/actions\_workflow\_runs.go](https://codecov.io/gh/google/go-github/pull/2206/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL2FjdGlvbnNfd29ya2Zsb3dfcnVucy5nbw==) | `100.00% <ø> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Last update [a4ce810...b046070](https://codecov.io/gh/google/go-github/pull/2206?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).


Patch:
diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go
index 3cd9df13..37bc819a 100644
--- a/github/actions_workflow_runs.go
+++ b/github/actions_workflow_runs.go
@@ -74,8 +74,15 @@ type WorkflowRunEnvironment struct {
 
 // WorkflowRunBill specifies billable time for a specific environment in a workflow run.
 type WorkflowRunBill struct {
-	TotalMS *int64 `json:"total_ms,omitempty"`
-	Jobs    *int   `json:"jobs,omitempty"`
+	TotalMS *int64               `json:"total_ms,omitempty"`
+	Jobs    *int                 `json:"jobs,omitempty"`
+	JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"`
+}
+
+// WorkflowRunJobRun represents a usage of individual jobs of a specific workflow run.
+type WorkflowRunJobRun struct {
+	JobID      *int   `json:"job_id,omitempty"`
+	DurationMS *int64 `json:"duration_ms,omitempty"`
 }
 
 func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
diff --git a/github/github-accessors.go b/github/github-accessors.go
index b2c53f36..7bd87016 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -18668,6 +18668,22 @@ func (w *WorkflowRunEvent) GetWorkflowRun() *WorkflowRun {
 	return w.WorkflowRun
 }
 
+// GetDurationMS returns the DurationMS field if it's non-nil, zero value otherwise.
+func (w *WorkflowRunJobRun) GetDurationMS() int64 {
+	if w == nil || w.DurationMS == nil {
+		return 0
+	}
+	return *w.DurationMS
+}
+
+// GetJobID returns the JobID field if it's non-nil, zero value otherwise.
+func (w *WorkflowRunJobRun) GetJobID() int {
+	if w == nil || w.JobID == nil {
+		return 0
+	}
+	return *w.JobID
+}
+
 // GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise.
 func (w *WorkflowRuns) GetTotalCount() int {
 	if w == nil || w.TotalCount == nil {


Test patch:
diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go
index 6803ace0..fcfbd360 100644
--- a/github/actions_workflow_runs_test.go
+++ b/github/actions_workflow_runs_test.go
@@ -366,7 +366,7 @@ func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) {
 
 	mux.HandleFunc("/repos/o/r/actions/runs/29679449/timing", func(w http.ResponseWriter, r *http.Request) {
 		testMethod(t, r, "GET")
-		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}`)
+		fmt.Fprint(w, `{"billable":{"UBUNTU":{"total_ms":180000,"jobs":1,"job_runs":[{"job_id":1,"duration_ms":60000}]},"MACOS":{"total_ms":240000,"jobs":2,"job_runs":[{"job_id":2,"duration_ms":30000},{"job_id":3,"duration_ms":10000}]},"WINDOWS":{"total_ms":300000,"jobs":2}},"run_duration_ms":500000}`)
 	})
 
 	ctx := context.Background()
@@ -380,10 +380,26 @@ func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) {
 			Ubuntu: &WorkflowRunBill{
 				TotalMS: Int64(180000),
 				Jobs:    Int(1),
+				JobRuns: []*WorkflowRunJobRun{
+					{
+						JobID:      Int(1),
+						DurationMS: Int64(60000),
+					},
+				},
 			},
 			MacOS: &WorkflowRunBill{
 				TotalMS: Int64(240000),
-				Jobs:    Int(4),
+				Jobs:    Int(2),
+				JobRuns: []*WorkflowRunJobRun{
+					{
+						JobID:      Int(2),
+						DurationMS: Int64(30000),
+					},
+					{
+						JobID:      Int(3),
+						DurationMS: Int64(10000),
+					},
+				},
 			},
 			Windows: &WorkflowRunBill{
 				TotalMS: Int64(300000),
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index 61b32451..bbdff0ad 100644
--- a/github/github-accessors_test.go
+++ b/github/github-accessors_test.go
@@ -21827,6 +21827,26 @@ func TestWorkflowRunEvent_GetWorkflowRun(tt *testing.T) {
 	w.GetWorkflowRun()
 }
 
+func TestWorkflowRunJobRun_GetDurationMS(tt *testing.T) {
+	var zeroValue int64
+	w := &WorkflowRunJobRun{DurationMS: &zeroValue}
+	w.GetDurationMS()
+	w = &WorkflowRunJobRun{}
+	w.GetDurationMS()
+	w = nil
+	w.GetDurationMS()
+}
+
+func TestWorkflowRunJobRun_GetJobID(tt *testing.T) {
+	var zeroValue int
+	w := &WorkflowRunJobRun{JobID: &zeroValue}
+	w.GetJobID()
+	w = &WorkflowRunJobRun{}
+	w.GetJobID()
+	w = nil
+	w.GetJobID()
+}
+
 func TestWorkflowRuns_GetTotalCount(tt *testing.T) {
 	var zeroValue int
 	w := &WorkflowRuns{TotalCount: &zeroValue}