github.com/fredbi/git-chglog@v0.0.0-20190706071416-d35c598eac81/cmd/git-chglog/config.go (about) 1 package main 2 3 import ( 4 "path/filepath" 5 "strings" 6 7 chglog "github.com/fredbi/git-chglog" 8 "github.com/imdario/mergo" 9 ) 10 11 // Info ... 12 type Info struct { 13 Title string `yaml:"title"` 14 RepositoryURL string `yaml:"repository_url"` 15 } 16 17 // CommitOptions ... 18 type CommitOptions struct { 19 Filters map[string][]string `yaml:"filters"` 20 SortBy string `yaml:"sort_by"` 21 MultilineCommit bool `yaml:"multiline_commit"` 22 } 23 24 // CommitGroupOptions ... 25 type CommitGroupOptions struct { 26 GroupBy string `yaml:"group_by"` 27 SortBy string `yaml:"sort_by"` 28 TitleMaps map[string]string `yaml:"title_maps"` 29 } 30 31 // PatternOptions ... 32 type PatternOptions struct { 33 Pattern string `yaml:"pattern"` 34 PatternMaps []string `yaml:"pattern_maps"` 35 } 36 37 // IssueOptions ... 38 type IssueOptions struct { 39 Prefix []string `yaml:"prefix"` 40 } 41 42 // RefOptions ... 43 type RefOptions struct { 44 Actions []string `yaml:"actions"` 45 } 46 47 // NoteOptions ... 48 type NoteOptions struct { 49 Keywords []string `yaml:"keywords"` 50 } 51 52 // Options ... 53 type Options struct { 54 Commits CommitOptions `yaml:"commits"` 55 CommitGroups CommitGroupOptions `yaml:"commit_groups"` 56 Header PatternOptions `yaml:"header"` 57 Issues IssueOptions `yaml:"issues"` 58 Refs RefOptions `yaml:"refs"` 59 Merges PatternOptions `yaml:"merges"` 60 Reverts PatternOptions `yaml:"reverts"` 61 Notes NoteOptions `yaml:"notes"` 62 } 63 64 // Config ... 65 type Config struct { 66 Bin string `yaml:"bin"` 67 Template string `yaml:"template"` 68 Style string `yaml:"style"` 69 Info Info `yaml:"info"` 70 Options Options `yaml:"options"` 71 } 72 73 // Normalize ... 74 func (config *Config) Normalize(ctx *CLIContext) error { 75 err := mergo.Merge(config, &Config{ 76 Bin: "git", 77 Template: "CHANGELOG.tpl.md", 78 Info: Info{ 79 Title: "CHANGELOG", 80 }, 81 Options: Options{ 82 Commits: CommitOptions{ 83 SortBy: "Scope", 84 }, 85 CommitGroups: CommitGroupOptions{ 86 GroupBy: "Type", 87 SortBy: "Title", 88 }, 89 }, 90 }) 91 92 if err != nil { 93 return err 94 } 95 96 config.Info.RepositoryURL = strings.TrimRight(config.Info.RepositoryURL, "/") 97 98 if !filepath.IsAbs(config.Template) { 99 config.Template = filepath.Join(filepath.Dir(ctx.ConfigPath), config.Template) 100 } 101 102 config.normalizeStyle() 103 104 return nil 105 } 106 107 // Normalize style 108 func (config *Config) normalizeStyle() { 109 switch config.Style { 110 case "github": 111 config.normalizeStyleOfGitHub() 112 case "gitlab": 113 config.normalizeStyleOfGitLab() 114 case "bitbucket": 115 config.normalizeStyleOfBitbucket() 116 } 117 } 118 119 // For GitHub 120 func (config *Config) normalizeStyleOfGitHub() { 121 opts := config.Options 122 123 if len(opts.Issues.Prefix) == 0 { 124 opts.Issues.Prefix = []string{ 125 "#", 126 "gh-", 127 } 128 } 129 130 if len(opts.Refs.Actions) == 0 { 131 opts.Refs.Actions = []string{ 132 "close", 133 "closes", 134 "closed", 135 "fix", 136 "fixes", 137 "fixed", 138 "resolve", 139 "resolves", 140 "resolved", 141 } 142 } 143 144 if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 { 145 opts.Merges.Pattern = "^Merge pull request #(\\d+) from (.*)$" 146 opts.Merges.PatternMaps = []string{ 147 "Ref", 148 "Source", 149 } 150 } 151 152 config.Options = opts 153 } 154 155 // For GitLab 156 func (config *Config) normalizeStyleOfGitLab() { 157 opts := config.Options 158 159 if len(opts.Issues.Prefix) == 0 { 160 opts.Issues.Prefix = []string{ 161 "#", 162 } 163 } 164 165 if len(opts.Refs.Actions) == 0 { 166 opts.Refs.Actions = []string{ 167 "close", 168 "closes", 169 "closed", 170 "closing", 171 "fix", 172 "fixes", 173 "fixed", 174 "fixing", 175 "resolve", 176 "resolves", 177 "resolved", 178 "resolving", 179 } 180 } 181 182 if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 { 183 opts.Merges.Pattern = "^Merge branch '.*' into '(.*)'$" 184 opts.Merges.PatternMaps = []string{ 185 "Source", 186 } 187 } 188 189 config.Options = opts 190 } 191 192 // For Bitbucket 193 func (config *Config) normalizeStyleOfBitbucket() { 194 opts := config.Options 195 196 if len(opts.Issues.Prefix) == 0 { 197 opts.Issues.Prefix = []string{ 198 "#", 199 } 200 } 201 202 if len(opts.Refs.Actions) == 0 { 203 opts.Refs.Actions = []string{ 204 "close", 205 "closes", 206 "closed", 207 "closing", 208 "fix", 209 "fixed", 210 "fixes", 211 "fixing", 212 "resolve", 213 "resolves", 214 "resolved", 215 "resolving", 216 "eopen", 217 "reopens", 218 "reopening", 219 "hold", 220 "holds", 221 "holding", 222 "wontfix", 223 "invalidate", 224 "invalidates", 225 "invalidated", 226 "invalidating", 227 "addresses", 228 "re", 229 "references", 230 "ref", 231 "refs", 232 "see", 233 } 234 } 235 236 if opts.Merges.Pattern == "" && len(opts.Merges.PatternMaps) == 0 { 237 opts.Merges.Pattern = "^Merged in (.*) \\(pull request #(\\d+)\\)$" 238 opts.Merges.PatternMaps = []string{ 239 "Source", 240 "Ref", 241 } 242 } 243 244 config.Options = opts 245 } 246 247 // Convert ... 248 func (config *Config) Convert(ctx *CLIContext) *chglog.Config { 249 info := config.Info 250 opts := config.Options 251 252 return &chglog.Config{ 253 Bin: config.Bin, 254 WorkingDir: ctx.WorkingDir, 255 Template: config.Template, 256 Info: &chglog.Info{ 257 Title: info.Title, 258 RepositoryURL: info.RepositoryURL, 259 }, 260 Options: &chglog.Options{ 261 NextTag: ctx.NextTag, 262 CommitFilters: opts.Commits.Filters, 263 CommitSortBy: opts.Commits.SortBy, 264 CommitGroupBy: opts.CommitGroups.GroupBy, 265 CommitGroupSortBy: opts.CommitGroups.SortBy, 266 CommitGroupTitleMaps: opts.CommitGroups.TitleMaps, 267 HeaderPattern: opts.Header.Pattern, 268 HeaderPatternMaps: opts.Header.PatternMaps, 269 IssuePrefix: opts.Issues.Prefix, 270 RefActions: opts.Refs.Actions, 271 MergePattern: opts.Merges.Pattern, 272 MergePatternMaps: opts.Merges.PatternMaps, 273 RevertPattern: opts.Reverts.Pattern, 274 RevertPatternMaps: opts.Reverts.PatternMaps, 275 NoteKeywords: opts.Notes.Keywords, 276 MultilineCommit: opts.Commits.MultilineCommit, 277 }, 278 } 279 }