github.com/google/go-github/v57@v57.0.0/github/actions_workflow_runs.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  )
    14  
    15  // WorkflowRun represents a repository action workflow run.
    16  type WorkflowRun struct {
    17  	ID                  *int64                `json:"id,omitempty"`
    18  	Name                *string               `json:"name,omitempty"`
    19  	NodeID              *string               `json:"node_id,omitempty"`
    20  	HeadBranch          *string               `json:"head_branch,omitempty"`
    21  	HeadSHA             *string               `json:"head_sha,omitempty"`
    22  	RunNumber           *int                  `json:"run_number,omitempty"`
    23  	RunAttempt          *int                  `json:"run_attempt,omitempty"`
    24  	Event               *string               `json:"event,omitempty"`
    25  	DisplayTitle        *string               `json:"display_title,omitempty"`
    26  	Status              *string               `json:"status,omitempty"`
    27  	Conclusion          *string               `json:"conclusion,omitempty"`
    28  	WorkflowID          *int64                `json:"workflow_id,omitempty"`
    29  	CheckSuiteID        *int64                `json:"check_suite_id,omitempty"`
    30  	CheckSuiteNodeID    *string               `json:"check_suite_node_id,omitempty"`
    31  	URL                 *string               `json:"url,omitempty"`
    32  	HTMLURL             *string               `json:"html_url,omitempty"`
    33  	PullRequests        []*PullRequest        `json:"pull_requests,omitempty"`
    34  	CreatedAt           *Timestamp            `json:"created_at,omitempty"`
    35  	UpdatedAt           *Timestamp            `json:"updated_at,omitempty"`
    36  	RunStartedAt        *Timestamp            `json:"run_started_at,omitempty"`
    37  	JobsURL             *string               `json:"jobs_url,omitempty"`
    38  	LogsURL             *string               `json:"logs_url,omitempty"`
    39  	CheckSuiteURL       *string               `json:"check_suite_url,omitempty"`
    40  	ArtifactsURL        *string               `json:"artifacts_url,omitempty"`
    41  	CancelURL           *string               `json:"cancel_url,omitempty"`
    42  	RerunURL            *string               `json:"rerun_url,omitempty"`
    43  	PreviousAttemptURL  *string               `json:"previous_attempt_url,omitempty"`
    44  	HeadCommit          *HeadCommit           `json:"head_commit,omitempty"`
    45  	WorkflowURL         *string               `json:"workflow_url,omitempty"`
    46  	Repository          *Repository           `json:"repository,omitempty"`
    47  	HeadRepository      *Repository           `json:"head_repository,omitempty"`
    48  	Actor               *User                 `json:"actor,omitempty"`
    49  	TriggeringActor     *User                 `json:"triggering_actor,omitempty"`
    50  	ReferencedWorkflows []*ReferencedWorkflow `json:"referenced_workflows,omitempty"`
    51  }
    52  
    53  // WorkflowRuns represents a slice of repository action workflow run.
    54  type WorkflowRuns struct {
    55  	TotalCount   *int           `json:"total_count,omitempty"`
    56  	WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"`
    57  }
    58  
    59  // ListWorkflowRunsOptions specifies optional parameters to ListWorkflowRuns.
    60  type ListWorkflowRunsOptions struct {
    61  	Actor               string `url:"actor,omitempty"`
    62  	Branch              string `url:"branch,omitempty"`
    63  	Event               string `url:"event,omitempty"`
    64  	Status              string `url:"status,omitempty"`
    65  	Created             string `url:"created,omitempty"`
    66  	HeadSHA             string `url:"head_sha,omitempty"`
    67  	ExcludePullRequests bool   `url:"exclude_pull_requests,omitempty"`
    68  	CheckSuiteID        int64  `url:"check_suite_id,omitempty"`
    69  	ListOptions
    70  }
    71  
    72  // WorkflowRunUsage represents a usage of a specific workflow run.
    73  type WorkflowRunUsage struct {
    74  	Billable      *WorkflowRunBillMap `json:"billable,omitempty"`
    75  	RunDurationMS *int64              `json:"run_duration_ms,omitempty"`
    76  }
    77  
    78  // WorkflowRunBillMap represents different runner environments available for a workflow run.
    79  // Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc.
    80  type WorkflowRunBillMap map[string]*WorkflowRunBill
    81  
    82  // WorkflowRunBill specifies billable time for a specific environment in a workflow run.
    83  type WorkflowRunBill struct {
    84  	TotalMS *int64               `json:"total_ms,omitempty"`
    85  	Jobs    *int                 `json:"jobs,omitempty"`
    86  	JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"`
    87  }
    88  
    89  // WorkflowRunJobRun represents a usage of individual jobs of a specific workflow run.
    90  type WorkflowRunJobRun struct {
    91  	JobID      *int   `json:"job_id,omitempty"`
    92  	DurationMS *int64 `json:"duration_ms,omitempty"`
    93  }
    94  
    95  // WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt.
    96  type WorkflowRunAttemptOptions struct {
    97  	ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"`
    98  }
    99  
   100  // PendingDeploymentsRequest specifies body parameters to PendingDeployments.
   101  type PendingDeploymentsRequest struct {
   102  	EnvironmentIDs []int64 `json:"environment_ids"`
   103  	// State can be one of: "approved", "rejected".
   104  	State   string `json:"state"`
   105  	Comment string `json:"comment"`
   106  }
   107  
   108  type ReferencedWorkflow struct {
   109  	Path *string `json:"path,omitempty"`
   110  	SHA  *string `json:"sha,omitempty"`
   111  	Ref  *string `json:"ref,omitempty"`
   112  }
   113  
   114  func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   115  	u, err := addOptions(endpoint, opts)
   116  	if err != nil {
   117  		return nil, nil, err
   118  	}
   119  
   120  	req, err := s.client.NewRequest("GET", u, nil)
   121  	if err != nil {
   122  		return nil, nil, err
   123  	}
   124  
   125  	runs := new(WorkflowRuns)
   126  	resp, err := s.client.Do(ctx, req, &runs)
   127  	if err != nil {
   128  		return nil, resp, err
   129  	}
   130  
   131  	return runs, resp, nil
   132  }
   133  
   134  // ListWorkflowRunsByID lists all workflow runs by workflow ID.
   135  //
   136  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#list-workflow-runs-for-a-workflow
   137  //
   138  //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs
   139  func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   140  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
   141  	return s.listWorkflowRuns(ctx, u, opts)
   142  }
   143  
   144  // ListWorkflowRunsByFileName lists all workflow runs by workflow file name.
   145  //
   146  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#list-workflow-runs-for-a-workflow
   147  //
   148  //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs
   149  func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   150  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
   151  	return s.listWorkflowRuns(ctx, u, opts)
   152  }
   153  
   154  // ListRepositoryWorkflowRuns lists all workflow runs for a repository.
   155  //
   156  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#list-workflow-runs-for-a-repository
   157  //
   158  //meta:operation GET /repos/{owner}/{repo}/actions/runs
   159  func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   160  	u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
   161  	u, err := addOptions(u, opts)
   162  	if err != nil {
   163  		return nil, nil, err
   164  	}
   165  
   166  	req, err := s.client.NewRequest("GET", u, nil)
   167  	if err != nil {
   168  		return nil, nil, err
   169  	}
   170  
   171  	runs := new(WorkflowRuns)
   172  	resp, err := s.client.Do(ctx, req, &runs)
   173  	if err != nil {
   174  		return nil, resp, err
   175  	}
   176  
   177  	return runs, resp, nil
   178  }
   179  
   180  // GetWorkflowRunByID gets a specific workflow run by ID.
   181  //
   182  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run
   183  //
   184  //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}
   185  func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
   186  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   187  
   188  	req, err := s.client.NewRequest("GET", u, nil)
   189  	if err != nil {
   190  		return nil, nil, err
   191  	}
   192  
   193  	run := new(WorkflowRun)
   194  	resp, err := s.client.Do(ctx, req, run)
   195  	if err != nil {
   196  		return nil, resp, err
   197  	}
   198  
   199  	return run, resp, nil
   200  }
   201  
   202  // GetWorkflowRunAttempt gets a specific workflow run attempt.
   203  //
   204  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-a-workflow-run-attempt
   205  //
   206  //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt_number}
   207  func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) {
   208  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber)
   209  	u, err := addOptions(u, opts)
   210  	if err != nil {
   211  		return nil, nil, err
   212  	}
   213  
   214  	req, err := s.client.NewRequest("GET", u, nil)
   215  	if err != nil {
   216  		return nil, nil, err
   217  	}
   218  
   219  	run := new(WorkflowRun)
   220  	resp, err := s.client.Do(ctx, req, run)
   221  	if err != nil {
   222  		return nil, resp, err
   223  	}
   224  
   225  	return run, resp, nil
   226  }
   227  
   228  // GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number.
   229  //
   230  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-attempt-logs
   231  //
   232  //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/attempts/{attempt_number}/logs
   233  func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, maxRedirects int) (*url.URL, *Response, error) {
   234  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v/logs", owner, repo, runID, attemptNumber)
   235  
   236  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
   237  	if err != nil {
   238  		return nil, nil, err
   239  	}
   240  	defer resp.Body.Close()
   241  
   242  	if resp.StatusCode != http.StatusFound {
   243  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   244  	}
   245  
   246  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   247  	return parsedURL, newResponse(resp), err
   248  }
   249  
   250  // RerunWorkflowByID re-runs a workflow by ID.
   251  //
   252  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-workflow
   253  //
   254  //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun
   255  func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   256  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
   257  
   258  	req, err := s.client.NewRequest("POST", u, nil)
   259  	if err != nil {
   260  		return nil, err
   261  	}
   262  
   263  	return s.client.Do(ctx, req, nil)
   264  }
   265  
   266  // RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID.
   267  //
   268  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run
   269  //
   270  //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/rerun-failed-jobs
   271  func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   272  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID)
   273  
   274  	req, err := s.client.NewRequest("POST", u, nil)
   275  	if err != nil {
   276  		return nil, err
   277  	}
   278  
   279  	return s.client.Do(ctx, req, nil)
   280  }
   281  
   282  // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID.
   283  //
   284  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run
   285  //
   286  //meta:operation POST /repos/{owner}/{repo}/actions/jobs/{job_id}/rerun
   287  func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) {
   288  	u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID)
   289  
   290  	req, err := s.client.NewRequest("POST", u, nil)
   291  	if err != nil {
   292  		return nil, err
   293  	}
   294  
   295  	return s.client.Do(ctx, req, nil)
   296  }
   297  
   298  // CancelWorkflowRunByID cancels a workflow run by ID.
   299  //
   300  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#cancel-a-workflow-run
   301  //
   302  //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/cancel
   303  func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   304  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID)
   305  
   306  	req, err := s.client.NewRequest("POST", u, nil)
   307  	if err != nil {
   308  		return nil, err
   309  	}
   310  
   311  	return s.client.Do(ctx, req, nil)
   312  }
   313  
   314  // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
   315  //
   316  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#download-workflow-run-logs
   317  //
   318  //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/logs
   319  func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, maxRedirects int) (*url.URL, *Response, error) {
   320  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   321  
   322  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, maxRedirects)
   323  	if err != nil {
   324  		return nil, nil, err
   325  	}
   326  	defer resp.Body.Close()
   327  
   328  	if resp.StatusCode != http.StatusFound {
   329  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   330  	}
   331  
   332  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   333  	return parsedURL, newResponse(resp), err
   334  }
   335  
   336  // DeleteWorkflowRun deletes a workflow run by ID.
   337  //
   338  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-a-workflow-run
   339  //
   340  //meta:operation DELETE /repos/{owner}/{repo}/actions/runs/{run_id}
   341  func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   342  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   343  
   344  	req, err := s.client.NewRequest("DELETE", u, nil)
   345  	if err != nil {
   346  		return nil, err
   347  	}
   348  
   349  	return s.client.Do(ctx, req, nil)
   350  }
   351  
   352  // DeleteWorkflowRunLogs deletes all logs for a workflow run.
   353  //
   354  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#delete-workflow-run-logs
   355  //
   356  //meta:operation DELETE /repos/{owner}/{repo}/actions/runs/{run_id}/logs
   357  func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   358  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   359  
   360  	req, err := s.client.NewRequest("DELETE", u, nil)
   361  	if err != nil {
   362  		return nil, err
   363  	}
   364  
   365  	return s.client.Do(ctx, req, nil)
   366  }
   367  
   368  // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
   369  //
   370  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#get-workflow-run-usage
   371  //
   372  //meta:operation GET /repos/{owner}/{repo}/actions/runs/{run_id}/timing
   373  func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
   374  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
   375  
   376  	req, err := s.client.NewRequest("GET", u, nil)
   377  	if err != nil {
   378  		return nil, nil, err
   379  	}
   380  
   381  	workflowRunUsage := new(WorkflowRunUsage)
   382  	resp, err := s.client.Do(ctx, req, workflowRunUsage)
   383  	if err != nil {
   384  		return nil, resp, err
   385  	}
   386  
   387  	return workflowRunUsage, resp, nil
   388  }
   389  
   390  // PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer.
   391  //
   392  // GitHub API docs: https://docs.github.com/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run
   393  //
   394  //meta:operation POST /repos/{owner}/{repo}/actions/runs/{run_id}/pending_deployments
   395  func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo string, runID int64, request *PendingDeploymentsRequest) ([]*Deployment, *Response, error) {
   396  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID)
   397  
   398  	req, err := s.client.NewRequest("POST", u, request)
   399  	if err != nil {
   400  		return nil, nil, err
   401  	}
   402  
   403  	var deployments []*Deployment
   404  	resp, err := s.client.Do(ctx, req, &deployments)
   405  	if err != nil {
   406  		return nil, resp, err
   407  	}
   408  
   409  	return deployments, resp, nil
   410  }