...

Source file src/github.com/google/go-github/v45/github/repos_environments.go

Documentation: github.com/google/go-github/v45/github

     1  // Copyright 2021 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  	"encoding/json"
    11  	"fmt"
    12  )
    13  
    14  // Environment represents a single environment in a repository.
    15  type Environment struct {
    16  	Owner                  *string         `json:"owner,omitempty"`
    17  	Repo                   *string         `json:"repo,omitempty"`
    18  	EnvironmentName        *string         `json:"environment_name,omitempty"`
    19  	WaitTimer              *int            `json:"wait_timer,omitempty"`
    20  	Reviewers              []*EnvReviewers `json:"reviewers,omitempty"`
    21  	DeploymentBranchPolicy *BranchPolicy   `json:"deployment_branch_policy,omitempty"`
    22  	// Return/response only values
    23  	ID              *int64            `json:"id,omitempty"`
    24  	NodeID          *string           `json:"node_id,omitempty"`
    25  	Name            *string           `json:"name,omitempty"`
    26  	URL             *string           `json:"url,omitempty"`
    27  	HTMLURL         *string           `json:"html_url,omitempty"`
    28  	CreatedAt       *Timestamp        `json:"created_at,omitempty"`
    29  	UpdatedAt       *Timestamp        `json:"updated_at,omitempty"`
    30  	ProtectionRules []*ProtectionRule `json:"protection_rules,omitempty"`
    31  }
    32  
    33  // EnvReviewers represents a single environment reviewer entry.
    34  type EnvReviewers struct {
    35  	Type *string `json:"type,omitempty"`
    36  	ID   *int64  `json:"id,omitempty"`
    37  }
    38  
    39  // BranchPolicy represents the options for whether a branch deployment policy is applied to this environment.
    40  type BranchPolicy struct {
    41  	ProtectedBranches    *bool `json:"protected_branches,omitempty"`
    42  	CustomBranchPolicies *bool `json:"custom_branch_policies,omitempty"`
    43  }
    44  
    45  // EnvResponse represents the slightly different format of response that comes back when you list an environment.
    46  type EnvResponse struct {
    47  	TotalCount   *int           `json:"total_count,omitempty"`
    48  	Environments []*Environment `json:"environments,omitempty"`
    49  }
    50  
    51  // ProtectionRule represents a single protection rule applied to the environment.
    52  type ProtectionRule struct {
    53  	ID        *int64              `json:"id,omitempty"`
    54  	NodeID    *string             `json:"node_id,omitempty"`
    55  	Type      *string             `json:"type,omitempty"`
    56  	WaitTimer *int                `json:"wait_timer,omitempty"`
    57  	Reviewers []*RequiredReviewer `json:"reviewers,omitempty"`
    58  }
    59  
    60  // RequiredReviewer represents a required reviewer.
    61  type RequiredReviewer struct {
    62  	Type     *string     `json:"type,omitempty"`
    63  	Reviewer interface{} `json:"reviewer,omitempty"`
    64  }
    65  
    66  // EnvironmentListOptions specifies the optional parameters to the
    67  // RepositoriesService.ListEnvironments method.
    68  type EnvironmentListOptions struct {
    69  	ListOptions
    70  }
    71  
    72  // UnmarshalJSON implements the json.Unmarshaler interface.
    73  // This helps us handle the fact that RequiredReviewer can have either a User or Team type reviewer field.
    74  func (r *RequiredReviewer) UnmarshalJSON(data []byte) error {
    75  	type aliasReviewer RequiredReviewer
    76  	var reviewer aliasReviewer
    77  	if err := json.Unmarshal(data, &reviewer); err != nil {
    78  		return err
    79  	}
    80  
    81  	r.Type = reviewer.Type
    82  
    83  	switch *reviewer.Type {
    84  	case "User":
    85  		reviewer.Reviewer = &User{}
    86  		if err := json.Unmarshal(data, &reviewer); err != nil {
    87  			return err
    88  		}
    89  		r.Reviewer = reviewer.Reviewer
    90  	case "Team":
    91  		reviewer.Reviewer = &Team{}
    92  		if err := json.Unmarshal(data, &reviewer); err != nil {
    93  			return err
    94  		}
    95  		r.Reviewer = reviewer.Reviewer
    96  	default:
    97  		r.Type = nil
    98  		r.Reviewer = nil
    99  		return fmt.Errorf("reviewer.Type is %T, not a string of 'User' or 'Team', unable to unmarshal", reviewer.Type)
   100  	}
   101  
   102  	return nil
   103  }
   104  
   105  // ListEnvironments lists all environments for a repository.
   106  //
   107  // GitHub API docs: https://docs.github.com/en/rest/deployments/environments#get-all-environments
   108  func (s *RepositoriesService) ListEnvironments(ctx context.Context, owner, repo string, opts *EnvironmentListOptions) (*EnvResponse, *Response, error) {
   109  	u := fmt.Sprintf("repos/%s/%s/environments", owner, repo)
   110  	u, err := addOptions(u, opts)
   111  	if err != nil {
   112  		return nil, nil, err
   113  	}
   114  
   115  	req, err := s.client.NewRequest("GET", u, nil)
   116  	if err != nil {
   117  		return nil, nil, err
   118  	}
   119  
   120  	var list *EnvResponse
   121  	resp, err := s.client.Do(ctx, req, &list)
   122  	if err != nil {
   123  		return nil, resp, err
   124  	}
   125  	return list, resp, nil
   126  }
   127  
   128  // GetEnvironment get a single environment for a repository.
   129  //
   130  // GitHub API docs: https://docs.github.com/en/rest/deployments/environments#get-an-environment
   131  func (s *RepositoriesService) GetEnvironment(ctx context.Context, owner, repo, name string) (*Environment, *Response, error) {
   132  	u := fmt.Sprintf("repos/%s/%s/environments/%s", owner, repo, name)
   133  
   134  	req, err := s.client.NewRequest("GET", u, nil)
   135  	if err != nil {
   136  		return nil, nil, err
   137  	}
   138  
   139  	var env *Environment
   140  	resp, err := s.client.Do(ctx, req, &env)
   141  	if err != nil {
   142  		return nil, resp, err
   143  	}
   144  	return env, resp, nil
   145  }
   146  
   147  // MarshalJSON implements the json.Marshaler interface.
   148  // As the only way to clear a WaitTimer is to set it to 0, a missing WaitTimer object should default to 0, not null.
   149  func (c *CreateUpdateEnvironment) MarshalJSON() ([]byte, error) {
   150  	type Alias CreateUpdateEnvironment
   151  	if c.WaitTimer == nil {
   152  		c.WaitTimer = Int(0)
   153  	}
   154  	return json.Marshal(&struct {
   155  		*Alias
   156  	}{
   157  		Alias: (*Alias)(c),
   158  	})
   159  }
   160  
   161  // CreateUpdateEnvironment represents the fields required for the create/update operation
   162  // following the Create/Update release example.
   163  // See https://github.com/google/go-github/issues/992 for more information.
   164  // Removed omitempty here as the API expects null values for reviewers and deployment_branch_policy to clear them.
   165  type CreateUpdateEnvironment struct {
   166  	WaitTimer              *int            `json:"wait_timer"`
   167  	Reviewers              []*EnvReviewers `json:"reviewers"`
   168  	DeploymentBranchPolicy *BranchPolicy   `json:"deployment_branch_policy"`
   169  }
   170  
   171  // CreateUpdateEnvironment create or update a new environment for a repository.
   172  //
   173  // GitHub API docs: https://docs.github.com/en/rest/deployments/environments#create-or-update-an-environment
   174  func (s *RepositoriesService) CreateUpdateEnvironment(ctx context.Context, owner, repo, name string, environment *CreateUpdateEnvironment) (*Environment, *Response, error) {
   175  	u := fmt.Sprintf("repos/%s/%s/environments/%s", owner, repo, name)
   176  
   177  	req, err := s.client.NewRequest("PUT", u, environment)
   178  	if err != nil {
   179  		return nil, nil, err
   180  	}
   181  
   182  	e := new(Environment)
   183  	resp, err := s.client.Do(ctx, req, e)
   184  	if err != nil {
   185  		return nil, resp, err
   186  	}
   187  	return e, resp, nil
   188  }
   189  
   190  // DeleteEnvironment delete an environment from a repository.
   191  //
   192  // GitHub API docs: https://docs.github.com/en/rest/deployments/environments#delete-an-environment
   193  func (s *RepositoriesService) DeleteEnvironment(ctx context.Context, owner, repo, name string) (*Response, error) {
   194  	u := fmt.Sprintf("repos/%s/%s/environments/%s", owner, repo, name)
   195  
   196  	req, err := s.client.NewRequest("DELETE", u, nil)
   197  	if err != nil {
   198  		return nil, err
   199  	}
   200  	return s.client.Do(ctx, req, nil)
   201  }
   202  

View as plain text