Skip to content

Commit 7a9dc17

Browse files
authored
tests: Ensure repository slug parsing works correctly. (#596)
* Enable debugging. * Bump CI. * Fix creds. * Bump version.
1 parent e203529 commit 7a9dc17

File tree

5 files changed

+72
-23
lines changed

5 files changed

+72
-23
lines changed

.github/workflows/moon.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
args: -- --color --log trace ci --base ${{ github.base_ref || 'master' }}
4646
env:
4747
MOONBASE_SECRET_KEY: ${{ secrets.MOONBASE_SECRET_KEY }}
48-
MOONBASE_API_KEY: ${{ secrets.MOONBASE_API_KEY }}
48+
MOONBASE_ACCESS_KEY: ${{ secrets.MOONBASE_ACCESS_KEY }}
4949
- uses: moonrepo/run-report-action@v1
5050
with:
5151
access-token: ${{ secrets.GITHUB_TOKEN }}

.moon/workspace.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Trigger CI: 9
1+
# Trigger CI: 10
22

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

.yarn/versions/ac3556d5.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
releases:
2+
"@moonrepo/cli": patch
3+
"@moonrepo/core-linux-arm64-gnu": patch
4+
"@moonrepo/core-linux-arm64-musl": patch
5+
"@moonrepo/core-linux-x64-gnu": patch
6+
"@moonrepo/core-linux-x64-musl": patch
7+
"@moonrepo/core-macos-arm64": patch
8+
"@moonrepo/core-macos-x64": patch
9+
"@moonrepo/core-windows-x64-msvc": patch

crates/core/vcs/src/git.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,30 @@ impl Git {
5151
})
5252
}
5353

54+
pub fn extract_slug_from_remote(output: String) -> Result<String, VcsError> {
55+
// [email protected]:moonrepo/moon.git
56+
let remote = if output.starts_with("git@") {
57+
format!("https://{}", output.replace(':', "/"))
58+
// https://github.com/moonrepo/moon
59+
} else {
60+
output
61+
};
62+
63+
let url = url::Url::parse(&remote)
64+
.map_err(|e| VcsError::FailedToParseGitRemote(e.to_string()))?;
65+
let mut slug = url.path();
66+
67+
if slug.starts_with('/') {
68+
slug = &slug[1..];
69+
}
70+
71+
if slug.ends_with(".git") {
72+
slug = &slug[0..(slug.len() - 4)];
73+
}
74+
75+
Ok(slug.to_owned())
76+
}
77+
5478
async fn get_merge_base(&self, base: &str, head: &str) -> VcsResult<String> {
5579
let mut args = string_vec!["merge-base", head];
5680
let mut candidates = string_vec![base.to_owned()];
@@ -240,27 +264,7 @@ impl Vcs for Git {
240264
)
241265
.await?;
242266

243-
// [email protected]:moonrepo/moon.git
244-
let remote = if output.starts_with("git@") {
245-
format!("https://{}", output.replace(':', "/"))
246-
// https://github.com/moonrepo/moon
247-
} else {
248-
output
249-
};
250-
251-
let url = url::Url::parse(&remote)
252-
.map_err(|e| VcsError::FailedToParseGitRemote(e.to_string()))?;
253-
let mut slug = url.path();
254-
255-
if slug.starts_with('/') {
256-
slug = &slug[1..];
257-
}
258-
259-
if slug.ends_with(".git") {
260-
slug = &slug[0..(slug.len() - 4)];
261-
}
262-
263-
Ok(slug.to_owned())
267+
Self::extract_slug_from_remote(output)
264268
}
265269

266270
// https://git-scm.com/docs/git-status#_short_format

crates/core/vcs/tests/git_test.rs

+36
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,39 @@ mod touched_files_via_diff {
463463
);
464464
}
465465
}
466+
467+
mod slug_parsing {
468+
use super::*;
469+
470+
#[test]
471+
fn supports_http() {
472+
assert_eq!(
473+
Git::extract_slug_from_remote("http://github.com/moonrepo/moon".into()).unwrap(),
474+
"moonrepo/moon"
475+
);
476+
assert_eq!(
477+
Git::extract_slug_from_remote("http://github.com/moonrepo/moon.git".into()).unwrap(),
478+
"moonrepo/moon"
479+
);
480+
assert_eq!(
481+
Git::extract_slug_from_remote("https://github.com/moonrepo/moon".into()).unwrap(),
482+
"moonrepo/moon"
483+
);
484+
assert_eq!(
485+
Git::extract_slug_from_remote("https://github.com/moonrepo/moon.git".into()).unwrap(),
486+
"moonrepo/moon"
487+
);
488+
}
489+
490+
#[test]
491+
fn supports_git() {
492+
assert_eq!(
493+
Git::extract_slug_from_remote("[email protected]:moonrepo/moon".into()).unwrap(),
494+
"moonrepo/moon"
495+
);
496+
assert_eq!(
497+
Git::extract_slug_from_remote("[email protected]:moonrepo/moon.git".into()).unwrap(),
498+
"moonrepo/moon"
499+
);
500+
}
501+
}

0 commit comments

Comments
 (0)