Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GitHub Environments for Pro/Teams pricing plans #2611

Merged
merged 9 commits into from Jan 4, 2023
41 changes: 41 additions & 0 deletions github/repos_environments.go
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
)

// Environment represents a single environment in a repository.
Expand Down Expand Up @@ -168,6 +169,13 @@ type CreateUpdateEnvironment struct {
DeploymentBranchPolicy *BranchPolicy `json:"deployment_branch_policy"`
}

// CreateUpdateEnvironmentWithoutEnterprise represents the fields accepted for Pro/Teams private repos.
AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
// Ref: https://docs.github.com/en/actions/deployment/targeting-different-environments/using-environments-for-deployment
// See https://github.com/google/go-github/issues/2602 for more information.
type createUpdateEnvironmentNoEnterprise struct {
DeploymentBranchPolicy *BranchPolicy `json:"deployment_branch_policy"`
}

// CreateUpdateEnvironment create or update a new environment for a repository.
//
// GitHub API docs: https://docs.github.com/en/rest/deployments/environments#create-or-update-an-environment
Expand All @@ -180,6 +188,39 @@ func (s *RepositoriesService) CreateUpdateEnvironment(ctx context.Context, owner
}

e := new(Environment)
resp, err := s.client.Do(ctx, req, e)

AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
// The API returns 422 when the pricing plan doesn't support all the fields sent.
// This path will be executed for Pro/Teams private repos.
// For public repos, regardless of the pricing plan, all fields supported.
// For Free plan private repos the returned error code is 404.
// We are checking that the user didn't try to send a value for unsupported fields,
// and return an error if they did.
if resp != nil && resp.StatusCode == http.StatusUnprocessableEntity && environment != nil && len(environment.Reviewers) == 0 && environment.GetWaitTimer() == 0 {
return s.createNewEnvNoEnterprise(ctx, u, environment)

} else {
return nil, resp, err
}

AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
}
return e, resp, nil
}

// Creating an internal function for cases where the original call returned 422
// Currently only the `deployment_branch_policy` paramter is supported for Pro/Team private repos
AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
func (s *RepositoriesService) createNewEnvNoEnterprise(ctx context.Context, u string, environment *CreateUpdateEnvironment) (*Environment, *Response, error) {
req, err := s.client.NewRequest("PUT", u, &createUpdateEnvironmentNoEnterprise{
DeploymentBranchPolicy: environment.DeploymentBranchPolicy,
})

AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, nil, err
}

e := new(Environment)

AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
resp, err := s.client.Do(ctx, req, e)
if err != nil {
return nil, resp, err
Expand Down