-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.go
176 lines (142 loc) · 4.66 KB
/
utils.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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"strings"
"github.com/mattermost/mattermost-server/v6/utils"
)
func getBaseURL() string {
return "https://api.bitbucket.org/2.0"
}
func getYourOrgReposSearchQuery(organizationName string) string {
return getBaseURL() + "/repositories/" + organizationName + "?role=member"
}
func getYourAllReposSearchQuery() string {
return getBaseURL() + "/repositories?role=member"
}
func getYourAssigneeIssuesSearchQuery(userAccountID, repoFullName string) string {
return getBaseURL() + "/repositories/" + repoFullName + "/issues?q=" +
utils.URLEncode("assignee.account_id=\""+userAccountID+"\" AND state!=\"closed\"")
}
func getYourAssigneePRsSearchQuery(userAccountID, repoFullName string) string {
return getBaseURL() + "/repositories/" + repoFullName + "/pullrequests?q=" +
utils.URLEncode("reviewers.account_id=\""+userAccountID+"\" AND state=\"open\"")
}
func getYourOpenPRsSearchQuery(userAccountID, repoFullName string) string {
return getBaseURL() + "/repositories/" + repoFullName + "/pullrequests?q=" +
utils.URLEncode("author.account_id=\""+userAccountID+"\" AND state=\"open\"")
}
func getSearchIssuesQuery(repoFullName, searchTerm string) string {
return getBaseURL() + "/repositories/" + repoFullName + "/issues?q=" +
utils.URLEncode("title ~ \""+searchTerm+"\"") + "&sort=-updated_on"
}
func pad(src []byte) []byte {
padding := aes.BlockSize - len(src)%aes.BlockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(src, padtext...)
}
func unpad(src []byte) ([]byte, error) {
length := len(src)
unpadding := int(src[length-1])
if unpadding > length {
return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
}
return src[:(length - unpadding)], nil
}
func encrypt(key []byte, text string) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
msg := pad([]byte(text))
ciphertext := make([]byte, aes.BlockSize+len(msg))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], msg)
finalMsg := base64.URLEncoding.EncodeToString(ciphertext)
return finalMsg, nil
}
func decrypt(key []byte, text string) (string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
decodedMsg, err := base64.URLEncoding.DecodeString(text)
if err != nil {
return "", err
}
if (len(decodedMsg) % aes.BlockSize) != 0 {
return "", errors.New("blocksize must be multiple of decoded message length")
}
iv := decodedMsg[:aes.BlockSize]
msg := decodedMsg[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(msg, msg)
unpadMsg, err := unpad(msg)
if err != nil {
return "", err
}
return string(unpadMsg), nil
}
func parseOwnerAndRepoAndReturnFullAlso(full, baseURL string) (string, string, string) {
if baseURL == "" {
baseURL = BitbucketBaseURL
}
full = strings.TrimSuffix(strings.TrimSpace(strings.Replace(full, baseURL, "", 1)), "/")
splitStr := strings.Split(full, "/")
if len(splitStr) == 1 {
owner := splitStr[0]
return owner, owner, ""
} else if len(splitStr) != 2 {
return "", "", ""
}
owner := splitStr[0]
repo := splitStr[1]
return fmt.Sprintf("%s/%s", owner, repo), owner, repo
}
func parseOwnerAndRepo(full, baseURL string) (string, string) {
if baseURL == "" {
baseURL = BitbucketBaseURL
}
full = strings.TrimSuffix(strings.TrimSpace(strings.Replace(full, baseURL, "", 1)), "/")
splitStr := strings.Split(full, "/")
if len(splitStr) == 1 {
owner := splitStr[0]
return owner, ""
} else if len(splitStr) != 2 && splitStr[2] != "pull-requests" && splitStr[2] != "issues" {
return "", ""
}
owner := splitStr[0]
repo := splitStr[1]
return owner, repo
}
// getToDoDisplayText returns the text to be displayed in todo listings.
func getToDoDisplayText(baseURL, title, url, notifType string) string {
owner, repo := parseOwnerAndRepo(url, baseURL)
repoURL := fmt.Sprintf("%s%s/%s", baseURL, owner, repo)
repoWords := strings.Split(repo, "-")
if len(repo) > 20 && len(repoWords) > 1 {
repo = "..." + repoWords[len(repoWords)-1]
}
repoPart := fmt.Sprintf("[%s/%s](%s)", owner, repo, repoURL)
if len(title) > 80 {
title = strings.TrimSpace(title[:80]) + "..."
}
titlePart := fmt.Sprintf("[%s](%s)", title, url)
if notifType == "" {
return fmt.Sprintf("* %s %s\n", repoPart, titlePart)
}
return fmt.Sprintf("* %s %s %s\n", repoPart, notifType, titlePart)
}
func fullNameFromOwnerAndRepo(owner, repo string) string {
return fmt.Sprintf("%s/%s", owner, repo)
}