Skip to content

Commit

Permalink
feat: add signature option (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbuck committed May 16, 2023
1 parent 3064d0b commit 3fe11f6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
// );
},
},
],
})
Expand Down
19 changes: 13 additions & 6 deletions src/create-commit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Committer, Changes, State } from "./types";
import type { CommitPayload, Changes, State } from "./types";

export async function createCommit(
state: Required<State>,
Expand All @@ -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,
}
);

Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,6 +36,8 @@ export type File = {
mode?: string;
};

export type SignatureFunction = (commitPayload: CommitPayload) => string;

export type UpdateFunctionFile =
| {
exists: true;
Expand Down Expand Up @@ -72,3 +75,11 @@ export type Author = {
email: string;
date?: string;
};

export type CommitPayload = {
message: string;
tree: string;
parents: string[];
author?: Author;
committer?: Committer;
};
7 changes: 7 additions & 0 deletions test/create-commits-with-author-and-committer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);
Expand Down Expand Up @@ -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",
},
],
Expand Down
8 changes: 7 additions & 1 deletion test/fixtures/create-commits-with-author-and-committer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2438,4 +2444,4 @@
}
}
}
]
]

0 comments on commit 3fe11f6

Please sign in to comment.