From 84cc7d50b18ae20b63e23fe86c7b7690e6c3063e Mon Sep 17 00:00:00 2001 From: James Turley Date: Mon, 26 Dec 2022 12:37:39 +0000 Subject: [PATCH] Change actions billing structs to maps (#2597) Fixes: #2595. --- github/actions_workflow_runs.go | 13 +++---- github/actions_workflow_runs_test.go | 31 ++++++++--------- github/actions_workflows.go | 11 +++--- github/actions_workflows_test.go | 36 +++++++++---------- github/billing.go | 7 ++-- github/billing_test.go | 24 ++++++------- github/github-accessors.go | 52 ++-------------------------- github/github-accessors_test.go | 42 ---------------------- 8 files changed, 58 insertions(+), 158 deletions(-) diff --git a/github/actions_workflow_runs.go b/github/actions_workflow_runs.go index 9fd01c4a56..6241dc629f 100644 --- a/github/actions_workflow_runs.go +++ b/github/actions_workflow_runs.go @@ -65,16 +65,13 @@ type ListWorkflowRunsOptions struct { // WorkflowRunUsage represents a usage of a specific workflow run. type WorkflowRunUsage struct { - Billable *WorkflowRunEnvironment `json:"billable,omitempty"` - RunDurationMS *int64 `json:"run_duration_ms,omitempty"` + Billable *WorkflowRunBillMap `json:"billable,omitempty"` + RunDurationMS *int64 `json:"run_duration_ms,omitempty"` } -// WorkflowRunEnvironment represents different runner environments available for a workflow run. -type WorkflowRunEnvironment struct { - Ubuntu *WorkflowRunBill `json:"UBUNTU,omitempty"` - MacOS *WorkflowRunBill `json:"MACOS,omitempty"` - Windows *WorkflowRunBill `json:"WINDOWS,omitempty"` -} +// WorkflowRunBillMap represents different runner environments available for a workflow run. +// Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc. +type WorkflowRunBillMap map[string]*WorkflowRunBill // WorkflowRunBill specifies billable time for a specific environment in a workflow run. type WorkflowRunBill struct { diff --git a/github/actions_workflow_runs_test.go b/github/actions_workflow_runs_test.go index 2705363ba6..641c4d739b 100644 --- a/github/actions_workflow_runs_test.go +++ b/github/actions_workflow_runs_test.go @@ -403,7 +403,6 @@ func TestActionService_ListRepositoryWorkflowRuns(t *testing.T) { opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}} ctx := context.Background() runs, _, err := client.Actions.ListRepositoryWorkflowRuns(ctx, "o", "r", opts) - if err != nil { t.Errorf("Actions.ListRepositoryWorkflowRuns returned error: %v", err) } @@ -505,8 +504,8 @@ func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) { } want := &WorkflowRunUsage{ - Billable: &WorkflowRunEnvironment{ - Ubuntu: &WorkflowRunBill{ + Billable: &WorkflowRunBillMap{ + "UBUNTU": &WorkflowRunBill{ TotalMS: Int64(180000), Jobs: Int(1), JobRuns: []*WorkflowRunJobRun{ @@ -516,7 +515,7 @@ func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) { }, }, }, - MacOS: &WorkflowRunBill{ + "MACOS": &WorkflowRunBill{ TotalMS: Int64(240000), Jobs: Int(2), JobRuns: []*WorkflowRunJobRun{ @@ -530,7 +529,7 @@ func TestActionsService_GetWorkflowRunUsageByID(t *testing.T) { }, }, }, - Windows: &WorkflowRunBill{ + "WINDOWS": &WorkflowRunBill{ TotalMS: Int64(300000), Jobs: Int(2), }, @@ -975,7 +974,7 @@ func TestWorkflowRuns_Marshal(t *testing.T) { "created_at": ` + referenceTimeStr + `, "suspended_at": ` + referenceTimeStr + `, "url": "u" - } + } } ] }` @@ -999,19 +998,19 @@ func TestWorkflowRunBill_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } -func TestWorkflowRunEnvironment_Marshal(t *testing.T) { - testJSONMarshal(t, &WorkflowRunEnvironment{}, "{}") +func TestWorkflowRunBillMap_Marshal(t *testing.T) { + testJSONMarshal(t, &WorkflowRunBillMap{}, "{}") - u := &WorkflowRunEnvironment{ - Ubuntu: &WorkflowRunBill{ + u := &WorkflowRunBillMap{ + "UBUNTU": &WorkflowRunBill{ TotalMS: Int64(1), Jobs: Int(1), }, - MacOS: &WorkflowRunBill{ + "MACOS": &WorkflowRunBill{ TotalMS: Int64(1), Jobs: Int(1), }, - Windows: &WorkflowRunBill{ + "WINDOWS": &WorkflowRunBill{ TotalMS: Int64(1), Jobs: Int(1), }, @@ -1039,16 +1038,16 @@ func TestWorkflowRunUsage_Marshal(t *testing.T) { testJSONMarshal(t, &WorkflowRunUsage{}, "{}") u := &WorkflowRunUsage{ - Billable: &WorkflowRunEnvironment{ - Ubuntu: &WorkflowRunBill{ + Billable: &WorkflowRunBillMap{ + "UBUNTU": &WorkflowRunBill{ TotalMS: Int64(1), Jobs: Int(1), }, - MacOS: &WorkflowRunBill{ + "MACOS": &WorkflowRunBill{ TotalMS: Int64(1), Jobs: Int(1), }, - Windows: &WorkflowRunBill{ + "WINDOWS": &WorkflowRunBill{ TotalMS: Int64(1), Jobs: Int(1), }, diff --git a/github/actions_workflows.go b/github/actions_workflows.go index 9973a5d3f3..c9b47ed4be 100644 --- a/github/actions_workflows.go +++ b/github/actions_workflows.go @@ -32,15 +32,12 @@ type Workflows struct { // WorkflowUsage represents a usage of a specific workflow. type WorkflowUsage struct { - Billable *WorkflowEnvironment `json:"billable,omitempty"` + Billable *WorkflowBillMap `json:"billable,omitempty"` } -// WorkflowEnvironment represents different runner environments available for a workflow. -type WorkflowEnvironment struct { - Ubuntu *WorkflowBill `json:"UBUNTU,omitempty"` - MacOS *WorkflowBill `json:"MACOS,omitempty"` - Windows *WorkflowBill `json:"WINDOWS,omitempty"` -} +// WorkflowBillMap represents different runner environments available for a workflow. +// Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc. +type WorkflowBillMap map[string]*WorkflowBill // WorkflowBill specifies billable time for a specific environment in a workflow. type WorkflowBill struct { diff --git a/github/actions_workflows_test.go b/github/actions_workflows_test.go index 60a9d91b37..5e0dbbc9c1 100644 --- a/github/actions_workflows_test.go +++ b/github/actions_workflows_test.go @@ -153,14 +153,14 @@ func TestActionsService_GetWorkflowUsageByID(t *testing.T) { } want := &WorkflowUsage{ - Billable: &WorkflowEnvironment{ - Ubuntu: &WorkflowBill{ + Billable: &WorkflowBillMap{ + "UBUNTU": &WorkflowBill{ TotalMS: Int64(180000), }, - MacOS: &WorkflowBill{ + "MACOS": &WorkflowBill{ TotalMS: Int64(240000), }, - Windows: &WorkflowBill{ + "WINDOWS": &WorkflowBill{ TotalMS: Int64(300000), }, }, @@ -200,14 +200,14 @@ func TestActionsService_GetWorkflowUsageByFileName(t *testing.T) { } want := &WorkflowUsage{ - Billable: &WorkflowEnvironment{ - Ubuntu: &WorkflowBill{ + Billable: &WorkflowBillMap{ + "UBUNTU": &WorkflowBill{ TotalMS: Int64(180000), }, - MacOS: &WorkflowBill{ + "MACOS": &WorkflowBill{ TotalMS: Int64(240000), }, - Windows: &WorkflowBill{ + "WINDOWS": &WorkflowBill{ TotalMS: Int64(300000), }, }, @@ -545,17 +545,17 @@ func TestWorkflowBill_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } -func TestWorkflowEnvironment_Marshal(t *testing.T) { - testJSONMarshal(t, &WorkflowEnvironment{}, "{}") +func TestWorkflowBillMap_Marshal(t *testing.T) { + testJSONMarshal(t, &WorkflowBillMap{}, "{}") - u := &WorkflowEnvironment{ - Ubuntu: &WorkflowBill{ + u := &WorkflowBillMap{ + "UBUNTU": &WorkflowBill{ TotalMS: Int64(1), }, - MacOS: &WorkflowBill{ + "MACOS": &WorkflowBill{ TotalMS: Int64(1), }, - Windows: &WorkflowBill{ + "WINDOWS": &WorkflowBill{ TotalMS: Int64(1), }, } @@ -579,14 +579,14 @@ func TestWorkflowUsage_Marshal(t *testing.T) { testJSONMarshal(t, &WorkflowUsage{}, "{}") u := &WorkflowUsage{ - Billable: &WorkflowEnvironment{ - Ubuntu: &WorkflowBill{ + Billable: &WorkflowBillMap{ + "UBUNTU": &WorkflowBill{ TotalMS: Int64(1), }, - MacOS: &WorkflowBill{ + "MACOS": &WorkflowBill{ TotalMS: Int64(1), }, - Windows: &WorkflowBill{ + "WINDOWS": &WorkflowBill{ TotalMS: Int64(1), }, }, diff --git a/github/billing.go b/github/billing.go index d516cd0c29..2f5861ce80 100644 --- a/github/billing.go +++ b/github/billing.go @@ -24,11 +24,8 @@ type ActionBilling struct { MinutesUsedBreakdown MinutesUsedBreakdown `json:"minutes_used_breakdown"` } -type MinutesUsedBreakdown struct { - Ubuntu int `json:"UBUNTU"` - MacOS int `json:"MACOS"` - Windows int `json:"WINDOWS"` -} +// MinutesUsedBreakdown counts the actions minutes used by machine type (e.g. UBUNTU, WINDOWS, MACOS). +type MinutesUsedBreakdown = map[string]int // PackageBilling represents a GitHub Package billing. type PackageBilling struct { diff --git a/github/billing_test.go b/github/billing_test.go index 2df9a0e97f..6a85cd22df 100644 --- a/github/billing_test.go +++ b/github/billing_test.go @@ -43,9 +43,9 @@ func TestBillingService_GetActionsBillingOrg(t *testing.T) { TotalPaidMinutesUsed: 0, IncludedMinutes: 3000, MinutesUsedBreakdown: MinutesUsedBreakdown{ - Ubuntu: 205, - MacOS: 10, - Windows: 90, + "UBUNTU": 205, + "MACOS": 10, + "WINDOWS": 90, }, } if !cmp.Equal(hook, want) { @@ -209,9 +209,9 @@ func TestBillingService_GetActionsBillingUser(t *testing.T) { TotalPaidMinutesUsed: 0, IncludedMinutes: 3000, MinutesUsedBreakdown: MinutesUsedBreakdown{ - Ubuntu: 205, - MacOS: 10, - Windows: 90, + "UBUNTU": 205, + "MACOS": 10, + "WINDOWS": 90, }, } if !cmp.Equal(hook, want) { @@ -350,9 +350,9 @@ func TestMinutesUsedBreakdown_Marshal(t *testing.T) { testJSONMarshal(t, &MinutesUsedBreakdown{}, "{}") u := &MinutesUsedBreakdown{ - Ubuntu: 1, - MacOS: 1, - Windows: 1, + "UBUNTU": 1, + "MACOS": 1, + "WINDOWS": 1, } want := `{ @@ -372,9 +372,9 @@ func TestActionBilling_Marshal(t *testing.T) { TotalPaidMinutesUsed: 1, IncludedMinutes: 1, MinutesUsedBreakdown: MinutesUsedBreakdown{ - Ubuntu: 1, - MacOS: 1, - Windows: 1, + "UBUNTU": 1, + "MACOS": 1, + "WINDOWS": 1, }, } diff --git a/github/github-accessors.go b/github/github-accessors.go index e78684ca1c..e49872ead2 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -20582,30 +20582,6 @@ func (w *WorkflowDispatchEvent) GetWorkflow() string { return *w.Workflow } -// GetMacOS returns the MacOS field. -func (w *WorkflowEnvironment) GetMacOS() *WorkflowBill { - if w == nil { - return nil - } - return w.MacOS -} - -// GetUbuntu returns the Ubuntu field. -func (w *WorkflowEnvironment) GetUbuntu() *WorkflowBill { - if w == nil { - return nil - } - return w.Ubuntu -} - -// GetWindows returns the Windows field. -func (w *WorkflowEnvironment) GetWindows() *WorkflowBill { - if w == nil { - return nil - } - return w.Windows -} - // GetCheckRunURL returns the CheckRunURL field if it's non-nil, zero value otherwise. func (w *WorkflowJob) GetCheckRunURL() string { if w == nil || w.CheckRunURL == nil { @@ -21062,30 +21038,6 @@ func (w *WorkflowRunBill) GetTotalMS() int64 { return *w.TotalMS } -// GetMacOS returns the MacOS field. -func (w *WorkflowRunEnvironment) GetMacOS() *WorkflowRunBill { - if w == nil { - return nil - } - return w.MacOS -} - -// GetUbuntu returns the Ubuntu field. -func (w *WorkflowRunEnvironment) GetUbuntu() *WorkflowRunBill { - if w == nil { - return nil - } - return w.Ubuntu -} - -// GetWindows returns the Windows field. -func (w *WorkflowRunEnvironment) GetWindows() *WorkflowRunBill { - if w == nil { - return nil - } - return w.Windows -} - // GetAction returns the Action field if it's non-nil, zero value otherwise. func (w *WorkflowRunEvent) GetAction() string { if w == nil || w.Action == nil { @@ -21167,7 +21119,7 @@ func (w *WorkflowRuns) GetTotalCount() int { } // GetBillable returns the Billable field. -func (w *WorkflowRunUsage) GetBillable() *WorkflowRunEnvironment { +func (w *WorkflowRunUsage) GetBillable() *WorkflowRunBillMap { if w == nil { return nil } @@ -21191,7 +21143,7 @@ func (w *Workflows) GetTotalCount() int { } // GetBillable returns the Billable field. -func (w *WorkflowUsage) GetBillable() *WorkflowEnvironment { +func (w *WorkflowUsage) GetBillable() *WorkflowBillMap { if w == nil { return nil } diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 52425ded18..1985dcd72f 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -24036,27 +24036,6 @@ func TestWorkflowDispatchEvent_GetWorkflow(tt *testing.T) { w.GetWorkflow() } -func TestWorkflowEnvironment_GetMacOS(tt *testing.T) { - w := &WorkflowEnvironment{} - w.GetMacOS() - w = nil - w.GetMacOS() -} - -func TestWorkflowEnvironment_GetUbuntu(tt *testing.T) { - w := &WorkflowEnvironment{} - w.GetUbuntu() - w = nil - w.GetUbuntu() -} - -func TestWorkflowEnvironment_GetWindows(tt *testing.T) { - w := &WorkflowEnvironment{} - w.GetWindows() - w = nil - w.GetWindows() -} - func TestWorkflowJob_GetCheckRunURL(tt *testing.T) { var zeroValue string w := &WorkflowJob{CheckRunURL: &zeroValue} @@ -24600,27 +24579,6 @@ func TestWorkflowRunBill_GetTotalMS(tt *testing.T) { w.GetTotalMS() } -func TestWorkflowRunEnvironment_GetMacOS(tt *testing.T) { - w := &WorkflowRunEnvironment{} - w.GetMacOS() - w = nil - w.GetMacOS() -} - -func TestWorkflowRunEnvironment_GetUbuntu(tt *testing.T) { - w := &WorkflowRunEnvironment{} - w.GetUbuntu() - w = nil - w.GetUbuntu() -} - -func TestWorkflowRunEnvironment_GetWindows(tt *testing.T) { - w := &WorkflowRunEnvironment{} - w.GetWindows() - w = nil - w.GetWindows() -} - func TestWorkflowRunEvent_GetAction(tt *testing.T) { var zeroValue string w := &WorkflowRunEvent{Action: &zeroValue}