Skip to content

Commit f6b0c90

Browse files
author
Nicholas M. Iodice
authored
Removing all occurences of string equality checks using == and replacing with strings.EqualFold() (#269)
1 parent 9588cef commit f6b0c90

18 files changed

+71
-32
lines changed

azuredevops/crud/serviceendpoint/crud_service_endpoint.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package crudserviceendpoint
33
import (
44
"errors"
55
"fmt"
6+
"strings"
67

78
"github.com/google/uuid"
89
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -141,7 +142,7 @@ func MakeUnprotectedSchema(r *schema.Resource, keyName, envVarName, description
141142

142143
// Make the Azure DevOps API call to create the endpoint
143144
func createServiceEndpoint(clients *config.AggregatedClient, endpoint *serviceendpoint.ServiceEndpoint, project *string) (*serviceendpoint.ServiceEndpoint, error) {
144-
if *endpoint.Type == "github" && *endpoint.Authorization.Scheme == "InstallationToken" {
145+
if strings.EqualFold(*endpoint.Type, "github") && strings.EqualFold(*endpoint.Authorization.Scheme, "InstallationToken") {
145146
return nil, fmt.Errorf("Github Apps must be created on Github and then can be imported")
146147
}
147148
createdServiceEndpoint, err := clients.ServiceEndpointClient.CreateServiceEndpoint(
@@ -166,7 +167,7 @@ func deleteServiceEndpoint(clients *config.AggregatedClient, project *string, en
166167
}
167168

168169
func updateServiceEndpoint(clients *config.AggregatedClient, endpoint *serviceendpoint.ServiceEndpoint, project *string) (*serviceendpoint.ServiceEndpoint, error) {
169-
if *endpoint.Type == "github" && *endpoint.Authorization.Scheme == "InstallationToken" {
170+
if strings.EqualFold(*endpoint.Type, "github") && strings.EqualFold(*endpoint.Authorization.Scheme, "InstallationToken") {
170171
return nil, fmt.Errorf("Github Apps can not be updated must match imported values exactly")
171172
}
172173
updatedServiceEndpoint, err := clients.ServiceEndpointClient.UpdateServiceEndpoint(

azuredevops/data_users_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func TestDataSourceUser_Read_TestFilterByOrigin(t *testing.T) {
264264
for _, elem := range usersSet.List() {
265265
upn := elem.(map[string]interface{})["principal_name"].(string)
266266
for _, usr := range usrList1 {
267-
if "aad" == *usr.Origin && upn == *usr.PrincipalName {
267+
if strings.EqualFold("aad", *usr.Origin) && strings.EqualFold(upn, *usr.PrincipalName) {
268268
iFound++
269269
break
270270
}
@@ -311,7 +311,7 @@ func TestDataSourceUser_Read_TestFilterByOriginId(t *testing.T) {
311311
for _, elem := range usersSet.List() {
312312
upn := elem.(map[string]interface{})["principal_name"].(string)
313313
for _, usr := range usrList1 {
314-
if "8c840d92-f19e-4dfe-8eab-5a1fd67a3a77" == *usr.OriginId && upn == *usr.PrincipalName {
314+
if strings.EqualFold("8c840d92-f19e-4dfe-8eab-5a1fd67a3a77", *usr.OriginId) && strings.EqualFold(upn, *usr.PrincipalName) {
315315
iFound++
316316
break
317317
}
@@ -359,7 +359,7 @@ func TestDataSourceUser_Read_TestFilterByOriginOriginId(t *testing.T) {
359359
for _, elem := range usersSet.List() {
360360
upn := elem.(map[string]interface{})["principal_name"].(string)
361361
for _, usr := range usrList1 {
362-
if "8c840d92-f19e-4dfe-8eab-5a1fd67a3a77" == *usr.OriginId && "aad" == *usr.Origin && upn == *usr.PrincipalName {
362+
if strings.EqualFold("8c840d92-f19e-4dfe-8eab-5a1fd67a3a77", *usr.OriginId) && strings.EqualFold("aad", *usr.Origin) && strings.EqualFold(upn, *usr.PrincipalName) {
363363
iFound++
364364
break
365365
}
@@ -415,7 +415,7 @@ func TestDataSourceUser_Read_TestFilterBySubjectType(t *testing.T) {
415415
for _, elem := range usersSet.List() {
416416
upn := elem.(map[string]interface{})["principal_name"].(string)
417417
for _, usr := range usrList {
418-
if upn == *usr.PrincipalName {
418+
if strings.EqualFold(upn, *usr.PrincipalName) {
419419
iFound++
420420
break
421421
}

azuredevops/resource_build_definition.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func resourceBuildDefinitionRead(d *schema.ResourceData, m interface{}) error {
189189
}
190190

191191
func resourceBuildDefinitionDelete(d *schema.ResourceData, m interface{}) error {
192-
if d.Id() == "" {
192+
if strings.EqualFold(d.Id(), "") {
193193
return nil
194194
}
195195

azuredevops/resource_git_repository.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package azuredevops
33
import (
44
"fmt"
55
"log"
6+
"strings"
67

78
"github.com/google/uuid"
89
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -134,7 +135,7 @@ func resourceGitRepositoryCreate(d *schema.ResourceData, m interface{}) error {
134135
return fmt.Errorf("Error creating repository in Azure DevOps: %+v", err)
135136
}
136137

137-
if initialization != nil && initialization.initType == "Clean" {
138+
if initialization != nil && strings.EqualFold(initialization.initType, "Clean") {
138139
err = initializeGitRepository(clients, createdRepo)
139140
if err != nil {
140141
if err := deleteGitRepository(clients, createdRepo.Id.String()); err != nil {
@@ -275,7 +276,7 @@ func deleteGitRepository(clients *config.AggregatedClient, repoID string) error
275276
// Lookup an Azure Git Repository using the ID, or name if the ID is not set.
276277
func gitRepositoryRead(clients *config.AggregatedClient, repoID string, repoName string, projectID string) (*git.GitRepository, error) {
277278
identifier := repoID
278-
if identifier == "" {
279+
if strings.EqualFold(identifier, "") {
279280
identifier = repoName
280281
}
281282

@@ -303,7 +304,7 @@ func expandGitRepository(d *schema.ResourceData) (*git.GitRepository, *repoIniti
303304
// an "error" is OK here as it is expected in the case that the ID is not set in the resource data
304305
var repoID *uuid.UUID
305306
id := d.Id()
306-
if id == "" {
307+
if strings.EqualFold(id, "") {
307308
log.Print("[DEBUG] expandGitRepository: ID is empty (not set)")
308309
} else {
309310
parsedID, err := uuid.Parse(id)
@@ -336,11 +337,11 @@ func expandGitRepository(d *schema.ResourceData) (*git.GitRepository, *repoIniti
336337
sourceURL: initValues["source_url"].(string),
337338
}
338339

339-
if initialization.initType == "Import" {
340+
if strings.EqualFold(initialization.initType, "import") {
340341
return nil, nil, nil, fmt.Errorf("Initialization strategy not implemented: %s", initialization.initType)
341342
}
342343

343-
if initialization.initType == "Clean" {
344+
if strings.EqualFold(initialization.initType, "clean") {
344345
initialization.sourceType = ""
345346
initialization.sourceURL = ""
346347
}

azuredevops/resource_group_membership.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package azuredevops
33
import (
44
"fmt"
55
"math/rand"
6+
"strings"
67
"time"
78

89
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
@@ -55,7 +56,7 @@ func resourceGroupMembershipCreate(d *schema.ResourceData, m interface{}) error
5556
membersToAdd := d.Get("members").(*schema.Set)
5657
var membersToRemove *schema.Set = nil
5758

58-
if "overwrite" == mode {
59+
if strings.EqualFold("overwrite", mode) {
5960
actualMemberships, err := getGroupMemberships(clients, group)
6061
if err != nil {
6162
return fmt.Errorf("Error reading group memberships during read: %+v", err)
@@ -277,7 +278,7 @@ func resourceGroupMembershipRead(d *schema.ResourceData, m interface{}) error {
277278
stateMembers := d.Get("members").(*schema.Set)
278279
members := make([]string, 0)
279280
for _, membership := range *actualMemberships {
280-
if "overwrite" == mode || stateMembers.Contains(*membership.MemberDescriptor) {
281+
if strings.EqualFold("overwrite", mode) || stateMembers.Contains(*membership.MemberDescriptor) {
281282
members = append(members, *membership.MemberDescriptor)
282283
}
283284
}

azuredevops/resource_project.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func expandProject(clients *config.AggregatedClient, d *schema.ResourceData, for
272272
}
273273

274274
func convertVisibility(v string) *core.ProjectVisibility {
275-
if strings.ToLower(v) == "public" {
275+
if strings.EqualFold(v, "public") {
276276
return &core.ProjectVisibilityValues.Public
277277
}
278278
return &core.ProjectVisibilityValues.Private

azuredevops/resource_serviceendpoint_github.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package azuredevops
22

33
import (
4+
"strings"
5+
46
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
57
"github.com/microsoft/azure-devops-go-api/azuredevops/serviceendpoint"
68
crud "github.com/microsoft/terraform-provider-azuredevops/azuredevops/crud/serviceendpoint"
@@ -136,14 +138,14 @@ func expandServiceEndpointGitHub(d *schema.ResourceData) (*serviceendpoint.Servi
136138
// Convert AzDO data structure to internal Terraform data structure
137139
func flattenServiceEndpointGitHub(d *schema.ResourceData, serviceEndpoint *serviceendpoint.ServiceEndpoint, projectID *string) {
138140
crud.DoBaseFlattening(d, serviceEndpoint, projectID)
139-
if *serviceEndpoint.Authorization.Scheme == "OAuth" {
141+
if strings.EqualFold(*serviceEndpoint.Authorization.Scheme, "OAuth") {
140142
d.Set("auth_oath", &[]map[string]interface{}{
141143
{
142144
"oauth_configuration_id": (*serviceEndpoint.Authorization.Parameters)["ConfigurationId"],
143145
},
144146
})
145147
}
146-
if *serviceEndpoint.Authorization.Scheme == "PersonalAccessToken" {
148+
if strings.EqualFold(*serviceEndpoint.Authorization.Scheme, "PersonalAccessToken") {
147149
authPersonalSet := d.Get("auth_personal").(*schema.Set).List()
148150
authPersonal := flattenAuthPerson(d, authPersonalSet)
149151
if authPersonal != nil {

azuredevops/resource_user_entitlement.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package azuredevops
33
import (
44
"bytes"
55
"fmt"
6+
"strings"
67

78
"github.com/google/uuid"
89
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -93,7 +94,7 @@ func resourceUserEntitlementRead(d *schema.ResourceData, m interface{}) error {
9394
}
9495

9596
func resourceUserEntitlementDelete(d *schema.ResourceData, m interface{}) error {
96-
if d.Id() == "" {
97+
if strings.EqualFold(d.Id(), "") {
9798
return nil
9899
}
99100
userEntitlementID := d.Id()

azuredevops/resource_user_entitlement_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ func MatchAddUserEntitlementArgs(t memberentitlementmanagement.AddUserEntitlemen
295295
func (m *matchAddUserEntitlementArgs) Matches(x interface{}) bool {
296296
args := x.(memberentitlementmanagement.AddUserEntitlementArgs)
297297
return *args.UserEntitlement.AccessLevel.AccountLicenseType == *m.t.UserEntitlement.AccessLevel.AccountLicenseType &&
298-
*args.UserEntitlement.User.Origin == *m.t.UserEntitlement.User.Origin &&
299-
*args.UserEntitlement.User.OriginId == *m.t.UserEntitlement.User.OriginId &&
300-
*args.UserEntitlement.User.PrincipalName == *m.t.UserEntitlement.User.PrincipalName
298+
strings.EqualFold(*args.UserEntitlement.User.Origin, *m.t.UserEntitlement.User.Origin) &&
299+
strings.EqualFold(*args.UserEntitlement.User.OriginId, *m.t.UserEntitlement.User.OriginId) &&
300+
strings.EqualFold(*args.UserEntitlement.User.PrincipalName, *m.t.UserEntitlement.User.PrincipalName)
301301
}
302302

303303
func (m *matchAddUserEntitlementArgs) String() string {

azuredevops/utils/config/config.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"strings"
78

89
"github.com/microsoft/azure-devops-go-api/azuredevops"
910
"github.com/microsoft/azure-devops-go-api/azuredevops/build"
@@ -39,11 +40,11 @@ type AggregatedClient struct {
3940
func GetAzdoClient(azdoPAT string, organizationURL string) (*AggregatedClient, error) {
4041
ctx := context.Background()
4142

42-
if azdoPAT == "" {
43+
if strings.EqualFold(azdoPAT, "") {
4344
return nil, fmt.Errorf("the personal access token is required")
4445
}
4546

46-
if organizationURL == "" {
47+
if strings.EqualFold(organizationURL, "") {
4748
return nil, fmt.Errorf("the url of the Azure DevOps is required")
4849
}
4950

azuredevops/utils/converter/converter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package converter
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/microsoft/azure-devops-go-api/azuredevops/licensing"
78
)
89

910
// String Get a pointer to a string
1011
func String(value string) *string {
11-
if value == "" {
12+
if strings.EqualFold(value, "") {
1213
return nil
1314
}
1415
return &value

azuredevops/utils/testhelper/hcl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ data "azuredevops_group" "group" {
3434

3535
// TestAccProjectResource HCL describing an AzDO project
3636
func TestAccProjectResource(projectName string) string {
37-
if projectName == "" {
37+
if strings.EqualFold(projectName, "") {
3838
return ""
3939
}
4040
return fmt.Sprintf(`

azuredevops/utils/testhelper/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testhelper
22

33
import (
44
"os"
5+
"strings"
56
"testing"
67
)
78

@@ -16,7 +17,7 @@ func TestAccPreCheck(t *testing.T, additionalEnvVars *[]string) {
1617
}
1718

1819
for _, variable := range requiredEnvVars {
19-
if os.Getenv(variable) == "" {
20+
if strings.EqualFold(os.Getenv(variable), "") {
2021
t.Fatalf("`%s` must be set for acceptance tests!", variable)
2122
}
2223
}

azuredevops/utils/tfhelper/tfhelper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func ParseProjectIDAndResourceID(d *schema.ResourceData) (string, int, error) {
103103
// ParseImportedID parse the imported int Id from the terraform import
104104
func ParseImportedID(id string) (string, int, error) {
105105
parts := strings.SplitN(id, "/", 2)
106-
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
106+
if len(parts) != 2 || strings.EqualFold(parts[0], "") || strings.EqualFold(parts[1], "") {
107107
return "", 0, fmt.Errorf("unexpected format of ID (%s), expected projectid/resourceId", id)
108108
}
109109
project := parts[0]
@@ -117,7 +117,7 @@ func ParseImportedID(id string) (string, int, error) {
117117
// ParseImportedName parse the imported Id (Name) from the terraform import
118118
func ParseImportedName(id string) (string, string, error) {
119119
parts := strings.SplitN(id, "/", 2)
120-
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
120+
if len(parts) != 2 || strings.EqualFold(parts[0], "") || strings.EqualFold(parts[1], "") {
121121
return "", "", fmt.Errorf("unexpected format of ID (%s), expected projectid/resourceName", id)
122122
}
123123
project := parts[0]
@@ -129,7 +129,7 @@ func ParseImportedName(id string) (string, string, error) {
129129
// ParseImportedUUID parse the imported uuid from the terraform import
130130
func ParseImportedUUID(id string) (string, string, error) {
131131
parts := strings.SplitN(id, "/", 2)
132-
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
132+
if len(parts) != 2 || strings.EqualFold(parts[0], "") || strings.EqualFold(parts[1], "") {
133133
return "", "", fmt.Errorf("unexpected format of ID (%s), expected projectid/resourceId", id)
134134
}
135135
project := parts[0]

azuredevops/utils/validate/strings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func NoEmptyStrings(i interface{}, k string) ([]string, []error) {
1212
return nil, []error{fmt.Errorf("expected type of %q to be string", k)}
1313
}
1414

15-
if strings.TrimSpace(v) == "" {
15+
if strings.EqualFold(strings.TrimSpace(v), "") {
1616
return nil, []error{fmt.Errorf("%q must not be empty", k)}
1717
}
1818

azuredevops/utils/validate/uuid.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package validate
33
import (
44
"fmt"
55
"regexp"
6+
"strings"
67

78
"github.com/hashicorp/go-uuid"
89
)
@@ -33,7 +34,7 @@ func UUIDOrEmpty(i interface{}, k string) (warnings []string, errors []error) {
3334
return
3435
}
3536

36-
if v == "" {
37+
if strings.EqualFold(v, "") {
3738
return
3839
}
3940

go.mod

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,11 @@ require (
1111
github.com/hashicorp/terraform-plugin-sdk v1.8.0
1212
github.com/microsoft/azure-devops-go-api/azuredevops v0.0.0-20200327121006-543de4815ec2
1313
github.com/stretchr/testify v1.3.0
14-
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550
14+
github.com/yuin/goldmark v1.1.30 // indirect
15+
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904
16+
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
17+
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect
18+
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a // indirect
19+
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4 // indirect
20+
golang.org/x/tools v0.0.0-20200416193827-92fa1ff4b140 // indirect
1521
)

0 commit comments

Comments
 (0)