Skip to content

Commit

Permalink
tests: Ensure repository slug parsing works correctly. (#596)
Browse files Browse the repository at this point in the history
* Enable debugging.

* Bump CI.

* Fix creds.

* Bump version.
  • Loading branch information
milesj authored Feb 4, 2023
1 parent e203529 commit 7a9dc17
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/moon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
args: -- --color --log trace ci --base ${{ github.base_ref || 'master' }}
env:
MOONBASE_SECRET_KEY: ${{ secrets.MOONBASE_SECRET_KEY }}
MOONBASE_API_KEY: ${{ secrets.MOONBASE_API_KEY }}
MOONBASE_ACCESS_KEY: ${{ secrets.MOONBASE_ACCESS_KEY }}
- uses: moonrepo/run-report-action@v1
with:
access-token: ${{ secrets.GITHUB_TOKEN }}
Expand Down
2 changes: 1 addition & 1 deletion .moon/workspace.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Trigger CI: 9
# Trigger CI: 10

$schema: '../website/static/schemas/workspace.json'

Expand Down
9 changes: 9 additions & 0 deletions .yarn/versions/ac3556d5.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
releases:
"@moonrepo/cli": patch
"@moonrepo/core-linux-arm64-gnu": patch
"@moonrepo/core-linux-arm64-musl": patch
"@moonrepo/core-linux-x64-gnu": patch
"@moonrepo/core-linux-x64-musl": patch
"@moonrepo/core-macos-arm64": patch
"@moonrepo/core-macos-x64": patch
"@moonrepo/core-windows-x64-msvc": patch
46 changes: 25 additions & 21 deletions crates/core/vcs/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@ impl Git {
})
}

pub fn extract_slug_from_remote(output: String) -> Result<String, VcsError> {
// [email protected]:moonrepo/moon.git
let remote = if output.starts_with("git@") {
format!("https://{}", output.replace(':', "/"))
// https://github.com/moonrepo/moon
} else {
output
};

let url = url::Url::parse(&remote)
.map_err(|e| VcsError::FailedToParseGitRemote(e.to_string()))?;
let mut slug = url.path();

if slug.starts_with('/') {
slug = &slug[1..];
}

if slug.ends_with(".git") {
slug = &slug[0..(slug.len() - 4)];
}

Ok(slug.to_owned())
}

async fn get_merge_base(&self, base: &str, head: &str) -> VcsResult<String> {
let mut args = string_vec!["merge-base", head];
let mut candidates = string_vec![base.to_owned()];
Expand Down Expand Up @@ -240,27 +264,7 @@ impl Vcs for Git {
)
.await?;

// [email protected]:moonrepo/moon.git
let remote = if output.starts_with("git@") {
format!("https://{}", output.replace(':', "/"))
// https://github.com/moonrepo/moon
} else {
output
};

let url = url::Url::parse(&remote)
.map_err(|e| VcsError::FailedToParseGitRemote(e.to_string()))?;
let mut slug = url.path();

if slug.starts_with('/') {
slug = &slug[1..];
}

if slug.ends_with(".git") {
slug = &slug[0..(slug.len() - 4)];
}

Ok(slug.to_owned())
Self::extract_slug_from_remote(output)
}

// https://git-scm.com/docs/git-status#_short_format
Expand Down
36 changes: 36 additions & 0 deletions crates/core/vcs/tests/git_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,39 @@ mod touched_files_via_diff {
);
}
}

mod slug_parsing {
use super::*;

#[test]
fn supports_http() {
assert_eq!(
Git::extract_slug_from_remote("http://github.com/moonrepo/moon".into()).unwrap(),
"moonrepo/moon"
);
assert_eq!(
Git::extract_slug_from_remote("http://github.com/moonrepo/moon.git".into()).unwrap(),
"moonrepo/moon"
);
assert_eq!(
Git::extract_slug_from_remote("https://github.com/moonrepo/moon".into()).unwrap(),
"moonrepo/moon"
);
assert_eq!(
Git::extract_slug_from_remote("https://github.com/moonrepo/moon.git".into()).unwrap(),
"moonrepo/moon"
);
}

#[test]
fn supports_git() {
assert_eq!(
Git::extract_slug_from_remote("[email protected]:moonrepo/moon".into()).unwrap(),
"moonrepo/moon"
);
assert_eq!(
Git::extract_slug_from_remote("[email protected]:moonrepo/moon.git".into()).unwrap(),
"moonrepo/moon"
);
}
}

0 comments on commit 7a9dc17

Please sign in to comment.