forked from git-chglog/git-chglog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.go
177 lines (139 loc) · 4.73 KB
/
processor.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
package chglog
import (
"regexp"
"strings"
)
// Processor hooks the internal processing of `Generator`, it is possible to adjust the contents
type Processor interface {
Bootstrap(*Config)
ProcessCommit(*Commit) *Commit
}
// GitHubProcessor is optimized for CHANGELOG used in GitHub
//
// The following processing is performed
// - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://github.com/tsuyoshiwada))
// - Automatic link to references (#123 -> [#123](https://github.com/owner/repo/issues/123))
type GitHubProcessor struct {
Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://github.com")
config *Config
reMention *regexp.Regexp
reIssue *regexp.Regexp
}
// Bootstrap ...
func (p *GitHubProcessor) Bootstrap(config *Config) {
p.config = config
if p.Host == "" {
p.Host = "https://github.com"
} else {
p.Host = strings.TrimRight(p.Host, "/")
}
p.reMention = regexp.MustCompile("@(\\w+)")
p.reIssue = regexp.MustCompile("(?i)(#|gh-)(\\d+)")
}
// ProcessCommit ...
func (p *GitHubProcessor) ProcessCommit(commit *Commit) *Commit {
commit.Header = p.addLinks(commit.Header)
commit.Subject = p.addLinks(commit.Subject)
commit.Body = p.addLinks(commit.Body)
for _, note := range commit.Notes {
note.Body = p.addLinks(note.Body)
}
if commit.Revert != nil {
commit.Revert.Header = p.addLinks(commit.Revert.Header)
}
return commit
}
func (p *GitHubProcessor) addLinks(input string) string {
repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/")
// mentions
input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1)")
// issues
input = p.reIssue.ReplaceAllString(input, "[$1$2]("+repoURL+"/issues/$2)")
return input
}
// GitLabProcessor is optimized for CHANGELOG used in GitLab
//
// The following processing is performed
// - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://gitlab.com/tsuyoshiwada))
// - Automatic link to references (#123 -> [#123](https://gitlab.com/owner/repo/issues/123))
type GitLabProcessor struct {
Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://gitlab.com")
config *Config
reMention *regexp.Regexp
reIssue *regexp.Regexp
}
// Bootstrap ...
func (p *GitLabProcessor) Bootstrap(config *Config) {
p.config = config
if p.Host == "" {
p.Host = "https://gitlab.com"
} else {
p.Host = strings.TrimRight(p.Host, "/")
}
p.reMention = regexp.MustCompile("@(\\w+)")
p.reIssue = regexp.MustCompile("(?i)#(\\d+)")
}
// ProcessCommit ...
func (p *GitLabProcessor) ProcessCommit(commit *Commit) *Commit {
commit.Header = p.addLinks(commit.Header)
commit.Subject = p.addLinks(commit.Subject)
commit.Body = p.addLinks(commit.Body)
for _, note := range commit.Notes {
note.Body = p.addLinks(note.Body)
}
if commit.Revert != nil {
commit.Revert.Header = p.addLinks(commit.Revert.Header)
}
return commit
}
func (p *GitLabProcessor) addLinks(input string) string {
repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/")
// mentions
input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1)")
// issues
input = p.reIssue.ReplaceAllString(input, "[#$1]("+repoURL+"/issues/$1)")
return input
}
// BitbucketProcessor is optimized for CHANGELOG used in Bitbucket
//
// The following processing is performed
// - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://bitbucket.org/tsuyoshiwada/))
// - Automatic link to references (#123 -> [#123](https://bitbucket.org/owner/repo/issues/123/))
type BitbucketProcessor struct {
Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://bitbucket.org")
config *Config
reMention *regexp.Regexp
reIssue *regexp.Regexp
}
// Bootstrap ...
func (p *BitbucketProcessor) Bootstrap(config *Config) {
p.config = config
if p.Host == "" {
p.Host = "https://bitbucket.org"
} else {
p.Host = strings.TrimRight(p.Host, "/")
}
p.reMention = regexp.MustCompile("@(\\w+)")
p.reIssue = regexp.MustCompile("(?i)#(\\d+)")
}
// ProcessCommit ...
func (p *BitbucketProcessor) ProcessCommit(commit *Commit) *Commit {
commit.Header = p.addLinks(commit.Header)
commit.Subject = p.addLinks(commit.Subject)
commit.Body = p.addLinks(commit.Body)
for _, note := range commit.Notes {
note.Body = p.addLinks(note.Body)
}
if commit.Revert != nil {
commit.Revert.Header = p.addLinks(commit.Revert.Header)
}
return commit
}
func (p *BitbucketProcessor) addLinks(input string) string {
repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/")
// mentions
input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1/)")
// issues
input = p.reIssue.ReplaceAllString(input, "[#$1]("+repoURL+"/issues/$1/)")
return input
}