-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgitlab_url_parser.go
220 lines (168 loc) · 4.22 KB
/
gitlab_url_parser.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
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"github.com/xanzy/go-gitlab"
"regexp"
"strconv"
"strings"
)
// GitlabURLParser represents GitLab URL parser
type GitlabURLParser struct {
baseURL string
client *gitlab.Client
}
// GitLabURLParserParams represents parameters of NewGitlabURLParser
type GitLabURLParserParams struct {
APIEndpoint string
BaseURL string
PrivateToken string
}
// NewGitlabURLParser create new GitlabURLParser instance
func NewGitlabURLParser(params *GitLabURLParserParams) (*GitlabURLParser, error) {
p := &GitlabURLParser{
baseURL: params.BaseURL,
}
p.client = gitlab.NewClient(nil, params.PrivateToken)
err := p.client.SetBaseURL(params.APIEndpoint)
if err != nil {
return nil, err
}
return p, nil
}
// FetchURL fetch GitLab url
func (p *GitlabURLParser) FetchURL(url string) (*GitLabPage, error) {
if !strings.HasPrefix(url, p.baseURL) {
return nil, nil
}
pos := len(p.baseURL)
if !strings.HasSuffix(url, "/") {
pos++
}
path := url[pos:]
// Issue URL
page, err := p.fetchIssueURL(path)
if err != nil {
return nil, err
}
if page != nil {
return page, nil
}
// MergeRequest URL
page, err = p.fetchMergeRequestURL(path)
if err != nil {
return nil, err
}
if page != nil {
return page, nil
}
// Project URL
page, err = p.fetchProjectURL(path)
if err != nil {
return nil, err
}
if page != nil {
return page, nil
}
// User URL
page, err = p.fetchUserURL(path)
if err != nil {
return nil, err
}
if page != nil {
return page, nil
}
return nil, nil
}
func (p *GitlabURLParser) fetchIssueURL(path string) (*GitLabPage, error) {
re := regexp.MustCompile("^([^/]+)/([^/]+)/issues/(\\d+)")
matched := re.FindStringSubmatch(path)
if matched == nil {
return nil, nil
}
projectName := matched[1] + "/" + matched[2]
issueID, _ := strconv.Atoi(matched[3])
issue, _, err := p.client.Issues.GetIssue(projectName, issueID)
if err != nil {
return nil, err
}
project, _, err := p.client.Projects.GetProject(projectName, nil)
if err != nil {
return nil, err
}
page := &GitLabPage{
Title: issue.Title,
Description: issue.Description,
AuthorName: issue.Author.Name,
AuthorAvatarURL: issue.Author.AvatarURL,
AvatarURL: project.AvatarURL,
}
return page, nil
}
func (p *GitlabURLParser) fetchMergeRequestURL(path string) (*GitLabPage, error) {
re := regexp.MustCompile("^([^/]+)/([^/]+)/merge_requests/(\\d+)")
matched := re.FindStringSubmatch(path)
if matched == nil {
return nil, nil
}
projectName := matched[1] + "/" + matched[2]
mrID, _ := strconv.Atoi(matched[3])
mr, _, err := p.client.MergeRequests.GetMergeRequest(projectName, mrID, nil)
if err != nil {
return nil, err
}
project, _, err := p.client.Projects.GetProject(projectName, nil)
if err != nil {
return nil, err
}
page := &GitLabPage{
Title: mr.Title,
Description: mr.Description,
AuthorName: mr.Author.Name,
AuthorAvatarURL: mr.Author.AvatarURL,
AvatarURL: project.AvatarURL,
}
return page, nil
}
func (p *GitlabURLParser) fetchProjectURL(path string) (*GitLabPage, error) {
re := regexp.MustCompile("^([^/]+)/([^/]+)/?$")
matched := re.FindStringSubmatch(path)
if matched == nil {
return nil, nil
}
project, _, err := p.client.Projects.GetProject(matched[1]+"/"+matched[2], nil)
if err != nil {
return nil, err
}
page := &GitLabPage{
Title: project.NameWithNamespace,
Description: project.Description,
AuthorName: project.Owner.Name,
AuthorAvatarURL: project.Owner.AvatarURL,
AvatarURL: project.AvatarURL,
}
return page, nil
}
func (p *GitlabURLParser) fetchUserURL(path string) (*GitLabPage, error) {
re := regexp.MustCompile("^([^/]+)/?$")
matched := re.FindStringSubmatch(path)
if matched == nil {
return nil, nil
}
username := matched[1]
users, _, err := p.client.Users.ListUsers(&gitlab.ListUsersOptions{Username: &username})
if err != nil {
return nil, err
}
if len(users) < 1 {
return nil, fmt.Errorf("NotFound user: %s", username)
}
user := users[0]
page := &GitLabPage{
Title: user.Name,
Description: user.Name,
AuthorName: user.Name,
AuthorAvatarURL: user.AvatarURL,
AvatarURL: user.AvatarURL,
}
return page, nil
}