github.com/google/go-github/v57@v57.0.0/github/actions_workflows.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  )
    12  
    13  // Workflow represents a repository action workflow.
    14  type Workflow struct {
    15  	ID        *int64     `json:"id,omitempty"`
    16  	NodeID    *string    `json:"node_id,omitempty"`
    17  	Name      *string    `json:"name,omitempty"`
    18  	Path      *string    `json:"path,omitempty"`
    19  	State     *string    `json:"state,omitempty"`
    20  	CreatedAt *Timestamp `json:"created_at,omitempty"`
    21  	UpdatedAt *Timestamp `json:"updated_at,omitempty"`
    22  	URL       *string    `json:"url,omitempty"`
    23  	HTMLURL   *string    `json:"html_url,omitempty"`
    24  	BadgeURL  *string    `json:"badge_url,omitempty"`
    25  }
    26  
    27  // Workflows represents a slice of repository action workflows.
    28  type Workflows struct {
    29  	TotalCount *int        `json:"total_count,omitempty"`
    30  	Workflows  []*Workflow `json:"workflows,omitempty"`
    31  }
    32  
    33  // WorkflowUsage represents a usage of a specific workflow.
    34  type WorkflowUsage struct {
    35  	Billable *WorkflowBillMap `json:"billable,omitempty"`
    36  }
    37  
    38  // WorkflowBillMap represents different runner environments available for a workflow.
    39  // Its key is the name of its environment, e.g. "UBUNTU", "MACOS", "WINDOWS", etc.
    40  type WorkflowBillMap map[string]*WorkflowBill
    41  
    42  // WorkflowBill specifies billable time for a specific environment in a workflow.
    43  type WorkflowBill struct {
    44  	TotalMS *int64 `json:"total_ms,omitempty"`
    45  }
    46  
    47  // CreateWorkflowDispatchEventRequest represents a request to create a workflow dispatch event.
    48  type CreateWorkflowDispatchEventRequest struct {
    49  	// Ref represents the reference of the workflow run.
    50  	// The reference can be a branch or a tag.
    51  	// Ref is required when creating a workflow dispatch event.
    52  	Ref string `json:"ref"`
    53  	// Inputs represents input keys and values configured in the workflow file.
    54  	// The maximum number of properties is 10.
    55  	// Default: Any default properties configured in the workflow file will be used when `inputs` are omitted.
    56  	Inputs map[string]interface{} `json:"inputs,omitempty"`
    57  }
    58  
    59  // ListWorkflows lists all workflows in a repository.
    60  //
    61  // GitHub API docs: https://docs.github.com/rest/actions/workflows#list-repository-workflows
    62  //
    63  //meta:operation GET /repos/{owner}/{repo}/actions/workflows
    64  func (s *ActionsService) ListWorkflows(ctx context.Context, owner, repo string, opts *ListOptions) (*Workflows, *Response, error) {
    65  	u := fmt.Sprintf("repos/%s/%s/actions/workflows", owner, repo)
    66  	u, err := addOptions(u, opts)
    67  	if err != nil {
    68  		return nil, nil, err
    69  	}
    70  
    71  	req, err := s.client.NewRequest("GET", u, nil)
    72  	if err != nil {
    73  		return nil, nil, err
    74  	}
    75  
    76  	workflows := new(Workflows)
    77  	resp, err := s.client.Do(ctx, req, &workflows)
    78  	if err != nil {
    79  		return nil, resp, err
    80  	}
    81  
    82  	return workflows, resp, nil
    83  }
    84  
    85  // GetWorkflowByID gets a specific workflow by ID.
    86  //
    87  // GitHub API docs: https://docs.github.com/rest/actions/workflows#get-a-workflow
    88  //
    89  //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}
    90  func (s *ActionsService) GetWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Workflow, *Response, error) {
    91  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowID)
    92  
    93  	return s.getWorkflow(ctx, u)
    94  }
    95  
    96  // GetWorkflowByFileName gets a specific workflow by file name.
    97  //
    98  // GitHub API docs: https://docs.github.com/rest/actions/workflows#get-a-workflow
    99  //
   100  //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}
   101  func (s *ActionsService) GetWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Workflow, *Response, error) {
   102  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowFileName)
   103  
   104  	return s.getWorkflow(ctx, u)
   105  }
   106  
   107  func (s *ActionsService) getWorkflow(ctx context.Context, url string) (*Workflow, *Response, error) {
   108  	req, err := s.client.NewRequest("GET", url, nil)
   109  	if err != nil {
   110  		return nil, nil, err
   111  	}
   112  
   113  	workflow := new(Workflow)
   114  	resp, err := s.client.Do(ctx, req, workflow)
   115  	if err != nil {
   116  		return nil, resp, err
   117  	}
   118  
   119  	return workflow, resp, nil
   120  }
   121  
   122  // GetWorkflowUsageByID gets a specific workflow usage by ID in the unit of billable milliseconds.
   123  //
   124  // GitHub API docs: https://docs.github.com/rest/actions/workflows#get-workflow-usage
   125  //
   126  //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/timing
   127  func (s *ActionsService) GetWorkflowUsageByID(ctx context.Context, owner, repo string, workflowID int64) (*WorkflowUsage, *Response, error) {
   128  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowID)
   129  
   130  	return s.getWorkflowUsage(ctx, u)
   131  }
   132  
   133  // GetWorkflowUsageByFileName gets a specific workflow usage by file name in the unit of billable milliseconds.
   134  //
   135  // GitHub API docs: https://docs.github.com/rest/actions/workflows#get-workflow-usage
   136  //
   137  //meta:operation GET /repos/{owner}/{repo}/actions/workflows/{workflow_id}/timing
   138  func (s *ActionsService) GetWorkflowUsageByFileName(ctx context.Context, owner, repo, workflowFileName string) (*WorkflowUsage, *Response, error) {
   139  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowFileName)
   140  
   141  	return s.getWorkflowUsage(ctx, u)
   142  }
   143  
   144  func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*WorkflowUsage, *Response, error) {
   145  	req, err := s.client.NewRequest("GET", url, nil)
   146  	if err != nil {
   147  		return nil, nil, err
   148  	}
   149  
   150  	workflowUsage := new(WorkflowUsage)
   151  	resp, err := s.client.Do(ctx, req, workflowUsage)
   152  	if err != nil {
   153  		return nil, resp, err
   154  	}
   155  
   156  	return workflowUsage, resp, nil
   157  }
   158  
   159  // CreateWorkflowDispatchEventByID manually triggers a GitHub Actions workflow run.
   160  //
   161  // GitHub API docs: https://docs.github.com/rest/actions/workflows#create-a-workflow-dispatch-event
   162  //
   163  //meta:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
   164  func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, owner, repo string, workflowID int64, event CreateWorkflowDispatchEventRequest) (*Response, error) {
   165  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowID)
   166  
   167  	return s.createWorkflowDispatchEvent(ctx, u, &event)
   168  }
   169  
   170  // CreateWorkflowDispatchEventByFileName manually triggers a GitHub Actions workflow run.
   171  //
   172  // GitHub API docs: https://docs.github.com/rest/actions/workflows#create-a-workflow-dispatch-event
   173  //
   174  //meta:operation POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
   175  func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, event CreateWorkflowDispatchEventRequest) (*Response, error) {
   176  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName)
   177  
   178  	return s.createWorkflowDispatchEvent(ctx, u, &event)
   179  }
   180  
   181  func (s *ActionsService) createWorkflowDispatchEvent(ctx context.Context, url string, event *CreateWorkflowDispatchEventRequest) (*Response, error) {
   182  	req, err := s.client.NewRequest("POST", url, event)
   183  	if err != nil {
   184  		return nil, err
   185  	}
   186  
   187  	return s.client.Do(ctx, req, nil)
   188  }
   189  
   190  // EnableWorkflowByID enables a workflow and sets the state of the workflow to "active".
   191  //
   192  // GitHub API docs: https://docs.github.com/rest/actions/workflows#enable-a-workflow
   193  //
   194  //meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable
   195  func (s *ActionsService) EnableWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Response, error) {
   196  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowID)
   197  	return s.doNewPutRequest(ctx, u)
   198  }
   199  
   200  // EnableWorkflowByFileName enables a workflow and sets the state of the workflow to "active".
   201  //
   202  // GitHub API docs: https://docs.github.com/rest/actions/workflows#enable-a-workflow
   203  //
   204  //meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/enable
   205  func (s *ActionsService) EnableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) {
   206  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowFileName)
   207  	return s.doNewPutRequest(ctx, u)
   208  }
   209  
   210  // DisableWorkflowByID disables a workflow and sets the state of the workflow to "disabled_manually".
   211  //
   212  // GitHub API docs: https://docs.github.com/rest/actions/workflows#disable-a-workflow
   213  //
   214  //meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable
   215  func (s *ActionsService) DisableWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Response, error) {
   216  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowID)
   217  	return s.doNewPutRequest(ctx, u)
   218  }
   219  
   220  // DisableWorkflowByFileName disables a workflow and sets the state of the workflow to "disabled_manually".
   221  //
   222  // GitHub API docs: https://docs.github.com/rest/actions/workflows#disable-a-workflow
   223  //
   224  //meta:operation PUT /repos/{owner}/{repo}/actions/workflows/{workflow_id}/disable
   225  func (s *ActionsService) DisableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) {
   226  	u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowFileName)
   227  	return s.doNewPutRequest(ctx, u)
   228  }
   229  
   230  func (s *ActionsService) doNewPutRequest(ctx context.Context, url string) (*Response, error) {
   231  	req, err := s.client.NewRequest("PUT", url, nil)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  
   236  	return s.client.Do(ctx, req, nil)
   237  }