-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add azure devops detection
- Loading branch information
azurebot
committed
Jun 11, 2024
1 parent
f7cd242
commit 56b8034
Showing
7 changed files
with
219 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { execFileSync } from "child_process"; | ||
import { createTestDir } from "../../../tests/create-test-directory"; | ||
import { detectGitHost } from "../detect-git-host"; | ||
|
||
describe("detect-git-host", () => { | ||
it("should detect a https azure git host", async () => { | ||
const { testFolder } = await createTestDir("detect-git-host"); | ||
|
||
execFileSync( | ||
"git", | ||
[ | ||
"remote", | ||
"add", | ||
"origin", | ||
"https://ORGANISATION@dev.azure.com/ORGANISATION/PROJECT/_git/REPOSITORY", | ||
], | ||
{ cwd: testFolder }, | ||
); | ||
|
||
const gitHost = await detectGitHost(testFolder); | ||
|
||
expect(gitHost?.detectedGitHost).toBe("Azure"); | ||
expect(gitHost?.commitUrlFormat).toBe( | ||
"{{host}}/ORGANISATION/PROJECT/_git/REPOSITORY/branchCompare?baseVersion=GT{{previousTag}}&targetVersion=GT{{currentTag}}", | ||
); | ||
expect(gitHost?.compareUrlFormat).toBe( | ||
"{{host}}/ORGANISATION/PROJECT/_git/REPOSITORY/commit/{{hash}}", | ||
); | ||
expect(gitHost?.issueUrlFormat).toBe("{{host}}/ORGANISATION/PROJECT/_workitems/edit/{{id}}"); | ||
}); | ||
|
||
it("should detect a ssh azure git host", async () => { | ||
const { testFolder } = await createTestDir("detect-git-host"); | ||
|
||
execFileSync( | ||
"git", | ||
["remote", "add", "origin", "git@ssh.dev.azure.com:v3/ORGANISATION/PROJECT/REPOSITORY"], | ||
{ cwd: testFolder }, | ||
); | ||
|
||
const gitHost = await detectGitHost(testFolder); | ||
|
||
expect(gitHost?.detectedGitHost).toBe("Azure"); | ||
expect(gitHost?.commitUrlFormat).toBe( | ||
"{{host}}/ORGANISATION/PROJECT/_git/REPOSITORY/branchCompare?baseVersion=GT{{previousTag}}&targetVersion=GT{{currentTag}}", | ||
); | ||
expect(gitHost?.compareUrlFormat).toBe( | ||
"{{host}}/ORGANISATION/PROJECT/_git/REPOSITORY/commit/{{hash}}", | ||
); | ||
expect(gitHost?.issueUrlFormat).toBe("{{host}}/ORGANISATION/PROJECT/_workitems/edit/{{id}}"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { execFile } from "node:child_process"; | ||
|
||
export interface DetectedGitHost { | ||
detectedGitHost: string; | ||
commitUrlFormat: string; | ||
compareUrlFormat: string; | ||
issueUrlFormat: string; | ||
} | ||
|
||
/** | ||
* Conventional-Changelog already supports the following git hosts: | ||
* - Github | ||
* - Gitlab | ||
* - Bitbucket | ||
* | ||
* We want to detect if the user is using another host such as Azure DevOps, | ||
* if so we need to create the correct URLs so the changelog is generated | ||
* correctly. | ||
*/ | ||
export async function detectGitHost(cwd: string): Promise<DetectedGitHost | null> { | ||
const remoteUrl = (await new Promise((onResolve, onReject) => { | ||
execFile("git", ["config", "--get", "remote.origin.url"], { cwd }, (error, stdout, stderr) => { | ||
if (error) { | ||
onReject(error); | ||
} | ||
onResolve(stdout ? stdout.trim() : stderr); | ||
}); | ||
})) as string; | ||
|
||
// A checked out Azure DevOps remote URL looks like one of these: | ||
// | ||
// | Checkout Type | Remote URL | | ||
// | ------------- | --------------------------------------------------------------------------------------- | | ||
// | HTTPS | https://{{ORGANISATION}}@dev.azure.com/{{ORGANISATION}}/{{PROJECT}}/_git/{{REPOSITORY}} | | ||
// | SSH | git@ssh.dev.azure.com:v3/{{ORGANISATION}}/{{PROJECT}}/{{REPOSITORY}} | | ||
// | ||
if (remoteUrl.startsWith("https://") && remoteUrl.includes("@dev.azure.com/")) { | ||
/** | ||
* [Regex101.com](https://regex101.com/r/fF7HUc/1) | ||
*/ | ||
const match = | ||
/^https:\/\/(?<atorganisation>.*?)@dev.azure.com\/(?<organisation>.*?)\/(?<project>.*?)\/_git\/(?<repository>.*?)(?:\.git)?$/.exec( | ||
remoteUrl, | ||
); | ||
|
||
if (match?.groups) { | ||
const { organisation = "", project = "", repository = "" } = match.groups; | ||
|
||
return { | ||
detectedGitHost: "Azure", | ||
commitUrlFormat: `{{host}}/${organisation}/${project}/_git/${repository}/branchCompare?baseVersion=GT{{previousTag}}&targetVersion=GT{{currentTag}}`, | ||
compareUrlFormat: `{{host}}/${organisation}/${project}/_git/${repository}/commit/{{hash}}`, | ||
issueUrlFormat: `{{host}}/${organisation}/${project}/_workitems/edit/{{id}}`, | ||
}; | ||
} | ||
} else if (remoteUrl.startsWith("git@ssh.dev.azure.com:")) { | ||
/** | ||
* [Regex101.com](https://regex101.com/r/VhNxWr/1) | ||
*/ | ||
const match = | ||
/^git@ssh.dev.azure.com:v\d\/(?<organisation>.*?)\/(?<project>.*?)\/(?<repository>.*?)(?:\.git)?$/.exec( | ||
remoteUrl, | ||
); | ||
|
||
if (match?.groups) { | ||
const { organisation = "", project = "", repository = "" } = match.groups; | ||
|
||
return { | ||
detectedGitHost: "Azure", | ||
commitUrlFormat: `{{host}}/${organisation}/${project}/_git/${repository}/branchCompare?baseVersion=GT{{previousTag}}&targetVersion=GT{{currentTag}}`, | ||
compareUrlFormat: `{{host}}/${organisation}/${project}/_git/${repository}/commit/{{hash}}`, | ||
issueUrlFormat: `{{host}}/${organisation}/${project}/_workitems/edit/{{id}}`, | ||
}; | ||
} | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters