summaryrefslogtreecommitdiff
path: root/packages/cli/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'packages/cli/src/utils')
-rw-r--r--packages/cli/src/utils/gitUtils.test.ts34
-rw-r--r--packages/cli/src/utils/gitUtils.ts25
2 files changed, 59 insertions, 0 deletions
diff --git a/packages/cli/src/utils/gitUtils.test.ts b/packages/cli/src/utils/gitUtils.test.ts
index 4a29f589..7a5f210c 100644
--- a/packages/cli/src/utils/gitUtils.test.ts
+++ b/packages/cli/src/utils/gitUtils.test.ts
@@ -10,6 +10,7 @@ import {
isGitHubRepository,
getGitRepoRoot,
getLatestGitHubRelease,
+ getGitHubRepoInfo,
} from './gitUtils.js';
vi.mock('child_process');
@@ -44,6 +45,39 @@ describe('isGitHubRepository', async () => {
});
});
+describe('getGitHubRepoInfo', async () => {
+ beforeEach(() => {
+ vi.resetAllMocks();
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it('throws an error if github repo info cannot be determined', async () => {
+ vi.mocked(child_process.execSync).mockImplementation((): string => {
+ throw new Error('oops');
+ });
+ expect(() => {
+ getGitHubRepoInfo();
+ }).toThrowError(/oops/);
+ });
+
+ it('throws an error if owner/repo could not be determined', async () => {
+ vi.mocked(child_process.execSync).mockReturnValueOnce('');
+ expect(() => {
+ getGitHubRepoInfo();
+ }).toThrowError(/Owner & repo could not be extracted from remote URL/);
+ });
+
+ it('returns the owner and repo', async () => {
+ vi.mocked(child_process.execSync).mockReturnValueOnce(
+ 'https://github.com/owner/repo.git ',
+ );
+ expect(getGitHubRepoInfo()).toEqual({ owner: 'owner', repo: 'repo' });
+ });
+});
+
describe('getGitRepoRoot', async () => {
beforeEach(() => {
vi.resetAllMocks();
diff --git a/packages/cli/src/utils/gitUtils.ts b/packages/cli/src/utils/gitUtils.ts
index 30ca2245..f5f9cb92 100644
--- a/packages/cli/src/utils/gitUtils.ts
+++ b/packages/cli/src/utils/gitUtils.ts
@@ -91,3 +91,28 @@ export const getLatestGitHubRelease = async (
);
}
};
+
+/**
+ * getGitHubRepoInfo returns the owner and repository for a GitHub repo.
+ * @returns the owner and repository of the github repo.
+ * @throws error if the exec command fails.
+ */
+export function getGitHubRepoInfo(): { owner: string; repo: string } {
+ const remoteUrl = execSync('git remote get-url origin', {
+ encoding: 'utf-8',
+ }).trim();
+
+ // Matches either https://github.com/owner/repo.git or [email protected]:owner/repo.git
+ const match = remoteUrl.match(
+ /(?:https?:\/\/|git@)github\.com(?::|\/)([^/]+)\/([^/]+?)(?:\.git)?$/,
+ );
+
+ // If the regex fails match, throw an error.
+ if (!match || !match[1] || !match[2]) {
+ throw new Error(
+ `Owner & repo could not be extracted from remote URL: ${remoteUrl}`,
+ );
+ }
+
+ return { owner: match[1], repo: match[2] };
+}