...

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

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

View as plain text