Skip to content

Commit

Permalink
(Fix): Parse GH branch and PR number correctly (#166)
Browse files Browse the repository at this point in the history
* fix

* semaphore fix
  • Loading branch information
TylerJang27 authored Nov 20, 2024
1 parent 1d4f9cd commit 31c0c81
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 4 deletions.
19 changes: 15 additions & 4 deletions context/src/env/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,22 @@ impl<'a> CIInfoParser<'a> {
}

fn parse_github_actions(&mut self) {
if let Some(branch) = self.get_env_var("GITHUB_REF") {
if branch.starts_with("refs/pull/") {
self.ci_info.pr_number = Self::parse_pr_number(branch.splitn(3, "/").last());
if let Some(gh_ref) = self.get_env_var("GITHUB_REF") {
if gh_ref.starts_with("refs/pull/") {
let stripped_ref = gh_ref
.strip_suffix("/merge")
.unwrap_or(gh_ref.as_str())
.splitn(3, "/")
.last();
self.ci_info.pr_number = Self::parse_pr_number(stripped_ref);
}
if let Some(gh_head_ref) = self.get_env_var("GITHUB_HEAD_REF") {
self.ci_info.branch = Some(gh_head_ref);
} else {
self.ci_info.branch = Some(gh_ref);
}
self.ci_info.branch = Some(branch);
}

self.ci_info.actor = self.get_env_var("GITHUB_ACTOR");
if let (Some(repo_name), Some(run_id)) = (
self.get_env_var("GITHUB_REPOSITORY"),
Expand Down Expand Up @@ -238,6 +248,7 @@ impl<'a> CIInfoParser<'a> {
}
self.ci_info.branch = self
.get_env_var("SEMAPHORE_GIT_PR_BRANCH")
.or_else(|| self.get_env_var("SEMAPHORE_GIT_WORKING_BRANCH"))
.or_else(|| self.get_env_var("SEMAPHORE_GIT_BRANCH"));
self.ci_info.pr_number = Self::parse_pr_number(self.get_env_var("SEMAPHORE_GIT_PR_NUMBER"));
self.ci_info.actor = self.get_env_var("SEMAPHORE_GIT_COMMIT_AUTHOR");
Expand Down
86 changes: 86 additions & 0 deletions context/tests/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn test_simple_github() {
let env_vars = EnvVars::from_iter(
vec![
(String::from("GITHUB_ACTIONS"), String::from("true")),
(String::from("GITHUB_EVENT_NAME"), String::from("schedule")),
(String::from("GITHUB_RUN_ID"), String::from(&run_id)),
(String::from("GITHUB_ACTOR"), String::from(&actor)),
(String::from("GITHUB_REPOSITORY"), String::from(&repository)),
Expand Down Expand Up @@ -227,6 +228,91 @@ fn test_simple_github() {
);
}

#[test]
fn test_simple_github_pr() {
let run_id = String::from("42069");
let pr_number = 123;
let actor = String::from("username");
let repository = String::from("test/tester");
let branch = String::from("some-branch-name");
let workflow = String::from("Pull Request");
let job = String::from("test-job");

let env_vars = EnvVars::from_iter(
vec![
(String::from("GITHUB_ACTIONS"), String::from("true")),
(
String::from("GITHUB_EVENT_NAME"),
String::from("pull_request"),
),
(String::from("GITHUB_RUN_ID"), String::from(&run_id)),
(String::from("GITHUB_ACTOR"), String::from(&actor)),
(String::from("GITHUB_REPOSITORY"), String::from(&repository)),
(String::from("GITHUB_HEAD_REF"), String::from(&branch)),
(
String::from("GITHUB_REF"),
format!("refs/pull/{pr_number}/merge"),
),
(String::from("GITHUB_WORKFLOW"), String::from(&workflow)),
(String::from("GITHUB_JOB"), String::from(&job)),
]
.into_iter(),
);

let mut env_parser = EnvParser::new();
env_parser.parse(&env_vars).unwrap();

let ci_info = env_parser.into_ci_info_parser().unwrap().info_ci_info();

pretty_assertions::assert_eq!(
ci_info,
CIInfo {
platform: CIPlatform::GitHubActions,
job_url: Some(format!(
"https://github.com/{repository}/actions/runs/{run_id}?pr={pr_number}"
)),
branch: Some(branch),
branch_class: Some(BranchClass::PullRequest),
pr_number: Some(pr_number),
actor: Some(actor),
committer_name: None,
committer_email: None,
author_name: None,
author_email: None,
commit_message: None,
title: None,
workflow: Some(workflow),
job: Some(job),
}
);

let env_validation = env::validator::validate(&ci_info);
assert_eq!(env_validation.max_level(), EnvValidationLevel::SubOptimal);
pretty_assertions::assert_eq!(
env_validation.issues(),
&[
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoAuthorEmailTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(EnvValidationIssueSubOptimal::CIInfoAuthorNameTooShort(
String::from(""),
),),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoCommitMessageTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoCommitterEmailTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(
EnvValidationIssueSubOptimal::CIInfoCommitterNameTooShort(String::from(""),),
),
EnvValidationIssue::SubOptimal(EnvValidationIssueSubOptimal::CIInfoTitleTooShort(
String::from(""),
),),
]
);
}

#[test]
fn test_simple_semaphore() {
let job_id = String::from("42069");
Expand Down

0 comments on commit 31c0c81

Please sign in to comment.