Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add column properties in the table jira_project Closes #89 #105

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions jira/table_jira_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/andygrunwald/go-jira"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
Expand Down Expand Up @@ -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.",
ParthaI marked this conversation as resolved.
Show resolved Hide resolved
Type: proto.ColumnType_JSON,
Hydrate: getProjectProperties,
Transform: transform.FromValue(),
},
{
Name: "issue_types",
Description: "List of the issue types available in the project.",
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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"`
}