github.com/google/go-github/v53@v53.2.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  }
    50  
    51  // WorkflowRuns represents a slice of repository action workflow run.
    52  type WorkflowRuns struct {
    53  	TotalCount   *int           `json:"total_count,omitempty"`
    54  	WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"`
    55  }
    56  
    57  // ListWorkflowRunsOptions specifies optional parameters to ListWorkflowRuns.
    58  type ListWorkflowRunsOptions struct {
    59  	Actor               string `url:"actor,omitempty"`
    60  	Branch              string `url:"branch,omitempty"`
    61  	Event               string `url:"event,omitempty"`
    62  	Status              string `url:"status,omitempty"`
    63  	Created             string `url:"created,omitempty"`
    64  	HeadSHA             string `url:"head_sha,omitempty"`
    65  	ExcludePullRequests bool   `url:"exclude_pull_requests,omitempty"`
    66  	CheckSuiteID        int64  `url:"check_suite_id,omitempty"`
    67  	ListOptions
    68  }
    69  
    70  // WorkflowRunUsage represents a usage of a specific workflow run.
    71  type WorkflowRunUsage struct {
    72  	Billable      *WorkflowRunBillMap `json:"billable,omitempty"`
    73  	RunDurationMS *int64              `json:"run_duration_ms,omitempty"`
    74  }
    75  
    76  // WorkflowRunBillMap represents different runner environments available for a workflow run.
    77  // Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc.
    78  type WorkflowRunBillMap map[string]*WorkflowRunBill
    79  
    80  // WorkflowRunBill specifies billable time for a specific environment in a workflow run.
    81  type WorkflowRunBill struct {
    82  	TotalMS *int64               `json:"total_ms,omitempty"`
    83  	Jobs    *int                 `json:"jobs,omitempty"`
    84  	JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"`
    85  }
    86  
    87  // WorkflowRunJobRun represents a usage of individual jobs of a specific workflow run.
    88  type WorkflowRunJobRun struct {
    89  	JobID      *int   `json:"job_id,omitempty"`
    90  	DurationMS *int64 `json:"duration_ms,omitempty"`
    91  }
    92  
    93  // WorkflowRunAttemptOptions specifies optional parameters to GetWorkflowRunAttempt.
    94  type WorkflowRunAttemptOptions struct {
    95  	ExcludePullRequests *bool `url:"exclude_pull_requests,omitempty"`
    96  }
    97  
    98  // PendingDeploymentsRequest specifies body parameters to PendingDeployments.
    99  type PendingDeploymentsRequest struct {
   100  	EnvironmentIDs []int64 `json:"environment_ids"`
   101  	// State can be one of: "approved", "rejected".
   102  	State   string `json:"state"`
   103  	Comment string `json:"comment"`
   104  }
   105  
   106  func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   107  	u, err := addOptions(endpoint, opts)
   108  	if err != nil {
   109  		return nil, nil, err
   110  	}
   111  
   112  	req, err := s.client.NewRequest("GET", u, nil)
   113  	if err != nil {
   114  		return nil, nil, err
   115  	}
   116  
   117  	runs := new(WorkflowRuns)
   118  	resp, err := s.client.Do(ctx, req, &runs)
   119  	if err != nil {
   120  		return nil, resp, err
   121  	}
   122  
   123  	return runs, resp, nil
   124  }
   125  
   126  // ListWorkflowRunsByID lists all workflow runs by workflow ID.
   127  //
   128  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs
   129  func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   130  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
   131  	return s.listWorkflowRuns(ctx, u, opts)
   132  }
   133  
   134  // ListWorkflowRunsByFileName lists all workflow runs by workflow file name.
   135  //
   136  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs
   137  func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   138  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
   139  	return s.listWorkflowRuns(ctx, u, opts)
   140  }
   141  
   142  // ListRepositoryWorkflowRuns lists all workflow runs for a repository.
   143  //
   144  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs-for-a-repository
   145  func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   146  	u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
   147  	u, err := addOptions(u, opts)
   148  	if err != nil {
   149  		return nil, nil, err
   150  	}
   151  
   152  	req, err := s.client.NewRequest("GET", u, nil)
   153  	if err != nil {
   154  		return nil, nil, err
   155  	}
   156  
   157  	runs := new(WorkflowRuns)
   158  	resp, err := s.client.Do(ctx, req, &runs)
   159  	if err != nil {
   160  		return nil, resp, err
   161  	}
   162  
   163  	return runs, resp, nil
   164  }
   165  
   166  // GetWorkflowRunByID gets a specific workflow run by ID.
   167  //
   168  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run
   169  func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
   170  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   171  
   172  	req, err := s.client.NewRequest("GET", u, nil)
   173  	if err != nil {
   174  		return nil, nil, err
   175  	}
   176  
   177  	run := new(WorkflowRun)
   178  	resp, err := s.client.Do(ctx, req, run)
   179  	if err != nil {
   180  		return nil, resp, err
   181  	}
   182  
   183  	return run, resp, nil
   184  }
   185  
   186  // GetWorkflowRunAttempt gets a specific workflow run attempt.
   187  //
   188  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run-attempt
   189  func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) {
   190  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber)
   191  	u, err := addOptions(u, opts)
   192  	if err != nil {
   193  		return nil, nil, err
   194  	}
   195  
   196  	req, err := s.client.NewRequest("GET", u, nil)
   197  	if err != nil {
   198  		return nil, nil, err
   199  	}
   200  
   201  	run := new(WorkflowRun)
   202  	resp, err := s.client.Do(ctx, req, run)
   203  	if err != nil {
   204  		return nil, resp, err
   205  	}
   206  
   207  	return run, resp, nil
   208  }
   209  
   210  // GetWorkflowRunAttemptLogs gets a redirect URL to download a plain text file of logs for a workflow run for attempt number.
   211  //
   212  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-attempt-logs
   213  func (s *ActionsService) GetWorkflowRunAttemptLogs(ctx context.Context, owner, repo string, runID int64, attemptNumber int, followRedirects bool) (*url.URL, *Response, error) {
   214  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v/logs", owner, repo, runID, attemptNumber)
   215  
   216  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
   217  	if err != nil {
   218  		return nil, nil, err
   219  	}
   220  	defer resp.Body.Close()
   221  
   222  	if resp.StatusCode != http.StatusFound {
   223  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   224  	}
   225  
   226  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   227  	return parsedURL, newResponse(resp), err
   228  }
   229  
   230  // RerunWorkflowByID re-runs a workflow by ID.
   231  //
   232  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-a-workflow
   233  func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   234  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
   235  
   236  	req, err := s.client.NewRequest("POST", u, nil)
   237  	if err != nil {
   238  		return nil, err
   239  	}
   240  
   241  	return s.client.Do(ctx, req, nil)
   242  }
   243  
   244  // RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID.
   245  //
   246  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run
   247  func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   248  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID)
   249  
   250  	req, err := s.client.NewRequest("POST", u, nil)
   251  	if err != nil {
   252  		return nil, err
   253  	}
   254  
   255  	return s.client.Do(ctx, req, nil)
   256  }
   257  
   258  // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID.
   259  //
   260  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run
   261  func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) {
   262  	u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID)
   263  
   264  	req, err := s.client.NewRequest("POST", u, nil)
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  
   269  	return s.client.Do(ctx, req, nil)
   270  }
   271  
   272  // CancelWorkflowRunByID cancels a workflow run by ID.
   273  //
   274  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#cancel-a-workflow-run
   275  func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   276  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID)
   277  
   278  	req, err := s.client.NewRequest("POST", u, nil)
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  
   283  	return s.client.Do(ctx, req, nil)
   284  }
   285  
   286  // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
   287  //
   288  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-logs
   289  func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
   290  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   291  
   292  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
   293  	if err != nil {
   294  		return nil, nil, err
   295  	}
   296  	defer resp.Body.Close()
   297  
   298  	if resp.StatusCode != http.StatusFound {
   299  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   300  	}
   301  
   302  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   303  	return parsedURL, newResponse(resp), err
   304  }
   305  
   306  // DeleteWorkflowRun deletes a workflow run by ID.
   307  //
   308  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#delete-a-workflow-run
   309  func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   310  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   311  
   312  	req, err := s.client.NewRequest("DELETE", u, nil)
   313  	if err != nil {
   314  		return nil, err
   315  	}
   316  
   317  	return s.client.Do(ctx, req, nil)
   318  }
   319  
   320  // DeleteWorkflowRunLogs deletes all logs for a workflow run.
   321  //
   322  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#delete-workflow-run-logs
   323  func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   324  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   325  
   326  	req, err := s.client.NewRequest("DELETE", u, nil)
   327  	if err != nil {
   328  		return nil, err
   329  	}
   330  
   331  	return s.client.Do(ctx, req, nil)
   332  }
   333  
   334  // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
   335  //
   336  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-workflow-run-usage
   337  func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
   338  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
   339  
   340  	req, err := s.client.NewRequest("GET", u, nil)
   341  	if err != nil {
   342  		return nil, nil, err
   343  	}
   344  
   345  	workflowRunUsage := new(WorkflowRunUsage)
   346  	resp, err := s.client.Do(ctx, req, workflowRunUsage)
   347  	if err != nil {
   348  		return nil, resp, err
   349  	}
   350  
   351  	return workflowRunUsage, resp, nil
   352  }
   353  
   354  // PendingDeployments approve or reject pending deployments that are waiting on approval by a required reviewer.
   355  //
   356  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#review-pending-deployments-for-a-workflow-run
   357  func (s *ActionsService) PendingDeployments(ctx context.Context, owner, repo string, runID int64, request *PendingDeploymentsRequest) ([]*Deployment, *Response, error) {
   358  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/pending_deployments", owner, repo, runID)
   359  
   360  	req, err := s.client.NewRequest("POST", u, request)
   361  	if err != nil {
   362  		return nil, nil, err
   363  	}
   364  
   365  	var deployments []*Deployment
   366  	resp, err := s.client.Do(ctx, req, &deployments)
   367  	if err != nil {
   368  		return nil, resp, err
   369  	}
   370  
   371  	return deployments, resp, nil
   372  }