Skip to content

Commit 6303e70

Browse files
committed
Allow message to be specified as single string
1 parent e68e0ee commit 6303e70

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
@@ -69,7 +69,7 @@ All the functions below accept a single object as its argument, and share the fo
6969
/**
7070
* The commit message
7171
*/
72-
message: CommitMessage;
72+
message: string | CommitMessage;
7373
log?: Logger;
7474
}
7575
```
@@ -113,9 +113,7 @@ await commitChangesFromRepo({
113113
octokit,
114114
...context.repo,
115115
branch: "new-branch-to-create",
116-
message: {
117-
headline: "[chore] do something",
118-
},
116+
message: "[chore] do something",
119117
});
120118

121119
// Commit & push the files from a specific directory
@@ -125,9 +123,7 @@ await commitChangesFromRepo({
125123
owner: "my-org",
126124
repository: "my-repo",
127125
branch: "another-new-branch-to-create",
128-
message: {
129-
headline: "[chore] do something else",
130-
},
126+
message: "[chore] do something else\n\nsome more details",
131127
repoDirectory: "/tmp/some-repo",
132128
});
133129

@@ -139,6 +135,7 @@ await commitChangesFromRepo({
139135
branch: "another-new-branch-to-create",
140136
message: {
141137
headline: "[chore] do something else",
138+
body: "some more details",
142139
},
143140
base: {
144141
// This will be the original sha from the workflow run,
@@ -192,9 +189,7 @@ await commitFilesFromDirectory({
192189
octokit,
193190
...context.repo,
194191
branch: "new-branch-to-create",
195-
message: {
196-
headline: "[chore] do something",
197-
},
192+
message: "[chore] do something",
198193
base: {
199194
branch: "main",
200195
},
@@ -209,9 +204,7 @@ await commitFilesFromDirectory({
209204
octokit,
210205
...context.repo,
211206
branch: "docs",
212-
message: {
213-
headline: "[chore] do something",
214-
},
207+
message: "[chore] do something",
215208
force: true, // Overwrite any existing branch
216209
base: {
217210
tag: "v1.0.0",
@@ -261,9 +254,7 @@ await commitFilesFromBuffers({
261254
octokit,
262255
...context.repo,
263256
branch: "new-branch-to-create",
264-
message: {
265-
headline: "[chore] do something",
266-
},
257+
message: "[chore] do something",
267258
base: {
268259
branch: "main",
269260
},

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
@@ -9,6 +9,7 @@ import {
99
getRefTreeQuery,
1010
getRepositoryMetadata,
1111
} from "../../github/graphql/queries.js";
12+
import { CommitMessage } from "../../github/graphql/generated/types.js";
1213
import git from "isomorphic-git";
1314

1415
// TODO: re-enable strict tree tests when GitHub have addressed the createRef
@@ -391,6 +392,67 @@ describe("node", () => {
391392
});
392393
});
393394
});
395+
396+
describe("commit message is correctly handled", () => {
397+
const testCommitMessage = async ({
398+
branch,
399+
input,
400+
expected,
401+
}: {
402+
branch: string;
403+
input: string | CommitMessage;
404+
expected: string;
405+
}) => {
406+
await commitFilesFromBuffers({
407+
octokit,
408+
...REPO,
409+
branch,
410+
base: {
411+
commit: testTargetCommit,
412+
},
413+
...BASIC_FILE_CONTENTS,
414+
message: input,
415+
});
416+
417+
const ref = (
418+
await getRefTreeQuery(octokit, {
419+
...REPO,
420+
ref: `refs/heads/${branch}`,
421+
path: "package.json",
422+
})
423+
).repository?.ref?.target;
424+
425+
if (!ref || !("message" in ref)) {
426+
throw new Error("Unexpected result");
427+
}
428+
429+
expect(ref.message).toEqual(expected);
430+
};
431+
432+
it("single string", async () => {
433+
const branch = `${TEST_BRANCH_PREFIX}-commit-message-single`;
434+
branches.push(branch);
435+
436+
await testCommitMessage({
437+
branch,
438+
input: "This is a basic commit message",
439+
expected: "This is a basic commit message!",
440+
});
441+
});
442+
443+
it("multi-line string", async () => {
444+
const branch = `${TEST_BRANCH_PREFIX}-commit-message-multi`;
445+
branches.push(branch);
446+
447+
await testCommitMessage({
448+
branch,
449+
input:
450+
"This is a basic commit message\nwith a second line\n\nand some more lines!",
451+
expected:
452+
"This is a basic commit message\nwith a second line\n\nand some more lines!",
453+
});
454+
});
455+
});
394456
});
395457

396458
afterAll(async () => {

0 commit comments

Comments
 (0)