github.com/google/go-github/v49@v49.1.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/en/rest/actions/workflows#list-repository-workflows 62 func (s *ActionsService) ListWorkflows(ctx context.Context, owner, repo string, opts *ListOptions) (*Workflows, *Response, error) { 63 u := fmt.Sprintf("repos/%s/%s/actions/workflows", owner, repo) 64 u, err := addOptions(u, opts) 65 if err != nil { 66 return nil, nil, err 67 } 68 69 req, err := s.client.NewRequest("GET", u, nil) 70 if err != nil { 71 return nil, nil, err 72 } 73 74 workflows := new(Workflows) 75 resp, err := s.client.Do(ctx, req, &workflows) 76 if err != nil { 77 return nil, resp, err 78 } 79 80 return workflows, resp, nil 81 } 82 83 // GetWorkflowByID gets a specific workflow by ID. 84 // 85 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#get-a-workflow 86 func (s *ActionsService) GetWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Workflow, *Response, error) { 87 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowID) 88 89 return s.getWorkflow(ctx, u) 90 } 91 92 // GetWorkflowByFileName gets a specific workflow by file name. 93 // 94 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#get-a-workflow 95 func (s *ActionsService) GetWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Workflow, *Response, error) { 96 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v", owner, repo, workflowFileName) 97 98 return s.getWorkflow(ctx, u) 99 } 100 101 func (s *ActionsService) getWorkflow(ctx context.Context, url string) (*Workflow, *Response, error) { 102 req, err := s.client.NewRequest("GET", url, nil) 103 if err != nil { 104 return nil, nil, err 105 } 106 107 workflow := new(Workflow) 108 resp, err := s.client.Do(ctx, req, workflow) 109 if err != nil { 110 return nil, resp, err 111 } 112 113 return workflow, resp, nil 114 } 115 116 // GetWorkflowUsageByID gets a specific workflow usage by ID in the unit of billable milliseconds. 117 // 118 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#get-workflow-usage 119 func (s *ActionsService) GetWorkflowUsageByID(ctx context.Context, owner, repo string, workflowID int64) (*WorkflowUsage, *Response, error) { 120 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowID) 121 122 return s.getWorkflowUsage(ctx, u) 123 } 124 125 // GetWorkflowUsageByFileName gets a specific workflow usage by file name in the unit of billable milliseconds. 126 // 127 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#get-workflow-usage 128 func (s *ActionsService) GetWorkflowUsageByFileName(ctx context.Context, owner, repo, workflowFileName string) (*WorkflowUsage, *Response, error) { 129 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/timing", owner, repo, workflowFileName) 130 131 return s.getWorkflowUsage(ctx, u) 132 } 133 134 func (s *ActionsService) getWorkflowUsage(ctx context.Context, url string) (*WorkflowUsage, *Response, error) { 135 req, err := s.client.NewRequest("GET", url, nil) 136 if err != nil { 137 return nil, nil, err 138 } 139 140 workflowUsage := new(WorkflowUsage) 141 resp, err := s.client.Do(ctx, req, workflowUsage) 142 if err != nil { 143 return nil, resp, err 144 } 145 146 return workflowUsage, resp, nil 147 } 148 149 // CreateWorkflowDispatchEventByID manually triggers a GitHub Actions workflow run. 150 // 151 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event 152 func (s *ActionsService) CreateWorkflowDispatchEventByID(ctx context.Context, owner, repo string, workflowID int64, event CreateWorkflowDispatchEventRequest) (*Response, error) { 153 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowID) 154 155 return s.createWorkflowDispatchEvent(ctx, u, &event) 156 } 157 158 // CreateWorkflowDispatchEventByFileName manually triggers a GitHub Actions workflow run. 159 // 160 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event 161 func (s *ActionsService) CreateWorkflowDispatchEventByFileName(ctx context.Context, owner, repo, workflowFileName string, event CreateWorkflowDispatchEventRequest) (*Response, error) { 162 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/dispatches", owner, repo, workflowFileName) 163 164 return s.createWorkflowDispatchEvent(ctx, u, &event) 165 } 166 167 func (s *ActionsService) createWorkflowDispatchEvent(ctx context.Context, url string, event *CreateWorkflowDispatchEventRequest) (*Response, error) { 168 req, err := s.client.NewRequest("POST", url, event) 169 if err != nil { 170 return nil, err 171 } 172 173 return s.client.Do(ctx, req, nil) 174 } 175 176 // EnableWorkflowByID enables a workflow and sets the state of the workflow to "active". 177 // 178 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#enable-a-workflow 179 func (s *ActionsService) EnableWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Response, error) { 180 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowID) 181 return s.doNewPutRequest(ctx, u) 182 } 183 184 // EnableWorkflowByFileName enables a workflow and sets the state of the workflow to "active". 185 // 186 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#enable-a-workflow 187 func (s *ActionsService) EnableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) { 188 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/enable", owner, repo, workflowFileName) 189 return s.doNewPutRequest(ctx, u) 190 } 191 192 // DisableWorkflowByID disables a workflow and sets the state of the workflow to "disabled_manually". 193 // 194 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#disable-a-workflow 195 func (s *ActionsService) DisableWorkflowByID(ctx context.Context, owner, repo string, workflowID int64) (*Response, error) { 196 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowID) 197 return s.doNewPutRequest(ctx, u) 198 } 199 200 // DisableWorkflowByFileName disables a workflow and sets the state of the workflow to "disabled_manually". 201 // 202 // GitHub API docs: https://docs.github.com/en/rest/actions/workflows#disable-a-workflow 203 func (s *ActionsService) DisableWorkflowByFileName(ctx context.Context, owner, repo, workflowFileName string) (*Response, error) { 204 u := fmt.Sprintf("repos/%v/%v/actions/workflows/%v/disable", owner, repo, workflowFileName) 205 return s.doNewPutRequest(ctx, u) 206 } 207 208 func (s *ActionsService) doNewPutRequest(ctx context.Context, url string) (*Response, error) { 209 req, err := s.client.NewRequest("PUT", url, nil) 210 if err != nil { 211 return nil, err 212 } 213 214 return s.client.Do(ctx, req, nil) 215 }