github.com/google/go-github/v33@v33.0.0/github/actions_workflow_jobs_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_ListWorkflowJobs(t *testing.T) { 19 client, mux, _, teardown := setup() 20 defer teardown() 21 22 mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", 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,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`) 26 }) 27 28 opts := &ListWorkflowJobsOptions{ListOptions: ListOptions{Page: 2, PerPage: 2}} 29 jobs, _, err := client.Actions.ListWorkflowJobs(context.Background(), "o", "r", 29679449, opts) 30 if err != nil { 31 t.Errorf("Actions.ListWorkflowJobs returned error: %v", err) 32 } 33 34 want := &Jobs{ 35 TotalCount: Int(4), 36 Jobs: []*WorkflowJob{ 37 {ID: Int64(399444496), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 38 {ID: Int64(399444497), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 39 }, 40 } 41 if !reflect.DeepEqual(jobs, want) { 42 t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want) 43 } 44 } 45 46 func TestActionsService_ListWorkflowJobs_Filter(t *testing.T) { 47 client, mux, _, teardown := setup() 48 defer teardown() 49 50 mux.HandleFunc("/repos/o/r/actions/runs/29679449/jobs", func(w http.ResponseWriter, r *http.Request) { 51 testMethod(t, r, "GET") 52 testFormValues(t, r, values{"filter": "all", "per_page": "2", "page": "2"}) 53 fmt.Fprint(w, `{"total_count":4,"jobs":[{"id":399444496,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"},{"id":399444497,"run_id":29679449,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}]}`) 54 }) 55 56 opts := &ListWorkflowJobsOptions{Filter: "all", ListOptions: ListOptions{Page: 2, PerPage: 2}} 57 jobs, _, err := client.Actions.ListWorkflowJobs(context.Background(), "o", "r", 29679449, opts) 58 if err != nil { 59 t.Errorf("Actions.ListWorkflowJobs returned error: %v", err) 60 } 61 62 want := &Jobs{ 63 TotalCount: Int(4), 64 Jobs: []*WorkflowJob{ 65 {ID: Int64(399444496), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 66 {ID: Int64(399444497), RunID: Int64(29679449), StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, 67 }, 68 } 69 if !reflect.DeepEqual(jobs, want) { 70 t.Errorf("Actions.ListWorkflowJobs returned %+v, want %+v", jobs, want) 71 } 72 } 73 74 func TestActionsService_GetWorkflowJobByID(t *testing.T) { 75 client, mux, _, teardown := setup() 76 defer teardown() 77 78 mux.HandleFunc("/repos/o/r/actions/jobs/399444496", func(w http.ResponseWriter, r *http.Request) { 79 testMethod(t, r, "GET") 80 fmt.Fprint(w, `{"id":399444496,"started_at":"2019-01-02T15:04:05Z","completed_at":"2020-01-02T15:04:05Z"}`) 81 }) 82 83 job, _, err := client.Actions.GetWorkflowJobByID(context.Background(), "o", "r", 399444496) 84 if err != nil { 85 t.Errorf("Actions.GetWorkflowJobByID returned error: %v", err) 86 } 87 88 want := &WorkflowJob{ 89 ID: Int64(399444496), 90 StartedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, 91 CompletedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, 92 } 93 if !reflect.DeepEqual(job, want) { 94 t.Errorf("Actions.GetWorkflowJobByID returned %+v, want %+v", job, want) 95 } 96 } 97 98 func TestActionsService_GetWorkflowJobLogs(t *testing.T) { 99 client, mux, _, teardown := setup() 100 defer teardown() 101 102 mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 103 testMethod(t, r, "GET") 104 http.Redirect(w, r, "http://github.com/a", http.StatusFound) 105 }) 106 107 url, resp, err := client.Actions.GetWorkflowJobLogs(context.Background(), "o", "r", 399444496, true) 108 if err != nil { 109 t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) 110 } 111 if resp.StatusCode != http.StatusFound { 112 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) 113 } 114 want := "http://github.com/a" 115 if url.String() != want { 116 t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) 117 } 118 } 119 120 func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_dontFollowRedirects(t *testing.T) { 121 client, mux, _, teardown := setup() 122 defer teardown() 123 124 mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 125 testMethod(t, r, "GET") 126 http.Redirect(w, r, "http://github.com/a", http.StatusMovedPermanently) 127 }) 128 129 _, resp, _ := client.Actions.GetWorkflowJobLogs(context.Background(), "o", "r", 399444496, false) 130 if resp.StatusCode != http.StatusMovedPermanently { 131 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusMovedPermanently) 132 } 133 } 134 135 func TestActionsService_GetWorkflowJobLogs_StatusMovedPermanently_followRedirects(t *testing.T) { 136 client, mux, serverURL, teardown := setup() 137 defer teardown() 138 139 // Mock a redirect link, which leads to an archive link 140 mux.HandleFunc("/repos/o/r/actions/jobs/399444496/logs", func(w http.ResponseWriter, r *http.Request) { 141 testMethod(t, r, "GET") 142 redirectURL, _ := url.Parse(serverURL + baseURLPath + "/redirect") 143 http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently) 144 }) 145 146 mux.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) { 147 testMethod(t, r, "GET") 148 http.Redirect(w, r, "http://github.com/a", http.StatusFound) 149 }) 150 151 url, resp, err := client.Actions.GetWorkflowJobLogs(context.Background(), "o", "r", 399444496, true) 152 if err != nil { 153 t.Errorf("Actions.GetWorkflowJobLogs returned error: %v", err) 154 } 155 156 if resp.StatusCode != http.StatusFound { 157 t.Errorf("Actions.GetWorkflowJobLogs returned status: %d, want %d", resp.StatusCode, http.StatusFound) 158 } 159 160 want := "http://github.com/a" 161 if url.String() != want { 162 t.Errorf("Actions.GetWorkflowJobLogs returned %+v, want %+v", url.String(), want) 163 } 164 }