Skip to content

Commit 2017229

Browse files
committed
Use context-aware CRUD functions for Project resource
1 parent 64d97cc commit 2017229

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

gitlab/resource_gitlab_project.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
436436

437437
log.Printf("[DEBUG] create gitlab project %q", *options.Name)
438438

439-
project, _, err := client.Projects.CreateProject(options)
439+
project, _, err := client.Projects.CreateProject(options, gitlab.WithContext(ctx))
440440
if err != nil {
441441
return diag.FromErr(err)
442442
}
@@ -454,7 +454,7 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
454454
Target: []string{"finished"},
455455
Timeout: 10 * time.Minute,
456456
Refresh: func() (interface{}, string, error) {
457-
status, _, err := client.ProjectImportExport.ImportStatus(d.Id())
457+
status, _, err := client.ProjectImportExport.ImportStatus(d.Id(), gitlab.WithContext(ctx))
458458
if err != nil {
459459
return nil, "", err
460460
}
@@ -468,21 +468,21 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
468468
}
469469

470470
// Read the project again, so that we can detect the default branch.
471-
project, _, err = client.Projects.GetProject(project.ID, nil)
471+
project, _, err = client.Projects.GetProject(project.ID, nil, gitlab.WithContext(ctx))
472472
if err != nil {
473473
return diag.Errorf("Failed to get project %q after completing import: %s", d.Id(), err)
474474
}
475475
}
476476

477477
if d.Get("archived").(bool) {
478478
// strange as it may seem, this project is created in archived state...
479-
if _, _, err := client.Projects.ArchiveProject(d.Id()); err != nil {
479+
if _, _, err := client.Projects.ArchiveProject(d.Id(), gitlab.WithContext(ctx)); err != nil {
480480
return diag.Errorf("new project %q could not be archived: %s", d.Id(), err)
481481
}
482482
}
483483

484484
if _, ok := d.GetOk("push_rules"); ok {
485-
err := editOrAddPushRules(client, d.Id(), d)
485+
err := editOrAddPushRules(ctx, client, d.Id(), d)
486486
var httpError *gitlab.ErrorResponse
487487
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
488488
log.Printf("[DEBUG] Failed to edit push rules for project %q: %v", d.Id(), err)
@@ -505,35 +505,35 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
505505
_, _, err := client.Branches.CreateBranch(project.ID, &gitlab.CreateBranchOptions{
506506
Branch: gitlab.String(newDefaultBranch),
507507
Ref: gitlab.String(oldDefaultBranch),
508-
})
508+
}, gitlab.WithContext(ctx))
509509
if err != nil {
510510
return diag.Errorf("Failed to create branch %q for project %q: %s", newDefaultBranch, d.Id(), err)
511511
}
512512

513513
log.Printf("[DEBUG] set new default branch to %q for project %q", newDefaultBranch, d.Id())
514514
_, _, err = client.Projects.EditProject(project.ID, &gitlab.EditProjectOptions{
515515
DefaultBranch: gitlab.String(newDefaultBranch),
516-
})
516+
}, gitlab.WithContext(ctx))
517517
if err != nil {
518518
return diag.Errorf("Failed to set default branch to %q for project %q: %s", newDefaultBranch, d.Id(), err)
519519
}
520520

521521
log.Printf("[DEBUG] protect new default branch %q for project %q", newDefaultBranch, d.Id())
522522
_, _, err = client.ProtectedBranches.ProtectRepositoryBranches(project.ID, &gitlab.ProtectRepositoryBranchesOptions{
523523
Name: gitlab.String(newDefaultBranch),
524-
})
524+
}, gitlab.WithContext(ctx))
525525
if err != nil {
526526
return diag.Errorf("Failed to protect default branch %q for project %q: %s", newDefaultBranch, d.Id(), err)
527527
}
528528

529529
log.Printf("[DEBUG] unprotect old default branch %q for project %q", oldDefaultBranch, d.Id())
530-
_, err = client.ProtectedBranches.UnprotectRepositoryBranches(project.ID, oldDefaultBranch)
530+
_, err = client.ProtectedBranches.UnprotectRepositoryBranches(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
531531
if err != nil {
532532
return diag.Errorf("Failed to unprotect undesired default branch %q for project %q: %s", oldDefaultBranch, d.Id(), err)
533533
}
534534

535535
log.Printf("[DEBUG] delete old default branch %q for project %q", oldDefaultBranch, d.Id())
536-
_, err = client.Branches.DeleteBranch(project.ID, oldDefaultBranch)
536+
_, err = client.Branches.DeleteBranch(project.ID, oldDefaultBranch, gitlab.WithContext(ctx))
537537
if err != nil {
538538
return diag.Errorf("Failed to clean up undesired default branch %q for project %q: %s", oldDefaultBranch, d.Id(), err)
539539
}
@@ -552,7 +552,7 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
552552
}
553553

554554
if (editProjectOptions != gitlab.EditProjectOptions{}) {
555-
if _, _, err := client.Projects.EditProject(d.Id(), &editProjectOptions); err != nil {
555+
if _, _, err := client.Projects.EditProject(d.Id(), &editProjectOptions, gitlab.WithContext(ctx)); err != nil {
556556
return diag.Errorf("Could not update project %q: %s", d.Id(), err)
557557
}
558558
}
@@ -564,7 +564,7 @@ func resourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta
564564
client := meta.(*gitlab.Client)
565565
log.Printf("[DEBUG] read gitlab project %s", d.Id())
566566

567-
project, _, err := client.Projects.GetProject(d.Id(), nil)
567+
project, _, err := client.Projects.GetProject(d.Id(), nil, gitlab.WithContext(ctx))
568568
if err != nil {
569569
return diag.FromErr(err)
570570
}
@@ -580,7 +580,7 @@ func resourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta
580580

581581
log.Printf("[DEBUG] read gitlab project %q push rules", d.Id())
582582

583-
pushRules, _, err := client.Projects.GetProjectPushRules(d.Id())
583+
pushRules, _, err := client.Projects.GetProjectPushRules(d.Id(), gitlab.WithContext(ctx))
584584
var httpError *gitlab.ErrorResponse
585585
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
586586
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
@@ -730,34 +730,34 @@ func resourceGitlabProjectUpdate(ctx context.Context, d *schema.ResourceData, me
730730

731731
if *options != (gitlab.EditProjectOptions{}) {
732732
log.Printf("[DEBUG] update gitlab project %s", d.Id())
733-
_, _, err := client.Projects.EditProject(d.Id(), options)
733+
_, _, err := client.Projects.EditProject(d.Id(), options, gitlab.WithContext(ctx))
734734
if err != nil {
735735
return diag.FromErr(err)
736736
}
737737
}
738738

739739
if *transferOptions != (gitlab.TransferProjectOptions{}) {
740740
log.Printf("[DEBUG] transferring project %s to namespace %d", d.Id(), transferOptions.Namespace)
741-
_, _, err := client.Projects.TransferProject(d.Id(), transferOptions)
741+
_, _, err := client.Projects.TransferProject(d.Id(), transferOptions, gitlab.WithContext(ctx))
742742
if err != nil {
743743
return diag.FromErr(err)
744744
}
745745
}
746746

747747
if d.HasChange("archived") {
748748
if d.Get("archived").(bool) {
749-
if _, _, err := client.Projects.ArchiveProject(d.Id()); err != nil {
749+
if _, _, err := client.Projects.ArchiveProject(d.Id(), gitlab.WithContext(ctx)); err != nil {
750750
return diag.Errorf("project %q could not be archived: %s", d.Id(), err)
751751
}
752752
} else {
753-
if _, _, err := client.Projects.UnarchiveProject(d.Id()); err != nil {
753+
if _, _, err := client.Projects.UnarchiveProject(d.Id(), gitlab.WithContext(ctx)); err != nil {
754754
return diag.Errorf("project %q could not be unarchived: %s", d.Id(), err)
755755
}
756756
}
757757
}
758758

759759
if d.HasChange("push_rules") {
760-
err := editOrAddPushRules(client, d.Id(), d)
760+
err := editOrAddPushRules(ctx, client, d.Id(), d)
761761
var httpError *gitlab.ErrorResponse
762762
if errors.As(err, &httpError) && httpError.Response.StatusCode == http.StatusNotFound {
763763
log.Printf("[DEBUG] Failed to get push rules for project %q: %v", d.Id(), err)
@@ -775,7 +775,7 @@ func resourceGitlabProjectDelete(ctx context.Context, d *schema.ResourceData, me
775775
client := meta.(*gitlab.Client)
776776
log.Printf("[DEBUG] Delete gitlab project %s", d.Id())
777777

778-
_, err := client.Projects.DeleteProject(d.Id())
778+
_, err := client.Projects.DeleteProject(d.Id(), gitlab.WithContext(ctx))
779779
if err != nil {
780780
return diag.FromErr(err)
781781
}
@@ -786,7 +786,7 @@ func resourceGitlabProjectDelete(ctx context.Context, d *schema.ResourceData, me
786786
Pending: []string{"Deleting"},
787787
Target: []string{"Deleted"},
788788
Refresh: func() (interface{}, string, error) {
789-
out, response, err := client.Projects.GetProject(d.Id(), nil)
789+
out, response, err := client.Projects.GetProject(d.Id(), nil, gitlab.WithContext(ctx))
790790
if err != nil {
791791
if response.StatusCode == 404 {
792792
return out, "Deleted", nil
@@ -813,11 +813,11 @@ func resourceGitlabProjectDelete(ctx context.Context, d *schema.ResourceData, me
813813
return nil
814814
}
815815

816-
func editOrAddPushRules(client *gitlab.Client, projectID string, d *schema.ResourceData) error {
816+
func editOrAddPushRules(ctx context.Context, client *gitlab.Client, projectID string, d *schema.ResourceData) error {
817817
log.Printf("[DEBUG] Editing push rules for project %q", projectID)
818818

819819
editOptions := expandEditProjectPushRuleOptions(d)
820-
_, _, err := client.Projects.EditProjectPushRule(projectID, editOptions)
820+
_, _, err := client.Projects.EditProjectPushRule(projectID, editOptions, gitlab.WithContext(ctx))
821821
if err == nil {
822822
return nil
823823
}
@@ -833,7 +833,7 @@ func editOrAddPushRules(client *gitlab.Client, projectID string, d *schema.Resou
833833
log.Printf("[DEBUG] Creating new push rules for project %q", projectID)
834834

835835
addOptions := expandAddProjectPushRuleOptions(d)
836-
_, _, err = client.Projects.AddProjectPushRule(projectID, addOptions)
836+
_, _, err = client.Projects.AddProjectPushRule(projectID, addOptions, gitlab.WithContext(ctx))
837837
if err != nil {
838838
return err
839839
}

0 commit comments

Comments
 (0)