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
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion github/repos_environments.go
Expand Up @@ -168,6 +168,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 CreateUpdateEnvironmentWithoutEnterprise struct {
AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -181,8 +188,32 @@ 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 {
return nil, resp, err
// 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.StatusCode == 422 && len(environment.Reviewers) == 0 && *environment.WaitTimer == 0 {
AnitaErnszt marked this conversation as resolved.
Show resolved Hide resolved

req, err = s.client.NewRequest("PUT", u, &CreateUpdateEnvironmentWithoutEnterprise{
DeploymentBranchPolicy: environment.DeploymentBranchPolicy,
})

if err != nil {
return nil, nil, err
}

resp, err = s.client.Do(ctx, req, e)
if err != nil {
return nil, resp, err
}
} else {
return nil, resp, err
}

}
return e, resp, nil
}
Expand Down