Skip to content

Commit

Permalink
Add timezone parameters to type schedule and raw
Browse files Browse the repository at this point in the history
  • Loading branch information
chroju committed Dec 29, 2022
1 parent c98ac5e commit 9b7f0ff
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,12 @@ tags: |
type=schedule,pattern=nightly
# handlebars
type=schedule,pattern={{date 'YYYYMMDD'}}
# handlebars with timezone
type=schedule,pattern={{date 'YYYYMMDD-hhmmss'}},timezone=Asia/Tokyo
```
Will be used on [schedule event](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule).
If you set `timezone`, tags will be generated accordingly. Default is `UTC`.
`pattern` is a specially crafted attribute to support [Handlebars' template](https://handlebarsjs.com/guide/)
with the following expressions:
Expand Down
37 changes: 34 additions & 3 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {beforeEach, describe, expect, jest, test} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import * as dotenv from 'dotenv';
import * as moment from 'moment';
import moment from 'moment-timezone';
import {getInputs, Inputs} from '../src/context';
import * as github from '../src/github';
import {Meta, Version} from '../src/meta';
Expand All @@ -21,8 +21,8 @@ jest.spyOn(global.Date.prototype, 'toISOString').mockImplementation(() => {
return '2020-01-10T00:30:00.000Z';
});

jest.mock('moment', () => {
return () => (jest.requireActual('moment') as typeof import('moment'))('2020-01-10T00:30:00.000Z');
jest.mock('moment-timezone', () => {
return () => (jest.requireActual('moment-timezone') as typeof import('moment-timezone'))('2020-01-10T00:30:00.000Z');
});

beforeEach(() => {
Expand Down Expand Up @@ -599,6 +599,7 @@ describe('push', () => {
tags: [
`type=raw,value=mytag-{{branch}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD'}}`,
`type=raw,value=mytag-{{date 'YYYYMMDD-HHmmss'}},timezone=Asia/Tokyo`,
`type=raw,value=mytag-tag-{{tag}}`,
`type=raw,value=mytag-baseref-{{base_ref}}`,
`type=raw,value=mytag-defbranch,enable={{is_default_branch}}`
Expand All @@ -608,6 +609,7 @@ describe('push', () => {
main: 'mytag-master',
partial: [
'mytag-20200110',
'mytag-20200110-093000',
'mytag-tag-',
'mytag-baseref-',
'mytag-defbranch'
Expand All @@ -617,6 +619,7 @@ describe('push', () => {
[
'user/app:mytag-master',
'user/app:mytag-20200110',
'user/app:mytag-20200110-093000',
'user/app:mytag-tag-',
'user/app:mytag-baseref-',
'user/app:mytag-defbranch'
Expand Down Expand Up @@ -2995,6 +2998,34 @@ describe('schedule', () => {
"org.opencontainers.image.licenses=MIT"
]
],
[
'schedule08',
'event_schedule.env',
{
images: ['user/app'],
tags: [
`type=schedule,pattern={{date 'YYYYMMDD-HHmmss'}},timezone=Asia/Tokyo`
]
} as Inputs,
{
main: '20200110-093000',
partial: [],
latest: false
} as Version,
[
'user/app:20200110-093000'
],
[
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=20200110-093000",
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.licenses=MIT"
]
],
])('given %p with %p event', tagsLabelsTest);
});

Expand Down
67 changes: 60 additions & 7 deletions __tests__/tag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ describe('transform', () => {
`type=ref,event=tag`,
`type=ref,event=pr`,
`type=schedule`,
`type=schedule,timezone=Asia/Tokyo`,
`type=sha`,
`type=raw,foo`,
`type=raw,foo,timezone=Asia/Tokyo`,
`type=edge`,
`type=semver,pattern={{version}}`,
`type=match,"pattern=\\d.\\d.\\d",group=0`
Expand All @@ -22,7 +24,17 @@ describe('transform', () => {
attrs: {
"priority": DefaultPriorities[Type.Schedule],
"enable": "true",
"pattern": "nightly"
"pattern": "nightly",
"timezone": "UTC"
}
},
{
type: Type.Schedule,
attrs: {
"priority": DefaultPriorities[Type.Schedule],
"enable": "true",
"pattern": "nightly",
"timezone": "Asia/Tokyo"
}
},
{
Expand Down Expand Up @@ -82,7 +94,17 @@ describe('transform', () => {
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "true",
"value": "foo"
"value": "foo",
"timezone": "UTC"
}
},
{
type: Type.Raw,
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "true",
"value": "foo",
"timezone": "Asia/Tokyo"
}
},
{
Expand Down Expand Up @@ -121,7 +143,21 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Schedule],
"enable": "true",
"pattern": "{{date 'YYYYMMDD'}}"
"pattern": "{{date 'YYYYMMDD'}}",
"timezone": "UTC"
}
} as Tag,
false
],
[
`type=schedule,enable=true,pattern={{date 'YYYYMMDD'}},timezone=Asia/Tokyo`,
{
type: Type.Schedule,
attrs: {
"priority": DefaultPriorities[Type.Schedule],
"enable": "true",
"pattern": "{{date 'YYYYMMDD'}}",
"timezone": "Asia/Tokyo"
}
} as Tag,
false
Expand Down Expand Up @@ -304,7 +340,8 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "true",
"value": "acustomtag"
"value": "acustomtag",
"timezone": "UTC"
}
} as Tag,
false
Expand All @@ -321,7 +358,21 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "true",
"value": "acustomtag2"
"value": "acustomtag2",
"timezone": "UTC"
}
} as Tag,
false
],
[
`type=raw,value=acustomtag2,timezone=Asia/Tokyo`,
{
type: Type.Raw,
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "true",
"value": "acustomtag2",
"timezone": "Asia/Tokyo"
}
} as Tag,
false
Expand All @@ -333,7 +384,8 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "true",
"value": "acustomtag4"
"value": "acustomtag4",
"timezone": "UTC"
}
} as Tag,
false
Expand All @@ -345,7 +397,8 @@ describe('parse', () => {
attrs: {
"priority": DefaultPriorities[Type.Raw],
"enable": "false",
"value": "acustomtag5"
"value": "acustomtag5",
"timezone": "UTC"
}
} as Tag,
false
Expand Down
30 changes: 18 additions & 12 deletions 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.

23 changes: 23 additions & 0 deletions dist/licenses.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"csv-parse": "^5.3.3",
"handlebars": "^4.7.7",
"moment": "^2.29.4",
"moment-timezone": "^0.5.40",
"semver": "^7.3.7"
},
"devDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions src/meta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as handlebars from 'handlebars';
import * as fs from 'fs';
import * as path from 'path';
import moment from 'moment';
import moment from 'moment-timezone';
import * as pep440 from '@renovate/pep440';
import * as semver from 'semver';
import {Inputs, tmpDir} from './context';
Expand Down Expand Up @@ -130,7 +130,7 @@ export class Meta {
const vraw = this.setValue(
handlebars.compile(tag.attrs['pattern'])({
date: function (format) {
return moment(currentDate).utc().format(format);
return moment(currentDate).tz(tag.attrs['timezone']).format(format);
}
}),
tag
Expand Down Expand Up @@ -299,7 +299,7 @@ export class Meta {
}

private procRaw(version: Version, tag: tcl.Tag): Version {
const vraw = this.setValue(this.setGlobalExp(tag.attrs['value']), tag);
const vraw = this.setValue(this.setGlobalExp(tag.attrs['value'], tag.attrs['timezone']), tag);
return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true');
}

Expand Down Expand Up @@ -359,7 +359,8 @@ export class Meta {
return val;
}

private setGlobalExp(val): string {
private setGlobalExp(val, tz = 'UTC'): string {
const timezone = tz;
const ctx = this.context;
const currentDate = this.date;
return handlebars.compile(val)({
Expand Down Expand Up @@ -412,7 +413,7 @@ export class Meta {
return 'false';
},
date: function (format) {
return moment(currentDate).utc().format(format);
return moment(currentDate).tz(timezone).format(format);
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export function Parse(s: string): Tag {
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'pattern')) {
tag.attrs['pattern'] = 'nightly';
}
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'timezone')) {
tag.attrs['timezone'] = 'UTC';
}
break;
}
case Type.Semver:
Expand Down Expand Up @@ -180,6 +183,9 @@ export function Parse(s: string): Tag {
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'value')) {
throw new Error(`Missing value attribute for ${s}`);
}
if (!Object.prototype.hasOwnProperty.call(tag.attrs, 'timezone')) {
tag.attrs['timezone'] = 'UTC';
}
break;
}
case Type.Sha: {
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2954,7 +2954,14 @@ minimist@^1.2.5:
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.6.tgz#8637a5b759ea0d6e98702cfb3a9283323c93af44"
integrity sha512-Jsjnk4bw3YJqYzbdyBiNsPWHPfO++UGG749Cxs6peCu5Xg4nrena6OVxOYxrQTqww0Jmwt+Ref8rggumkTLz9Q==

moment@^2.29.4:
moment-timezone@^0.5.40:
version "0.5.40"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.40.tgz#c148f5149fd91dd3e29bf481abc8830ecba16b89"
integrity sha512-tWfmNkRYmBkPJz5mr9GVDn9vRlVZOTe6yqY92rFxiOdWXbjaR0+9LwQnZGGuNR63X456NqmEkbskte8tWL5ePg==
dependencies:
moment ">= 2.9.0"

"moment@>= 2.9.0", moment@^2.29.4:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==
Expand Down

0 comments on commit 9b7f0ff

Please sign in to comment.