From 2365f7b3803092d85504a0318fa28386a2d2d3b5 Mon Sep 17 00:00:00 2001 From: ParthaI Date: Fri, 10 Nov 2023 17:12:25 +0530 Subject: [PATCH] Add column properties in the table jira_project Closes #89 --- jira/table_jira_project.go | 85 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/jira/table_jira_project.go b/jira/table_jira_project.go index b830257..02dec49 100644 --- a/jira/table_jira_project.go +++ b/jira/table_jira_project.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "io" + "strings" "github.com/andygrunwald/go-jira" "github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto" @@ -100,6 +101,13 @@ func tableProject(_ context.Context) *plugin.Table { Hydrate: getProject, Transform: transform.FromField("Components").Transform(extractProjectComponentIds), }, + { + Name: "properties", + Description: "This resource represents project properties, which provides for storing custom data against a project.", + Type: proto.ColumnType_JSON, + Hydrate: getProjectProperties, + Transform: transform.FromValue(), + }, { Name: "issue_types", Description: "List of the issue types available in the project.", @@ -235,8 +243,72 @@ func getProject(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) return project, err } +func getProjectProperties(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { + project := getProjectInfo(ctx, h.Item) + + client, err := connect(ctx, d) + if err != nil { + plugin.Logger(ctx).Error("jira_project.getProjectProperties", "connection_error", err) + return nil, err + } + + keys, err := getProjectPropertyKeys(ctx, client, project.ID) + if err != nil { + return nil, err + } + + var properties []KeyPropertyValue + for _, key := range keys { + apiEndpoint := fmt.Sprintf("rest/api/3/project/%s/properties/%s", project.ID, key.Key) + + req, err := client.NewRequest("GET", strings.Trim(apiEndpoint, " "), nil) + if err != nil { + plugin.Logger(ctx).Error("jira_project.getProjectProperties", "get_request_error", err) + return nil, err + } + + property := new(KeyPropertyValue) + _, err = client.Do(req, property) + if err != nil { + return nil, err + } + properties = append(properties, *property) + + } + + return properties, nil +} + +func getProjectPropertyKeys(ctx context.Context, client *jira.Client, projectId string) ([]ProjectKey, error) { + apiEndpoint := fmt.Sprintf("rest/api/3/project/%s/properties", projectId) + + req, err := client.NewRequest("GET", apiEndpoint, nil) + if err != nil { + plugin.Logger(ctx).Error("jira_project.getProjectPropertyKeys", "get_request_error", err) + return nil, err + } + + keys := new(ProjectKeys) + _, err = client.Do(req, keys) + if err != nil { + return nil, err + } + + return keys.Keys, nil +} + //// TRANSFORM FUNCTION +func getProjectInfo(ctx context.Context, projectInfo interface{}) Project { + switch item := projectInfo.(type) { + case *Project: + return *item + case Project: + return item + } + return Project{} +} + func extractProjectComponentIds(_ context.Context, d *transform.TransformData) (interface{}, error) { var componentIds []string for _, item := range d.Value.([]jira.ProjectComponent) { @@ -276,3 +348,16 @@ type Project struct { ProjectCategory jira.ProjectCategory `json:"projectCategory,omitempty" structs:"projectCategory,omitempty"` ProjectTypeKey string `json:"projectTypeKey" structs:"projectTypeKey"` } + +type KeyPropertyValue struct { + Key string `json:"key,omitempty" structs:"key,omitempty"` + Value interface{} `json:"value,omitempty" structs:"value,omitempty"` +} + +type ProjectKeys struct { + Keys []ProjectKey `json:"keys,omitempty" structs:"keys,omitempty"` +} +type ProjectKey struct { + Self string `json:"self,omitempty" structs:"self,omitempty"` + Key string `json:"key,omitempty" structs:"key,omitempty"` +} \ No newline at end of file