Skip to content

Commit

Permalink
feat: add variable commit_date
Browse files Browse the repository at this point in the history
Signed-off-by: Trim21 <[email protected]>
  • Loading branch information
trim21 committed Nov 14, 2024
1 parent 44d81d6 commit ce0e6b3
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 21 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,19 @@ Default `tz` is UTC.
| `{{date 'dddd, MMMM Do YYYY, h:mm:ss a'}}` | `Friday, January 10th 2020, 3:25:50 pm` |
| `{{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}` | `20200110-093000` |

#### `{{commit_date '<format>' tz='<timezone>'}}`

Returns the date when current git commit is committed.
rendered by its [moment format](https://momentjs.com/docs/#/displaying/format/).

Default `tz` is UTC.

| Expression | Output example |
|----------------------------------------------|-----------------------------------------|
| `{{commit_date 'YYYYMMDD'}}` | `20200110` |
| `{{commit_date 'dddd, MMMM Do YYYY, h:mm:ss a'}}` | `Friday, January 10th 2020, 3:25:50 pm` |
| `{{commit_date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}` | `20200110-093000` |

### Major version zero

Major version zero (`0.y.z`) is for initial development and **may** change at
Expand Down
4 changes: 3 additions & 1 deletion __mocks__/@actions/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,6 @@ export const context = {
}
};

export const getOctokit = jest.fn();
export const getOctokit = jest.fn(() => ({
request: () => Promise.resolve({data: {committer: {date: '2024-11-13T13:42:28Z'}}})
}));
10 changes: 8 additions & 2 deletions __tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {Git} from '@docker/actions-toolkit/lib/git';
import {GitHub} from '@docker/actions-toolkit/lib/github';

import {ContextSource, getContext, getInputs, Inputs} from '../src/context';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';

const toolkit = new Toolkit({githubToken: 'fake-github-token'});

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -113,7 +116,7 @@ describe('getContext', () => {
});

it('workflow', async () => {
const context = await getContext(ContextSource.workflow);
const context = await getContext(ContextSource.workflow, toolkit);
expect(context.ref).toEqual('refs/heads/dev');
expect(context.sha).toEqual('5f3331d7f7044c18ca9f12c77d961c4d7cf3276a');
});
Expand All @@ -125,7 +128,10 @@ describe('getContext', () => {
sha: 'git-test-sha'
} as Context);
});
const context = await getContext(ContextSource.git);
jest.spyOn(Git, 'commitDate').mockImplementation(async (): Promise<Date> => {
return new Date();
});
const context = await getContext(ContextSource.git, toolkit);
expect(context.ref).toEqual('refs/heads/git-test');
expect(context.sha).toEqual('git-test-sha');
});
Expand Down
27 changes: 20 additions & 7 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ beforeEach(() => {
delete process.env[key];
}
});

jest.spyOn(GitHub, 'context', 'get').mockImplementation((): Context => {
return new Context();
//@ts-expect-error partial info
return {
...new Context(),
repo: {
owner: 'docker',
repo: 'repo'
}
};
});
});

Expand All @@ -53,7 +61,7 @@ const tagsLabelsTest = async (name: string, envFile: string, inputs: Inputs, exV
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow, toolkit), repo);

const version = meta.version;
expect(version).toEqual(exVersion);
Expand Down Expand Up @@ -622,6 +630,7 @@ describe('push', () => {
tags: [
`type=raw,value=mytag-{{branch}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD'}}`,
`type=raw,value=mytag-cd-{{commit_date 'YYYYMMDD'}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}`,
`type=raw,value=mytag-tag-{{tag}}`,
`type=raw,value=mytag-baseref-{{base_ref}}`,
Expand All @@ -632,6 +641,7 @@ describe('push', () => {
main: 'mytag-master',
partial: [
'mytag-20200110',
"mytag-cd-20200110",
'mytag-20200110-093000',
'mytag-tag-',
'mytag-baseref-',
Expand All @@ -642,6 +652,7 @@ describe('push', () => {
[
'user/app:mytag-master',
'user/app:mytag-20200110',
'user/app:mytag-cd-20200110',
'user/app:mytag-20200110-093000',
'user/app:mytag-tag-',
'user/app:mytag-baseref-',
Expand Down Expand Up @@ -2975,7 +2986,7 @@ describe('pr-head-sha', () => {

const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow, toolkit), repo);

const version = meta.version;
expect(version).toEqual(exVersion);
Expand Down Expand Up @@ -4012,7 +4023,7 @@ describe('json', () => {

const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow,toolkit), repo);

const jsonOutput = meta.getJSON(['manifest']);
expect(jsonOutput).toEqual(exJSON);
Expand Down Expand Up @@ -4528,7 +4539,7 @@ describe('bakeFile', () => {

const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow,toolkit), repo);

const bakeFileTags = meta.getBakeFile('tags');
expect(JSON.parse(fs.readFileSync(bakeFileTags, 'utf8'))).toEqual(exBakeTags);
Expand Down Expand Up @@ -4592,7 +4603,7 @@ describe('bakeFileTagsLabels', () => {

const toolkit = new Toolkit();
const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow,toolkit), repo);

const bakeFile = meta.getBakeFileTagsLabels();
expect(JSON.parse(fs.readFileSync(bakeFile, 'utf8'))).toEqual(exBakeDefinition);
Expand Down Expand Up @@ -4638,8 +4649,10 @@ describe('sepTags', () => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));

const toolkit = new Toolkit();

const repo = await toolkit.github.repoData();
const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow), repo);

const meta = new Meta({...getInputs(), ...inputs}, await getContext(ContextSource.workflow, toolkit), repo);

expect(meta.getTags().join(inputs.sepTags)).toEqual(expTags);
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

64 changes: 58 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import * as core from '@actions/core';
import {Context} from '@actions/github/lib/context';
import {Context as GithubContext} from '@actions/github/lib/context';
import {Util} from '@docker/actions-toolkit/lib/util';
import {Git} from '@docker/actions-toolkit/lib/git';
import {GitHub} from '@docker/actions-toolkit/lib/github';
import {Toolkit} from '@docker/actions-toolkit/lib/toolkit';

export interface Context extends GithubContext {
commitDate: Date;
}

export interface Inputs {
context: ContextSource;
Expand Down Expand Up @@ -39,18 +44,18 @@ export enum ContextSource {
git = 'git'
}

export async function getContext(source: ContextSource): Promise<Context> {
export async function getContext(source: ContextSource, toolkit: Toolkit): Promise<Context> {
switch (source) {
case ContextSource.workflow:
return getContextFromWorkflow();
return await getContextFromWorkflow(toolkit);
case ContextSource.git:
return await getContextFromGit();
default:
throw new Error(`Invalid context source: ${source}`);
}
}

function getContextFromWorkflow(): Context {
async function getContextFromWorkflow(toolkit: Toolkit): Promise<Context> {
const context = GitHub.context;

// Needs to override Git reference with pr ref instead of upstream branch ref
Expand All @@ -69,9 +74,56 @@ function getContextFromWorkflow(): Context {
}
}

return context;
return {
commitDate: await getCommitDateFromWorkflow(context.sha, toolkit),
...context
} as Context;
}

async function getContextFromGit(): Promise<Context> {
return await Git.context();
const ctx = await Git.context();

return {
commitDate: await Git.commitDate(ctx.sha),
...ctx
} as Context;
}

async function getCommitDateFromWorkflow(sha: string, toolkit: Toolkit): Promise<Date> {
const event = GitHub.context.payload as unknown as {
// branch push
commits?: Array<{
timestamp: string;
// commit sha
id: string;
}>;
// tags
head_commit?: {
timestamp: string;
// commit sha
id: string;
};
};

if (event.commits) {
const commitDate = event.commits.find(x => x.id === sha)?.timestamp;
if (commitDate) {
return new Date(commitDate);
}
}

if (event.head_commit) {
if (event.head_commit.id === sha) {
return new Date(event.head_commit.timestamp);
}
}

// fallback to github api for commit date
const commit = await toolkit.github.octokit.request('GET /repos/{owner}/{repo}/commits/{commit_sha}', {
commit_sha: sha,
owner: GitHub.context.repo.owner,
repo: GitHub.context.repo.repo
});

return new Date(commit.data.committer.date);
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ actionsToolkit.run(
async () => {
const inputs: Inputs = getInputs();
const toolkit = new Toolkit({githubToken: inputs.githubToken});
const context = await getContext(inputs.context);
const context = await getContext(inputs.context, toolkit);
const repo = await toolkit.github.repoData();

await core.group(`Context info`, async () => {
Expand All @@ -23,6 +23,7 @@ actionsToolkit.run(
core.info(`actor: ${context.actor}`);
core.info(`runNumber: ${context.runNumber}`);
core.info(`runId: ${context.runId}`);
core.info(`commitDate: ${context.commitDate}`);
});

if (core.isDebug()) {
Expand Down
18 changes: 16 additions & 2 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import moment from 'moment-timezone';
import * as pep440 from '@renovate/pep440';
import * as semver from 'semver';
import * as core from '@actions/core';
import {Context} from '@actions/github/lib/context';
import {Context as ToolkitContext} from '@docker/actions-toolkit/lib/context';
import {GitHubRepo} from '@docker/actions-toolkit/lib/types/github';

import {Inputs} from './context';
import {Inputs, Context} from './context';
import * as icl from './image';
import * as tcl from './tag';
import * as fcl from './flavor';
Expand Down Expand Up @@ -361,6 +360,7 @@ export class Meta {
private setGlobalExp(val): string {
const context = this.context;
const currentDate = this.date;
const commitDate = this.context.commitDate;
return handlebars.compile(val)({
branch: function () {
if (!/^refs\/heads\//.test(context.ref)) {
Expand Down Expand Up @@ -388,6 +388,20 @@ export class Meta {
}
return '';
},
commit_date: function (format, options) {
const m = moment(commitDate);
let tz = 'UTC';
Object.keys(options.hash).forEach(key => {
switch (key) {
case 'tz':
tz = options.hash[key];
break;
default:
throw new Error(`Unknown ${key} attribute`);
}
});
return m.tz(tz).format(format);
},
is_default_branch: function () {
const branch = context.ref.replace(/^refs\/heads\//g, '');
// TODO: "base_ref" is available in the push payload but doesn't always seem to
Expand Down

0 comments on commit ce0e6b3

Please sign in to comment.