-
Notifications
You must be signed in to change notification settings - Fork 3
/
jira.go
140 lines (113 loc) · 3.81 KB
/
jira.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package credential
import (
"context"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/v2/modconfig"
"github.com/turbot/pipe-fittings/v2/utils"
"github.com/zclconf/go-cty/cty"
)
type JiraCredential struct {
CredentialImpl
APIToken *string `json:"api_token,omitempty" cty:"api_token" hcl:"api_token,optional"`
BaseURL *string `json:"base_url,omitempty" cty:"base_url" hcl:"base_url,optional"`
Username *string `json:"username,omitempty" cty:"username" hcl:"username,optional"`
}
func (c *JiraCredential) getEnv() map[string]cty.Value {
return nil
}
func (c *JiraCredential) CtyValue() (cty.Value, error) {
ctyValue, err := modconfig.GetCtyValue(c)
if err != nil {
return cty.NilVal, err
}
valueMap := ctyValue.AsValueMap()
valueMap["env"] = cty.ObjectVal(c.getEnv())
return cty.ObjectVal(valueMap), nil
}
func (c *JiraCredential) Equals(otherCredential Credential) bool {
// If both pointers are nil, they are considered equal
if c == nil && helpers.IsNil(otherCredential) {
return true
}
if (c == nil && !helpers.IsNil(otherCredential)) || (c != nil && helpers.IsNil(otherCredential)) {
return false
}
other, ok := otherCredential.(*JiraCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.APIToken, other.APIToken) {
return false
}
if !utils.PtrEqual(c.BaseURL, other.BaseURL) {
return false
}
if !utils.PtrEqual(c.Username, other.Username) {
return false
}
return true
}
func (c *JiraCredential) Resolve(ctx context.Context) (Credential, error) {
if c.APIToken == nil && c.BaseURL == nil && c.Username == nil {
// The order of precedence for the Jira API token environment variable
// 1. JIRA_API_TOKEN
// 2. JIRA_TOKEN
jiraAPITokenEnvVar := os.Getenv("JIRA_TOKEN")
if os.Getenv("JIRA_API_TOKEN") != "" {
jiraAPITokenEnvVar = os.Getenv("JIRA_API_TOKEN")
}
jiraURLEnvVar := os.Getenv("JIRA_URL")
jiraUserEnvVar := os.Getenv("JIRA_USER")
// Don't modify existing credential, resolve to a new one
newCreds := &JiraCredential{
CredentialImpl: c.CredentialImpl,
APIToken: &jiraAPITokenEnvVar,
BaseURL: &jiraURLEnvVar,
Username: &jiraUserEnvVar,
}
return newCreds, nil
}
return c, nil
}
func (c *JiraCredential) GetTtl() int {
return -1
}
func (c *JiraCredential) Validate() hcl.Diagnostics {
return hcl.Diagnostics{}
}
type JiraConnectionConfig struct {
APIToken *string `cty:"api_token" hcl:"api_token,optional"`
BaseURL *string `cty:"base_url" hcl:"base_url,optional"`
PersonalAccessToken *string `cty:"personal_access_token" hcl:"personal_access_token,optional"`
Token *string `cty:"token" hcl:"token,optional"`
Username *string `cty:"username" hcl:"username,optional"`
}
func (c *JiraConnectionConfig) GetCredential(name string, shortName string) Credential {
// Steampipe Jira plugin uses the attribute token to configure the credential, whereas
// the Flowpipe uses the attribute `api_token` which is intended to distinguish between 2 different token, i.e. token and personal_access_token
// Hence, we need a special handling to support both token and api_token, and the order of precedence will be api_token and token.
var jiraAPIToken string
if c.Token != nil {
jiraAPIToken = *c.Token
}
if c.APIToken != nil {
jiraAPIToken = *c.APIToken
}
jiraCred := &JiraCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "jira",
},
// In Flowpipe we went with api_token (same as token in Steampipe) since
// there is another type of token (personal access token)
// In future we could update the Steampipe plugin arg too, but not necessary right now.
APIToken: &jiraAPIToken,
}
return jiraCred
}