github.com/Trim21/git-chglog@v0.0.0-20200414013904-db796966b373/processor.go (about) 1 package chglog 2 3 import ( 4 "regexp" 5 "strings" 6 ) 7 8 // Processor hooks the internal processing of `Generator`, it is possible to adjust the contents 9 type Processor interface { 10 Bootstrap(*Config) 11 ProcessCommit(*Commit) *Commit 12 } 13 14 // GitHubProcessor is optimized for CHANGELOG used in GitHub 15 // 16 // The following processing is performed 17 // - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://github.com/tsuyoshiwada)) 18 // - Automatic link to references (#123 -> [#123](https://github.com/owner/repo/issues/123)) 19 type GitHubProcessor struct { 20 Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://github.com") 21 config *Config 22 reMention *regexp.Regexp 23 reIssue *regexp.Regexp 24 } 25 26 // Bootstrap ... 27 func (p *GitHubProcessor) Bootstrap(config *Config) { 28 p.config = config 29 30 if p.Host == "" { 31 p.Host = "https://github.com" 32 } else { 33 p.Host = strings.TrimRight(p.Host, "/") 34 } 35 36 p.reMention = regexp.MustCompile("@(\\w+)") 37 p.reIssue = regexp.MustCompile("(?i)(#|gh-)(\\d+)") 38 } 39 40 // ProcessCommit ... 41 func (p *GitHubProcessor) ProcessCommit(commit *Commit) *Commit { 42 commit.Header = p.addLinks(commit.Header) 43 commit.Subject = p.addLinks(commit.Subject) 44 commit.Body = p.addLinks(commit.Body) 45 46 for _, note := range commit.Notes { 47 note.Body = p.addLinks(note.Body) 48 } 49 50 if commit.Revert != nil { 51 commit.Revert.Header = p.addLinks(commit.Revert.Header) 52 } 53 54 return commit 55 } 56 57 func (p *GitHubProcessor) addLinks(input string) string { 58 repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/") 59 60 // mentions 61 input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1)") 62 63 // issues 64 input = p.reIssue.ReplaceAllString(input, "[$1$2]("+repoURL+"/issues/$2)") 65 66 return input 67 } 68 69 // GitLabProcessor is optimized for CHANGELOG used in GitLab 70 // 71 // The following processing is performed 72 // - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://gitlab.com/tsuyoshiwada)) 73 // - Automatic link to references (#123 -> [#123](https://gitlab.com/owner/repo/issues/123)) 74 type GitLabProcessor struct { 75 Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://gitlab.com") 76 config *Config 77 reMention *regexp.Regexp 78 reIssue *regexp.Regexp 79 } 80 81 // Bootstrap ... 82 func (p *GitLabProcessor) Bootstrap(config *Config) { 83 p.config = config 84 85 if p.Host == "" { 86 p.Host = "https://gitlab.com" 87 } else { 88 p.Host = strings.TrimRight(p.Host, "/") 89 } 90 91 p.reMention = regexp.MustCompile("@(\\w+)") 92 p.reIssue = regexp.MustCompile("(?i)#(\\d+)") 93 } 94 95 // ProcessCommit ... 96 func (p *GitLabProcessor) ProcessCommit(commit *Commit) *Commit { 97 commit.Header = p.addLinks(commit.Header) 98 commit.Subject = p.addLinks(commit.Subject) 99 commit.Body = p.addLinks(commit.Body) 100 101 for _, note := range commit.Notes { 102 note.Body = p.addLinks(note.Body) 103 } 104 105 if commit.Revert != nil { 106 commit.Revert.Header = p.addLinks(commit.Revert.Header) 107 } 108 109 return commit 110 } 111 112 func (p *GitLabProcessor) addLinks(input string) string { 113 repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/") 114 115 // mentions 116 input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1)") 117 118 // issues 119 input = p.reIssue.ReplaceAllString(input, "[#$1]("+repoURL+"/issues/$1)") 120 121 return input 122 } 123 124 // BitbucketProcessor is optimized for CHANGELOG used in Bitbucket 125 // 126 // The following processing is performed 127 // - Mentions automatic link (@tsuyoshiwada -> [@tsuyoshiwada](https://bitbucket.org/tsuyoshiwada/)) 128 // - Automatic link to references (#123 -> [#123](https://bitbucket.org/owner/repo/issues/123/)) 129 type BitbucketProcessor struct { 130 Host string // Host name used for link destination. Note: You must include the protocol (e.g. "https://bitbucket.org") 131 config *Config 132 reMention *regexp.Regexp 133 reIssue *regexp.Regexp 134 } 135 136 // Bootstrap ... 137 func (p *BitbucketProcessor) Bootstrap(config *Config) { 138 p.config = config 139 140 if p.Host == "" { 141 p.Host = "https://bitbucket.org" 142 } else { 143 p.Host = strings.TrimRight(p.Host, "/") 144 } 145 146 p.reMention = regexp.MustCompile("@(\\w+)") 147 p.reIssue = regexp.MustCompile("(?i)#(\\d+)") 148 } 149 150 // ProcessCommit ... 151 func (p *BitbucketProcessor) ProcessCommit(commit *Commit) *Commit { 152 commit.Header = p.addLinks(commit.Header) 153 commit.Subject = p.addLinks(commit.Subject) 154 commit.Body = p.addLinks(commit.Body) 155 156 for _, note := range commit.Notes { 157 note.Body = p.addLinks(note.Body) 158 } 159 160 if commit.Revert != nil { 161 commit.Revert.Header = p.addLinks(commit.Revert.Header) 162 } 163 164 return commit 165 } 166 167 func (p *BitbucketProcessor) addLinks(input string) string { 168 repoURL := strings.TrimRight(p.config.Info.RepositoryURL, "/") 169 170 // mentions 171 input = p.reMention.ReplaceAllString(input, "[@$1]("+p.Host+"/$1/)") 172 173 // issues 174 input = p.reIssue.ReplaceAllString(input, "[#$1]("+repoURL+"/issues/$1/)") 175 176 return input 177 }