Skip to content

Commit 47d08d2

Browse files
committed
Allow message to be specified as single string
1 parent 1c1ff95 commit 47d08d2

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
lines changed

.changeset/long-rabbits-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@s0/ghcommit": minor
3+
---
4+
5+
Allow message to be specified as single string

README.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ All the functions below accept a single object as its argument, and share the fo
6161
/**
6262
* The commit message
6363
*/
64-
message: CommitMessage;
64+
message: string | CommitMessage;
6565
log?: Logger;
6666
}
6767
```
@@ -105,9 +105,7 @@ await commitChangesFromRepo({
105105
octokit,
106106
...context.repo,
107107
branch: "new-branch-to-create",
108-
message: {
109-
headline: "[chore] do something",
110-
},
108+
message: "[chore] do something",
111109
});
112110

113111
// Commit & push the files from a specific directory
@@ -117,9 +115,7 @@ await commitChangesFromRepo({
117115
owner: "my-org",
118116
repository: "my-repo",
119117
branch: "another-new-branch-to-create",
120-
message: {
121-
headline: "[chore] do something else",
122-
},
118+
message: "[chore] do something else\n\nsome more details",
123119
repoDirectory: "/tmp/some-repo",
124120
});
125121

@@ -131,6 +127,7 @@ await commitChangesFromRepo({
131127
branch: "another-new-branch-to-create",
132128
message: {
133129
headline: "[chore] do something else",
130+
body: "some more details",
134131
},
135132
base: {
136133
// This will be the original sha from the workflow run,
@@ -184,9 +181,7 @@ await commitFilesFromDirectory({
184181
octokit,
185182
...context.repo,
186183
branch: "new-branch-to-create",
187-
message: {
188-
headline: "[chore] do something",
189-
},
184+
message: "[chore] do something",
190185
base: {
191186
branch: "main",
192187
},
@@ -201,9 +196,7 @@ await commitFilesFromDirectory({
201196
octokit,
202197
...context.repo,
203198
branch: "docs",
204-
message: {
205-
headline: "[chore] do something",
206-
},
199+
message: "[chore] do something",
207200
force: true, // Overwrite any existing branch
208201
base: {
209202
tag: "v1.0.0",
@@ -253,9 +246,7 @@ await commitFilesFromBuffers({
253246
octokit,
254247
...context.repo,
255248
branch: "new-branch-to-create",
256-
message: {
257-
headline: "[chore] do something",
258-
},
249+
message: "[chore] do something",
259250
base: {
260251
branch: "main",
261252
},

src/core.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
CommitFilesResult,
1414
GitBase,
1515
} from "./interface.js";
16+
import { CommitMessage } from "./github/graphql/generated/types.js";
1617

1718
const getBaseRef = (base: GitBase): string => {
1819
if ("branch" in base) {
@@ -161,14 +162,22 @@ export const commitFilesFromBase64 = async ({
161162
}
162163
}
163164

165+
const finalMessage: CommitMessage =
166+
typeof message === "string"
167+
? {
168+
headline: message.split("\n")[0]?.trim() ?? "",
169+
body: message.split("\n").slice(1).join("\n").trim(),
170+
}
171+
: message;
172+
164173
await log?.debug(`Creating commit on branch ${branch}`);
165174
const createCommitMutation: CreateCommitOnBranchMutationVariables = {
166175
input: {
167176
branch: {
168177
id: refId,
169178
},
170179
expectedHeadOid: baseOid,
171-
message,
180+
message: finalMessage,
172181
fileChanges,
173182
},
174183
};

src/github/graphql/queries.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ const GET_REF_TREE = /* GraphQL */ `
105105
oid
106106
}
107107
}
108+
message
108109
file(path: $path) {
109110
oid
110111
}

src/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface CommitFilesBasedArgs {
3636
/**
3737
* The commit message
3838
*/
39-
message: CommitMessage;
39+
message: string | CommitMessage;
4040
log?: Logger;
4141
}
4242

src/test/integration/node.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getRefTreeQuery,
99
getRepositoryMetadata,
1010
} from "../../github/graphql/queries.js";
11+
import { CommitMessage } from "../../github/graphql/generated/types.js";
1112

1213
const octokit = getOctokit(ENV.GITHUB_TOKEN);
1314

@@ -366,6 +367,67 @@ describe("node", () => {
366367
});
367368
});
368369
});
370+
371+
describe("commit message is correctly handled", () => {
372+
const testCommitMessage = async ({
373+
branch,
374+
input,
375+
expected,
376+
}: {
377+
branch: string;
378+
input: string | CommitMessage;
379+
expected: string;
380+
}) => {
381+
await commitFilesFromBuffers({
382+
octokit,
383+
...REPO,
384+
branch,
385+
base: {
386+
commit: TEST_TARGET_COMMIT,
387+
},
388+
...BASIC_FILE_CONTENTS,
389+
message: input,
390+
});
391+
392+
const ref = (
393+
await getRefTreeQuery(octokit, {
394+
...REPO,
395+
ref: `refs/heads/${branch}`,
396+
path: "package.json",
397+
})
398+
).repository?.ref?.target;
399+
400+
if (!ref || !("message" in ref)) {
401+
throw new Error("Unexpected result");
402+
}
403+
404+
expect(ref.message).toEqual(expected);
405+
};
406+
407+
it("single string", async () => {
408+
const branch = `${TEST_BRANCH_PREFIX}-commit-message-single`;
409+
branches.push(branch);
410+
411+
await testCommitMessage({
412+
branch,
413+
input: "This is a basic commit message",
414+
expected: "This is a basic commit message!",
415+
});
416+
});
417+
418+
it("multi-line string", async () => {
419+
const branch = `${TEST_BRANCH_PREFIX}-commit-message-multi`;
420+
branches.push(branch);
421+
422+
await testCommitMessage({
423+
branch,
424+
input:
425+
"This is a basic commit message\nwith a second line\n\nand some more lines!",
426+
expected:
427+
"This is a basic commit message\nwith a second line\n\nand some more lines!",
428+
});
429+
});
430+
});
369431
});
370432

371433
afterAll(async () => {

0 commit comments

Comments
 (0)