Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create/Pass commit date to commit for signing #485

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 35 additions & 11 deletions src/github/create-commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,35 @@
options: CreateCommitOptions = {}
): Promise<string> {
try {
const signature = options.signer
? await options.signer.generateSignature({
message,
tree: treeSha,
parents: [refHead],
author: options.author,
committer: options.committer,
})
: undefined;
let signature: string | undefined;
if (options.signer) {
const commitDate = new Date();
let author, committer: Required<UserData> | undefined = undefined;

Check failure on line 50 in src/github/create-commit.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·······`
// Attach author/commit date.
if (options.author) {
author = {
...options.author,
date: options.author.date ?? commitDate,
};
}
if (options.committer) {
committer = {
...options.committer,
date: options.committer.date ?? commitDate,
}

Check failure on line 62 in src/github/create-commit.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`
}

signature = await options.signer.generateSignature({
message,
tree: treeSha,
parents: [refHead],
author,
committer,
});
} else {
signature = undefined;
}

const {
data: {sha, url},
} = await octokit.git.createCommit({
Expand All @@ -62,8 +82,12 @@
tree: treeSha,
parents: [refHead],
signature,
author: options.author,
committer: options.committer,
author: options.author
? {...options.author, date: options.author.date?.toISOString()}
: undefined,
committer: options.committer
? {...options.committer, date: options.committer.date?.toISOString()}
: undefined,
});
logger.info(`Successfully created commit. See commit at ${url}`);
return sha;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
FileDiffContent,
CreateReviewCommentUserOptions,
} from './types';
export {Changes, CommitData, CommitSigner} from './types';
export {Changes, CommitData, CommitDataWithRequiredDate, CommitSigner} from './types';

Check failure on line 25 in src/index.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `Changes,·CommitData,·CommitDataWithRequiredDate,·CommitSigner` with `⏎··Changes,⏎··CommitData,⏎··CommitDataWithRequiredDate,⏎··CommitSigner,⏎`
import {Octokit} from '@octokit/rest';
import {logger, setupLogger} from './logger';
import {
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export interface Logger {
export interface UserData {
name: string;
email: string;
date?: Date;
}

export interface CommitData {
Expand All @@ -216,6 +217,11 @@ export interface CommitData {
committer?: UserData;
}

export interface CommitDataWithRequiredDate extends CommitData {
author?: Required<UserData>;
committer?: Required<UserData>;
}

export interface CommitSigner {
generateSignature(commit: CommitData): Promise<string>;
generateSignature(commit: CommitDataWithRequiredDate): Promise<string>;
}
45 changes: 44 additions & 1 deletion test/commit-and-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/* eslint-disable node/no-unsupported-features/node-builtins */

import * as assert from 'assert';
import {describe, it, before, afterEach} from 'mocha';
import {describe, it, before, afterEach, beforeEach} from 'mocha';
import {octokit, setup} from './util';
import * as sinon from 'sinon';
import {GetResponseTypeFromEndpointMethod} from '@octokit/types';
Expand All @@ -31,6 +31,7 @@ import {
} from '../src/types';
import {createCommit} from '../src/github/create-commit';
import {CommitError} from '../src/errors';
import {SinonFakeTimers} from 'sinon';

type GetCommitResponse = GetResponseTypeFromEndpointMethod<
typeof octokit.git.getCommit
Expand Down Expand Up @@ -190,8 +191,13 @@ describe('Push', () => {

describe('Commit', () => {
const sandbox = sinon.createSandbox();
let clock: SinonFakeTimers;
beforeEach(() => {
clock = sinon.useFakeTimers();
});
afterEach(() => {
sandbox.restore();
clock.restore();
});
const origin: RepoDomain = {
owner: 'Foo',
Expand Down Expand Up @@ -225,6 +231,43 @@ describe('Commit', () => {
parents: [head],
});
});
it('Uses current date when date in author/committer is undefined and commit signing is enabled', async () => {
// setup
const createCommitResponseData = await import(
'./fixtures/create-commit-response.json'
);
const createCommitResponse = {
headers: {},
status: 201,
url: 'http://fake-url.com',
data: createCommitResponseData,
} as unknown as CreateCommitResponse;
const stubCreateCommit = sandbox
.stub(octokit.git, 'createCommit')
.resolves(createCommitResponse);
// tests
const sha = await createCommit(octokit, origin, head, treeSha, message, {
author: {
name: 'Fake Author',
email: '[email protected]',
},
signer: new FakeCommitSigner('fake-signature'),
});
assert.strictEqual(sha, createCommitResponse.data.sha);
sandbox.assert.calledOnceWithMatch(stubCreateCommit, {
owner: origin.owner,
repo: origin.repo,
message,
tree: treeSha,
parents: [head],
author: {
name: 'Fake Author',
email: '[email protected]',
date: new Date(clock.now).toISOString(),
},
signature: 'fake-signature',
});
});
});

describe('Update branch reference', () => {
Expand Down
Loading