Skip to content

Commit

Permalink
squash
Browse files Browse the repository at this point in the history
Signed-off-by: Trim21 <trim21.me@gmail.com>
  • Loading branch information
trim21 committed Nov 13, 2024
1 parent 32323e5 commit 02b92e6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 11 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
2 changes: 2 additions & 0 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,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 @@ -642,6 +643,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
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.

56 changes: 50 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import * as fs from 'node:fs/promises';

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 +46,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 +76,46 @@ 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 eventFile = await fs.readFile(process.env.GITHUB_EVENT_PATH!, 'utf-8');

const event = JSON.parse(eventFile) as unknown as {
commits?: Array<{
timestamp: string;
// commit sha
id: string;
}>;
};

if (event.commits) {
const commitDate = event.commits.find(x => x.id === sha)?.timestamp;
if (!commitDate) {
throw new Error('failed to get commit date from event');
}

return new Date(commitDate);
}

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);
}
2 changes: 1 addition & 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 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 02b92e6

Please sign in to comment.