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 repository evironments #138

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"os"
"path"
"strconv"
"strings"

github "github.com/pulumi/pulumi-github/sdk/v6/go/github"
jku marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -157,6 +158,55 @@ func main() {
return err
}

for _, env := range repo.Environments {
var reviewerIDs []int
for _, username := range env.Reviewers {
user, err := github.GetUser(ctx, &github.GetUserArgs{Username: username})
if err != nil {
return err
}
userID, err := strconv.Atoi(user.Id)
if err != nil {
return err
}
reviewerIDs = append(reviewerIDs, userID)
}

pulumiEnv, err := github.NewRepositoryEnvironment(ctx, env.Name, &github.RepositoryEnvironmentArgs{
Environment: pulumi.String(env.Name),
Repository: newRepo.Name,
CanAdminsBypass: pulumi.Bool(env.CanAdminsBypass),
DeploymentBranchPolicy: &github.RepositoryEnvironmentDeploymentBranchPolicyArgs{
ProtectedBranches: pulumi.Bool(env.ProtectedBranches),
CustomBranchPolicies: pulumi.Bool(env.CustomBranchPolicies),
},
PreventSelfReview: pulumi.Bool(env.PreventSelfReview),
Reviewers: github.RepositoryEnvironmentReviewerArray{
&github.RepositoryEnvironmentReviewerArgs{
Users: pulumi.ToIntArray(reviewerIDs),
},
},
WaitTimer: pulumi.Int(env.WaitTimer),
})
if err != nil {
return err
}

for _, policy := range env.DeploymentBranchPolicies {
_, err = github.NewRepositoryDeploymentBranchPolicy(ctx, policy.Name, &github.RepositoryDeploymentBranchPolicyArgs{
Repository: newRepo.Name,
EnvironmentName: pulumi.String(env.Name),
Name: pulumi.String(policy.Pattern),
}, pulumi.DependsOn([]pulumi.Resource{
pulumiEnv,
}))

if err != nil {
return err
}
}
}

_, err = github.NewBranchDefault(ctx, repo.Name, &github.BranchDefaultArgs{
Branch: pulumi.String(repo.DefaultBranch),
Repository: pulumi.String(repo.Name),
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ type Team struct {
ParentTeamID int `yaml:"parentTeamId"`
}

type DeploymentBranchPolicy struct {
Name string `yaml:"name"`
Pattern string `yaml:"pattern"`
}

type Environment struct {
Name string `yaml:"name"`
CanAdminsBypass bool `yaml:"canAdminsBypass"`
Reviewers []string `yaml:"reviewers"`
PreventSelfReview bool `yaml:"preventSelfReview"`
WaitTimer int `yaml:"waitTimer"`
ProtectedBranches bool `yaml:"protectedBranches"`
CustomBranchPolicies bool `yaml:"protectedBranches"`
DeploymentBranchPolicies []DeploymentBranchPolicy `yaml:"deploymentBranchPolicies"`
}

type Repository struct {
AllowAutoMerge bool `yaml:"allowAutoMerge"`
AllowMergeCommit bool `yaml:"allowMergeCommit"`
Expand All @@ -33,6 +49,7 @@ type Repository struct {
Archived bool `yaml:"archived"`
AutoInit bool `yaml:"autoInit"`
DeleteBranchOnMerge bool `yaml:"deleteBranchOnMerge"`
Environments []Environment `yaml:"environments"`
HasDiscussions bool `yaml:"hasDiscussions"`
HasDownloads bool `yaml:"hasDownloads"`
HasIssues bool `yaml:"hasIssues"`
Expand Down