Skip to content

Commit 3fe11f6

Browse files
authored
feat: add signature option (#120)
1 parent 3064d0b commit 3fe11f6

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ octokit
107107
108108
date: new Date().toISOString(), // must be ISO date string
109109
},
110+
/* optional: if not passed, commit won't be signed*/
111+
signature: async function (commitPayload) {
112+
// import { createSignature } from 'github-api-signature'
113+
//
114+
// return createSignature(
115+
// commitPayload,
116+
// privateKey,
117+
// passphrase
118+
// );
119+
},
110120
},
111121
],
112122
})

src/create-commit.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Committer, Changes, State } from "./types";
1+
import type { CommitPayload, Changes, State } from "./types";
22

33
export async function createCommit(
44
state: Required<State>,
@@ -13,17 +13,24 @@ export async function createCommit(
1313
? changes.emptyCommit
1414
: changes.commit;
1515

16+
const commit: CommitPayload = {
17+
message,
18+
author: changes.author,
19+
committer: changes.committer,
20+
tree: state.latestCommitTreeSha,
21+
parents: [latestCommitSha],
22+
};
23+
1624
// https://developer.github.com/v3/git/commits/#create-a-commit
1725
const { data: latestCommit } = await octokit.request(
1826
"POST /repos/{owner}/{repo}/git/commits",
1927
{
2028
owner: ownerOrFork,
2129
repo,
22-
message,
23-
author: changes.author,
24-
committer: changes.committer,
25-
tree: state.latestCommitTreeSha,
26-
parents: [latestCommitSha],
30+
...commit,
31+
signature: changes.signature
32+
? await changes.signature(commit)
33+
: undefined,
2734
}
2835
);
2936

src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export type Changes = {
2626
commit: string;
2727
committer?: Committer;
2828
author?: Author | undefined;
29+
signature?: SignatureFunction | undefined;
2930
};
3031

3132
// https://developer.github.com/v3/git/blobs/#parameters
@@ -35,6 +36,8 @@ export type File = {
3536
mode?: string;
3637
};
3738

39+
export type SignatureFunction = (commitPayload: CommitPayload) => string;
40+
3841
export type UpdateFunctionFile =
3942
| {
4043
exists: true;
@@ -72,3 +75,11 @@ export type Author = {
7275
email: string;
7376
date?: string;
7477
};
78+
79+
export type CommitPayload = {
80+
message: string;
81+
tree: string;
82+
parents: string[];
83+
author?: Author;
84+
committer?: Committer;
85+
};

test/create-commits-with-author-and-committer.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ test("author and committer", async () => {
2727
).toEqual(`${options.method} ${options.url}`);
2828

2929
Object.keys(params).forEach((paramName) => {
30+
if (paramName === "signature") {
31+
expect(currentFixtures.request.verification.signature).toStrictEqual(
32+
"my-signature"
33+
);
34+
return;
35+
}
3036
expect(currentFixtures.request[paramName]).toStrictEqual(
3137
params[paramName]
3238
);
@@ -76,6 +82,7 @@ test("author and committer", async () => {
7682
7783
date: "2022-12-06T19:58:39.672Z",
7884
},
85+
signature: () => "my-signature",
7986
commit: "Make a fix",
8087
},
8188
],

test/fixtures/create-commits-with-author-and-committer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,12 @@
18391839
"email": "[email protected]",
18401840
"date": "2022-12-06T19:58:39.672Z"
18411841
},
1842+
"verification": {
1843+
"verified": false,
1844+
"reason": "signed",
1845+
"signature": "my-signature",
1846+
"payload": null
1847+
},
18421848
"tree": "342b3bfaaa972fe97be3e14d3665f9649327913b",
18431849
"parents": [
18441850
"61165f91e197e5759eff4c7b27bb87ef2c200bf2"
@@ -2438,4 +2444,4 @@
24382444
}
24392445
}
24402446
}
2441-
]
2447+
]

0 commit comments

Comments
 (0)