| 1 |
// Copyright 2023 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 |
// DeploymentBranchPolicy represents a single deployment branch policy for an environment. |
| 14 |
type DeploymentBranchPolicy struct { |
| 15 |
Name *string `json:"name,omitempty"` |
| 16 |
ID *int64 `json:"id,omitempty"` |
| 17 |
NodeID *string `json:"node_id,omitempty"` |
| 18 |
Type *string `json:"type,omitempty"` |
| 19 |
} |
| 20 |
|
| 21 |
// DeploymentBranchPolicyResponse represents the slightly different format of response that comes back when you list deployment branch policies. |
| 22 |
type DeploymentBranchPolicyResponse struct { |
| 23 |
TotalCount *int `json:"total_count,omitempty"` |
| 24 |
BranchPolicies []*DeploymentBranchPolicy `json:"branch_policies,omitempty"` |
| 25 |
} |
| 26 |
|
| 27 |
// DeploymentBranchPolicyRequest represents a deployment branch policy request. |
| 28 |
type DeploymentBranchPolicyRequest struct { |
| 29 |
Name *string `json:"name,omitempty"` |
| 30 |
Type *string `json:"type,omitempty"` |
| 31 |
} |
| 32 |
|
| 33 |
// ListDeploymentBranchPolicies lists the deployment branch policies for an environment. |
| 34 |
// |
| 35 |
// GitHub API docs: https://docs.github.com/rest/deployments/branch-policies#list-deployment-branch-policies |
| 36 |
// |
| 37 |
//meta:operation GET /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies |
| 38 |
func (s *RepositoriesService) ListDeploymentBranchPolicies(ctx context.Context, owner, repo, environment string) (*DeploymentBranchPolicyResponse, *Response, error) { |
| 39 |
u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies", owner, repo, environment) |
| 40 |
|
| 41 |
req, err := s.client.NewRequest("GET", u, nil) |
| 42 |
if err != nil { |
| 43 |
return nil, nil, err |
| 44 |
} |
| 45 |
|
| 46 |
var list *DeploymentBranchPolicyResponse |
| 47 |
resp, err := s.client.Do(ctx, req, &list) |
| 48 |
if err != nil { |
| 49 |
return nil, resp, err |
| 50 |
} |
| 51 |
|
| 52 |
return list, resp, nil |
| 53 |
} |
| 54 |
|
| 55 |
// GetDeploymentBranchPolicy gets a deployment branch policy for an environment. |
| 56 |
// |
| 57 |
// GitHub API docs: https://docs.github.com/rest/deployments/branch-policies#get-a-deployment-branch-policy |
| 58 |
// |
| 59 |
//meta:operation GET /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies/{branch_policy_id} |
| 60 |
func (s *RepositoriesService) GetDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, branchPolicyID int64) (*DeploymentBranchPolicy, *Response, error) { |
| 61 |
u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies/%v", owner, repo, environment, branchPolicyID) |
| 62 |
|
| 63 |
req, err := s.client.NewRequest("GET", u, nil) |
| 64 |
if err != nil { |
| 65 |
return nil, nil, err |
| 66 |
} |
| 67 |
|
| 68 |
var policy *DeploymentBranchPolicy |
| 69 |
resp, err := s.client.Do(ctx, req, &policy) |
| 70 |
if err != nil { |
| 71 |
return nil, resp, err |
| 72 |
} |
| 73 |
|
| 74 |
return policy, resp, nil |
| 75 |
} |
| 76 |
|
| 77 |
// CreateDeploymentBranchPolicy creates a deployment branch policy for an environment. |
| 78 |
// |
| 79 |
// GitHub API docs: https://docs.github.com/rest/deployments/branch-policies#create-a-deployment-branch-policy |
| 80 |
// |
| 81 |
//meta:operation POST /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies |
| 82 |
func (s *RepositoriesService) CreateDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, request *DeploymentBranchPolicyRequest) (*DeploymentBranchPolicy, *Response, error) { |
| 83 |
u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies", owner, repo, environment) |
| 84 |
|
| 85 |
req, err := s.client.NewRequest("POST", u, request) |
| 86 |
if err != nil { |
| 87 |
return nil, nil, err |
| 88 |
} |
| 89 |
|
| 90 |
var policy *DeploymentBranchPolicy |
| 91 |
resp, err := s.client.Do(ctx, req, &policy) |
| 92 |
if err != nil { |
| 93 |
return nil, resp, err |
| 94 |
} |
| 95 |
|
| 96 |
return policy, resp, nil |
| 97 |
} |
| 98 |
|
| 99 |
// UpdateDeploymentBranchPolicy updates a deployment branch policy for an environment. |
| 100 |
// |
| 101 |
// GitHub API docs: https://docs.github.com/rest/deployments/branch-policies#update-a-deployment-branch-policy |
| 102 |
// |
| 103 |
//meta:operation PUT /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies/{branch_policy_id} |
| 104 |
func (s *RepositoriesService) UpdateDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, branchPolicyID int64, request *DeploymentBranchPolicyRequest) (*DeploymentBranchPolicy, *Response, error) { |
| 105 |
u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies/%v", owner, repo, environment, branchPolicyID) |
| 106 |
|
| 107 |
req, err := s.client.NewRequest("PUT", u, request) |
| 108 |
if err != nil { |
| 109 |
return nil, nil, err |
| 110 |
} |
| 111 |
|
| 112 |
var policy *DeploymentBranchPolicy |
| 113 |
resp, err := s.client.Do(ctx, req, &policy) |
| 114 |
if err != nil { |
| 115 |
return nil, resp, err |
| 116 |
} |
| 117 |
|
| 118 |
return policy, resp, nil |
| 119 |
} |
| 120 |
|
| 121 |
// DeleteDeploymentBranchPolicy deletes a deployment branch policy for an environment. |
| 122 |
// |
| 123 |
// GitHub API docs: https://docs.github.com/rest/deployments/branch-policies#delete-a-deployment-branch-policy |
| 124 |
// |
| 125 |
//meta:operation DELETE /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies/{branch_policy_id} |
| 126 |
func (s *RepositoriesService) DeleteDeploymentBranchPolicy(ctx context.Context, owner, repo, environment string, branchPolicyID int64) (*Response, error) { |
| 127 |
u := fmt.Sprintf("repos/%v/%v/environments/%v/deployment-branch-policies/%v", owner, repo, environment, branchPolicyID) |
| 128 |
|
| 129 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 130 |
if err != nil { |
| 131 |
return nil, err |
| 132 |
} |
| 133 |
|
| 134 |
return s.client.Do(ctx, req, nil) |
| 135 |
} |