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