Skip to content

Commit

Permalink
feat: Detect contributors for squashed commits (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr authored Nov 21, 2024
1 parent 0c102ad commit 7ae1346
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 40 deletions.
59 changes: 31 additions & 28 deletions R/parse-news-items.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ------
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand Down
27 changes: 19 additions & 8 deletions tests/testthat/_snaps/parse-news-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@

Code
extract_newsworthy_items(
"- blop (#42)\n\nCo-authored-by: Person (<[email protected]>)\nCo-authored-by: Someone Else (<[email protected]>)")
"- blop\n\nCo-authored-by: Person (<[email protected]>)\nCo-authored-by: Someone Else (<[email protected]>)")
Output
# A tibble: 1 x 4
description type breaking scope
<chr> <chr> <lgl> <lgl>
1 blop (@person, @else, #42). Uncategorized FALSE NA
description type breaking scope
<chr> <chr> <lgl> <lgl>
1 blop (@person, @else). Uncategorized FALSE NA

---

Code
extract_newsworthy_items(
"feat: blop (#42)\n\nCo-authored-by: Person (<[email protected]>)")
"feat: blop\n\nCo-authored-by: Person (<[email protected]>)")
Output
# A tibble: 1 x 4
description type breaking scope
<chr> <chr> <lgl> <lgl>
1 blop (@person, #42) Features FALSE NA
description type breaking scope
<chr> <chr> <lgl> <lgl>
1 blop (@person) Features FALSE NA

# Can parse PR merge commits

Expand Down Expand Up @@ -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"
}
]

1 change: 0 additions & 1 deletion tests/testthat/_snaps/parse-news-items/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
obsolete now.

Reviewed-by: Z
Refs: #123

## Features

Expand Down
1 change: 0 additions & 1 deletion tests/testthat/helper-conventional-commits.R
Original file line number Diff line number Diff line change
Expand Up @@ -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:",
Expand Down
17 changes: 15 additions & 2 deletions tests/testthat/test-parse-news-items.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ test_that("Can parse Co-authored-by", {

expect_snapshot(
extract_newsworthy_items(
"- blop (#42)\n\nCo-authored-by: Person (<[email protected]>)\nCo-authored-by: Someone Else (<[email protected]>)"
"- blop\n\nCo-authored-by: Person (<[email protected]>)\nCo-authored-by: Someone Else (<[email protected]>)"
)
)

expect_snapshot(
extract_newsworthy_items(
"feat: blop (#42)\n\nCo-authored-by: Person (<[email protected]>)"
"feat: blop\n\nCo-authored-by: Person (<[email protected]>)"
)
)
})
Expand Down Expand Up @@ -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")
)
})
})

0 comments on commit 7ae1346

Please sign in to comment.