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鈥檒l 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 4 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 35 additions & 11 deletions src/github/create-commit.ts
Expand Up @@ -44,15 +44,35 @@ export async function createCommit(
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;
// 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,
}
}

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 @@ export async function createCommit(
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
Expand Up @@ -22,7 +22,7 @@ import {
FileDiffContent,
CreateReviewCommentUserOptions,
} from './types';
export {Changes, CommitData, CommitSigner} from './types';
export {Changes, CommitData, CommitDataWithRequiredDate, CommitSigner} from './types';
import {Octokit} from '@octokit/rest';
import {logger, setupLogger} from './logger';
import {
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
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
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: 'fake.email@example.com',
},
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: 'fake.email@example.com',
date: new Date(clock.now).toISOString(),
},
signature: 'fake-signature',
});
});
});

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