PLT-8018: Bundled jira plugin (#7920)
* bundled jira plugin * fix generated file formatting, add prepackaged key * whoops, uploaded wrong file * whitelist generated files for license check * make it work for people without go/bin in their path
Этот коммит содержится в:
@@ -1,10 +0,0 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package jira
|
||||
|
||||
type Configuration struct {
|
||||
Enabled bool
|
||||
Secret string
|
||||
UserName string
|
||||
}
|
||||
@@ -1,79 +1,10 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
// +build !amd64 !darwin,!linux,!windows
|
||||
|
||||
package jira
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
|
||||
l4g "github.com/alecthomas/log4go"
|
||||
"github.com/mattermost/mattermost-server/app/plugin"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
plugin.Base
|
||||
api plugin.API
|
||||
configuration atomic.Value
|
||||
}
|
||||
|
||||
func (p *Plugin) Initialize(api plugin.API) {
|
||||
p.api = api
|
||||
p.OnConfigurationChange()
|
||||
api.PluginRouter().HandleFunc("/webhook", p.handleWebhook).Methods("POST")
|
||||
}
|
||||
|
||||
func (p *Plugin) config() *Configuration {
|
||||
return p.configuration.Load().(*Configuration)
|
||||
}
|
||||
|
||||
func (p *Plugin) OnConfigurationChange() {
|
||||
var configuration Configuration
|
||||
if err := p.api.LoadPluginConfiguration(&configuration); err != nil {
|
||||
l4g.Error(err.Error())
|
||||
}
|
||||
p.configuration.Store(&configuration)
|
||||
}
|
||||
|
||||
func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
config := p.config()
|
||||
if !config.Enabled || config.Secret == "" || config.UserName == "" {
|
||||
http.Error(w, "This plugin is not configured.", http.StatusForbidden)
|
||||
return
|
||||
} else if subtle.ConstantTimeCompare([]byte(r.URL.Query().Get("secret")), []byte(config.Secret)) != 1 {
|
||||
http.Error(w, "You must provide the configured secret.", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
var webhook Webhook
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&webhook); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
} else if attachment, err := webhook.SlackAttachment(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
} else if attachment == nil {
|
||||
return
|
||||
} else if r.URL.Query().Get("channel") == "" {
|
||||
http.Error(w, "You must provide a channel.", http.StatusBadRequest)
|
||||
} else if user, err := p.api.GetUserByName(config.UserName); err != nil {
|
||||
http.Error(w, p.api.I18n(err.Message, r), err.StatusCode)
|
||||
} else if team, err := p.api.GetTeamByName(r.URL.Query().Get("team")); err != nil {
|
||||
http.Error(w, p.api.I18n(err.Message, r), err.StatusCode)
|
||||
} else if channel, err := p.api.GetChannelByName(team.Id, r.URL.Query().Get("channel")); err != nil {
|
||||
http.Error(w, p.api.I18n(err.Message, r), err.StatusCode)
|
||||
} else if _, err := p.api.CreatePost(&model.Post{
|
||||
ChannelId: channel.Id,
|
||||
Type: model.POST_SLACK_ATTACHMENT,
|
||||
UserId: user.Id,
|
||||
Props: map[string]interface{}{
|
||||
"from_webhook": "true",
|
||||
"use_user_icon": "true",
|
||||
"attachments": []*model.SlackAttachment{attachment},
|
||||
},
|
||||
}); err != nil {
|
||||
http.Error(w, p.api.I18n(err.Message, r), err.StatusCode)
|
||||
}
|
||||
func Asset(name string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
235
app/plugin/jira/plugin_darwin_amd64.go
Обычный файл
235
app/plugin/jira/plugin_darwin_amd64.go
Обычный файл
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
235
app/plugin/jira/plugin_linux_amd64.go
Обычный файл
235
app/plugin/jira/plugin_linux_amd64.go
Обычный файл
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
235
app/plugin/jira/plugin_windows_amd64.go
Обычный файл
235
app/plugin/jira/plugin_windows_amd64.go
Обычный файл
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
@@ -1,174 +0,0 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package jira
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
type Webhook struct {
|
||||
WebhookEvent string
|
||||
Issue struct {
|
||||
Self string
|
||||
Key string
|
||||
Fields struct {
|
||||
Assignee *struct {
|
||||
DisplayName string
|
||||
Name string
|
||||
}
|
||||
Summary string
|
||||
Description string
|
||||
Priority *struct {
|
||||
Id string
|
||||
Name string
|
||||
}
|
||||
IssueType struct {
|
||||
Name string
|
||||
IconURL string
|
||||
}
|
||||
Resolution *struct {
|
||||
Id string
|
||||
}
|
||||
Status struct {
|
||||
Id string
|
||||
}
|
||||
}
|
||||
}
|
||||
User struct {
|
||||
Name string
|
||||
AvatarUrls map[string]string
|
||||
DisplayName string
|
||||
}
|
||||
Comment struct {
|
||||
Body string
|
||||
}
|
||||
ChangeLog struct {
|
||||
Items []struct {
|
||||
FromString string
|
||||
ToString string
|
||||
Field string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the text to be placed in the resulting post or an empty string if nothing should be
|
||||
// posted.
|
||||
func (w *Webhook) SlackAttachment() (*model.SlackAttachment, error) {
|
||||
switch w.WebhookEvent {
|
||||
case "jira:issue_created":
|
||||
case "jira:issue_updated":
|
||||
isResolutionChange := false
|
||||
for _, change := range w.ChangeLog.Items {
|
||||
if change.Field == "resolution" {
|
||||
isResolutionChange = (change.FromString == "") != (change.ToString == "")
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isResolutionChange {
|
||||
return nil, nil
|
||||
}
|
||||
case "jira:issue_deleted":
|
||||
if w.Issue.Fields.Resolution != nil {
|
||||
return nil, nil
|
||||
}
|
||||
default:
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
pretext, err := w.renderText("" +
|
||||
"{{.User.DisplayName}} {{.Verb}} {{.Issue.Fields.IssueType.Name}} " +
|
||||
"[{{.Issue.Key}}]({{.JIRAURL}}/browse/{{.Issue.Key}})" +
|
||||
"")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
text, err := w.renderText("" +
|
||||
"[{{.Issue.Fields.Summary}}]({{.JIRAURL}}/browse/{{.Issue.Key}})" +
|
||||
"{{if eq .WebhookEvent \"jira:issue_created\"}}{{if ne .Issue.Fields.Description \"\"}}" +
|
||||
"{{if len .Issue.Fields.Description | lt 3000}}" +
|
||||
"\n\n{{printf \"%.3000s\" .Issue.Fields.Description}}..." +
|
||||
"{{else}}" +
|
||||
"\n\n{{.Issue.Fields.Description}}" +
|
||||
"{{end}}" +
|
||||
"{{end}}{{end}}" +
|
||||
"")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var fields []*model.SlackAttachmentField
|
||||
if w.WebhookEvent == "jira:issue_created" {
|
||||
if w.Issue.Fields.Assignee != nil {
|
||||
fields = append(fields, &model.SlackAttachmentField{
|
||||
Title: "Assignee",
|
||||
Value: w.Issue.Fields.Assignee.DisplayName,
|
||||
Short: true,
|
||||
})
|
||||
}
|
||||
if w.Issue.Fields.Priority != nil {
|
||||
fields = append(fields, &model.SlackAttachmentField{
|
||||
Title: "Priority",
|
||||
Value: w.Issue.Fields.Priority.Name,
|
||||
Short: true,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &model.SlackAttachment{
|
||||
Fallback: pretext,
|
||||
Color: "#95b7d0",
|
||||
Pretext: pretext,
|
||||
Text: text,
|
||||
Fields: fields,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (w *Webhook) JIRAURL() string {
|
||||
pos := strings.LastIndex(w.Issue.Self, "/rest/api")
|
||||
if pos < 0 {
|
||||
return ""
|
||||
}
|
||||
return w.Issue.Self[:pos]
|
||||
}
|
||||
|
||||
func (w *Webhook) renderText(tplBody string) (string, error) {
|
||||
verb := strings.TrimPrefix(w.WebhookEvent, "jira:issue_")
|
||||
|
||||
if w.WebhookEvent == "jira:issue_updated" {
|
||||
for _, change := range w.ChangeLog.Items {
|
||||
if change.Field == "resolution" {
|
||||
if change.ToString == "" && change.FromString != "" {
|
||||
verb = "reopened"
|
||||
} else if change.ToString != "" && change.FromString == "" {
|
||||
verb = "resolved"
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tpl, err := template.New("post").Parse(tplBody)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := tpl.Execute(&buf, struct {
|
||||
*Webhook
|
||||
JIRAURL string
|
||||
Verb string
|
||||
}{
|
||||
Webhook: w,
|
||||
JIRAURL: w.JIRAURL(),
|
||||
Verb: verb,
|
||||
}); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package jira
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWebhookJIRAURL(t *testing.T) {
|
||||
var w Webhook
|
||||
w.Issue.Self = "http://localhost:8080/rest/api/2/issue/10006"
|
||||
assert.Equal(t, "http://localhost:8080", w.JIRAURL())
|
||||
|
||||
w.Issue.Self = "http://localhost:8080/foo/bar/rest/api/2/issue/10006"
|
||||
assert.Equal(t, "http://localhost:8080/foo/bar", w.JIRAURL())
|
||||
}
|
||||
Ссылка в новой задаче
Block a user