/
instance.go
140 lines (115 loc) · 3.65 KB
/
instance.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 instance
import (
"context"
"net/http"
"time"
"github.com/kobsio/kobs/pkg/hub/auth"
"github.com/kobsio/kobs/pkg/hub/auth/jwt"
"github.com/kobsio/kobs/pkg/middleware/roundtripper"
"github.com/andygrunwald/go-jira"
"github.com/mitchellh/mapstructure"
)
type Token struct {
Email string `json:"email"`
Token string `json:"token"`
}
// Config is the structure of the configuration for a single GitHub instance.
type Config struct {
URL string `json:"url"`
Session auth.SessionConfig `json:"session"`
}
type Instance interface {
GetName() string
GetURL() string
TokenToCookie(token *Token) (*http.Cookie, error)
TokenFromCookie(r *http.Request) (*Token, error)
GetSelf(ctx context.Context, token *Token) (*jira.User, error)
GetProjects(ctx context.Context, token *Token) (*jira.ProjectList, error)
GetIssues(ctx context.Context, token *Token, jql string, startAt, maxResults int) ([]jira.Issue, int, error)
}
type instance struct {
name string
config Config
client *http.Client
}
func (i *instance) getAuthenticatedClient(token *Token) (*jira.Client, error) {
client, err := jira.NewClient(i.client, i.config.URL)
if err != nil {
return nil, err
}
client.Authentication.SetBasicAuth(token.Email, token.Token)
return client, nil
}
func (i *instance) GetName() string {
return i.name
}
func (i *instance) GetURL() string {
return i.config.URL
}
// TokenToCookie returns a cookie for the given oauth token.
func (i *instance) TokenToCookie(token *Token) (*http.Cookie, error) {
jwtToken, err := jwt.CreateToken(token, i.config.Session.Token, i.config.Session.ParsedInterval)
if err != nil {
return nil, err
}
return &http.Cookie{
Name: "kobs-plugin-jira-" + i.name,
Value: jwtToken,
Path: "/",
Secure: true,
HttpOnly: true,
Expires: time.Now().Add(i.config.Session.ParsedInterval),
}, nil
}
// TokenFromCookie returns the token from the "kobs-oauth-github" cookie in the given request.
func (i *instance) TokenFromCookie(r *http.Request) (*Token, error) {
cookie, err := r.Cookie("kobs-plugin-jira-" + i.name)
if err != nil {
return nil, err
}
return jwt.ValidateToken[Token](cookie.Value, i.config.Session.Token)
}
func (i *instance) GetSelf(ctx context.Context, token *Token) (*jira.User, error) {
client, err := i.getAuthenticatedClient(token)
if err != nil {
return nil, err
}
user, _, err := client.User.GetSelfWithContext(ctx)
return user, err
}
func (i *instance) GetProjects(ctx context.Context, token *Token) (*jira.ProjectList, error) {
client, err := i.getAuthenticatedClient(token)
if err != nil {
return nil, err
}
projects, _, err := client.Project.GetListWithContext(ctx)
return projects, err
}
func (i *instance) GetIssues(ctx context.Context, token *Token, jql string, startAt, maxResults int) ([]jira.Issue, int, error) {
client, err := i.getAuthenticatedClient(token)
if err != nil {
return nil, 0, err
}
issues, response, err := client.Issue.SearchWithContext(ctx, jql, &jira.SearchOptions{StartAt: startAt, MaxResults: maxResults})
return issues, response.Total, err
}
// New returns a new GitHub instance for the given configuration.
func New(name string, options map[string]any) (Instance, error) {
var config Config
err := mapstructure.Decode(options, &config)
if err != nil {
return nil, err
}
sessionInterval := time.Duration(48 * time.Hour)
if parsedSessionInterval, err := time.ParseDuration(config.Session.Interval); err == nil {
sessionInterval = parsedSessionInterval
}
config.Session.ParsedInterval = sessionInterval
return &instance{
name: name,
config: config,
client: &http.Client{
Transport: roundtripper.DefaultRoundTripper,
},
}, nil
}