Skip to content

Commit 7680c34

Browse files
committed
Update branch protection for archived repos
1 parent e39f178 commit 7680c34

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

github/resource_github_branch_protection.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func resourceGithubBranchProtection() *schema.Resource {
134134
Type: schema.TypeSet,
135135
Optional: true,
136136
Computed: true,
137+
Deprecated: "GitHub is deprecating the use of `contexts`. Use a `checks` array instead.",
137138
Description: "The list of status checks to require in order to merge into this branch. No status checks are required by default.",
138139
Elem: &schema.Schema{Type: schema.TypeString},
139140
},
@@ -294,6 +295,12 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta any) error
294295
}
295296
protection := query.Node.Node
296297

298+
if protection.Repository.IsArchived {
299+
log.Printf("[INFO] Removing branch protection (%s) from state because the repository (%s) is archived", d.Id(), protection.Repository.Name)
300+
d.SetId("")
301+
return nil
302+
}
303+
297304
err = d.Set(PROTECTION_PATTERN, protection.Pattern)
298305
if err != nil {
299306
log.Printf("[DEBUG] Problem setting '%s' in %s %s branch protection (%s)", PROTECTION_PATTERN, protection.Repository.Name, protection.Pattern, d.Id())
@@ -367,6 +374,25 @@ func resourceGithubBranchProtectionRead(d *schema.ResourceData, meta any) error
367374
}
368375

369376
func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta any) error {
377+
var query struct {
378+
Node struct {
379+
Node BranchProtectionRule `graphql:"... on BranchProtectionRule"`
380+
} `graphql:"node(id: $id)"`
381+
}
382+
variables := map[string]any{
383+
"id": d.Id(),
384+
}
385+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
386+
client := meta.(*Owner).v4client
387+
err := client.Query(ctx, &query, variables)
388+
if err == nil {
389+
protection := query.Node.Node
390+
if protection.Repository.IsArchived {
391+
log.Printf("[INFO] Skipping update of branch protection (%s) because the repository (%s) is archived", d.Id(), protection.Repository.Name)
392+
return nil
393+
}
394+
}
395+
370396
var mutate struct {
371397
UpdateBranchProtectionRule struct {
372398
BranchProtectionRule struct {
@@ -445,6 +471,25 @@ func resourceGithubBranchProtectionUpdate(d *schema.ResourceData, meta any) erro
445471
}
446472

447473
func resourceGithubBranchProtectionDelete(d *schema.ResourceData, meta any) error {
474+
var query struct {
475+
Node struct {
476+
Node BranchProtectionRule `graphql:"... on BranchProtectionRule"`
477+
} `graphql:"node(id: $id)"`
478+
}
479+
variables := map[string]any{
480+
"id": d.Id(),
481+
}
482+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
483+
client := meta.(*Owner).v4client
484+
err := client.Query(ctx, &query, variables)
485+
if err == nil {
486+
protection := query.Node.Node
487+
if protection.Repository.IsArchived {
488+
log.Printf("[INFO] Skipping deletion of branch protection (%s) because the repository (%s) is archived", d.Id(), protection.Repository.Name)
489+
return nil
490+
}
491+
}
492+
448493
var mutate struct {
449494
DeleteBranchProtectionRule struct { // Empty struct does not work
450495
ClientMutationId githubv4.ID

github/resource_github_branch_protection_v3.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,26 @@ func resourceGithubBranchProtectionV3Read(d *schema.ResourceData, meta any) erro
275275
orgName := meta.(*Owner).name
276276

277277
ctx := context.WithValue(context.Background(), ctxId, d.Id())
278+
279+
repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
280+
if err != nil {
281+
var ghErr *github.ErrorResponse
282+
if errors.As(err, &ghErr) {
283+
if ghErr.Response.StatusCode == http.StatusNotFound {
284+
log.Printf("[INFO] Removing branch protection %s/%s (%s) from state because the repository no longer exists",
285+
orgName, repoName, branch)
286+
d.SetId("")
287+
return nil
288+
}
289+
}
290+
return err
291+
}
292+
if repo.GetArchived() {
293+
log.Printf("[INFO] Removing branch protection %s/%s (%s) from state because the repository is archived", orgName, repoName, branch)
294+
d.SetId("")
295+
return nil
296+
}
297+
278298
if !d.IsNewResource() {
279299
ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string))
280300
}
@@ -349,6 +369,16 @@ func resourceGithubBranchProtectionV3Update(d *schema.ResourceData, meta any) er
349369
if err != nil {
350370
return err
351371
}
372+
orgName := meta.(*Owner).name
373+
ctx := context.WithValue(context.Background(), ctxId, d.Id())
374+
375+
repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
376+
if err == nil {
377+
if repo.GetArchived() {
378+
log.Printf("[INFO] Skipping update of branch protection %s/%s (%s) because the repository is archived", orgName, repoName, branch)
379+
return nil
380+
}
381+
}
352382

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

440+
repo, _, err := client.Repositories.Get(ctx, orgName, repoName)
441+
if err == nil {
442+
if repo.GetArchived() {
443+
log.Printf("[INFO] Skipping deletion of branch protection %s/%s (%s) because the repository is archived", orgName, repoName, branch)
444+
return nil
445+
}
446+
}
447+
410448
_, err = client.Repositories.RemoveBranchProtection(ctx,
411449
orgName, repoName, branch)
412450
return err

github/util_v4_branch_protection.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ type PushActorTypes struct {
5656

5757
type BranchProtectionRule struct {
5858
Repository struct {
59-
ID githubv4.String
60-
Name githubv4.String
59+
ID githubv4.String
60+
Name githubv4.String
61+
IsArchived githubv4.Boolean
6162
}
6263
PushAllowances struct {
6364
Nodes []PushActorTypes

0 commit comments

Comments
 (0)