github.com/google/go-github/v50@v50.2.0/github/repos_deployment_branch_policies.go (about)

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