Skip to content

Commit 1e79c1e

Browse files
committed
Remove git-url-parse dependency
Replace the single usage of git-url-parse (extracting owner/repo from a git remote URL) with a trivial regex-based implementation in src/utils/git.ts. Remove both git-url-parse and @types/git-url-parse from dependencies, and clean up the related dependabot ignore entries.
1 parent be22559 commit 1e79c1e

6 files changed

Lines changed: 78 additions & 75 deletions

File tree

.github/dependabot.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ updates:
5454
ignore:
5555
# The latest updates to typescript break our build and require code changes
5656
- dependency-name: "typescript"
57-
# this dependency appears to have been updated by the maintainer into a somewhat broken state
58-
# I think it would be good to abandon our use of it
59-
- dependency-name: "@types/git-url-parse again"
60-
- dependency-name: "git-url-parse"
6157
# The version of AWS CDK libraries must match those from @guardian/cdk.
6258
# We'd never be able to update them here independently, so just ignore them.
6359
- dependency-name: "aws-cdk"

package-lock.json

Lines changed: 32 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"@guardian/eslint-config": "14.0.1",
3636
"@guardian/eslint-plugin-tsdoc-required": "^0.1.3",
3737
"@guardian/tsconfig": "^2.0.0",
38-
"@types/git-url-parse": "^9.0.3",
3938
"@types/jest": "^29.5.14",
4039
"@types/js-yaml": "^4.0.9",
4140
"@types/node": "22.15.30",
@@ -59,7 +58,6 @@
5958
"@aws-sdk/credential-providers": "^3.1034.0",
6059
"chalk": "^4.1.2",
6160
"codemaker": "^1.128.0",
62-
"git-url-parse": "^16.0.1",
6361
"js-yaml": "^4.1.1",
6462
"read-pkg-up": "7.0.1",
6563
"yargs": "^17.7.2"

src/constructs/core/stack.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import type { App, CfnElement, StackProps } from "aws-cdk-lib";
22
import { Annotations, Aspects, CfnParameter, LegacyStackSynthesizer, Stack, Tags } from "aws-cdk-lib";
33
import type { IConstruct } from "constructs";
4-
import gitUrlParse from "git-url-parse";
54
import { CfnIncludeReporter } from "../../aspects/cfn-include-reporter";
65
import { CfnParameterReporter } from "../../aspects/cfn-parameter-reporter";
76
import { Metadata } from "../../aspects/metadata";
87
import { ContextKeys, MetadataKeys, TrackingTag } from "../../constants";
9-
import { gitRemoteOriginUrl } from "../../utils/git";
8+
import { gitRemoteOriginUrl, gitRepoFullName } from "../../utils/git";
109
import type { StackStageIdentity } from "./identity";
1110
import type { GuStaticLogicalId } from "./migrating";
1211

@@ -187,7 +186,7 @@ export class GuStack extends Stack implements StackStageIdentity {
187186
try {
188187
const urlFromContext = this.node.tryGetContext(ContextKeys.REPOSITORY_URL) as string | undefined;
189188
const repositoryUrl: string = urlFromContext ?? gitRemoteOriginUrl();
190-
return gitUrlParse(repositoryUrl).full_name;
189+
return gitRepoFullName(repositoryUrl);
191190
} catch {
192191
console.info(
193192
`Unable to find git repository name. Set the ${ContextKeys.REPOSITORY_URL} context value or configure a git remote`,

src/utils/git.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { gitRepoFullName } from "./git";
2+
3+
describe("gitRepoFullName", () => {
4+
it("should parse an HTTPS URL", () => {
5+
expect(gitRepoFullName("https://github.com/guardian/cdk")).toBe("guardian/cdk");
6+
});
7+
8+
it("should parse an HTTPS URL with .git suffix", () => {
9+
expect(gitRepoFullName("https://github.com/guardian/cdk.git")).toBe("guardian/cdk");
10+
});
11+
12+
it("should parse an SSH shorthand URL", () => {
13+
expect(gitRepoFullName("git@github.com:guardian/cdk.git")).toBe("guardian/cdk");
14+
});
15+
16+
it("should parse an SSH shorthand URL without .git suffix", () => {
17+
expect(gitRepoFullName("git@github.com:guardian/cdk")).toBe("guardian/cdk");
18+
});
19+
20+
it("should parse an ssh:// URL", () => {
21+
expect(gitRepoFullName("ssh://git@github.com/guardian/cdk.git")).toBe("guardian/cdk");
22+
});
23+
24+
it("should handle repo names with dots and hyphens", () => {
25+
expect(gitRepoFullName("https://github.com/guardian/my-dotfiles.v2")).toBe("guardian/my-dotfiles.v2");
26+
});
27+
28+
it("should throw for an unparseable URL", () => {
29+
expect(() => gitRepoFullName("not-a-url")).toThrow("Unable to parse git URL");
30+
});
31+
});

src/utils/git.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ export function gitRemoteOriginUrl(): string {
66
});
77
}
88

9+
/**
10+
* Extract the `owner/repo` full name from a git remote URL.
11+
*
12+
* Supports HTTPS, SSH shorthand (`git@host:owner/repo`), and `ssh://` URLs.
13+
*/
14+
export function gitRepoFullName(url: string): string {
15+
const match = /([\w.-]+\/[\w.-]+?)(?:\.git)?$/.exec(url);
16+
if (!match?.[1]) {
17+
throw new Error(`Unable to parse git URL: ${url}`);
18+
}
19+
return match[1];
20+
}
21+
922
export function gitRootOrCwd(): string {
1023
const cwd = process.cwd();
1124

0 commit comments

Comments
 (0)