Skip to content

Commit 9ae1eea

Browse files
committed
Use ghcommit to push changes
To allow for all commits to be signed, use the GitHub API to push changes.
1 parent 4a94b8e commit 9ae1eea

6 files changed

Lines changed: 159 additions & 69 deletions

File tree

package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
"main": "dist/index.js",
55
"license": "MIT",
66
"devDependencies": {
7-
"@changesets/changelog-github": "^0.4.2",
8-
"@changesets/cli": "^2.20.0",
9-
"@changesets/write": "^0.1.6",
10-
"@vercel/ncc": "^0.36.1",
11-
"fixturez": "^1.1.0",
12-
"prettier": "^2.0.5",
13-
"typescript": "^5.0.4",
147
"@babel/core": "^7.13.10",
158
"@babel/preset-env": "^7.13.10",
169
"@babel/preset-typescript": "^7.13.0",
10+
"@changesets/changelog-github": "^0.4.2",
11+
"@changesets/cli": "^2.20.0",
12+
"@changesets/write": "^0.1.6",
1713
"@types/fs-extra": "^8.0.0",
1814
"@types/jest": "^29.5.1",
1915
"@types/node": "^20.11.17",
2016
"@types/semver": "^7.5.0",
17+
"@vercel/ncc": "^0.36.1",
2118
"babel-jest": "^29.5.0",
19+
"fixturez": "^1.1.0",
2220
"husky": "^3.0.3",
23-
"jest": "^29.5.0"
21+
"jest": "^29.5.0",
22+
"prettier": "^2.0.5",
23+
"typescript": "^5.0.4"
2424
},
2525
"scripts": {
2626
"build": "ncc build src/index.ts -o dist --transpile-only --minify",
@@ -41,6 +41,7 @@
4141
"@changesets/read": "^0.5.3",
4242
"@manypkg/get-packages": "^1.1.3",
4343
"@octokit/plugin-throttling": "^5.2.1",
44+
"@s0/ghcommit": "^1.1.0",
4445
"fs-extra": "^8.1.0",
4546
"mdast-util-to-string": "^1.0.6",
4647
"remark-parse": "^7.0.1",

src/gitUtils.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,49 +11,4 @@ export const setupUser = async () => {
1111
"user.email",
1212
`"github-actions[bot]@users.noreply.github.com"`,
1313
]);
14-
};
15-
16-
export const pullBranch = async (branch: string) => {
17-
await exec("git", ["pull", "origin", branch]);
18-
};
19-
20-
export const push = async (
21-
branch: string,
22-
{ force }: { force?: boolean } = {}
23-
) => {
24-
await exec(
25-
"git",
26-
["push", "origin", `HEAD:${branch}`, force && "--force"].filter<string>(
27-
Boolean as any
28-
)
29-
);
30-
};
31-
32-
export const switchToMaybeExistingBranch = async (branch: string) => {
33-
let { stderr } = await getExecOutput("git", ["checkout", branch], {
34-
ignoreReturnCode: true,
35-
});
36-
let isCreatingBranch = !stderr
37-
.toString()
38-
.includes(`Switched to a new branch '${branch}'`);
39-
if (isCreatingBranch) {
40-
await exec("git", ["checkout", "-b", branch]);
41-
}
42-
};
43-
44-
export const reset = async (
45-
pathSpec: string,
46-
mode: "hard" | "soft" | "mixed" = "hard"
47-
) => {
48-
await exec("git", ["reset", `--${mode}`, pathSpec]);
49-
};
50-
51-
export const commitAll = async (message: string) => {
52-
await exec("git", ["add", "."]);
53-
await exec("git", ["commit", "-m", message]);
54-
};
55-
56-
export const checkIfClean = async (): Promise<boolean> => {
57-
const { stdout } = await getExecOutput("git", ["status", "--porcelain"]);
58-
return !stdout.length;
59-
};
14+
};

src/run.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jest.mock("@actions/github/lib/utils", () => ({
3131
getOctokitOptions: jest.fn(),
3232
}));
3333
jest.mock("./gitUtils");
34+
jest.mock("@s0/ghcommit/git");
3435

3536
let mockedGithubMethods = {
3637
pulls: {

src/run.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as gitUtils from "./gitUtils";
1717
import readChangesetState from "./readChangesetState";
1818
import resolveFrom from "resolve-from";
1919
import { throttling } from "@octokit/plugin-throttling";
20+
import { commitChangesFromRepo } from "@s0/ghcommit/git";
2021

2122
// GitHub Issues/PRs messages have a max size limit on the
2223
// message body payload.
@@ -334,9 +335,6 @@ export async function runVersion({
334335

335336
let { preState } = await readChangesetState(cwd);
336337

337-
await gitUtils.switchToMaybeExistingBranch(versionBranch);
338-
await gitUtils.reset(github.context.sha);
339-
340338
let versionsByDirectory = await getVersionsByDirectory(cwd);
341339

342340
if (script) {
@@ -377,16 +375,25 @@ export async function runVersion({
377375
);
378376

379377
const finalPrTitle = `${prTitle}${!!preState ? ` (${preState.tag})` : ""}`;
380-
381-
// project with `commit: true` setting could have already committed files
382-
if (!(await gitUtils.checkIfClean())) {
383-
const finalCommitMessage = `${commitMessage}${
384-
!!preState ? ` (${preState.tag})` : ""
385-
}`;
386-
await gitUtils.commitAll(finalCommitMessage);
387-
}
388-
389-
await gitUtils.push(versionBranch, { force: true });
378+
const finalCommitMessage = `${commitMessage}${
379+
!!preState ? ` (${preState.tag})` : ""
380+
}`;
381+
382+
await commitChangesFromRepo({
383+
octokit,
384+
owner: github.context.repo.owner,
385+
repository: github.context.repo.repo,
386+
branch: versionBranch,
387+
// TODO: switch this to use direct string input when supported
388+
message: {
389+
headline: finalCommitMessage.split("\n", 2)[0].trim(),
390+
body: finalCommitMessage.split("\n", 2)[1]?.trim(),
391+
},
392+
base: {
393+
commit: github.context.sha,
394+
},
395+
force: true,
396+
});
390397

391398
let existingPullRequests = await existingPullRequestsPromise;
392399
core.info(JSON.stringify(existingPullRequests.data, null, 2));

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
4040

4141
/* Module Resolution Options */
42-
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
42+
"moduleResolution": "nodenext", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
4343
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
4444
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
4545
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */

0 commit comments

Comments
 (0)