...

Source file src/github.com/google/go-github/v47/github/actions_workflow_runs.go

Documentation: github.com/google/go-github/v47/github

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

View as plain text