-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce CommitSigner interface (#442)
* feat: introduce CommitSigner interface * chore: fix lint * ignore eslint config * address review
- Loading branch information
Showing
7 changed files
with
160 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"extends": "./node_modules/gts" | ||
"extends": "./node_modules/gts", | ||
"root": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,6 @@ | |
'.github/release-please.yml', | ||
'renovate.json', | ||
'.eslintignore', | ||
'.eslintrc.json', | ||
'.prettierignore', | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,14 @@ import * as sinon from 'sinon'; | |
import {GetResponseTypeFromEndpointMethod} from '@octokit/types'; | ||
import * as handler from '../src/github/commit-and-push'; | ||
import * as createCommitModule from '../src/github/create-commit'; | ||
import {Changes, FileData, TreeObject, RepoDomain} from '../src/types'; | ||
import { | ||
Changes, | ||
FileData, | ||
TreeObject, | ||
RepoDomain, | ||
CommitData, | ||
CommitSigner, | ||
} from '../src/types'; | ||
import {createCommit} from '../src/github/create-commit'; | ||
import {CommitError} from '../src/errors'; | ||
|
||
|
@@ -37,6 +44,17 @@ type CreateCommitResponse = GetResponseTypeFromEndpointMethod< | |
typeof octokit.git.createCommit | ||
>; | ||
|
||
class FakeCommitSigner implements CommitSigner { | ||
signature: string; | ||
constructor(signature: string) { | ||
this.signature = signature; | ||
} | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
async generateSignature(_commit: CommitData): Promise<string> { | ||
return this.signature; | ||
} | ||
} | ||
|
||
before(() => { | ||
setup(); | ||
}); | ||
|
@@ -199,7 +217,7 @@ describe('Commit', () => { | |
// tests | ||
const sha = await createCommit(octokit, origin, head, treeSha, message); | ||
assert.strictEqual(sha, createCommitResponse.data.sha); | ||
sandbox.assert.calledOnceWithExactly(stubCreateCommit, { | ||
sandbox.assert.calledOnceWithMatch(stubCreateCommit, { | ||
owner: origin.owner, | ||
repo: origin.repo, | ||
message, | ||
|
@@ -324,12 +342,85 @@ describe('Commit and push function', async () => { | |
], | ||
base_tree: getCommitResponse.data.tree.sha, | ||
}); | ||
sandbox.assert.calledOnceWithExactly(stubCreateCommit, { | ||
sandbox.assert.calledOnceWithMatch(stubCreateCommit, { | ||
owner: origin.owner, | ||
repo: origin.repo, | ||
message, | ||
tree: createTreeResponse.data.sha, | ||
parents: [oldHeadSha], | ||
}); | ||
sandbox.assert.calledOnceWithExactly(stubUpdateRef, { | ||
owner: origin.owner, | ||
repo: origin.repo, | ||
sha: createCommitResponse.data.sha, | ||
ref: 'heads/test-branch-name', | ||
force: true, | ||
}); | ||
}); | ||
it('allows configuring a commit signer', async () => { | ||
changes.set('foo.txt', { | ||
mode: '100755', | ||
content: 'some file content', | ||
}); | ||
// setup | ||
const stubGetCommit = sandbox | ||
.stub(octokit.git, 'getCommit') | ||
.resolves(getCommitResponse); | ||
const stubCreateTree = sandbox | ||
.stub(octokit.git, 'createTree') | ||
.resolves(createTreeResponse); | ||
const stubCreateCommit = sandbox | ||
.stub(octokit.git, 'createCommit') | ||
.resolves(createCommitResponse); | ||
const stubUpdateRef = sandbox.stub(octokit.git, 'updateRef'); | ||
const fakeSigner = new FakeCommitSigner('fake-signature'); | ||
const options = { | ||
author: { | ||
name: 'Test Committer', | ||
email: '[email protected]', | ||
}, | ||
signer: fakeSigner, | ||
}; | ||
const signatureSpy = sandbox.spy(fakeSigner, 'generateSignature'); | ||
// tests | ||
await handler.commitAndPush( | ||
octokit, | ||
oldHeadSha, | ||
changes, | ||
{branch: branchName, ...origin}, | ||
message, | ||
true, | ||
options | ||
); | ||
sandbox.assert.calledOnceWithExactly(stubGetCommit, { | ||
owner: origin.owner, | ||
repo: origin.repo, | ||
commit_sha: oldHeadSha, | ||
}); | ||
sandbox.assert.calledWithExactly(stubCreateTree, { | ||
owner: origin.owner, | ||
repo: origin.repo, | ||
tree: [ | ||
{ | ||
path: 'foo.txt', | ||
mode: '100755', | ||
type: 'blob', | ||
content: 'some file content', | ||
}, | ||
], | ||
base_tree: getCommitResponse.data.tree.sha, | ||
}); | ||
sandbox.assert.calledOnceWithMatch(stubCreateCommit, { | ||
owner: origin.owner, | ||
repo: origin.repo, | ||
message, | ||
tree: createTreeResponse.data.sha, | ||
parents: [oldHeadSha], | ||
signature: 'fake-signature', | ||
author: { | ||
name: 'Test Committer', | ||
email: '[email protected]', | ||
}, | ||
}); | ||
sandbox.assert.calledOnceWithExactly(stubUpdateRef, { | ||
owner: origin.owner, | ||
|
@@ -338,6 +429,15 @@ describe('Commit and push function', async () => { | |
ref: 'heads/test-branch-name', | ||
force: true, | ||
}); | ||
sandbox.assert.calledOnceWithMatch(signatureSpy, { | ||
message, | ||
tree: createTreeResponse.data.sha, | ||
parents: [oldHeadSha], | ||
author: { | ||
name: 'Test Committer', | ||
email: '[email protected]', | ||
}, | ||
}); | ||
}); | ||
it('Forwards GitHub error if getCommit fails', async () => { | ||
const error = new Error('Error committing'); | ||
|
@@ -396,7 +496,7 @@ describe('Commit and push function', async () => { | |
{branch: branchName, ...origin}, | ||
message, | ||
true, | ||
6 | ||
{filesPerCommit: 6} | ||
); | ||
|
||
sinon.assert.calledTwice(createTreeStub); | ||
|