github.com/google/go-github/v42@v42.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  	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  }
    48  
    49  // WorkflowRuns represents a slice of repository action workflow run.
    50  type WorkflowRuns struct {
    51  	TotalCount   *int           `json:"total_count,omitempty"`
    52  	WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"`
    53  }
    54  
    55  // ListWorkflowRunsOptions specifies optional parameters to ListWorkflowRuns.
    56  type ListWorkflowRunsOptions struct {
    57  	Actor   string `url:"actor,omitempty"`
    58  	Branch  string `url:"branch,omitempty"`
    59  	Event   string `url:"event,omitempty"`
    60  	Status  string `url:"status,omitempty"`
    61  	Created string `url:"created,omitempty"`
    62  	ListOptions
    63  }
    64  
    65  // WorkflowRunUsage represents a usage of a specific workflow run.
    66  type WorkflowRunUsage struct {
    67  	Billable      *WorkflowRunEnvironment `json:"billable,omitempty"`
    68  	RunDurationMS *int64                  `json:"run_duration_ms,omitempty"`
    69  }
    70  
    71  // WorkflowRunEnvironment represents different runner environments available for a workflow run.
    72  type WorkflowRunEnvironment struct {
    73  	Ubuntu  *WorkflowRunBill `json:"UBUNTU,omitempty"`
    74  	MacOS   *WorkflowRunBill `json:"MACOS,omitempty"`
    75  	Windows *WorkflowRunBill `json:"WINDOWS,omitempty"`
    76  }
    77  
    78  // WorkflowRunBill specifies billable time for a specific environment in a workflow run.
    79  type WorkflowRunBill struct {
    80  	TotalMS *int64               `json:"total_ms,omitempty"`
    81  	Jobs    *int                 `json:"jobs,omitempty"`
    82  	JobRuns []*WorkflowRunJobRun `json:"job_runs,omitempty"`
    83  }
    84  
    85  // WorkflowRunJobRun represents a usage of individual jobs of a specific workflow run.
    86  type WorkflowRunJobRun struct {
    87  	JobID      *int   `json:"job_id,omitempty"`
    88  	DurationMS *int64 `json:"duration_ms,omitempty"`
    89  }
    90  
    91  func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
    92  	u, err := addOptions(endpoint, opts)
    93  	if err != nil {
    94  		return nil, nil, err
    95  	}
    96  
    97  	req, err := s.client.NewRequest("GET", u, nil)
    98  	if err != nil {
    99  		return nil, nil, err
   100  	}
   101  
   102  	runs := new(WorkflowRuns)
   103  	resp, err := s.client.Do(ctx, req, &runs)
   104  	if err != nil {
   105  		return nil, resp, err
   106  	}
   107  
   108  	return runs, resp, nil
   109  }
   110  
   111  // ListWorkflowRunsByID lists all workflow runs by workflow ID.
   112  //
   113  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-workflow-runs
   114  func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   115  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
   116  	return s.listWorkflowRuns(ctx, u, opts)
   117  }
   118  
   119  // ListWorkflowRunsByFileName lists all workflow runs by workflow file name.
   120  //
   121  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-workflow-runs
   122  func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   123  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
   124  	return s.listWorkflowRuns(ctx, u, opts)
   125  }
   126  
   127  // ListRepositoryWorkflowRuns lists all workflow runs for a repository.
   128  //
   129  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-workflow-runs-for-a-repository
   130  func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   131  	u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
   132  	u, err := addOptions(u, opts)
   133  	if err != nil {
   134  		return nil, nil, err
   135  	}
   136  
   137  	req, err := s.client.NewRequest("GET", u, nil)
   138  	if err != nil {
   139  		return nil, nil, err
   140  	}
   141  
   142  	runs := new(WorkflowRuns)
   143  	resp, err := s.client.Do(ctx, req, &runs)
   144  	if err != nil {
   145  		return nil, resp, err
   146  	}
   147  
   148  	return runs, resp, nil
   149  }
   150  
   151  // GetWorkflowRunByID gets a specific workflow run by ID.
   152  //
   153  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-workflow-run
   154  func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
   155  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   156  
   157  	req, err := s.client.NewRequest("GET", u, nil)
   158  	if err != nil {
   159  		return nil, nil, err
   160  	}
   161  
   162  	run := new(WorkflowRun)
   163  	resp, err := s.client.Do(ctx, req, run)
   164  	if err != nil {
   165  		return nil, resp, err
   166  	}
   167  
   168  	return run, resp, nil
   169  }
   170  
   171  // RerunWorkflowByID re-runs a workflow by ID.
   172  //
   173  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#re-run-a-workflow
   174  func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   175  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
   176  
   177  	req, err := s.client.NewRequest("POST", u, nil)
   178  	if err != nil {
   179  		return nil, err
   180  	}
   181  
   182  	return s.client.Do(ctx, req, nil)
   183  }
   184  
   185  // CancelWorkflowRunByID cancels a workflow run by ID.
   186  //
   187  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#cancel-a-workflow-run
   188  func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   189  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", owner, repo, runID)
   190  
   191  	req, err := s.client.NewRequest("POST", u, nil)
   192  	if err != nil {
   193  		return nil, err
   194  	}
   195  
   196  	return s.client.Do(ctx, req, nil)
   197  }
   198  
   199  // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
   200  //
   201  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#download-workflow-run-logs
   202  func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
   203  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   204  
   205  	resp, err := s.getWorkflowLogsFromURL(ctx, u, followRedirects)
   206  	if err != nil {
   207  		return nil, nil, err
   208  	}
   209  
   210  	if resp.StatusCode != http.StatusFound {
   211  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   212  	}
   213  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   214  	return parsedURL, newResponse(resp), err
   215  }
   216  
   217  // DeleteWorkflowRun deletes a workflow run by ID.
   218  //
   219  // GitHub API docs: https://docs.github.com/en/rest/reference/actions#delete-a-workflow-run
   220  func (s *ActionsService) DeleteWorkflowRun(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   221  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   222  
   223  	req, err := s.client.NewRequest("DELETE", u, nil)
   224  	if err != nil {
   225  		return nil, err
   226  	}
   227  
   228  	return s.client.Do(ctx, req, nil)
   229  }
   230  
   231  // DeleteWorkflowRunLogs deletes all logs for a workflow run.
   232  //
   233  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#delete-workflow-run-logs
   234  func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   235  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   236  
   237  	req, err := s.client.NewRequest("DELETE", u, nil)
   238  	if err != nil {
   239  		return nil, err
   240  	}
   241  
   242  	return s.client.Do(ctx, req, nil)
   243  }
   244  
   245  // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
   246  //
   247  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-workflow-run-usage
   248  func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
   249  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
   250  
   251  	req, err := s.client.NewRequest("GET", u, nil)
   252  	if err != nil {
   253  		return nil, nil, err
   254  	}
   255  
   256  	workflowRunUsage := new(WorkflowRunUsage)
   257  	resp, err := s.client.Do(ctx, req, workflowRunUsage)
   258  	if err != nil {
   259  		return nil, resp, err
   260  	}
   261  
   262  	return workflowRunUsage, resp, nil
   263  }