forked from mattermost/mattermost-plugin-github
/
command.go
211 lines (180 loc) · 8.26 KB
/
command.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"context"
"fmt"
"strings"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/plugin"
"github.com/google/go-github/github"
"github.com/mattermost/mattermost-server/model"
)
const COMMAND_HELP = `* |/github connect| - Connect your Mattermost account to your GitHub account
* |/github disconnect| - Disconnect your Mattermost account from your GitHub account
* |/github todo| - Get a list of unread messages and pull requests awaiting your review
* |/github subscribe list| - Will list the current channel subscriptions
* |/github subscribe owner[/repo] [features]| - Subscribe the current channel to receive notifications about opened pull requests and issues for an organization or repository
* |features| is a comma-delimited list of one or more the following:
* issues - includes new and closed issues
* pulls - includes new and closed pull requests
* pushes - includes pushes
* creates - includes branch and tag creations
* deletes - includes branch and tag deletions
* issue_comments - includes new issue comments
* pull_reviews - includes pull request reviews
* label:"<labelname>" - must include "pulls" or "issues" in feature list when using a label
* Defaults to "pulls,issues,creates,deletes"
* |/github unsubscribe owner/repo| - Unsubscribe the current channel from a repository
* |/github me| - Display the connected GitHub account
* |/github settings [setting] [value]| - Update your user settings
* |setting| can be "notifications" or "reminders"
* |value| can be "on" or "off"`
func getCommand() *model.Command {
return &model.Command{
Trigger: "github",
DisplayName: "Github",
Description: "Integration with Github.",
AutoComplete: true,
AutoCompleteDesc: "Available commands: connect, disconnect, todo, me, settings, subscribe, unsubscribe, help",
AutoCompleteHint: "[command]",
}
}
func (p *Plugin) getCommandResponse(responseType, text string) *model.CommandResponse {
return &model.CommandResponse{
ResponseType: responseType,
Text: text,
Username: GITHUB_USERNAME,
IconURL: p.getConfiguration().ProfileImageURL,
Type: model.POST_DEFAULT,
}
}
func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) {
split := strings.Fields(args.Command)
command := split[0]
parameters := []string{}
action := ""
if len(split) > 1 {
action = split[1]
}
if len(split) > 2 {
parameters = split[2:]
}
if command != "/github" {
return &model.CommandResponse{}, nil
}
if action == "connect" {
config := p.API.GetConfig()
if config.ServiceSettings.SiteURL == nil {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Encountered an error connecting to GitHub."), nil
}
resp := p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("[Click here to link your GitHub account.](%s/plugins/github/oauth/connect)", *config.ServiceSettings.SiteURL))
return resp, nil
}
ctx := context.Background()
var githubClient *github.Client
info, apiErr := p.getGitHubUserInfo(args.UserId)
if apiErr != nil {
text := "Unknown error."
if apiErr.ID == API_ERROR_ID_NOT_CONNECTED {
text = "You must connect your account to GitHub first. Either click on the GitHub logo in the bottom left of the screen or enter `/github connect`."
}
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, text), nil
}
githubClient = p.githubConnect(*info.Token)
switch action {
case "subscribe":
config := p.getConfiguration()
features := "pulls,issues,creates,deletes"
txt := ""
if len(parameters) == 0 {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Please specify a repository or 'list' command."), nil
} else if len(parameters) == 1 && parameters[0] == "list" {
subs, err := p.GetSubscriptionsByChannel(args.ChannelId)
if err != nil {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, err.Error()), nil
}
if len(subs) == 0 {
txt = "Currently there are no subscriptions in this channel"
} else {
txt = "### Subscriptions in this channel\n"
}
for _, sub := range subs {
txt += fmt.Sprintf("* `%s` - %s\n", strings.Trim(sub.Repository, "/"), sub.Features)
}
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, txt), nil
} else if len(parameters) > 1 {
features = strings.Join(parameters[1:], " ")
}
_, owner, repo := parseOwnerAndRepo(parameters[0], config.EnterpriseBaseURL)
if repo == "" {
if err := p.SubscribeOrg(context.Background(), githubClient, args.UserId, owner, args.ChannelId, features); err != nil {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, err.Error()), nil
}
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("Successfully subscribed to organization %s.", owner)), nil
}
if err := p.Subscribe(context.Background(), githubClient, args.UserId, owner, repo, args.ChannelId, features); err != nil {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, err.Error()), nil
}
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("Successfully subscribed to %s.", repo)), nil
case "unsubscribe":
if len(parameters) == 0 {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Please specify a repository."), nil
}
repo := parameters[0]
if err := p.Unsubscribe(args.ChannelId, repo); err != nil {
mlog.Error(err.Error())
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Encountered an error trying to unsubscribe. Please try again."), nil
}
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, fmt.Sprintf("Succesfully unsubscribed from %s.", repo)), nil
case "disconnect":
p.disconnectGitHubAccount(args.UserId)
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Disconnected your GitHub account."), nil
case "todo":
text, err := p.GetToDo(ctx, info.GitHubUsername, githubClient)
if err != nil {
mlog.Error(err.Error())
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Encountered an error getting your to do items."), nil
}
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, text), nil
case "me":
gitUser, _, err := githubClient.Users.Get(ctx, "")
if err != nil {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Encountered an error getting your GitHub profile."), nil
}
text := fmt.Sprintf("You are connected to GitHub as:\n# [![image](%s =40x40)](%s) [%s](%s)", gitUser.GetAvatarURL(), gitUser.GetHTMLURL(), gitUser.GetLogin(), gitUser.GetHTMLURL())
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, text), nil
case "help":
text := "###### Mattermost GitHub Plugin - Slash Command Help\n" + strings.Replace(COMMAND_HELP, "|", "`", -1)
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, text), nil
case "":
text := "###### Mattermost GitHub Plugin - Slash Command Help\n" + strings.Replace(COMMAND_HELP, "|", "`", -1)
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, text), nil
case "settings":
if len(parameters) < 2 {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Please specify both a setting and value. Use `/github help` for more usage information."), nil
}
setting := parameters[0]
if setting != SETTING_NOTIFICATIONS && setting != SETTING_REMINDERS {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Unknown setting."), nil
}
strValue := parameters[1]
value := false
if strValue == SETTING_ON {
value = true
} else if strValue != SETTING_OFF {
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Invalid value. Accepted values are: \"on\" or \"off\"."), nil
}
if setting == SETTING_NOTIFICATIONS {
if value {
p.storeGitHubToUserIDMapping(info.GitHubUsername, info.UserID)
} else {
p.API.KVDelete(info.GitHubUsername + GITHUB_USERNAME_KEY)
}
info.Settings.Notifications = value
} else if setting == SETTING_REMINDERS {
info.Settings.DailyReminder = value
}
p.storeGitHubUserInfo(info)
return p.getCommandResponse(model.COMMAND_RESPONSE_TYPE_EPHEMERAL, "Settings updated."), nil
}
return &model.CommandResponse{}, nil
}