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: add variable commit_date #471

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
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'}}})
}));
12 changes: 10 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,9 +116,10 @@ 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');
trim21 marked this conversation as resolved.
Show resolved Hide resolved
expect(context.commitDate).toEqual(new Date('2024-11-13T13:42:28.000Z'));
});

it('git', async () => {
Expand All @@ -125,9 +129,13 @@ 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('2023-01-01T13:42:28.000Z');
});
const context = await getContext(ContextSource.git, toolkit);
expect(context.ref).toEqual('refs/heads/git-test');
expect(context.sha).toEqual('git-test-sha');
trim21 marked this conversation as resolved.
Show resolved Hide resolved
expect(context.commitDate).toEqual(new Date('2023-01-01T13:42:28.000Z'));
});
});

Expand Down
140 changes: 127 additions & 13 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 @@ -768,6 +779,7 @@ describe('push', () => {
`type=raw,value=mytag-{{branch}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD'}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}`,
`type=raw,value=mytag-src-{{commit_date 'YYYYMMDD'}}`,
`type=raw,value=mytag-tag-{{tag}}`,
`type=raw,value=mytag-baseref-{{base_ref}}`,
`type=raw,value=mytag-defbranch,enable={{is_default_branch}}`
Expand All @@ -778,6 +790,7 @@ describe('push', () => {
partial: [
'mytag-20200110',
'mytag-20200110-093000',
'mytag-src-20200110',
'mytag-tag-',
'mytag-baseref-',
'mytag-defbranch'
Expand All @@ -788,6 +801,7 @@ describe('push', () => {
'mytag-master',
'mytag-20200110',
'mytag-20200110-093000',
'mytag-src-20200110',
'mytag-tag-',
'mytag-baseref-',
'mytag-defbranch'
Expand Down Expand Up @@ -2621,6 +2635,35 @@ describe('pr', () => {
],
undefined
],
[
'pr12',
'event_pull_request.env',
{
images: ['org/app'],
tags: [
`type=raw,value={{commit_date YYYY-MM-DD-HHmmSS}}`,
]
} as Inputs,
{
main: "2020-01-10T00-30-00Z",
partial: [],
latest: false
} as Version,
[
'org/app:2020-01-10T00-30-00Z'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=a9c8c5828b91be19d9728548b24759e352367ef1",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=2020-01-10T00-30-00Z"
],
undefined
],
])('given %p with %p event', tagsLabelsTest);
});

Expand Down Expand Up @@ -2969,13 +3012,41 @@ describe('pr-head-sha', () => {
"org.opencontainers.image.version=mytag-master"
]
],
[
'pr12',
'event_pull_request.env',
{
images: ['org/app'],
tags: [
`type=raw,value=src-{{commit_date YYYY-MM-DD}}`,
]
} as Inputs,
{
main: "src-2020-01-10T00-30-00Z",
partial: [],
latest: false
} as Version,
[
"org/app:src-2020-01-10T00-30-00Z",
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=3370e228f2209994d57af4427fe64e71bb79ac96",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=src-2020-01-10T00-30-00Z",
]
],
])('given %p with %p event', async (name: string, envFile: string, inputs: Inputs, exVersion: Version, exTags: Array<string>, exLabelsAnnotations: Array<string>) => {
process.env = dotenv.parse(fs.readFileSync(path.join(__dirname, 'fixtures', envFile)));
process.env.DOCKER_METADATA_PR_HEAD_SHA = 'true';

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 @@ -3028,16 +3099,20 @@ describe('schedule', () => {
{
images: ['user/app'],
tags: [
`type=schedule,pattern={{date 'YYYYMMDD'}}`
`type=schedule,pattern={{date 'YYYYMMDD'}}`,
`type=schedule,pattern=source-date-{{commit_date 'YYYY-MM-DD'}}`
]
} as Inputs,
{
main: '20200110',
partial: [],
partial: [
"source-date-2020-01-10",
],
latest: false
} as Version,
[
'user/app:20200110'
'user/app:20200110',
'user/app:source-date-2020-01-10'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
Expand Down Expand Up @@ -3221,16 +3296,20 @@ describe('schedule', () => {
{
images: ['user/app'],
tags: [
`type=schedule,pattern={{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}`
`type=schedule,pattern={{date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}`,
`type=schedule,pattern=src-{{commit_date 'YYYYMMDD-HHmmss' tz='Asia/Tokyo'}}`,
]
} as Inputs,
{
main: '20200110-093000',
partial: [],
partial: [
"src-20200110-093000",
],
latest: false
} as Version,
[
'user/app:20200110-093000'
'user/app:20200110-093000',
'user/app:src-20200110-093000'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
Expand Down Expand Up @@ -3312,6 +3391,39 @@ describe('release', () => {
"org.opencontainers.image.version=v1.1.1"
],
undefined
],
[
'release03',
'event_release_created.env',
{
images: ['user/app'],
tags: [
`type=raw,value=src-{{commit_date 'YYYYMMDD-HHmmss'}}`,
`type=raw,value={{date 'YYYYMMDD-HHmmss'}}`,
]
} as Inputs,
{
"main": "src-20200110-003000",
partial: [
"20200110-003000",
],
"latest": false,
} as Version,
[
"user/app:src-20200110-003000",
"user/app:20200110-003000",
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=src-20200110-003000",
],
undefined
]
])('given %s with %p event', tagsLabelsTest);
});
Expand Down Expand Up @@ -4012,7 +4124,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 +4640,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 +4704,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 +4750,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.

Loading