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