-
Notifications
You must be signed in to change notification settings - Fork 127
/
config_jira.go
80 lines (68 loc) · 2.38 KB
/
config_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
package evergreen
import (
"context"
"strings"
"github.com/mongodb/grip/send"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
// JiraConfig stores auth info for interacting with Atlassian Jira.
type JiraConfig struct {
Host string `yaml:"host" bson:"host" json:"host"`
BasicAuthConfig JiraBasicAuthConfig `yaml:"basic_auth" bson:"basic_auth" json:"basic_auth"`
OAuth1Config JiraOAuth1Config `yaml:"oauth1" bson:"oauth1" json:"oauth1"`
Email string `yaml:"email" bson:"email" json:"email"`
}
type JiraBasicAuthConfig struct {
Username string `yaml:"username" bson:"username" json:"username"`
Password string `yaml:"password" bson:"password" json:"password"`
}
type JiraOAuth1Config struct {
PrivateKey string `yaml:"private_key" bson:"private_key" json:"private_key"`
AccessToken string `yaml:"access_token" bson:"access_token" json:"access_token"`
TokenSecret string `yaml:"token_secret" bson:"token_secret" json:"token_secret"`
ConsumerKey string `yaml:"consumer_key" bson:"consumer_key" json:"consumer_key"`
}
func (c *JiraConfig) SectionId() string { return "jira" }
func (c *JiraConfig) Get(ctx context.Context) error {
return getConfigSection(ctx, c)
}
func (c *JiraConfig) Set(ctx context.Context) error {
return errors.Wrapf(setConfigSection(ctx, c.SectionId(), bson.M{
"$set": bson.M{
"host": c.Host,
"basic_auth": c.BasicAuthConfig,
"oauth1": c.OAuth1Config,
"email": c.Email,
}}), "updating config section '%s'", c.SectionId(),
)
}
func (c *JiraConfig) ValidateAndDefault() error {
if (c.Host != "") && (c.BasicAuthConfig.Username != "") == (c.OAuth1Config.AccessToken != "") {
return errors.New("must specify exactly 1 Jira auth method")
}
return nil
}
func (c JiraConfig) GetHostURL() string {
if strings.HasPrefix("http", c.Host) {
return c.Host
}
return "https://" + c.Host
}
func (c JiraConfig) Export() *send.JiraOptions {
return &send.JiraOptions{
Name: "evergreen",
BaseURL: c.GetHostURL(),
BasicAuthOpts: send.JiraBasicAuth{
Username: c.BasicAuthConfig.Username,
Password: c.BasicAuthConfig.Password,
UseBasicAuth: true,
},
Oauth1Opts: send.JiraOauth1{
AccessToken: c.OAuth1Config.AccessToken,
TokenSecret: c.OAuth1Config.TokenSecret,
PrivateKey: []byte(c.OAuth1Config.PrivateKey),
ConsumerKey: c.OAuth1Config.ConsumerKey,
},
}
}