Skip to content

Commit

Permalink
Merge pull request #251 from OpsLevel/kr/tag-key-validation
Browse files Browse the repository at this point in the history
add tag key validation
  • Loading branch information
rocktavious authored Sep 22, 2023
2 parents 7fba461 + 67233aa commit 3650052
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Feature-20230922-082512.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Feature
body: Add tag key name validation to `CreateTag` `AssignTag` and `UpdateTag` methods
time: 2023-09-22T08:25:12.029174-05:00
31 changes: 31 additions & 0 deletions tags.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package opslevel

import (
"fmt"
"regexp"
)

type TagOwner string

const (
TagOwnerService TagOwner = "Service"
TagOwnerRepository TagOwner = "Repository"
)

var (
TagKeyRegex = regexp.MustCompile(`\A[a-z][0-9a-z_\.\/\\-]*\z`)
TagKeyErrorMsg = "tag key name '%s' must start with a letter and be only lowercase alphanumerics, underscores, hyphens, periods, and slashes"
)

type Tag struct {
Id ID `json:"id"`
Key string `json:"key"`
Expand Down Expand Up @@ -49,6 +59,15 @@ type TagDeleteInput struct {
Id ID `json:"id"`
}

//#region Helpers

func ValidateTagKey(key string) error {
if !TagKeyRegex.MatchString(key) {
return fmt.Errorf(TagKeyErrorMsg, key)
}
return nil
}

//#region Assign

// Deprecated: Use AssignTagsFor instead
Expand Down Expand Up @@ -76,6 +95,9 @@ func (client *Client) AssignTags(identifier string, tags map[string]string) ([]T
Tags: []TagInput{},
}
for key, value := range tags {
if err := ValidateTagKey(key); err != nil {
return nil, err
}
input.Tags = append(input.Tags, TagInput{
Key: key,
Value: value,
Expand Down Expand Up @@ -110,6 +132,9 @@ func (client *Client) AssignTag(input TagAssignInput) ([]Tag, error) {
func (client *Client) CreateTags(identifier string, tags map[string]string) ([]Tag, error) {
var output []Tag
for key, value := range tags {
if err := ValidateTagKey(key); err != nil {
return nil, err
}
input := TagCreateInput{
Key: key,
Value: value,
Expand Down Expand Up @@ -141,6 +166,9 @@ func (client *Client) CreateTag(input TagCreateInput) (*Tag, error) {
Errors []OpsLevelErrors
} `graphql:"tagCreate(input: $input)"`
}
if err := ValidateTagKey(input.Key); err != nil {
return nil, err
}
v := PayloadVariables{
"input": input,
}
Expand Down Expand Up @@ -187,6 +215,9 @@ func (client *Client) UpdateTag(input TagUpdateInput) (*Tag, error) {
Errors []OpsLevelErrors
} `graphql:"tagUpdate(input: $input)"`
}
if err := ValidateTagKey(input.Key); err != nil {
return nil, err
}
v := PayloadVariables{
"input": input,
}
Expand Down

0 comments on commit 3650052

Please sign in to comment.