Skip to content

Commit

Permalink
🎨 Added the Jira Assets dedicated methods for the issue customfield e…
Browse files Browse the repository at this point in the history
…xtraction
  • Loading branch information
ctreminiom committed Sep 23, 2023
1 parent baaa222 commit c632714
Show file tree
Hide file tree
Showing 3 changed files with 573 additions and 35 deletions.
16 changes: 4 additions & 12 deletions pkg/infra/models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,7 @@ var (
ErrInternalError = errors.New("client: atlassian internal error")
ErrBadRequestError = errors.New("client: atlassian invalid payload")
ErrNoSiteError = errors.New("client: no atlassian site set")
ErrNilPayloadError = errors.New("client: please provide the necessary payload struct")
ErrNonPayloadPointerError = errors.New("client: please provide a valid payload struct pointer (&)")
ErrNoFieldInformationError = errors.New("custom-field: no json fields object found")
ErrNoFloatTypeError = errors.New("custom-field: no float type set")
ErrNoCustomFieldUnmarshalError = errors.New("custom-field: no valid json provided")
ErrNoMultiSelectTypeError = errors.New("custom-field: no multiselect type found")
ErrNoSprintTypeError = errors.New("custom-field: no sprint type found")
ErrNoMultiVersionTypeError = errors.New("custom-field: no multiversion type found")
ErrNilPayloadError = errors.New("client: please provide the necessary payload struct")
Expand All @@ -168,8 +163,6 @@ var (
ErrNoAttachmentIdsError = errors.New("sm: no attachment id's set")
ErrNoLabelsError = errors.New("sm: no label names set")
ErrNoComponentsError = errors.New("sm: no components set")
ErrNoCreateIssuesError = errors.New("jira: no issues payload set")
ErrNoIssueSchemeError = errors.New("jira: no issue instance set")
ErrNoIssuesSliceError = errors.New("jira: no issues object set")
ErrNoMapValuesError = errors.New("jira: no map values set")
ErrNCoComponentError = errors.New("sm: no component set")
Expand All @@ -180,11 +173,10 @@ var (
ErrNoObjectSchemaIDError = errors.New("assets: no object schema id set")
ErrNoObjectTypeIDError = errors.New("assets: no object type id set")
ErrNoObjectTypeAttributeIDError = errors.New("assets: no object type attribute id set")
ErrNoSiteError = errors.New("client: no atlassian site set")
ErrNoCreateIssuesError = errors.New("jira: no issues payload set")
ErrNoIssueSchemeError = errors.New("jira: no issue instance set")
ErrNoWorkspaceError = errors.New("bitbucket: no workspace set")
ErrNoMemberIDError = errors.New("bitbucket: no member id set")
ErrNoWebhookIDError = errors.New("bitbucket: no webhook id set")
ErrNoRepositoryError = errors.New("bitbucket: no repository set")
ErrNoWorkspaceError = errors.New("bitbucket: no workspace set")
ErrNoMemberIDError = errors.New("bitbucket: no member id set")
ErrNoWebhookIDError = errors.New("bitbucket: no webhook id set")
ErrNoRepositoryError = errors.New("bitbucket: no repository set")
)
129 changes: 107 additions & 22 deletions pkg/infra/models/jira_issue_custom_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -1398,40 +1398,125 @@ func ParseSelectCustomFields(buffer bytes.Buffer, customField string) (map[strin
return customfieldsAsMap, nil
}

// ParseAssetCustomField parses the Jira assets elements from the given buffer data
// associated with the specified custom field ID and returns a struct CustomFieldAssetScheme slice
//
// Parameters:
// - customfieldID: A string representing the unique identifier of the custom field.
// - buffer: A bytes.Buffer containing the serialized data to be parsed.
//
// Returns:
// - []*CustomFieldAssetScheme: the customfield value as CustomFieldAssetScheme slice type
//
// Example usage:
//
// customfieldID := "customfield_10001"
// buffer := bytes.NewBuffer([]byte{ /* Serialized data */ })
// assets, err := ParseAssetCustomField(customfieldID, buffer)
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Println(assets)
func ParseAssetCustomField(buffer bytes.Buffer, customField string) ([]*CustomFieldAssetScheme, error) {

raw, err := marshmallow.Unmarshal(buffer.Bytes(), &struct{}{})
if err != nil {
return nil, ErrNoCustomFieldUnmarshalError
}
raw := gjson.ParseBytes(buffer.Bytes())
path := fmt.Sprintf("fields.%v", customField)

fields, containsFields := raw["fields"]
if !containsFields {
// Check if the buffer contains the "fields" object
if !raw.Get("fields").Exists() {
return nil, ErrNoFieldInformationError
}

customFields := fields.(map[string]interface{})
var records []*CustomFieldAssetScheme
// Check if the issue iteration contains information on the customfield selected,
// if not, continue
if raw.Get(path).Type == gjson.Null {
return nil, ErrNoAssetTypeError
}

switch options := customFields[customField].(type) {
case []interface{}:
var assets []*CustomFieldAssetScheme
if err := json.Unmarshal([]byte(raw.Get(path).String()), &assets); err != nil {
return nil, ErrNoAssetTypeError
}

for _, option := range options {
return assets, nil
}

record := &CustomFieldAssetScheme{
WorkspaceId: option.(map[string]interface{})["workspaceId"].(string),
Id: option.(map[string]interface{})["id"].(string),
ObjectId: option.(map[string]interface{})["objectId"].(string),
}
// ParseAssetCustomFields extracts and parses jira assets customfield data from a given bytes.Buffer from multiple issues
//
// This function takes the name of the custom field to parse and a bytes.Buffer containing
// JSON data representing the custom field values associated with different issues. It returns
// a map where the key is the issue key and the value is a slice of CustomFieldAssetScheme
// structs, representing the parsed assets associated with a Jira issues.
//
// The JSON data within the buffer is expected to have a specific structure where the custom field
// values are organized by issue keys and options are represented within a context. The function
// parses this structure to extract and organize the custom field values.
//
// If the custom field data cannot be parsed successfully, an error is returned.
//
// Example Usage:
//
// customFieldName := "customfield_10001"
// buffer := // Populate the buffer with JSON data
// customFields, err := ParseAssetCustomFields(customFieldName, buffer)
// if err != nil {
// // Handle the error
// }
//
// // Iterate through the parsed custom fields
// for issueKey, customFieldValues := range customFields {
// fmt.Printf("Issue Key: %s\n", issueKey)
// for _, value := range customFieldValues {
// fmt.Printf("Custom Field Value: %+v\n", value)
// }
// }
//
// Parameters:
// - customField: The name of the multi-select custom field to parse.
// - buffer: A bytes.Buffer containing JSON data representing custom field values.
//
// Returns:
// - map[string]*CustomFieldAssetScheme: A map where the key is the issue key and the
// value is a CustomFieldAssetScheme struct representing the parsed
// jira assets values.
// - error: An error if there was a problem parsing the custom field data or if the JSON data
// did not conform to the expected structure.
func ParseAssetCustomFields(buffer bytes.Buffer, customField string) (map[string][]*CustomFieldAssetScheme, error) {

records = append(records, record)
raw := gjson.ParseBytes(buffer.Bytes())

// Check if the buffer contains the "issues" object
if !raw.Get("issues").Exists() {
return nil, ErrNoIssuesSliceError
}

// Loop through each custom field, extract the information and stores the data on a map
customfieldsAsMap := make(map[string][]*CustomFieldAssetScheme)
raw.Get("issues").ForEach(func(key, value gjson.Result) bool {

path, issueKey := fmt.Sprintf("fields.%v", customField), value.Get("key").String()

// Check if the issue iteration contains information on the customfield selected,
// if not, continue
if value.Get(path).Type == gjson.Null {
return true
}

case nil:
return nil, nil
default:
return nil, ErrNoAssetTypeError
var customFields []*CustomFieldAssetScheme
if err := json.Unmarshal([]byte(value.Get(path).String()), &customFields); err != nil {
return true
}

customfieldsAsMap[issueKey] = customFields
return true
})

// Check if the map processed contains elements
// if so, return an error interface
if len(customfieldsAsMap) == 0 {
return nil, ErrNoMapValuesError

Check warning on line 1518 in pkg/infra/models/jira_issue_custom_fields.go

View check run for this annotation

Codecov / codecov/patch

pkg/infra/models/jira_issue_custom_fields.go#L1518

Added line #L1518 was not covered by tests
}

return records, nil
return customfieldsAsMap, nil
}
Loading

0 comments on commit c632714

Please sign in to comment.