-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathclient.go
More file actions
129 lines (102 loc) · 2.79 KB
/
client.go
File metadata and controls
129 lines (102 loc) · 2.79 KB
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
package client
import (
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"github.com/google/go-github/v48/github"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/mattermost/mattermost-plugin-github/server/plugin"
)
type PluginAPI interface {
PluginHTTP(*http.Request) *http.Response
}
type Client struct {
httpClient http.Client
}
type pluginAPIRoundTripper struct {
api PluginAPI
}
func (p *pluginAPIRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
resp := p.api.PluginHTTP(req)
if resp == nil {
return nil, errors.Errorf("Failed to make interplugin request")
}
return resp, nil
}
func NewPluginClient(api PluginAPI) *Client {
client := &Client{}
client.httpClient.Transport = &pluginAPIRoundTripper{api}
return client
}
func (c *Client) GetGitHubClient(userID string) (*github.Client, error) {
token, err := c.GetToken(userID)
if err != nil {
return nil, err
}
config, err := c.GetConfiguration()
if err != nil {
return nil, err
}
client, err := plugin.GetGitHubClient(*token, config)
if err != nil {
return nil, err
}
return client, nil
}
func (c *Client) GetConfiguration() (*plugin.Configuration, error) {
req, err := http.NewRequest(http.MethodGet, "/"+plugin.Manifest.Id+"/api/v1/config", http.NoBody)
if err != nil {
return nil, err
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("Unable to get GitHub config. Error: %v, %v", resp.StatusCode, string(respBody))
}
config := &plugin.Configuration{}
err = json.Unmarshal(respBody, config)
config.GitHubOrg = strings.TrimSpace(config.GitHubOrg)
config.GitHubOAuthClientID = strings.TrimSpace(config.GitHubOAuthClientID)
config.GitHubOAuthClientSecret = strings.TrimSpace(config.GitHubOAuthClientSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to decode GitHub config")
}
return config, nil
}
func (c *Client) GetToken(userID string) (*oauth2.Token, error) {
req, err := http.NewRequest(http.MethodGet, "/"+plugin.Manifest.Id+"/api/v1/token", http.NoBody)
if err != nil {
return nil, err
}
values := url.Values{}
values.Add("userID", userID)
req.URL.RawQuery = values.Encode()
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("Unable to get GitHub token. Error: %v, %v", resp.StatusCode, string(respBody))
}
token := &oauth2.Token{}
err = json.Unmarshal(respBody, token)
if err != nil {
return nil, errors.Wrap(err, "failed to decode GitHub token")
}
return token, nil
}