diff --git a/README.md b/README.md index 9d3f971..eea6bf8 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,16 @@ octokit email: "Committer.LastName@acme.com", date: new Date().toISOString(), // must be ISO date string }, + /* optional: if not passed, commit won't be signed*/ + signature: async function (commitPayload) { + // import { createSignature } from 'github-api-signature' + // + // return createSignature( + // commitPayload, + // privateKey, + // passphrase + // ); + }, }, ], }) diff --git a/src/create-commit.ts b/src/create-commit.ts index 6cb31c0..d5477de 100644 --- a/src/create-commit.ts +++ b/src/create-commit.ts @@ -1,4 +1,4 @@ -import type { Committer, Changes, State } from "./types"; +import type { CommitPayload, Changes, State } from "./types"; export async function createCommit( state: Required, @@ -13,17 +13,24 @@ export async function createCommit( ? changes.emptyCommit : changes.commit; + const commit: CommitPayload = { + message, + author: changes.author, + committer: changes.committer, + tree: state.latestCommitTreeSha, + parents: [latestCommitSha], + }; + // https://developer.github.com/v3/git/commits/#create-a-commit const { data: latestCommit } = await octokit.request( "POST /repos/{owner}/{repo}/git/commits", { owner: ownerOrFork, repo, - message, - author: changes.author, - committer: changes.committer, - tree: state.latestCommitTreeSha, - parents: [latestCommitSha], + ...commit, + signature: changes.signature + ? await changes.signature(commit) + : undefined, } ); diff --git a/src/types.ts b/src/types.ts index 844e41a..6164fef 100644 --- a/src/types.ts +++ b/src/types.ts @@ -26,6 +26,7 @@ export type Changes = { commit: string; committer?: Committer; author?: Author | undefined; + signature?: SignatureFunction | undefined; }; // https://developer.github.com/v3/git/blobs/#parameters @@ -35,6 +36,8 @@ export type File = { mode?: string; }; +export type SignatureFunction = (commitPayload: CommitPayload) => string; + export type UpdateFunctionFile = | { exists: true; @@ -72,3 +75,11 @@ export type Author = { email: string; date?: string; }; + +export type CommitPayload = { + message: string; + tree: string; + parents: string[]; + author?: Author; + committer?: Committer; +}; diff --git a/test/create-commits-with-author-and-committer.test.ts b/test/create-commits-with-author-and-committer.test.ts index b74e561..1913822 100644 --- a/test/create-commits-with-author-and-committer.test.ts +++ b/test/create-commits-with-author-and-committer.test.ts @@ -27,6 +27,12 @@ test("author and committer", async () => { ).toEqual(`${options.method} ${options.url}`); Object.keys(params).forEach((paramName) => { + if (paramName === "signature") { + expect(currentFixtures.request.verification.signature).toStrictEqual( + "my-signature" + ); + return; + } expect(currentFixtures.request[paramName]).toStrictEqual( params[paramName] ); @@ -76,6 +82,7 @@ test("author and committer", async () => { email: "Committer.Smith@acme.com", date: "2022-12-06T19:58:39.672Z", }, + signature: () => "my-signature", commit: "Make a fix", }, ], diff --git a/test/fixtures/create-commits-with-author-and-committer.json b/test/fixtures/create-commits-with-author-and-committer.json index 9819371..6bcbd18 100644 --- a/test/fixtures/create-commits-with-author-and-committer.json +++ b/test/fixtures/create-commits-with-author-and-committer.json @@ -1839,6 +1839,12 @@ "email": "Committer.Smith@acme.com", "date": "2022-12-06T19:58:39.672Z" }, + "verification": { + "verified": false, + "reason": "signed", + "signature": "my-signature", + "payload": null + }, "tree": "342b3bfaaa972fe97be3e14d3665f9649327913b", "parents": [ "61165f91e197e5759eff4c7b27bb87ef2c200bf2" @@ -2438,4 +2444,4 @@ } } } - ] \ No newline at end of file + ]