Skip to content

Commit 2b3d05a

Browse files
fix: initialize git remote if needed (#725)
## PR Checklist - [x] Addresses an existing open issue: fixes #722 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview In `initializeGitHubRepository`, if there isn't yet an `origin` remote per `git remote`, adds one.
1 parent f7d016f commit 2b3d05a

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Octokit } from "octokit";
22

33
import { Options } from "../../shared/types.js";
4+
import { initializeGitRemote } from "../initializeGitRemote.js";
45
import { initializeRepositorySettings } from "../initializeRepositorySettings.js";
56
import { initializeBranchProtectionSettings } from "./initializeBranchProtectionSettings.js";
67
import { initializeRepositoryLabels } from "./labels/initializeRepositoryLabels.js";
@@ -9,7 +10,8 @@ export async function initializeGitHubRepository(
910
octokit: Octokit,
1011
options: Options,
1112
) {
13+
await initializeGitRemote(options);
14+
await initializeRepositorySettings(octokit, options);
1215
await initializeBranchProtectionSettings(octokit, options);
1316
await initializeRepositoryLabels();
14-
await initializeRepositorySettings(octokit, options);
1517
}

src/steps/initializeGitRemote.test.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
3+
import { initializeGitRemote } from "./initializeGitRemote.js";
4+
5+
const mock$ = vi.fn();
6+
7+
vi.mock("execa", () => ({
8+
get $() {
9+
return mock$;
10+
},
11+
}));
12+
13+
const options = {
14+
owner: "TestOwner",
15+
repository: "test-repository",
16+
};
17+
18+
describe("initializeGitRemote", () => {
19+
it("does not add an origin or fetch when remotes already includes origin", async () => {
20+
mock$.mockResolvedValue({
21+
stdout: "origin",
22+
});
23+
24+
await initializeGitRemote(options);
25+
26+
expect(mock$.mock.calls).toMatchInlineSnapshot(`
27+
[
28+
[
29+
[
30+
"git remote",
31+
],
32+
],
33+
]
34+
`);
35+
});
36+
37+
it("add an origin and fetch when remotes does not include origin", async () => {
38+
mock$.mockResolvedValue({
39+
stdout: "",
40+
});
41+
42+
await initializeGitRemote(options);
43+
44+
expect(mock$.mock.calls).toMatchInlineSnapshot(`
45+
[
46+
[
47+
[
48+
"git remote",
49+
],
50+
],
51+
[
52+
[
53+
"git remote add origin https://github.com/",
54+
"/",
55+
"",
56+
],
57+
"TestOwner",
58+
"test-repository",
59+
],
60+
[
61+
[
62+
"git fetch",
63+
],
64+
],
65+
]
66+
`);
67+
});
68+
});

src/steps/initializeGitRemote.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { $ } from "execa";
2+
3+
import { Options } from "../shared/types.js";
4+
5+
type InitializeRepositorySettings = Pick<Options, "owner" | "repository">;
6+
7+
export async function initializeGitRemote({
8+
owner,
9+
repository,
10+
}: InitializeRepositorySettings) {
11+
const remotes = (await $`git remote`).stdout.trim().split("\n");
12+
13+
if (!remotes.includes("origin")) {
14+
await $`git remote add origin https://github.com/${owner}/${repository}`;
15+
await $`git fetch`;
16+
}
17+
}

0 commit comments

Comments
 (0)