Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Nov 13, 2024
1 parent 708cc3b commit 67c7c03
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ 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 { 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 } from './context';
import * as icl from './image';
import * as tcl from './tag';
import * as fcl from './flavor';
import { Git } from '@docker/actions-toolkit/lib/git';

const defaultShortShaLength = 12;

Expand All @@ -32,6 +33,7 @@ export class Meta {
private readonly tags: tcl.Tag[];
private readonly flavor: fcl.Flavor;
private readonly date: Date;
private readonly commit_date: Promise<Date>;

constructor(inputs: Inputs, context: Context, repo: GitHubRepo) {
this.inputs = inputs;
Expand All @@ -42,6 +44,11 @@ export class Meta {
this.flavor = fcl.Transform(inputs.flavor);
this.date = new Date();
this.version = this.getVersion();
this.commit_date = this.getCommitDate(this.context.sha);
}

private async getCommitDate(rev: string): Promise<Date> {
return await Git.commitDate(rev)
}

private getVersion(): Version {
Expand Down Expand Up @@ -361,6 +368,7 @@ export class Meta {
private setGlobalExp(val): string {
const context = this.context;
const currentDate = this.date;
const commitDate = this.commit_date;
return handlebars.compile(val)({
branch: function () {
if (!/^refs\/heads\//.test(context.ref)) {
Expand Down Expand Up @@ -388,6 +396,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 Expand Up @@ -584,7 +606,7 @@ export class Meta {

private generateBakeFile(dt, suffix?: string): string {
const bakeFile = path.join(ToolkitContext.tmpDir(), `docker-metadata-action-bake${suffix ? `-${suffix}` : ''}.json`);
fs.writeFileSync(bakeFile, JSON.stringify({target: {[this.inputs.bakeTarget]: dt}}, null, 2));
fs.writeFileSync(bakeFile, JSON.stringify({ target: { [this.inputs.bakeTarget]: dt } }, null, 2));
return bakeFile;
}

Expand Down

0 comments on commit 67c7c03

Please sign in to comment.