github.com/google/go-github/v33@v33.0.0/github/actions_workflow_runs_test.go (about) 1 // Copyright 2020 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "net/http" 12 "net/url" 13 "reflect" 14 "testing" 15 "time" 16 ) 17 18 func TestActionsService_ListWorkflowRunsByID(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/actions/workflows/29679449/runs", func(w http.ResponseWriter, r *http.Request) { 23 testMethod(t, r, "GET") 24 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 25 fmt.Fprint(w, `{"total_count":4,"workflow_runs":[{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 26 }) 27 28 opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}} 29 runs, _, err := client.Actions.ListWorkflowRunsByID(context.Background(), "o", "r", 29679449, opts) 30 if err != nil { 31 t.Errorf("Actions.ListWorkFlowRunsByID returned error: %v", err) 32 } 33 34 want := &WorkflowRuns{ 35 TotalCount: Int(4), 36 WorkflowRuns: []*WorkflowRun{ 37 {ID: Int64(399444496), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 38 {ID: Int64(399444497), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 39 }, 40 } 41 if !reflect.DeepEqual(runs, want) { 42 t.Errorf("Actions.ListWorkflowRunsByID returned %+v, want %+v", runs, want) 43 } 44 } 45 46 func TestActionsService_ListWorkflowRunsFileName(t *testing.T) { 47 client, mux, _, teardown := setup() 48 defer teardown() 49 50 mux.HandleFunc("/repos/o/r/actions/workflows/29679449/runs", func(w http.ResponseWriter, r *http.Request) { 51 testMethod(t, r, "GET") 52 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 53 fmt.Fprint(w, `{"total_count":4,"workflow_runs":[{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) 54 }) 55 56 opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}} 57 runs, _, err := client.Actions.ListWorkflowRunsByFileName(context.Background(), "o", "r", "29679449", opts) 58 if err != nil { 59 t.Errorf("Actions.ListWorkFlowRunsByFileName returned error: %v", err) 60 } 61 62 want := &WorkflowRuns{ 63 TotalCount: Int(4), 64 WorkflowRuns: []*WorkflowRun{ 65 {ID: Int64(399444496), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 66 {ID: Int64(399444497), RunNumber: Int(296), CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 67 }, 68 } 69 if !reflect.DeepEqual(runs, want) { 70 t.Errorf("Actions.ListWorkflowRunsByFileName returned %+v, want %+v", runs, want) 71 } 72 } 73 74 func TestActionsService_GetWorkflowRunByID(t *testing.T) { 75 client, mux, _, teardown := setup() 76 defer teardown() 77 78 mux.HandleFunc("/repos/o/r/actions/runs/29679449", func(w http.ResponseWriter, r *http.Request) { 79 testMethod(t, r, "GET") 80 fmt.Fprint(w, `{"id":399444496,"run_number":296,"created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}}`) 81 }) 82 83 runs, _, err := client.Actions.GetWorkflowRunByID(context.Background(), "o", "r", 29679449) 84 if err != nil { 85 t.Errorf("Actions.GetWorkflowRunByID returned error: %v", err) 86 } 87 88 want := &WorkflowRun{ 89 ID: Int64(399444496), 90 RunNumber: Int(296), 91 CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 92 UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 93 } 94 95 if !reflect.DeepEqual(runs, want) { 96 t.Errorf("Actions.GetWorkflowRunByID returned %+v, want %+v", runs, want) 97 } 98 } 99 100 func TestActionsService_RerunWorkflowRunByID(t *testing.T) { 101 client, mux, _, teardown := setup() 102 defer teardown() 103 104 mux.HandleFunc("/repos/o/r/actions/runs/3434/rerun", func(w http.ResponseWriter, r *http.Request) { 105 testMethod(t, r, "POST") 106 w.WriteHeader(http.StatusCreated) 107 }) 108 109 resp, err := client.Actions.RerunWorkflowByID(context.Background(), "o", "r", 3434) 110 if err != nil { 111 t.Errorf("Actions.RerunWorkflowByID returned error: %v", err) 112 } 113 if resp.StatusCode != http.StatusCreated { 114 t.Errorf("Actions.RerunWorkflowRunByID returned status: %d, want %d", resp.StatusCode, http.StatusCreated) 115 } 116 } 117 118 func TestActionsService_CancelWorkflowRunByID(t *testing.T) { 119 client, mux, _, teardown := setup() 120 defer teardown() 121 122 mux.HandleFunc("/repos/o/r/actions/runs/3434/cancel", func(w http.ResponseWriter, r *http.Request) { 123 testMethod(t, r, "POST") 124 w.WriteHeader(http.StatusAccepted) 125 }) 126 127 resp, err := client.Actions.CancelWorkflowRunByID(context.Background(), "o", "r", 3434) 128 if _, ok := err.(*AcceptedError); !ok { 129 t.Errorf("Actions.CancelWorkflowRunByID returned error: %v (want AcceptedError)", err) 130 } 131 if resp.StatusCode != http.StatusAccepted { 132 t.Errorf("Actions.CancelWorkflowRunByID returned status: %d, want %d", resp.StatusCode, http.StatusAccepted) 133 } 134 } 135 136 func TestActionsService_GetWorkflowRunLogs(t *testing.T) { 137 client, mux, _, teardown := setup() 138 defer teardown() 139 140 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 141 testMethod(t, r, "GET") 142 http.Redirect(w, r, "http://github.com/a", http.StatusFound) 143 }) 144 145 url, resp, err := client.Actions.GetWorkflowRunLogs(context.Background(), "o", "r", 399444496, true) 146 if err != nil { 147 t.Errorf("Actions.GetWorkflowRunLogs returned error: %v", err) 148 } 149 if resp.StatusCode != http.StatusFound { 150 t.Errorf("Actions.GetWorkflowRunLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) 151 } 152 want := "http://github.com/a" 153 if url.String() != want { 154 t.Errorf("Actions.GetWorkflowRunLogs returned %+v, want %+v", url.String(), want) 155 } 156 } 157 158 func TestActionsService_GetWorkflowRunLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) { 159 client, mux, _, teardown := setup() 160 defer teardown() 161 162 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 163 testMethod(t, r, "GET") 164 http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently) 165 }) 166 167 _, resp, _ := client.Actions.GetWorkflowRunLogs(context.Background(), "o", "r", 399444496, false) 168 if resp.StatusCode != http.StatusMovedPermanently { 169 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently) 170 } 171 } 172 173 func TestActionsService_GetWorkflowRunLogs_StatusMovedPermanently_followRedirects(t *testing.T) { 174 client, mux, serverURL, teardown := setup() 175 defer teardown() 176 177 // Mock a redirect link, which leads to an archive link 178 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 179 testMethod(t, r, "GET") 180 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect") 181 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently) 182 }) 183 184 mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { 185 testMethod(t, r, "GET") 186 http.Redirect(w, r, "http://github.com/a", http.StatusFound) 187 }) 188 189 url, resp, err := client.Actions.GetWorkflowRunLogs(context.Background(), "o", "r", 399444496, true) 190 if err != nil { 191 t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) 192 } 193 194 if resp.StatusCode != http.StatusFound { 195 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) 196 } 197 198 want := "http://github.com/a" 199 if url.String() != want { 200 t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) 201 } 202 } 203 204 func TestActionService_ListRepositoryWorkflowRuns(t *testing.T) { 205 client, mux, _, teardown := setup() 206 defer teardown() 207 208 mux.HandleFunc("/repos/o/r/actions/runs", func(w http.ResponseWriter, r *http.Request) { 209 testMethod(t, r, "GET") 210 testFormValues(t, r, values{"per_page": "2", "page": "2"}) 211 fmt.Fprint(w, `{"total_count":2, 212 "workflow_runs":[ 213 {"id":298499444,"run_number":301,"created_at":"2020-04-11T11:14:54Z","updated_at":"2020-04-11T11:14:54Z"}, 214 {"id":298499445,"run_number":302,"created_at":"2020-04-11T11:14:54Z","updated_at":"2020-04-11T11:14:54Z"}]}`) 215 216 }) 217 218 opts := &ListWorkflowRunsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}} 219 runs, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), "o", "r", opts) 220 221 if err != nil { 222 t.Errorf("Actions.ListRepositoryWorkflowRuns returned error: %v", err) 223 } 224 225 expected := &WorkflowRuns{ 226 TotalCount: Int(2), 227 WorkflowRuns: []*WorkflowRun{ 228 {ID: Int64(298499444), RunNumber: Int(301), CreatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}}, 229 {ID: Int64(298499445), RunNumber: Int(302), CreatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.April, 11, 11, 14, 54, 0, time.UTC)}}, 230 }, 231 } 232 233 if !reflect.DeepEqual(runs, expected) { 234 t.Errorf("Actions.ListRepositoryWorkflowRuns returned %+v, want %+v", runs, expected) 235 } 236 237 } 238 239 func TestActionService_DeleteWorkflowRunLogs(t *testing.T) { 240 client, mux, _, teardown := setup() 241 defer teardown() 242 243 mux.HandleFunc("/repos/o/r/actions/runs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 244 testMethod(t, r, "DELETE") 245 246 w.WriteHeader(http.StatusNoContent) 247 }) 248 249 if _, err := client.Actions.DeleteWorkflowRunLogs(context.Background(), "o", "r", 399444496); err != nil { 250 t.Errorf("DeleteWorkflowRunLogs returned error: %v", err) 251 } 252 } 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 }