-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathclient.go
More file actions
115 lines (96 loc) · 3.25 KB
/
Copy pathclient.go
File metadata and controls
115 lines (96 loc) · 3.25 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
// Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package graphql
import (
"context"
"net/url"
"github.com/pkg/errors"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"github.com/mattermost/mattermost/server/public/pluginapi"
)
// Client encapsulates the third party package that communicates with Github GraphQL API
type Client struct {
client *githubv4.Client
org string
username string
logger pluginapi.LogService
getOrganizations func() []string
}
// NewClient creates and returns Client. The third party package that queries GraphQL is initialized here.
func NewClient(logger pluginapi.LogService, getOrganizations func() []string, token oauth2.Token, username, orgName, enterpriseBaseURL string) *Client {
ts := oauth2.StaticTokenSource(&token)
httpClient := oauth2.NewClient(context.Background(), ts)
var client Client
if enterpriseBaseURL == "" {
client = Client{
username: username,
client: githubv4.NewClient(httpClient),
logger: logger,
org: orgName,
getOrganizations: getOrganizations,
}
} else {
baseURL, err := url.JoinPath(enterpriseBaseURL, "api", "graphql")
if err != nil {
logger.Debug("Not able to parse the enterprise URL", "error", err.Error())
return nil
}
client = Client{
client: githubv4.NewEnterpriseClient(baseURL, httpClient),
username: username,
org: orgName,
logger: logger,
getOrganizations: getOrganizations,
}
}
return &client
}
// executeQuery takes a query struct and sends it to Github GraphQL API via helper package.
func (c *Client) executeQuery(ctx context.Context, qry interface{}, params map[string]interface{}) error {
if err := c.client.Query(ctx, qry, params); err != nil {
return errors.Wrap(err, "error in executing query")
}
return nil
}
type changeUserStatusMutation struct {
ChangeUserStatus struct {
Status struct {
Message githubv4.String
Emoji githubv4.String
}
} `graphql:"changeUserStatus(input: $input)"`
}
func (c *Client) UpdateUserStatus(ctx context.Context, emoji, message string, busy bool) (string, error) {
var mutation changeUserStatusMutation
input := githubv4.ChangeUserStatusInput{
Emoji: githubv4.NewString(githubv4.String(emoji)),
Message: githubv4.NewString(githubv4.String(message)),
LimitedAvailability: githubv4.NewBoolean(githubv4.Boolean(busy)),
}
err := c.client.Mutate(ctx, &mutation, input, nil)
if err != nil {
return "", err
}
return string(mutation.ChangeUserStatus.Status.Message), nil
}
type getUserStatusQuery struct {
User struct {
Status struct {
Message githubv4.String
Emoji githubv4.String
LimitedAvailability githubv4.Boolean
}
} `graphql:"user(login: $login)"`
}
func (c *Client) GetUserStatus(ctx context.Context, login string) (string, string, bool, error) {
var query getUserStatusQuery
variables := map[string]interface{}{
"login": githubv4.String(login),
}
err := c.client.Query(ctx, &query, variables)
if err != nil {
return "", "", false, err
}
return string(query.User.Status.Message), string(query.User.Status.Emoji), bool(query.User.Status.LimitedAvailability), nil
}