forked from evergreen-ci/evergreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_jira.go
104 lines (88 loc) · 3.04 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package evergreen
import (
"strings"
"github.com/mongodb/grip/send"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// 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"`
DefaultProject string `yaml:"default_project" bson:"default_project" json:"default_project"`
}
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(env Environment) error {
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
res := coll.FindOne(ctx, byId(c.SectionId()))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
*c = JiraConfig{}
return nil
}
return errors.Wrapf(err, "error retrieving section %s", c.SectionId())
}
if err := res.Decode(c); err != nil {
return errors.Wrap(err, "problem decoding result")
}
return nil
}
func (c *JiraConfig) Set() error {
env := GetEnvironment()
ctx, cancel := env.Context()
defer cancel()
coll := env.DB().Collection(ConfigCollection)
_, err := coll.UpdateOne(ctx, byId(c.SectionId()), bson.M{
"$set": bson.M{
"host": c.Host,
"basic_auth": c.BasicAuthConfig,
"oauth1": c.OAuth1Config,
"default_project": c.DefaultProject,
},
}, options.Update().SetUpsert(true))
return errors.Wrapf(err, "error updating 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,
},
}
}