feat: add commit footers for pull merge #109

closed
opened by dvjn.dev targeting master from dvjn.dev/core: push-rwutqnoxqxlr
Changed files
+282 -1
appview
pulls
patchutil
+6
patchutil/patchutil.go
··· 316 316 317 317 return nd 318 318 } 319 + 320 + // add commit message footers to a format-patch 321 + func AddCommitMessageFooters(patch string, footers string) string { 322 + re := regexp.MustCompile(`\n(Subject: )([\s\S]*?)(---\n|\ndiff --git )`) 323 + return re.ReplaceAllString(patch, "\n${1}${2}\n"+footers+"\n${3}") 324 + }
+265
patchutil/patchutil_test.go
··· 322 322 }) 323 323 } 324 324 } 325 + 326 + func TestAddCommmitMessageFooters(t *testing.T) { 327 + tests := []struct { 328 + name string 329 + input string 330 + footers string 331 + expected string 332 + }{ 333 + { 334 + name: "Single patch", 335 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 336 + From: Author <author@example.com> 337 + Date: Wed, 16 Apr 2025 11:01:00 +0300 338 + Subject: [PATCH] Example patch 339 + 340 + more message body 341 + 342 + --- 343 + file.txt | 2 +- 344 + 1 file changed, 1 insertions(+), 1 deletions(-) 345 + 346 + diff --git a/file.txt b/file.txt 347 + index 123456..789012 100644 348 + --- a/file.txt 349 + +++ b/file.txt 350 + @@ -1 +1 @@ 351 + -old content 352 + +new content 353 + -- 354 + 2.48.1`, 355 + footers: `Pull-Id: Author <author@example.com> 356 + Merge-Request: 123`, 357 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 358 + From: Author <author@example.com> 359 + Date: Wed, 16 Apr 2025 11:01:00 +0300 360 + Subject: [PATCH] Example patch 361 + 362 + more message body 363 + 364 + 365 + Pull-Id: Author <author@example.com> 366 + Merge-Request: 123 367 + --- 368 + file.txt | 2 +- 369 + 1 file changed, 1 insertions(+), 1 deletions(-) 370 + 371 + diff --git a/file.txt b/file.txt 372 + index 123456..789012 100644 373 + --- a/file.txt 374 + +++ b/file.txt 375 + @@ -1 +1 @@ 376 + -old content 377 + +new content 378 + -- 379 + 2.48.1`, 380 + }, 381 + { 382 + name: "Two patches with --no-stat", 383 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 384 + From: Author <author@example.com> 385 + Date: Wed, 16 Apr 2025 11:01:00 +0300 386 + Subject: [PATCH 1/2] First patch 387 + 388 + more message body 389 + 390 + --- 391 + file.txt | 2 +- 392 + 1 file changed, 1 insertions(+), 1 deletions(-) 393 + 394 + diff --git a/file1.txt b/file1.txt 395 + index 123456..789012 100644 396 + --- a/file1.txt 397 + +++ b/file1.txt 398 + @@ -1 +1 @@ 399 + -old content 400 + +new content 401 + -- 402 + 2.48.1 403 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 404 + From: Author <author@example.com> 405 + Date: Wed, 16 Apr 2025 11:03:11 +0300 406 + Subject: [PATCH 2/2] Second patch 407 + 408 + more message body 409 + 410 + --- 411 + file.txt | 2 +- 412 + 1 file changed, 1 insertions(+), 1 deletions(-) 413 + 414 + diff --git a/file2.txt b/file2.txt 415 + index abcdef..ghijkl 100644 416 + --- a/file2.txt 417 + +++ b/file2.txt 418 + @@ -1 +1 @@ 419 + -foo bar 420 + +baz qux 421 + -- 422 + 2.48.1`, 423 + footers: `Pull-Id: Author <author@example.com> 424 + Merge-Request: 123`, 425 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 426 + From: Author <author@example.com> 427 + Date: Wed, 16 Apr 2025 11:01:00 +0300 428 + Subject: [PATCH 1/2] First patch 429 + 430 + more message body 431 + 432 + 433 + Pull-Id: Author <author@example.com> 434 + Merge-Request: 123 435 + --- 436 + file.txt | 2 +- 437 + 1 file changed, 1 insertions(+), 1 deletions(-) 438 + 439 + diff --git a/file1.txt b/file1.txt 440 + index 123456..789012 100644 441 + --- a/file1.txt 442 + +++ b/file1.txt 443 + @@ -1 +1 @@ 444 + -old content 445 + +new content 446 + -- 447 + 2.48.1 448 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 449 + From: Author <author@example.com> 450 + Date: Wed, 16 Apr 2025 11:03:11 +0300 451 + Subject: [PATCH 2/2] Second patch 452 + 453 + more message body 454 + 455 + 456 + Pull-Id: Author <author@example.com> 457 + Merge-Request: 123 458 + --- 459 + file.txt | 2 +- 460 + 1 file changed, 1 insertions(+), 1 deletions(-) 461 + 462 + diff --git a/file2.txt b/file2.txt 463 + index abcdef..ghijkl 100644 464 + --- a/file2.txt 465 + +++ b/file2.txt 466 + @@ -1 +1 @@ 467 + -foo bar 468 + +baz qux 469 + -- 470 + 2.48.1`, 471 + }, 472 + { 473 + name: "Single patch with no stat", 474 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 475 + From: Author <author@example.com> 476 + Date: Wed, 16 Apr 2025 11:01:00 +0300 477 + Subject: [PATCH] Example patch 478 + 479 + diff --git a/file.txt b/file.txt 480 + index 123456..789012 100644 481 + --- a/file.txt 482 + +++ b/file.txt 483 + @@ -1 +1 @@ 484 + -old content 485 + +new content 486 + -- 487 + 2.48.1`, 488 + footers: `Pull-Id: Author <author@example.com> 489 + Merge-Request: 123`, 490 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 491 + From: Author <author@example.com> 492 + Date: Wed, 16 Apr 2025 11:01:00 +0300 493 + Subject: [PATCH] Example patch 494 + 495 + Pull-Id: Author <author@example.com> 496 + Merge-Request: 123 497 + 498 + diff --git a/file.txt b/file.txt 499 + index 123456..789012 100644 500 + --- a/file.txt 501 + +++ b/file.txt 502 + @@ -1 +1 @@ 503 + -old content 504 + +new content 505 + -- 506 + 2.48.1`, 507 + }, 508 + { 509 + name: "Two patches with no stat", 510 + input: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 511 + From: Author <author@example.com> 512 + Date: Wed, 16 Apr 2025 11:01:00 +0300 513 + Subject: [PATCH 1/2] First patch 514 + 515 + with long message body 516 + 517 + diff --git a/file1.txt b/file1.txt 518 + index 123456..789012 100644 519 + --- a/file1.txt 520 + +++ b/file1.txt 521 + @@ -1 +1 @@ 522 + -old content 523 + +new content 524 + -- 525 + 2.48.1 526 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 527 + From: Author <author@example.com> 528 + Date: Wed, 16 Apr 2025 11:03:11 +0300 529 + Subject: [PATCH 2/2] Second patch 530 + 531 + diff --git a/file2.txt b/file2.txt 532 + index abcdef..ghijkl 100644 533 + --- a/file2.txt 534 + +++ b/file2.txt 535 + @@ -1 +1 @@ 536 + -foo bar 537 + +baz qux 538 + -- 539 + 2.48.1`, 540 + footers: `Pull-Id: Author <author@example.com> 541 + Merge-Request: 123`, 542 + expected: `From 3c5035488318164b81f60fe3adcd6c9199d76331 Mon Sep 17 00:00:00 2001 543 + From: Author <author@example.com> 544 + Date: Wed, 16 Apr 2025 11:01:00 +0300 545 + Subject: [PATCH 1/2] First patch 546 + 547 + with long message body 548 + 549 + Pull-Id: Author <author@example.com> 550 + Merge-Request: 123 551 + 552 + diff --git a/file1.txt b/file1.txt 553 + index 123456..789012 100644 554 + --- a/file1.txt 555 + +++ b/file1.txt 556 + @@ -1 +1 @@ 557 + -old content 558 + +new content 559 + -- 560 + 2.48.1 561 + From a9529f3b3a653329a5268f0f4067225480207e3c Mon Sep 17 00:00:00 2001 562 + From: Author <author@example.com> 563 + Date: Wed, 16 Apr 2025 11:03:11 +0300 564 + Subject: [PATCH 2/2] Second patch 565 + 566 + Pull-Id: Author <author@example.com> 567 + Merge-Request: 123 568 + 569 + diff --git a/file2.txt b/file2.txt 570 + index abcdef..ghijkl 100644 571 + --- a/file2.txt 572 + +++ b/file2.txt 573 + @@ -1 +1 @@ 574 + -foo bar 575 + +baz qux 576 + -- 577 + 2.48.1`, 578 + }, 579 + } 580 + 581 + for _, tt := range tests { 582 + t.Run(tt.name, func(t *testing.T) { 583 + result := AddCommitMessageFooters(tt.input, tt.footers) 584 + if result != tt.expected { 585 + t.Errorf("Got:\n========\n%v\n========\nExpected:\n========\n%v\n========\n", result, tt.expected) 586 + } 587 + }) 588 + } 589 + }
+11 -1
appview/pulls/pulls.go
··· 1915 1915 return 1916 1916 } 1917 1917 1918 + actor := s.oauth.GetUser(r) // no need to check for nil as this is an authenticated request 1919 + 1920 + footers := strings.Join([]string{ 1921 + fmt.Sprintf("Pull-id: %s", pull.PullAt()), 1922 + fmt.Sprintf("Merged-by: %s", actor.Did), 1923 + }, "\n") 1924 + 1925 + pullBody := pull.Body + "\n\n" + footers 1926 + patch = patchutil.AddCommitMessageFooters(patch, footers) 1927 + 1918 1928 // Merge the pull request 1919 - resp, err := ksClient.Merge([]byte(patch), f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.Title, pull.Body, ident.Handle.String(), email.Address) 1929 + resp, err := ksClient.Merge([]byte(patch), f.OwnerDid(), f.RepoName, pull.TargetBranch, pull.Title, pullBody, ident.Handle.String(), email.Address) 1920 1930 if err != nil { 1921 1931 log.Printf("failed to merge pull request: %s", err) 1922 1932 s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.")