Skip to content

Commit 3dfbdd8

Browse files
committed
Update 404 errors to use the is404 helper method
1 parent 98c5fb8 commit 3dfbdd8

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

internal/provider/resource_gitlab_project.go

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package provider
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"log"
8-
"net/http"
97
"strconv"
108
"time"
119

@@ -565,12 +563,11 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
565563

566564
if _, ok := d.GetOk("push_rules"); ok {
567565
err := editOrAddPushRules(ctx, client, d.Id(), d)
568-
var httpError *gitlab.ErrorResponse
569-
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
570-
log.Printf("[DEBUG] Failed to edit push rules for project %q: %v", d.Id(), err)
571-
return diag.Errorf("Project push rules are not supported in your version of GitLab")
572-
}
573566
if err != nil {
567+
if is404(err) {
568+
log.Printf("[DEBUG] Failed to edit push rules for project %q: %v", d.Id(), err)
569+
return diag.Errorf("Project push rules are not supported in your version of GitLab")
570+
}
574571
return diag.Errorf("Failed to edit push rules for project %q: %s", d.Id(), err)
575572
}
576573
}
@@ -629,9 +626,9 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
629626
Target: []string{"true"},
630627
Timeout: 2 * time.Minute, //The async action usually completes very quickly, within seconds. Don't wait too long.
631628
Refresh: func() (interface{}, string, error) {
632-
branch, response, err := client.Branches.GetBranch(project.ID, project.DefaultBranch, gitlab.WithContext(ctx))
629+
branch, _, err := client.Branches.GetBranch(project.ID, project.DefaultBranch, gitlab.WithContext(ctx))
633630
if err != nil {
634-
if response.StatusCode == 404 {
631+
if is404(err) {
635632
// When we hit a 404 here, it means the default branch wasn't created at all as part of the project
636633
// this will happen when "default_branch" isn't set, or "initialize_with_readme" is set to false.
637634
// We don't need to wait anymore, so return "true" to exist the wait loop.
@@ -700,8 +697,7 @@ func resourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta
700697
log.Printf("[DEBUG] read gitlab project %q push rules", d.Id())
701698

702699
pushRules, _, err := client.Projects.GetProjectPushRules(d.Id(), gitlab.WithContext(ctx))
703-
var httpError *gitlab.ErrorResponse
704-
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
700+
if is404(err) {
705701
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
706702
} else if err != nil {
707703
return diag.Errorf("Failed to get push rules for project %q: %s", d.Id(), err)
@@ -889,12 +885,11 @@ func resourceGitlabProjectUpdate(ctx context.Context, d *schema.ResourceData, me
889885

890886
if d.HasChange("push_rules") {
891887
err := editOrAddPushRules(ctx, client, d.Id(), d)
892-
var httpError *gitlab.ErrorResponse
893-
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
894-
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
895-
return diag.Errorf("Project push rules are not supported in your version of GitLab")
896-
}
897888
if err != nil {
889+
if is404(err) {
890+
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
891+
return diag.Errorf("Project push rules are not supported in your version of GitLab")
892+
}
898893
return diag.Errorf("Failed to edit push rules for project %q: %s", d.Id(), err)
899894
}
900895
}
@@ -918,9 +913,9 @@ func resourceGitlabProjectDelete(ctx context.Context, d *schema.ResourceData, me
918913
Pending: []string{"Deleting"},
919914
Target: []string{"Deleted"},
920915
Refresh: func() (interface{}, string, error) {
921-
out, response, err := client.Projects.GetProject(d.Id(), nil, gitlab.WithContext(ctx))
916+
out, _, err := client.Projects.GetProject(d.Id(), nil, gitlab.WithContext(ctx))
922917
if err != nil {
923-
if response.StatusCode == 404 {
918+
if is404(err) {
924919
return out, "Deleted", nil
925920
}
926921
log.Printf("[ERROR] Received error: %#v", err)

0 commit comments

Comments
 (0)