-
-
Notifications
You must be signed in to change notification settings - Fork 235
feat(build): Add auto-detection of PR number from GitHub Actions #2722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
54682d0
c56e32e
4046833
b9fd298
5509fd9
1a4e171
c4f4bfd
9dabcbb
3a42364
b2f7057
54f47bd
69fafe2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,28 @@ fn find_merge_base_ref( | |
Ok(merge_base_sha) | ||
} | ||
|
||
/// Attempts to get the PR number from GitHub Actions environment variables. | ||
/// Returns the PR number if running in a GitHub Actions pull request environment. | ||
pub fn get_github_pr_number() -> Option<u32> { | ||
let github_ref = std::env::var("GITHUB_REF").ok()?; | ||
let event_name = std::env::var("GITHUB_EVENT_NAME").ok()?; | ||
|
||
if event_name != "pull_request" { | ||
debug!("Not running in pull_request event, got: {}", event_name); | ||
return None; | ||
} | ||
|
||
let pr_number_str = github_ref.strip_prefix("refs/pull/")?; | ||
debug!("Extracted PR reference: {}", pr_number_str); | ||
|
||
let pr_number_str = pr_number_str.split('/').next()?; | ||
debug!("Parsing PR number from: {}", pr_number_str); | ||
|
||
let pr_number = pr_number_str.parse().ok()?; | ||
debug!("Auto-detected PR number from GitHub Actions: {}", pr_number); | ||
Some(pr_number) | ||
} | ||
|
||
fn find_reference_url(repo: &str, repos: &[Repo]) -> Result<Option<String>> { | ||
let mut non_git = false; | ||
for configured_repo in repos { | ||
|
@@ -1278,3 +1300,25 @@ fn test_git_repo_head_ref() { | |
"HEAD is detached - no branch reference available" | ||
); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_get_github_pr_number() { | ||
std::env::set_var("GITHUB_EVENT_NAME", "pull_request"); | ||
std::env::set_var("GITHUB_REF", "refs/pull/123/merge"); | ||
let pr_number = get_github_pr_number(); | ||
assert_eq!(pr_number, Some(123)); | ||
std::env::set_var("GITHUB_EVENT_NAME", "push"); | ||
let pr_number = get_github_pr_number(); | ||
assert_eq!(pr_number, None); | ||
std::env::set_var("GITHUB_EVENT_NAME", "pull_request"); | ||
std::env::set_var("GITHUB_REF", "refs/heads/main"); | ||
let pr_number = get_github_pr_number(); | ||
assert_eq!(pr_number, None); | ||
std::env::remove_var("GITHUB_EVENT_NAME"); | ||
std::env::remove_var("GITHUB_REF"); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Test Pollution from Global Environment ModificationsThe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this function, I didn't log a warning if we failed to fetch here because i figure there are so many different CI providers we'd be logging a warning every time. Until we cover a broader range of CI providers it doesn't make sense to log something. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Debug logging should be fine, I'd recommend adding some logs around failure points, particularly in the like 258 block and any other early return points just to make debugging easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second this, as debug logs are hidden by default. Default log level is
WARN
. However, when folks report bugs, we ask them to set a log level ofDEBUG
, as the debug logs, though verbose, often help identify the problem.