diff --git a/Makefile b/Makefile index d69ce0e..beb8ee9 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SOURCE=./... GOFMT_FILES?=$$(find . -type f -name '*.go') -VERSION?=0.1.0 +VERSION?=0.1.1 default: build diff --git a/internal/dispatch/conf.go b/internal/dispatch/conf.go new file mode 100644 index 0000000..a0ff72d --- /dev/null +++ b/internal/dispatch/conf.go @@ -0,0 +1,21 @@ +package dispatch + +import ( + "github.com/cli/go-gh/pkg/auth" + "github.com/cli/go-gh/pkg/config" +) + +// Conf implements the cliapi tokenGetter interface. +// In the context of gh-dispatch, its only purpose is to provide +// a configuration to the GH HTTP client configuration that +// enables the retrieval of an auth token in the same standard way +// that gh itself retrieves the auth token. +type Conf struct { + *config.Config +} + +// AuthToken implements the cliapi tokenGetter interface +// by providing a method for retrieving the auth token. +func (c *Conf) AuthToken(hostname string) (string, string) { + return auth.TokenForHost(hostname) +} diff --git a/internal/dispatch/ghrepo.go b/internal/dispatch/ghrepo.go new file mode 100644 index 0000000..efd197e --- /dev/null +++ b/internal/dispatch/ghrepo.go @@ -0,0 +1,35 @@ +package dispatch + +import ( + "fmt" + + "github.com/cli/go-gh/pkg/auth" +) + +// ghRepo satisfies the ghrepo interface. +// In the context of gh-dispatch, it enables the reuse of +// functions packaged in the upstream github.com/cli/cli +// codebase for rendering GH Actions run output. +// See github.com/cli/cli/v2/internal/ghrepo. +type ghRepo struct { + Name string + Owner string +} + +func (r ghRepo) RepoName() string { + return r.Name +} + +func (r ghRepo) RepoOwner() string { + return r.Owner +} + +func (r ghRepo) RepoHost() string { + host, _ := auth.DefaultHost() + + return host +} + +func (r ghRepo) RepoFullName() string { + return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName()) +} diff --git a/internal/dispatch/ghrepo_test.go b/internal/dispatch/ghrepo_test.go new file mode 100644 index 0000000..a5e1a35 --- /dev/null +++ b/internal/dispatch/ghrepo_test.go @@ -0,0 +1,19 @@ +package dispatch + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGHRepo(t *testing.T) { + ghRepo := &ghRepo{ + Name: "REPO", + Owner: "OWNER", + } + + assert.Equal(t, "OWNER/REPO", ghRepo.RepoFullName()) + assert.Equal(t, "OWNER", ghRepo.RepoOwner()) + assert.Equal(t, "REPO", ghRepo.RepoName()) + assert.Equal(t, "github.com", ghRepo.RepoHost()) +} diff --git a/internal/dispatch/shared.go b/internal/dispatch/renderutils.go similarity index 76% rename from internal/dispatch/shared.go rename to internal/dispatch/renderutils.go index f11a8ec..3b768b1 100644 --- a/internal/dispatch/shared.go +++ b/internal/dispatch/renderutils.go @@ -4,71 +4,14 @@ import ( "bytes" "fmt" "io" - "net/http" "time" cliapi "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/pkg/cmd/run/shared" "github.com/cli/cli/v2/pkg/cmdutil" "github.com/cli/cli/v2/pkg/iostreams" - "github.com/cli/go-gh/pkg/auth" - "github.com/cli/go-gh/pkg/config" ) -// Conf implements the cliapi tokenGetter interface -type Conf struct { - *config.Config -} - -// AuthToken implements the cliapi tokenGetter interface -// by providing a method for retrieving the auth token. -func (c *Conf) AuthToken(hostname string) (string, string) { - return auth.TokenForHost(hostname) -} - -type workflowRun struct { - ID int64 `json:"id"` - WorkflowID int `json:"workflow_id"` - Name string `json:"name"` - Status shared.Status `json:"status"` - Conclusion string `json:"conclusion"` -} - -type workflowRunsResponse struct { - WorkflowRuns []workflowRun `json:"workflow_runs"` -} - -type dispatchOptions struct { - repo *ghRepo - httpClient *http.Client - io *iostreams.IOStreams -} - -// ghRepo satisfies the ghrepo interface... -// See github.com/cli/cli/v2/internal/ghrepo. -type ghRepo struct { - Name string - Owner string -} - -func (r ghRepo) RepoName() string { - return r.Name -} - -func (r ghRepo) RepoOwner() string { - return r.Owner -} - -func (r ghRepo) RepoHost() string { - host, _ := auth.DefaultHost() - - return host -} - -func (r ghRepo) RepoFullName() string { - return fmt.Sprintf("%s/%s", r.RepoOwner(), r.RepoName()) -} - func render(ios *iostreams.IOStreams, client *cliapi.Client, repo *ghRepo, run *shared.Run) error { cs := ios.ColorScheme() annotationCache := map[int64][]shared.Annotation{} diff --git a/internal/dispatch/repository.go b/internal/dispatch/repository.go index cef592b..67597d9 100644 --- a/internal/dispatch/repository.go +++ b/internal/dispatch/repository.go @@ -26,6 +26,7 @@ type repositoryDispatchOptions struct { dispatchOptions } +// NewCmdRepository returns a new repository command. func NewCmdRepository() *cobra.Command { var ( repositoryEventType string @@ -100,6 +101,8 @@ func NewCmdRepository() *cobra.Command { } func repositoryDispatchRun(opts *repositoryDispatchOptions) error { + ghClient := cliapi.NewClientFromHTTP(opts.httpClient) + var buf bytes.Buffer err := json.NewEncoder(&buf).Encode(repositoryDispatchRequest{ EventType: opts.eventType, @@ -109,8 +112,6 @@ func repositoryDispatchRun(opts *repositoryDispatchOptions) error { return err } - ghClient := cliapi.NewClientFromHTTP(opts.httpClient) - var in interface{} err = ghClient.REST(opts.repo.RepoHost(), "POST", fmt.Sprintf("repos/%s/dispatches", opts.repo.RepoFullName()), &buf, &in) if err != nil { diff --git a/internal/dispatch/repository_test.go b/internal/dispatch/repository_test.go index 1bde627..243ad34 100644 --- a/internal/dispatch/repository_test.go +++ b/internal/dispatch/repository_test.go @@ -19,6 +19,79 @@ func TestRepositoryDispatchRun(t *testing.T) { repo := ghRepo.RepoFullName() event := "repository_dispatch" + createMockRegistry := func(reg *httpmock.Registry, conclusion, jobsResponse string) { + reg.Register( + httpmock.REST("POST", fmt.Sprintf("repos/%s/dispatches", repo)), + httpmock.RESTPayload(201, "{}", func(params map[string]interface{}) { + assert.Equal(t, map[string]interface{}{ + "event_type": "hello", + "client_payload": interface{}(nil), + }, params) + })) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows", repo)), + httpmock.StringResponse(getWorkflowsResponse)) + + reg.Register( + httpmock.GraphQL("query UserCurrent{viewer{login}}"), + httpmock.StringResponse(currentUserResponse)) + + v := url.Values{} + v.Set("per_page", "50") + + reg.Register( + httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows/456/runs", repo), v), + httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event, repo))) + + q := url.Values{} + q.Set("per_page", "100") + q.Set("page", "1") + + reg.Register( + httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows", repo), q), + httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event, repo))) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), + httpmock.StringResponse(`{ + "id": 123, + "workflow_id": 456, + "event": "repository_dispatch" + }`)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), + httpmock.StringResponse(fmt.Sprintf(`{ + "id": 123, + "workflow_id": 456, + "status": "completed", + "event": "repository_dispatch", + "conclusion": "%s", + "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs" + }`, conclusion, repo))) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123/jobs", repo)), + httpmock.StringResponse(jobsResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/check-runs/123/annotations", repo)), + httpmock.StringResponse("[]")) + } + tests := []struct { name string opts *repositoryDispatchOptions @@ -34,71 +107,7 @@ func TestRepositoryDispatchRun(t *testing.T) { workflow: "foo", }, httpStubs: func(reg *httpmock.Registry) { - reg.Register( - httpmock.REST("POST", fmt.Sprintf("repos/%s/dispatches", repo)), - httpmock.StringResponse("{}")) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows", repo)), - httpmock.StringResponse(getWorkflowsResponse)) - - reg.Register( - httpmock.GraphQL("query UserCurrent{viewer{login}}"), - httpmock.StringResponse(currentUserResponse)) - - v := url.Values{} - v.Set("per_page", "50") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows/456/runs", repo), v), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event, repo))) - - q := url.Values{} - q.Set("per_page", "100") - q.Set("page", "1") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows", repo), q), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event, repo))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(`{ - "id": 123, - "workflow_id": 456, - "event": "repository_dispatch" - }`)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(fmt.Sprintf(`{ - "id": 123, - "workflow_id": 456, - "status": "completed", - "event": "repository_dispatch", - "conclusion": "success", - "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs" - }`, repo))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123/jobs", repo)), - httpmock.StringResponse(getJobsResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/check-runs/123/annotations", repo)), - httpmock.StringResponse("[]")) + createMockRegistry(reg, "success", getJobsResponse) }, wantOut: `Refreshing run status every 2 seconds. Press Ctrl+C to quit. @@ -119,75 +128,7 @@ JOBS workflow: "foo", }, httpStubs: func(reg *httpmock.Registry) { - // TODO: test that proper request body is sent on POSTs - reg.Register( - httpmock.REST("POST", fmt.Sprintf("repos/%s/dispatches", repo)), - httpmock.StringResponse("{}")) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows", repo)), - httpmock.StringResponse(getWorkflowsResponse)) - - reg.Register( - httpmock.GraphQL("query UserCurrent{viewer{login}}"), - httpmock.StringResponse(currentUserResponse)) - - v := url.Values{} - v.Set("per_page", "50") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows/456/runs", repo), v), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event, repo))) - - q := url.Values{} - q.Set("per_page", "100") - q.Set("page", "1") - - // TODO: is this correct? is it the correct response? - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows", repo), q), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event, repo))) - - // TODO: is this correct? is it the correct response? - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - // TODO: is this correct? is it the correct response? - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(`{ - "id": 123, - "workflow_id": 456, - "event": "repository_dispatch" - }`)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(fmt.Sprintf(`{ - "id": 123, - "workflow_id": 456, - "status": "completed", - "event": "repository_dispatch", - "conclusion": "failure", - "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs" - }`, repo))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123/jobs", repo)), - httpmock.StringResponse(getFailingJobsResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/check-runs/123/annotations", repo)), - httpmock.StringResponse("[]")) + createMockRegistry(reg, "failure", getFailingJobsResponse) }, wantOut: `Refreshing run status every 2 seconds. Press Ctrl+C to quit. diff --git a/internal/dispatch/types.go b/internal/dispatch/types.go new file mode 100644 index 0000000..e37e395 --- /dev/null +++ b/internal/dispatch/types.go @@ -0,0 +1,26 @@ +package dispatch + +import ( + "net/http" + + "github.com/cli/cli/v2/pkg/cmd/run/shared" + "github.com/cli/cli/v2/pkg/iostreams" +) + +type workflowRun struct { + ID int64 `json:"id"` + WorkflowID int `json:"workflow_id"` + Name string `json:"name"` + Status shared.Status `json:"status"` + Conclusion string `json:"conclusion"` +} + +type workflowRunsResponse struct { + WorkflowRuns []workflowRun `json:"workflow_runs"` +} + +type dispatchOptions struct { + repo *ghRepo + httpClient *http.Client + io *iostreams.IOStreams +} diff --git a/internal/dispatch/workflow.go b/internal/dispatch/workflow.go index cf4c2ca..b5f542d 100644 --- a/internal/dispatch/workflow.go +++ b/internal/dispatch/workflow.go @@ -26,6 +26,7 @@ type workflowDispatchOptions struct { dispatchOptions } +// NewCmdWorkflow returns a new workflow command. func NewCmdWorkflow() *cobra.Command { var ( workflowInputs string @@ -110,6 +111,8 @@ func NewCmdWorkflow() *cobra.Command { } func workflowDispatchRun(opts *workflowDispatchOptions) error { + ghClient := cliapi.NewClientFromHTTP(opts.httpClient) + var buf bytes.Buffer err := json.NewEncoder(&buf).Encode(workflowDispatchRequest{ Inputs: opts.inputs, @@ -119,8 +122,6 @@ func workflowDispatchRun(opts *workflowDispatchOptions) error { return err } - ghClient := cliapi.NewClientFromHTTP(opts.httpClient) - var in interface{} err = ghClient.REST(opts.repo.RepoHost(), "POST", fmt.Sprintf("repos/%s/actions/workflows/%s/dispatches", opts.repo.RepoFullName(), opts.workflow), &buf, &in) if err != nil { diff --git a/internal/dispatch/workflow_test.go b/internal/dispatch/workflow_test.go index bf2773f..97a02ae 100644 --- a/internal/dispatch/workflow_test.go +++ b/internal/dispatch/workflow_test.go @@ -20,6 +20,79 @@ func TestWorkflowDispatchRun(t *testing.T) { workflow := "workflow.yaml" event := "workflow_dispatch" + createMockRegistry := func(reg *httpmock.Registry, conclusion, jobsResponse string) { + reg.Register( + httpmock.REST("POST", fmt.Sprintf("repos/%s/actions/workflows/%s/dispatches", repo, "workflow.yaml")), + httpmock.RESTPayload(201, "{}", func(params map[string]interface{}) { + assert.Equal(t, map[string]interface{}{ + "inputs": "{\"foo\": \"bar\"}", + "ref": "", + }, params) + })) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/workflow.yaml", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.GraphQL("query UserCurrent{viewer{login}}"), + httpmock.StringResponse(currentUserResponse)) + + v := url.Values{} + v.Set("per_page", "50") + + reg.Register( + httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows/456/runs", repo), v), + httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event))) + + q := url.Values{} + q.Set("per_page", "100") + q.Set("page", "1") + + reg.Register( + httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows", repo), q), + httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event))) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), + httpmock.StringResponse(`{ + "id": 123, + "workflow_id": 456, + "event": "workflow_dispatch" + }`)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), + httpmock.StringResponse(getWorkflowResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), + httpmock.StringResponse(fmt.Sprintf(`{ + "id": 123, + "workflow_id": 456, + "event": "workflow_dispatch", + "status": "completed", + "conclusion": "%s", + "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs" + }`, conclusion, repo))) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123/jobs", repo)), + httpmock.StringResponse(jobsResponse)) + + reg.Register( + httpmock.REST("GET", fmt.Sprintf("repos/%s/check-runs/123/annotations", repo)), + httpmock.StringResponse("[]")) + } + tests := []struct { name string opts *workflowDispatchOptions @@ -35,72 +108,7 @@ func TestWorkflowDispatchRun(t *testing.T) { workflow: workflow, }, httpStubs: func(reg *httpmock.Registry) { - // TODO: test that proper request body is sent on POSTs - reg.Register( - httpmock.REST("POST", fmt.Sprintf("repos/%s/actions/workflows/%s/dispatches", repo, "workflow.yaml")), - httpmock.StringResponse("{}")) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/workflow.yaml", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.GraphQL("query UserCurrent{viewer{login}}"), - httpmock.StringResponse(currentUserResponse)) - - v := url.Values{} - v.Set("per_page", "50") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows/456/runs", repo), v), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event))) - - q := url.Values{} - q.Set("per_page", "100") - q.Set("page", "1") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows", repo), q), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(`{ - "id": 123, - "workflow_id": 456, - "event": "workflow_dispatch" - }`)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(fmt.Sprintf(`{ - "id": 123, - "workflow_id": 456, - "event": "workflow_dispatch", - "status": "completed", - "conclusion": "success", - "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs" - }`, repo))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123/jobs", repo)), - httpmock.StringResponse(getJobsResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/check-runs/123/annotations", repo)), - httpmock.StringResponse("[]")) + createMockRegistry(reg, "success", getJobsResponse) }, wantOut: `Refreshing run status every 2 seconds. Press Ctrl+C to quit. @@ -121,73 +129,7 @@ JOBS workflow: workflow, }, httpStubs: func(reg *httpmock.Registry) { - // TODO: test that proper request body is sent on POSTs - reg.Register( - httpmock.REST("POST", fmt.Sprintf("repos/%s/actions/workflows/%s/dispatches", repo, "workflow.yaml")), - httpmock.StringResponse("{}")) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/workflow.yaml", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.GraphQL("query UserCurrent{viewer{login}}"), - httpmock.StringResponse(currentUserResponse)) - - v := url.Values{} - v.Set("per_page", "50") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows/456/runs", repo), v), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event))) - - q := url.Values{} - q.Set("per_page", "100") - q.Set("page", "1") - - reg.Register( - httpmock.QueryMatcher("GET", fmt.Sprintf("repos/%s/actions/workflows", repo), q), - httpmock.StringResponse(fmt.Sprintf(getWorkflowRunsResponse, event))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - // TODO: is this correct? is it the correct response? - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(`{ - "id": 123, - "workflow_id": 456, - "event": "workflow_dispatch" - }`)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/workflows/456", repo)), - httpmock.StringResponse(getWorkflowResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123", repo)), - httpmock.StringResponse(fmt.Sprintf(`{ - "id": 123, - "workflow_id": 456, - "event": "workflow_dispatch", - "status": "completed", - "conclusion": "failure", - "jobs_url": "https://api.github.com/repos/%s/actions/runs/123/jobs" - }`, repo))) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/actions/runs/123/jobs", repo)), - httpmock.StringResponse(getFailingJobsResponse)) - - reg.Register( - httpmock.REST("GET", fmt.Sprintf("repos/%s/check-runs/123/annotations", repo)), - httpmock.StringResponse("[]")) + createMockRegistry(reg, "failure", getFailingJobsResponse) }, wantOut: `Refreshing run status every 2 seconds. Press Ctrl+C to quit.