...

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

Documentation: github.com/google/go-github/v45/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  func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
    98  	u, err := addOptions(endpoint, opts)
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  
   103  	req, err := s.client.NewRequest("GET", u, nil)
   104  	if err != nil {
   105  		return nil, nil, err
   106  	}
   107  
   108  	runs := new(WorkflowRuns)
   109  	resp, err := s.client.Do(ctx, req, &runs)
   110  	if err != nil {
   111  		return nil, resp, err
   112  	}
   113  
   114  	return runs, resp, nil
   115  }
   116  
   117  // ListWorkflowRunsByID lists all workflow runs by workflow ID.
   118  //
   119  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs
   120  func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   121  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
   122  	return s.listWorkflowRuns(ctx, u, opts)
   123  }
   124  
   125  // ListWorkflowRunsByFileName lists all workflow runs by workflow file name.
   126  //
   127  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs
   128  func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   129  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
   130  	return s.listWorkflowRuns(ctx, u, opts)
   131  }
   132  
   133  // ListRepositoryWorkflowRuns lists all workflow runs for a repository.
   134  //
   135  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs-for-a-repository
   136  func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   137  	u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
   138  	u, err := addOptions(u, opts)
   139  	if err != nil {
   140  		return nil, nil, err
   141  	}
   142  
   143  	req, err := s.client.NewRequest("GET", u, nil)
   144  	if err != nil {
   145  		return nil, nil, err
   146  	}
   147  
   148  	runs := new(WorkflowRuns)
   149  	resp, err := s.client.Do(ctx, req, &runs)
   150  	if err != nil {
   151  		return nil, resp, err
   152  	}
   153  
   154  	return runs, resp, nil
   155  }
   156  
   157  // GetWorkflowRunByID gets a specific workflow run by ID.
   158  //
   159  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run
   160  func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
   161  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   162  
   163  	req, err := s.client.NewRequest("GET", u, nil)
   164  	if err != nil {
   165  		return nil, nil, err
   166  	}
   167  
   168  	run := new(WorkflowRun)
   169  	resp, err := s.client.Do(ctx, req, run)
   170  	if err != nil {
   171  		return nil, resp, err
   172  	}
   173  
   174  	return run, resp, nil
   175  }
   176  
   177  // GetWorkflowRunAttempt gets a specific workflow run attempt.
   178  //
   179  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-a-workflow-run-attempt
   180  func (s *ActionsService) GetWorkflowRunAttempt(ctx context.Context, owner, repo string, runID int64, attemptNumber int, opts *WorkflowRunAttemptOptions) (*WorkflowRun, *Response, error) {
   181  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/attempts/%v", owner, repo, runID, attemptNumber)
   182  	u, err := addOptions(u, opts)
   183  	if err != nil {
   184  		return nil, nil, err
   185  	}
   186  
   187  	req, err := s.client.NewRequest("GET", u, nil)
   188  	if err != nil {
   189  		return nil, nil, err
   190  	}
   191  
   192  	run := new(WorkflowRun)
   193  	resp, err := s.client.Do(ctx, req, run)
   194  	if err != nil {
   195  		return nil, resp, err
   196  	}
   197  
   198  	return run, resp, nil
   199  }
   200  
   201  // RerunWorkflowByID re-runs a workflow by ID.
   202  //
   203  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-a-workflow
   204  func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   205  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
   206  
   207  	req, err := s.client.NewRequest("POST", u, nil)
   208  	if err != nil {
   209  		return nil, err
   210  	}
   211  
   212  	return s.client.Do(ctx, req, nil)
   213  }
   214  
   215  // RerunFailedJobsByID re-runs all of the failed jobs and their dependent jobs in a workflow run by ID.
   216  //
   217  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run
   218  func (s *ActionsService) RerunFailedJobsByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   219  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun-failed-jobs", owner, repo, runID)
   220  
   221  	req, err := s.client.NewRequest("POST", u, nil)
   222  	if err != nil {
   223  		return nil, err
   224  	}
   225  
   226  	return s.client.Do(ctx, req, nil)
   227  }
   228  
   229  // RerunJobByID re-runs a job and its dependent jobs in a workflow run by ID.
   230  //
   231  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#re-run-a-job-from-a-workflow-run
   232  func (s *ActionsService) RerunJobByID(ctx context.Context, owner, repo string, jobID int64) (*Response, error) {
   233  	u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/rerun", owner, repo, jobID)
   234  
   235  	req, err := s.client.NewRequest("POST", u, nil)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  
   240  	return s.client.Do(ctx, req, nil)
   241  }
   242  
   243  // CancelWorkflowRunByID cancels a workflow run by ID.
   244  //
   245  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#cancel-a-workflow-run
   246  func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   247  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID)
   248  
   249  	req, err := s.client.NewRequest("POST", u, nil)
   250  	if err != nil {
   251  		return nil, err
   252  	}
   253  
   254  	return s.client.Do(ctx, req, nil)
   255  }
   256  
   257  // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
   258  //
   259  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#download-workflow-run-logs
   260  func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
   261  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   262  
   263  	resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
   264  	if err != nil {
   265  		return nil, nil, err
   266  	}
   267  	defer resp.Body.Close()
   268  
   269  	if resp.StatusCode != http.StatusFound {
   270  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   271  	}
   272  
   273  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   274  	return parsedURL, newResponse(resp), err
   275  }
   276  
   277  // DeleteWorkflowRun deletes a workflow run by ID.
   278  //
   279  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#delete-a-workflow-run
   280  func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   281  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   282  
   283  	req, err := s.client.NewRequest("DELETE", u, nil)
   284  	if err != nil {
   285  		return nil, err
   286  	}
   287  
   288  	return s.client.Do(ctx, req, nil)
   289  }
   290  
   291  // DeleteWorkflowRunLogs deletes all logs for a workflow run.
   292  //
   293  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#delete-workflow-run-logs
   294  func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   295  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   296  
   297  	req, err := s.client.NewRequest("DELETE", u, nil)
   298  	if err != nil {
   299  		return nil, err
   300  	}
   301  
   302  	return s.client.Do(ctx, req, nil)
   303  }
   304  
   305  // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
   306  //
   307  // GitHub API docs: https://docs.github.com/en/rest/actions/workflow-runs#get-workflow-run-usage
   308  func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
   309  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
   310  
   311  	req, err := s.client.NewRequest("GET", u, nil)
   312  	if err != nil {
   313  		return nil, nil, err
   314  	}
   315  
   316  	workflowRunUsage := new(WorkflowRunUsage)
   317  	resp, err := s.client.Do(ctx, req, workflowRunUsage)
   318  	if err != nil {
   319  		return nil, resp, err
   320  	}
   321  
   322  	return workflowRunUsage, resp, nil
   323  }
   324  

View as plain text