From 7ae13462d769ba77174b4b6144895e241b29e793 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 21 Nov 2024 20:17:47 +0100 Subject: [PATCH] feat: Detect contributors for squashed commits (#813) --- R/parse-news-items.R | 59 ++++++++++--------- tests/testthat/_snaps/parse-news-items.md | 27 ++++++--- .../testthat/_snaps/parse-news-items/NEWS.md | 1 - tests/testthat/helper-conventional-commits.R | 1 - tests/testthat/test-parse-news-items.R | 17 +++++- 5 files changed, 65 insertions(+), 40 deletions(-) diff --git a/R/parse-news-items.R b/R/parse-news-items.R index c29e9e315..406f80c30 100644 --- a/R/parse-news-items.R +++ b/R/parse-news-items.R @@ -76,19 +76,8 @@ extract_newsworthy_items <- function(message) { return(tibble::tibble()) } - # Merge messages - if (is_merge_commit(message)) { - return(parse_merge_commit(message)) - } - - # Conventional commits messages - if (is_conventional_commit(message)) { - return(parse_conventional_commit(message)) - } - - # Bullets messages - # There can be several bullets per message! - parse_bullet_commit(message) + # Calls parse_conventional_commit() or parse_bullet_commit() + parse_merge_commit(message) } # Bullet commits ------ @@ -250,20 +239,26 @@ add_squash_info <- function(description) { parse_merge_commit <- function(message) { pr_data <- harvest_pr_data(message) pr_number <- pr_data$pr_number - pr_numbers <- toString(c(unlist(pr_data$issue_numbers), paste0("#", pr_number))) - title <- if (is.na(pr_data$title)) { - sprintf("- PLACEHOLDER https://github.com/%s/pull/%s", github_slug(), pr_number) + if (is.na(pr_number)) { + title <- pr_data$title + description <- message } else { - pr_data$title - } - ctb <- if (is.na(pr_data$external_ctb)) { - "" - } else { - sprintf("@%s, ", pr_data$external_ctb) - } + pr_numbers <- toString(c(unlist(pr_data$issue_numbers), if (!is.na(pr_number)) paste0("#", pr_number))) + + title <- if (is.na(pr_data$title)) { + sprintf("- PLACEHOLDER https://github.com/%s/pull/%s", github_slug(), pr_number) + } else { + pr_data$title + } + ctb <- if (is.na(pr_data$external_ctb)) { + "" + } else { + sprintf("@%s, ", pr_data$external_ctb) + } - description <- sprintf("%s (%s%s).", title, ctb, pr_numbers) + description <- sprintf("%s (%s%s).", title, ctb, pr_numbers) + } if (is_conventional_commit(title)) { return(parse_conventional_commit(description)) @@ -274,14 +269,22 @@ parse_merge_commit <- function(message) { is_merge_commit <- function(message) { - grepl("^Merge pull request #([0-9]*) from", message) + grepl("(^Merge pull request #([0-9]+) from)|( [(]#[0-9]+[)]\n)", message) } harvest_pr_data <- function(message) { - check_gh_pat(NULL) + pr_number <- regmatches(message, regexpr("(?<=#)[0-9]*", message, perl = TRUE)) + + if (length(pr_number) == 0) { + return(tibble::tibble( + title = strsplit(message, "\n")[[1]][[1]], + pr_number = NA_integer_, + issue_numbers = list(numeric()), + external_ctb = NA_character_, + )) + } - pr_number <- regmatches(message, regexpr("#[0-9]*", message)) - pr_number <- sub("#", "", pr_number) + check_gh_pat(NULL) slug <- github_slug() org <- sub("/.*", "", slug) diff --git a/tests/testthat/_snaps/parse-news-items.md b/tests/testthat/_snaps/parse-news-items.md index e01af3d61..d03210b4e 100644 --- a/tests/testthat/_snaps/parse-news-items.md +++ b/tests/testthat/_snaps/parse-news-items.md @@ -14,23 +14,23 @@ Code extract_newsworthy_items( - "- blop (#42)\n\nCo-authored-by: Person ()\nCo-authored-by: Someone Else ()") + "- blop\n\nCo-authored-by: Person ()\nCo-authored-by: Someone Else ()") Output # A tibble: 1 x 4 - description type breaking scope - - 1 blop (@person, @else, #42). Uncategorized FALSE NA + description type breaking scope + + 1 blop (@person, @else). Uncategorized FALSE NA --- Code extract_newsworthy_items( - "feat: blop (#42)\n\nCo-authored-by: Person ()") + "feat: blop\n\nCo-authored-by: Person ()") Output # A tibble: 1 x 4 - description type breaking scope - - 1 blop (@person, #42) Features FALSE NA + description type breaking scope + + 1 blop (@person) Features FALSE NA # Can parse PR merge commits @@ -99,3 +99,14 @@ } ] +# Can parse PR squash commits - linked issues + + [ + { + "description": "improve bump_version() (error) messages (#153, cynkra/dm#325, #328).", + "type": "Features", + "breaking": false, + "scope": "NA" + } + ] + diff --git a/tests/testthat/_snaps/parse-news-items/NEWS.md b/tests/testthat/_snaps/parse-news-items/NEWS.md index 72d457fc4..d534c376a 100644 --- a/tests/testthat/_snaps/parse-news-items/NEWS.md +++ b/tests/testthat/_snaps/parse-news-items/NEWS.md @@ -13,7 +13,6 @@ obsolete now. Reviewed-by: Z - Refs: #123 ## Features diff --git a/tests/testthat/helper-conventional-commits.R b/tests/testthat/helper-conventional-commits.R index f1f7f5de8..c23a47772 100644 --- a/tests/testthat/helper-conventional-commits.R +++ b/tests/testthat/helper-conventional-commits.R @@ -32,7 +32,6 @@ Remove timeouts which were used to mitigate the racing issue but are obsolete now. Reviewed-by: Z -Refs: #123 --- Also tweak the CI workflow accordingly. :sweat_smile:", diff --git a/tests/testthat/test-parse-news-items.R b/tests/testthat/test-parse-news-items.R index c2089d7d2..8ec007c42 100644 --- a/tests/testthat/test-parse-news-items.R +++ b/tests/testthat/test-parse-news-items.R @@ -46,13 +46,13 @@ test_that("Can parse Co-authored-by", { expect_snapshot( extract_newsworthy_items( - "- blop (#42)\n\nCo-authored-by: Person ()\nCo-authored-by: Someone Else ()" + "- blop\n\nCo-authored-by: Person ()\nCo-authored-by: Someone Else ()" ) ) expect_snapshot( extract_newsworthy_items( - "feat: blop (#42)\n\nCo-authored-by: Person ()" + "feat: blop\n\nCo-authored-by: Person ()" ) ) }) @@ -134,3 +134,16 @@ test_that("Can parse PR merge commits - other error", { ) }) }) + +test_that("Can parse PR squash commits - linked issues", { + withr::local_envvar("FLEDGE_TEST_GITHUB_SLUG" = "cynkra/fledge") + + with_mock_dir("pr2", { + withr::local_envvar("FLEDGE_YES_INTERNET_TEST" = "yes") + withr::local_envvar("FLEDGE_TEST_SCOPES" = "bla") + withr::local_envvar("GITHUB_PAT" = "ghp_111111111111111111111111111111111111111") + expect_snapshot_tibble( + extract_newsworthy_items("feat: blop (#328)\n") + ) + }) +})