github.com/git-chglog/git-chglog@v0.15.5-0.20240126074033-6a6993d52d69/commit_parser_test.go (about) 1 package chglog 2 3 import ( 4 "errors" 5 "fmt" 6 "os" 7 "path/filepath" 8 "testing" 9 "time" 10 11 agjira "github.com/andygrunwald/go-jira" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestCommitParserParse(t *testing.T) { 16 assert := assert.New(t) 17 assert.True(true) 18 19 mock := &mockClient{ 20 ReturnExec: func(subcmd string, args ...string) (string, error) { 21 if subcmd != "log" { 22 return "", errors.New("") 23 } 24 25 bytes, _ := os.ReadFile(filepath.Join("testdata", "gitlog.txt")) 26 27 return string(bytes), nil 28 }, 29 } 30 31 parser := newCommitParser(NewLogger(os.Stdout, os.Stderr, false, true), 32 mock, nil, &Config{ 33 Options: &Options{ 34 CommitFilters: map[string][]string{ 35 "Type": { 36 "feat", 37 "fix", 38 "perf", 39 "refactor", 40 }, 41 }, 42 HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$", 43 HeaderPatternMaps: []string{ 44 "Type", 45 "Scope", 46 "Subject", 47 }, 48 IssuePrefix: []string{ 49 "#", 50 "gh-", 51 }, 52 RefActions: []string{ 53 "close", 54 "closes", 55 "closed", 56 "fix", 57 "fixes", 58 "fixed", 59 "resolve", 60 "resolves", 61 "resolved", 62 }, 63 MergePattern: "^Merge pull request #(\\d+) from (.*)$", 64 MergePatternMaps: []string{ 65 "Ref", 66 "Source", 67 }, 68 RevertPattern: "^Revert \"([\\s\\S]*)\"$", 69 RevertPatternMaps: []string{ 70 "Header", 71 }, 72 NoteKeywords: []string{ 73 "BREAKING CHANGE", 74 }, 75 }, 76 }) 77 78 commits, err := parser.Parse("HEAD") 79 assert.Nil(err) 80 assert.Equal([]*Commit{ 81 { 82 Hash: &Hash{ 83 Long: "65cf1add9735dcc4810dda3312b0792236c97c4e", 84 Short: "65cf1add", 85 }, 86 Author: &Author{ 87 Name: "tsuyoshi wada", 88 Email: "mail@example.com", 89 Date: time.Unix(int64(1514808000), 0), 90 }, 91 Committer: &Committer{ 92 Name: "tsuyoshi wada", 93 Email: "mail@example.com", 94 Date: time.Unix(int64(1514808000), 0), 95 }, 96 Merge: nil, 97 Revert: nil, 98 Refs: []*Ref{ 99 { 100 Action: "", 101 Ref: "123", 102 Source: "", 103 }, 104 }, 105 Notes: []*Note{}, 106 Mentions: []string{}, 107 Header: "feat(*): Add new feature #123", 108 Type: "feat", 109 Scope: "*", 110 Subject: "Add new feature #123", 111 Body: "", 112 TrimmedBody: "", 113 }, 114 { 115 Hash: &Hash{ 116 Long: "14ef0b6d386c5432af9292eab3c8314fa3001bc7", 117 Short: "14ef0b6d", 118 }, 119 Author: &Author{ 120 Name: "tsuyoshi wada", 121 Email: "mail@example.com", 122 Date: time.Unix(int64(1515153600), 0), 123 }, 124 Committer: &Committer{ 125 Name: "tsuyoshi wada", 126 Email: "mail@example.com", 127 Date: time.Unix(int64(1515153600), 0), 128 }, 129 Merge: &Merge{ 130 Ref: "3", 131 Source: "username/branchname", 132 }, 133 Revert: nil, 134 Refs: []*Ref{ 135 { 136 Action: "", 137 Ref: "3", 138 Source: "", 139 }, 140 { 141 Action: "Fixes", 142 Ref: "3", 143 Source: "", 144 }, 145 { 146 Action: "Closes", 147 Ref: "1", 148 Source: "", 149 }, 150 }, 151 Notes: []*Note{ 152 { 153 Title: "BREAKING CHANGE", 154 Body: "This is breaking point message.", 155 }, 156 }, 157 Mentions: []string{}, 158 Header: "Merge pull request #3 from username/branchname", 159 Type: "", 160 Scope: "", 161 Subject: "", 162 Body: `This is body message. 163 164 Fixes #3 165 166 Closes #1 167 168 BREAKING CHANGE: This is breaking point message.`, 169 TrimmedBody: `This is body message.`, 170 }, 171 { 172 Hash: &Hash{ 173 Long: "809a8280ffd0dadb0f4e7ba9fc835e63c37d6af6", 174 Short: "809a8280", 175 }, 176 Author: &Author{ 177 Name: "tsuyoshi wada", 178 Email: "mail@example.com", 179 Date: time.Unix(int64(1517486400), 0), 180 }, 181 Committer: &Committer{ 182 Name: "tsuyoshi wada", 183 Email: "mail@example.com", 184 Date: time.Unix(int64(1517486400), 0), 185 }, 186 Merge: nil, 187 Revert: nil, 188 Refs: []*Ref{}, 189 Notes: []*Note{}, 190 Mentions: []string{ 191 "tsuyoshiwada", 192 "hogefuga", 193 "FooBarBaz", 194 }, 195 Header: "fix(controller): Fix cors configure", 196 Type: "fix", 197 Scope: "controller", 198 Subject: "Fix cors configure", 199 Body: `Has mention body 200 201 @tsuyoshiwada 202 @hogefuga 203 @FooBarBaz`, 204 TrimmedBody: `Has mention body`, 205 }, 206 { 207 Hash: &Hash{ 208 Long: "74824d6bd1470b901ec7123d13a76a1b8938d8d0", 209 Short: "74824d6b", 210 }, 211 Author: &Author{ 212 Name: "tsuyoshi wada", 213 Email: "mail@example.com", 214 Date: time.Unix(int64(1517488587), 0), 215 }, 216 Committer: &Committer{ 217 Name: "tsuyoshi wada", 218 Email: "mail@example.com", 219 Date: time.Unix(int64(1517488587), 0), 220 }, 221 Merge: nil, 222 Revert: nil, 223 Refs: []*Ref{ 224 { 225 Action: "Fixes", 226 Ref: "123", 227 Source: "", 228 }, 229 { 230 Action: "Closes", 231 Ref: "456", 232 Source: "username/repository", 233 }, 234 }, 235 Notes: []*Note{ 236 { 237 Title: "BREAKING CHANGE", 238 Body: fmt.Sprintf(`This is multiline breaking change note. 239 It is treated as the body of the Note until a mention or reference appears. 240 241 We also allow blank lines :) 242 243 Example: 244 245 %sjavascript 246 import { Controller } from 'hoge-fuga'; 247 248 @autobind 249 class MyController extends Controller { 250 constructor() { 251 super(); 252 } 253 } 254 %s`, "```", "```"), 255 }, 256 }, 257 Mentions: []string{}, 258 Header: "fix(model): Remove hoge attributes", 259 Type: "fix", 260 Scope: "model", 261 Subject: "Remove hoge attributes", 262 Body: fmt.Sprintf(`This mixed body message. 263 264 BREAKING CHANGE: 265 This is multiline breaking change note. 266 It is treated as the body of the Note until a mention or reference appears. 267 268 We also allow blank lines :) 269 270 Example: 271 272 %sjavascript 273 import { Controller } from 'hoge-fuga'; 274 275 @autobind 276 class MyController extends Controller { 277 constructor() { 278 super(); 279 } 280 } 281 %s 282 283 Fixes #123 284 Closes username/repository#456`, "```", "```"), 285 TrimmedBody: `This mixed body message.`, 286 }, 287 { 288 Hash: &Hash{ 289 Long: "123456789735dcc4810dda3312b0792236c97c4e", 290 Short: "12345678", 291 }, 292 Author: &Author{ 293 Name: "tsuyoshi wada", 294 Email: "mail@example.com", 295 Date: time.Unix(int64(1517488587), 0), 296 }, 297 Committer: &Committer{ 298 Name: "tsuyoshi wada", 299 Email: "mail@example.com", 300 Date: time.Unix(int64(1517488587), 0), 301 }, 302 Merge: nil, 303 Revert: &Revert{ 304 Header: "fix(core): commit message", 305 }, 306 Refs: []*Ref{}, 307 Notes: []*Note{}, 308 Mentions: []string{}, 309 Header: "Revert \"fix(core): commit message\"", 310 Type: "", 311 Scope: "", 312 Subject: "", 313 Body: "This reverts commit f755db78dcdf461dc42e709b3ab728ceba353d1d.", 314 TrimmedBody: "This reverts commit f755db78dcdf461dc42e709b3ab728ceba353d1d.", 315 }, 316 }, commits) 317 } 318 319 type mockJiraClient struct { 320 } 321 322 func (jira mockJiraClient) GetJiraIssue(id string) (*agjira.Issue, error) { 323 return &agjira.Issue{ 324 ID: id, 325 Fields: &agjira.IssueFields{ 326 Expand: "", 327 Type: agjira.IssueType{Name: "Story"}, 328 Project: agjira.Project{}, 329 Resolution: nil, 330 Priority: nil, 331 Resolutiondate: agjira.Time{}, 332 Created: agjira.Time{}, 333 Duedate: agjira.Date{}, 334 Watches: nil, 335 Assignee: nil, 336 Updated: agjira.Time{}, 337 Description: fmt.Sprintf("description of %s", id), 338 Summary: fmt.Sprintf("summary of %s", id), 339 Creator: nil, 340 Reporter: nil, 341 Components: nil, 342 Status: nil, 343 Progress: nil, 344 AggregateProgress: nil, 345 TimeTracking: nil, 346 TimeSpent: 0, 347 TimeEstimate: 0, 348 TimeOriginalEstimate: 0, 349 Worklog: nil, 350 IssueLinks: nil, 351 Comments: nil, 352 FixVersions: nil, 353 AffectsVersions: nil, 354 Labels: []string{"GA"}, 355 Subtasks: nil, 356 Attachments: nil, 357 Epic: nil, 358 Sprint: nil, 359 Parent: nil, 360 AggregateTimeOriginalEstimate: 0, 361 AggregateTimeSpent: 0, 362 AggregateTimeEstimate: 0, 363 Unknowns: nil, 364 }, 365 }, nil 366 } 367 368 func TestCommitParserParseWithJira(t *testing.T) { 369 assert := assert.New(t) 370 assert.True(true) 371 372 mock := &mockClient{ 373 ReturnExec: func(subcmd string, args ...string) (string, error) { 374 if subcmd != "log" { 375 return "", errors.New("") 376 } 377 378 bytes, _ := os.ReadFile(filepath.Join("testdata", "gitlog_jira.txt")) 379 380 return string(bytes), nil 381 }, 382 } 383 384 parser := newCommitParser(NewLogger(os.Stdout, os.Stderr, false, true), 385 mock, mockJiraClient{}, &Config{ 386 Options: &Options{ 387 CommitFilters: map[string][]string{ 388 "Type": { 389 "feat", 390 "fix", 391 "perf", 392 "refactor", 393 }, 394 }, 395 HeaderPattern: "^(?:(\\w*)|(?:\\[(.*)\\])?)\\:\\s(.*)$", 396 HeaderPatternMaps: []string{ 397 "Type", 398 "JiraIssueID", 399 "Subject", 400 }, 401 JiraTypeMaps: map[string]string{ 402 "Story": "feat", 403 }, 404 }, 405 }) 406 407 commits, err := parser.Parse("HEAD") 408 assert.Nil(err) 409 commit := commits[0] 410 assert.Equal(commit.JiraIssueID, "JIRA-1111") 411 assert.Equal(commit.JiraIssue.Type, "Story") 412 assert.Equal(commit.JiraIssue.Summary, "summary of JIRA-1111") 413 assert.Equal(commit.JiraIssue.Description, "description of JIRA-1111") 414 assert.Equal(commit.JiraIssue.Labels, []string{"GA"}) 415 assert.Equal(commit.Type, "feat") 416 }