...

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

Documentation: github.com/google/go-github/v33/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  	NodeID         *string        `json:"node_id,omitempty"`
    19  	HeadBranch     *string        `json:"head_branch,omitempty"`
    20  	HeadSHA        *string        `json:"head_sha,omitempty"`
    21  	RunNumber      *int           `json:"run_number,omitempty"`
    22  	Event          *string        `json:"event,omitempty"`
    23  	Status         *string        `json:"status,omitempty"`
    24  	Conclusion     *string        `json:"conclusion,omitempty"`
    25  	WorkflowID     *int64         `json:"workflow_id,omitempty"`
    26  	URL            *string        `json:"url,omitempty"`
    27  	HTMLURL        *string        `json:"html_url,omitempty"`
    28  	PullRequests   []*PullRequest `json:"pull_requests,omitempty"`
    29  	CreatedAt      *Timestamp     `json:"created_at,omitempty"`
    30  	UpdatedAt      *Timestamp     `json:"updated_at,omitempty"`
    31  	JobsURL        *string        `json:"jobs_url,omitempty"`
    32  	LogsURL        *string        `json:"logs_url,omitempty"`
    33  	CheckSuiteURL  *string        `json:"check_suite_url,omitempty"`
    34  	ArtifactsURL   *string        `json:"artifacts_url,omitempty"`
    35  	CancelURL      *string        `json:"cancel_url,omitempty"`
    36  	RerunURL       *string        `json:"rerun_url,omitempty"`
    37  	HeadCommit     *HeadCommit    `json:"head_commit,omitempty"`
    38  	WorkflowURL    *string        `json:"workflow_url,omitempty"`
    39  	Repository     *Repository    `json:"repository,omitempty"`
    40  	HeadRepository *Repository    `json:"head_repository,omitempty"`
    41  }
    42  
    43  // WorkflowRuns represents a slice of repository action workflow run.
    44  type WorkflowRuns struct {
    45  	TotalCount   *int           `json:"total_count,omitempty"`
    46  	WorkflowRuns []*WorkflowRun `json:"workflow_runs,omitempty"`
    47  }
    48  
    49  // ListWorkflowRunsOptions specifies optional parameters to ListWorkflowRuns.
    50  type ListWorkflowRunsOptions struct {
    51  	Actor  string `url:"actor,omitempty"`
    52  	Branch string `url:"branch,omitempty"`
    53  	Event  string `url:"event,omitempty"`
    54  	Status string `url:"status,omitempty"`
    55  	ListOptions
    56  }
    57  
    58  // WorkflowRunUsage represents a usage of a specific workflow run.
    59  type WorkflowRunUsage struct {
    60  	Billable      *WorkflowRunEnvironment `json:"billable,omitempty"`
    61  	RunDurationMS *int64                  `json:"run_duration_ms,omitempty"`
    62  }
    63  
    64  // WorkflowRunEnvironment represents different runner environments available for a workflow run.
    65  type WorkflowRunEnvironment struct {
    66  	Ubuntu  *WorkflowRunBill `json:"UBUNTU,omitempty"`
    67  	MacOS   *WorkflowRunBill `json:"MACOS,omitempty"`
    68  	Windows *WorkflowRunBill `json:"WINDOWS,omitempty"`
    69  }
    70  
    71  // WorkflowRunBill specifies billable time for a specific environment in a workflow run.
    72  type WorkflowRunBill struct {
    73  	TotalMS *int64 `json:"total_ms,omitempty"`
    74  	Jobs    *int   `json:"jobs,omitempty"`
    75  }
    76  
    77  func (s *ActionsService) listWorkflowRuns(ctx context.Context, endpoint string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
    78  	u, err := addOptions(endpoint, opts)
    79  	if err != nil {
    80  		return nil, nil, err
    81  	}
    82  
    83  	req, err := s.client.NewRequest("GET", u, nil)
    84  	if err != nil {
    85  		return nil, nil, err
    86  	}
    87  
    88  	runs := new(WorkflowRuns)
    89  	resp, err := s.client.Do(ctx, req, &runs)
    90  	if err != nil {
    91  		return nil, resp, err
    92  	}
    93  
    94  	return runs, resp, nil
    95  }
    96  
    97  // ListWorkflowRunsByID lists all workflow runs by workflow ID.
    98  //
    99  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-workflow-runs
   100  func (s *ActionsService) ListWorkflowRunsByID(ctx context.Context, owner, repo string, workflowID int64, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   101  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowID)
   102  	return s.listWorkflowRuns(ctx, u, opts)
   103  }
   104  
   105  // ListWorkflowRunsByFileName lists all workflow runs by workflow file name.
   106  //
   107  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-workflow-runs
   108  func (s *ActionsService) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   109  	u := fmt.Sprintf("repos/%s/%s/actions/workflows/%v/runs", owner, repo, workflowFileName)
   110  	return s.listWorkflowRuns(ctx, u, opts)
   111  }
   112  
   113  // ListRepositoryWorkflowRuns lists all workflow runs for a repository.
   114  //
   115  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#list-workflow-runs-for-a-repository
   116  func (s *ActionsService) ListRepositoryWorkflowRuns(ctx context.Context, owner, repo string, opts *ListWorkflowRunsOptions) (*WorkflowRuns, *Response, error) {
   117  	u := fmt.Sprintf("repos/%s/%s/actions/runs", owner, repo)
   118  	u, err := addOptions(u, opts)
   119  	if err != nil {
   120  		return nil, nil, err
   121  	}
   122  
   123  	req, err := s.client.NewRequest("GET", u, nil)
   124  	if err != nil {
   125  		return nil, nil, err
   126  	}
   127  
   128  	runs := new(WorkflowRuns)
   129  	resp, err := s.client.Do(ctx, req, &runs)
   130  	if err != nil {
   131  		return nil, resp, err
   132  	}
   133  
   134  	return runs, resp, nil
   135  }
   136  
   137  // GetWorkflowRunByID gets a specific workflow run by ID.
   138  //
   139  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-a-workflow-run
   140  func (s *ActionsService) GetWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRun, *Response, error) {
   141  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v", owner, repo, runID)
   142  
   143  	req, err := s.client.NewRequest("GET", u, nil)
   144  	if err != nil {
   145  		return nil, nil, err
   146  	}
   147  
   148  	run := new(WorkflowRun)
   149  	resp, err := s.client.Do(ctx, req, run)
   150  	if err != nil {
   151  		return nil, resp, err
   152  	}
   153  
   154  	return run, resp, nil
   155  }
   156  
   157  // RerunWorkflowByID re-runs a workflow by ID.
   158  //
   159  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#re-run-a-workflow
   160  func (s *ActionsService) RerunWorkflowByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   161  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/rerun", owner, repo, runID)
   162  
   163  	req, err := s.client.NewRequest("POST", u, nil)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	return s.client.Do(ctx, req, nil)
   169  }
   170  
   171  // CancelWorkflowRunByID cancels a workflow run by ID.
   172  //
   173  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#cancel-a-workflow-run
   174  func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   175  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/cancel", 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  // GetWorkflowRunLogs gets a redirect URL to download a plain text file of logs for a workflow run.
   186  //
   187  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#download-workflow-run-logs
   188  func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
   189  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   190  
   191  	resp, err := s.getWorkflowLogsFromURL(ctx, u, followRedirects)
   192  	if err != nil {
   193  		return nil, nil, err
   194  	}
   195  
   196  	if resp.StatusCode != http.StatusFound {
   197  		return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
   198  	}
   199  	parsedURL, err := url.Parse(resp.Header.Get("Location"))
   200  	return parsedURL, newResponse(resp), err
   201  }
   202  
   203  // DeleteWorkflowRunLogs deletes all logs for a workflow run.
   204  //
   205  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#delete-workflow-run-logs
   206  func (s *ActionsService) DeleteWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64) (*Response, error) {
   207  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
   208  
   209  	req, err := s.client.NewRequest("DELETE", u, nil)
   210  	if err != nil {
   211  		return nil, err
   212  	}
   213  
   214  	return s.client.Do(ctx, req, nil)
   215  }
   216  
   217  // GetWorkflowRunUsageByID gets a specific workflow usage run by run ID in the unit of billable milliseconds.
   218  //
   219  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#get-workflow-run-usage
   220  func (s *ActionsService) GetWorkflowRunUsageByID(ctx context.Context, owner, repo string, runID int64) (*WorkflowRunUsage, *Response, error) {
   221  	u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/timing", owner, repo, runID)
   222  
   223  	req, err := s.client.NewRequest("GET", u, nil)
   224  	if err != nil {
   225  		return nil, nil, err
   226  	}
   227  
   228  	workflowRunUsage := new(WorkflowRunUsage)
   229  	resp, err := s.client.Do(ctx, req, workflowRunUsage)
   230  	if err != nil {
   231  		return nil, resp, err
   232  	}
   233  
   234  	return workflowRunUsage, resp, nil
   235  }
   236  

View as plain text