Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions github/resource_github_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func resourceGithubBranchProtection() *schema.Resource {
PROTECTION_REQUIRED_STATUS_CHECK_CONTEXTS: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Deprecated: "GitHub is deprecating the use of `contexts`. Use a `checks` array instead.",
Description: "The list of status checks to require in order to merge into this branch. No status checks are required by default.",
Elem: &schema.Schema{Type: schema.TypeString},
},
Expand Down Expand Up @@ -293,6 +295,12 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta any) error
}
protection := query.Node.Node

if protection.Repository.IsArchived {
log.Printf("[INFO] Removing branch protection (%s) from state because the repository (%s) is archived", d.Id(), protection.Repository.Name)
d.SetId("")
return nil
}

err = d.Set(PROTECTION_PATTERN, protection.Pattern)
if err != nil {
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_PATTERN, protection.Repository.Name, protection.Pattern, d.Id())
Expand Down Expand Up @@ -366,6 +374,25 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta any) error
}

func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta any) error {
var query struct {
Node struct {
Node BranchProtectionRule `graphql:"... on BranchProtectionRule"`
} `graphql:"node(id: $id)"`
}
variables := map[string]any{
"id": d.Id(),
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
if err == nil {
protection := query.Node.Node
if protection.Repository.IsArchived {
log.Printf("[INFO] Skipping update of branch protection (%s) because the repository (%s) is archived", d.Id(), protection.Repository.Name)
return nil
}
}

Comment thread
hanyouqing marked this conversation as resolved.
Outdated
var mutate struct {
UpdateBranchProtectionRule struct {
BranchProtectionRule struct {
Expand Down Expand Up @@ -444,6 +471,25 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta any) erro
}

func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta any) error {
var query struct {
Node struct {
Node BranchProtectionRule `graphql:"... on BranchProtectionRule"`
} `graphql:"node(id: $id)"`
}
variables := map[string]any{
"id": d.Id(),
}
ctx := context.WithValue(context.Background(), ctxId, d.Id())
client := meta.(*Owner).v4client
err := client.Query(ctx, &query, variables)
if err == nil {
protection := query.Node.Node
if protection.Repository.IsArchived {
log.Printf("[INFO] Skipping deletion of branch protection (%s) because the repository (%s) is archived", d.Id(), protection.Repository.Name)
return nil
}
}

Comment thread
hanyouqing marked this conversation as resolved.
Outdated
var mutate struct {
DeleteBranchProtectionRule struct { // Empty struct does not work
ClientMutationId githubv4.ID
Expand Down
38 changes: 38 additions & 0 deletions github/resource_github_branch_protection_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,26 @@ func resourceGithubBranchProtectionV3Read(d *schema.ResourceData, meta any) erro
orgName := meta.(*Owner).name

ctx := context.WithValue(context.Background(), ctxId, d.Id())

repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
if err != nil {
var ghErr *github.ErrorResponse
if errors.As(err, &ghErr) {
if ghErr.Response.StatusCode == http.StatusNotFound {
log.Printf("[INFO] Removing branch protection %s/%s (%s) from state because the repository no longer exists",
orgName, repoName, branch)
d.SetId("")
return nil
}
}
return err
}
if repo.GetArchived() {
log.Printf("[INFO] Removing branch protection %s/%s (%s) from state because the repository is archived", orgName, repoName, branch)
d.SetId("")
return nil
}

if !d.IsNewResource() {
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
}
Expand Down Expand Up @@ -349,6 +369,16 @@ func resourceGithubBranchProtectionV3Update(d *schema.ResourceData, meta any) er
if err != nil {
return err
}
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
if err == nil {
if repo.GetArchived() {
log.Printf("[INFO] Skipping update of branch protection %s/%s (%s) because the repository is archived", orgName, repoName, branch)
return nil
}
}
Comment thread
hanyouqing marked this conversation as resolved.
Outdated

protectionRequest, err := buildProtectionRequest(d)
if err != nil {
Expand Down Expand Up @@ -407,6 +437,14 @@ func resourceGithubBranchProtectionV3Delete(d *schema.ResourceData, meta any) er
orgName := meta.(*Owner).name
ctx := context.WithValue(context.Background(), ctxId, d.Id())

repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
if err == nil {
if repo.GetArchived() {
log.Printf("[INFO] Skipping deletion of branch protection %s/%s (%s) because the repository is archived", orgName, repoName, branch)
return nil
}
}

Comment thread
hanyouqing marked this conversation as resolved.
Outdated
_, err = client.Repositories.RemoveBranchProtection(ctx,
orgName, repoName, branch)
return err
Expand Down
29 changes: 20 additions & 9 deletions github/resource_github_branch_protection_v3_utils.go
Comment thread
hanyouqing marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,29 @@ func flattenAndSetRequiredStatusChecks(d *schema.ResourceData, protection *githu

// TODO: Remove once contexts is fully deprecated.
// Flatten contexts
for _, c := range *rsc.Contexts {
// Parse into contexts
contexts = append(contexts, c)
if rsc.Contexts != nil {
for _, c := range *rsc.Contexts {
// Parse into contexts
contexts = append(contexts, c)
}
}

// Fallback to populating contexts from checks if it's empty (e.g. archived repo)
if len(contexts) == 0 && rsc.Checks != nil {
for _, chk := range *rsc.Checks {
contexts = append(contexts, chk.Context)
}
}

// Flatten checks
for _, chk := range *rsc.Checks {
// Parse into checks
if chk.AppID != nil {
checks = append(checks, fmt.Sprintf("%s:%d", chk.Context, *chk.AppID))
} else {
checks = append(checks, chk.Context)
if rsc.Checks != nil {
for _, chk := range *rsc.Checks {
// Parse into checks
if chk.AppID != nil {
checks = append(checks, fmt.Sprintf("%s:%d", chk.Context, *chk.AppID))
} else {
checks = append(checks, chk.Context)
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions github/util_v4_branch_protection.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ type PushActorTypes struct {

type BranchProtectionRule struct {
Repository struct {
ID githubv4.String
Name githubv4.String
ID githubv4.String
Name githubv4.String
IsArchived githubv4.Boolean
}
PushAllowances struct {
Nodes []PushActorTypes
Expand Down