Skip to content

Commit 71c711d

Browse files
committed
Use context when querying GitLab version
1 parent 67c7783 commit 71c711d

6 files changed

+20
-18
lines changed

internal/provider/resource_gitlab_managed_license_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package provider
22

33
import (
4+
"context"
45
"fmt"
56
"strconv"
67
"testing"
@@ -60,7 +61,7 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
6061
// Create a managed license with an "approved" state
6162
SkipFunc: orSkipFunc(
6263
isRunningInCE,
63-
isGitLabVersionLessThan(testGitlabClient, "15.0"),
64+
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
6465
),
6566
Config: testManagedLicenseConfig(rInt, "approved"),
6667
Check: resource.ComposeTestCheckFunc(
@@ -71,7 +72,7 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
7172
// Update the managed license to have a blacklisted state
7273
SkipFunc: orSkipFunc(
7374
isRunningInCE,
74-
isGitLabVersionLessThan(testGitlabClient, "15.0"),
75+
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
7576
),
7677
Config: testManagedLicenseConfig(rInt, "blacklisted"),
7778
Check: resource.ComposeTestCheckFunc(
@@ -81,7 +82,7 @@ func TestAccGitlabManagedLicense_deprecatedConfigValues(t *testing.T) {
8182
{
8283
SkipFunc: orSkipFunc(
8384
isRunningInCE,
84-
isGitLabVersionLessThan(testGitlabClient, "15.0"),
85+
isGitLabVersionLessThan(context.Background(), testGitlabClient, "15.0"),
8586
),
8687
ResourceName: "gitlab_managed_license.fixme",
8788
ImportState: true,

internal/provider/resource_gitlab_project.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ A project can either be created in a group or user namespace.
663663
}
664664
})
665665

666-
func resourceGitlabProjectSetToState(client *gitlab.Client, d *schema.ResourceData, project *gitlab.Project) error {
666+
func resourceGitlabProjectSetToState(ctx context.Context, client *gitlab.Client, d *schema.ResourceData, project *gitlab.Project) error {
667667
d.SetId(fmt.Sprintf("%d", project.ID))
668668
d.Set("name", project.Name)
669669
d.Set("path", project.Path)
@@ -694,7 +694,7 @@ func resourceGitlabProjectSetToState(client *gitlab.Client, d *schema.ResourceDa
694694
return err
695695
}
696696
d.Set("archived", project.Archived)
697-
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
697+
if supportsSquashOption, err := isGitLabVersionAtLeast(ctx, client, "14.1")(); err != nil {
698698
return err
699699
} else if supportsSquashOption {
700700
d.Set("squash_option", project.SquashOption)
@@ -938,7 +938,7 @@ func resourceGitlabProjectCreate(ctx context.Context, d *schema.ResourceData, me
938938
options.MergeCommitTemplate = gitlab.String(v.(string))
939939
}
940940

941-
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
941+
if supportsSquashOption, err := isGitLabVersionAtLeast(ctx, client, "14.1")(); err != nil {
942942
return diag.FromErr(err)
943943
} else if supportsSquashOption {
944944
if v, ok := d.GetOk("squash_option"); ok {
@@ -1153,7 +1153,7 @@ func resourceGitlabProjectRead(ctx context.Context, d *schema.ResourceData, meta
11531153
return nil
11541154
}
11551155

1156-
if err := resourceGitlabProjectSetToState(client, d, project); err != nil {
1156+
if err := resourceGitlabProjectSetToState(ctx, client, d, project); err != nil {
11571157
return diag.FromErr(err)
11581158
}
11591159

@@ -1262,7 +1262,7 @@ func resourceGitlabProjectUpdate(ctx context.Context, d *schema.ResourceData, me
12621262
options.LFSEnabled = gitlab.Bool(d.Get("lfs_enabled").(bool))
12631263
}
12641264

1265-
if supportsSquashOption, err := isGitLabVersionAtLeast(client, "14.1")(); err != nil {
1265+
if supportsSquashOption, err := isGitLabVersionAtLeast(ctx, client, "14.1")(); err != nil {
12661266
return diag.FromErr(err)
12671267
} else if supportsSquashOption && d.HasChange("squash_option") {
12681268
options.SquashOption = stringToSquashOptionValue(d.Get("squash_option").(string))

internal/provider/resource_gitlab_project_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package provider
44

55
import (
6+
"context"
67
"errors"
78
"fmt"
89
"regexp"
@@ -1192,11 +1193,11 @@ func testAccCheckAggregateGitlabProject(expected, received *gitlab.Project) reso
11921193
}
11931194
}
11941195

1195-
if err := resourceGitlabProjectSetToState(testGitlabClient, expectedData, expected); err != nil {
1196+
if err := resourceGitlabProjectSetToState(context.Background(), testGitlabClient, expectedData, expected); err != nil {
11961197
return err
11971198
}
11981199

1199-
if err := resourceGitlabProjectSetToState(testGitlabClient, receivedData, received); err != nil {
1200+
if err := resourceGitlabProjectSetToState(context.Background(), testGitlabClient, receivedData, received); err != nil {
12001201
return err
12011202
}
12021203

internal/provider/resource_gitlab_project_variable_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func TestAccGitlabProjectVariable_scoped(t *testing.T) {
180180
CheckDestroy: func(state *terraform.State) error {
181181
// Destroy behavior is nondeterministic for variables with scopes in GitLab versions prior to 13.4
182182
// ref: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39209
183-
if isAtLeast134, err := isGitLabVersionAtLeast(testGitlabClient, "13.4")(); err != nil {
183+
if isAtLeast134, err := isGitLabVersionAtLeast(context.Background(), testGitlabClient, "13.4")(); err != nil {
184184
return err
185185
} else if isAtLeast134 {
186186
return testAccGitlabProjectVariableCheckAllVariablesDestroyed(ctx)(state)
@@ -246,7 +246,7 @@ resource "gitlab_project_variable" "bar" {
246246
// Update an attribute on one of the variables.
247247
// Updating a variable with a non-unique key only works reliably on GitLab v13.4+.
248248
{
249-
SkipFunc: isGitLabVersionLessThan(testGitlabClient, "13.4"),
249+
SkipFunc: isGitLabVersionLessThan(context.Background(), testGitlabClient, "13.4"),
250250
Config: fmt.Sprintf(`
251251
resource "gitlab_project_variable" "foo" {
252252
project = %[1]d
@@ -270,7 +270,7 @@ resource "gitlab_project_variable" "bar" {
270270
// Try to have two variables with the same keys and scopes.
271271
// On versions of GitLab < 13.4 this can sometimes result in an inconsistent state instead of an error.
272272
{
273-
SkipFunc: isGitLabVersionLessThan(testGitlabClient, "13.4"),
273+
SkipFunc: isGitLabVersionLessThan(context.Background(), testGitlabClient, "13.4"),
274274
Config: fmt.Sprintf(`
275275
resource "gitlab_project_variable" "foo" {
276276
project = %[1]d

internal/provider/resource_gitlab_topic.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func resourceGitlabTopicDelete(ctx context.Context, d *schema.ResourceData, meta
185185
}
186186
softDestroy := d.Get("soft_destroy").(bool)
187187

188-
deleteNotSupported, err := isGitLabVersionLessThan(client, "14.9")()
188+
deleteNotSupported, err := isGitLabVersionLessThan(ctx, client, "14.9")()
189189
if err != nil {
190190
return diag.FromErr(err)
191191
}

internal/provider/util.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,23 @@ func intSetToIntSlice(intSet *schema.Set) *[]int {
225225

226226
// isGitLabVersionLessThan is a SkipFunc that returns true if the provided version is lower then
227227
// the current version of GitLab. It only checks the major and minor version numbers, not the patch.
228-
func isGitLabVersionLessThan(client *gitlab.Client, version string) func() (bool, error) {
228+
func isGitLabVersionLessThan(ctx context.Context, client *gitlab.Client, version string) func() (bool, error) {
229229
return func() (bool, error) {
230-
isAtLeast, err := isGitLabVersionAtLeast(client, version)()
230+
isAtLeast, err := isGitLabVersionAtLeast(ctx, client, version)()
231231
return !isAtLeast, err
232232
}
233233
}
234234

235235
// isGitLabVersionAtLeast is a SkipFunc that checks that the version of GitLab is at least the
236236
// provided wantVersion. It only checks the major and minor version numbers, not the patch.
237-
func isGitLabVersionAtLeast(client *gitlab.Client, wantVersion string) func() (bool, error) {
237+
func isGitLabVersionAtLeast(ctx context.Context, client *gitlab.Client, wantVersion string) func() (bool, error) {
238238
return func() (bool, error) {
239239
wantMajor, wantMinor, err := parseVersionMajorMinor(wantVersion)
240240
if err != nil {
241241
return false, fmt.Errorf("failed to parse wanted version %q: %w", wantVersion, err)
242242
}
243243

244-
actualVersion, _, err := client.Version.GetVersion()
244+
actualVersion, _, err := client.Version.GetVersion(gitlab.WithContext(ctx))
245245
if err != nil {
246246
return false, err
247247
}

0 commit comments

Comments
 (0)