From 85f4f732f234d496a575df3507c2025928022467 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Tue, 25 May 2021 18:45:23 +0200 Subject: [PATCH] Fix `pull_request_target` event Signed-off-by: CrazyMax --- __tests__/meta.test.ts | 93 +++++++++++++++++++++++++++++------------- dist/index.js | 13 +++--- src/meta.ts | 14 ++++--- 3 files changed, 80 insertions(+), 40 deletions(-) diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index 358492e..2e0c3e9 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -619,36 +619,36 @@ describe('push', () => { ], [ 'push18', - 'event_push.env', - { - images: ['org/app', 'ghcr.io/user/app'], - tags: [ - `type=ref,event=branch`, - `type=sha,format=long` - ], - } as Inputs, - { - main: 'dev', - partial: ['sha-90dd6032fac8bda1b6c4436a2e65de27961ed071'], - latest: false - } as Version, - [ - 'org/app:dev', - 'org/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071', - 'ghcr.io/user/app:dev', - 'ghcr.io/user/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071' + 'event_push.env', + { + images: ['org/app', 'ghcr.io/user/app'], + tags: [ + `type=ref,event=branch`, + `type=sha,format=long` ], - [ - "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=dev", - "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", - "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", - "org.opencontainers.image.licenses=MIT" - ] + } as Inputs, + { + main: 'dev', + partial: ['sha-90dd6032fac8bda1b6c4436a2e65de27961ed071'], + latest: false + } as Version, + [ + 'org/app:dev', + 'org/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071', + 'ghcr.io/user/app:dev', + 'ghcr.io/user/app:sha-90dd6032fac8bda1b6c4436a2e65de27961ed071' ], + [ + "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=dev", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] + ] ])('given %p with %p event', tagsLabelsTest); }); @@ -1840,6 +1840,43 @@ describe('pr', () => { "org.opencontainers.image.revision=f24900bfcfee76d8055c8421a164c7e57ad20e68", "org.opencontainers.image.licenses=MIT" ] + ], + [ + 'pr09', + 'event_pull_request_target.env', + { + images: ['org/app'], + tags: [ + `type=ref,event=tag`, + `type=ref,event=pr`, + `type=ref,event=branch`, + `type=sha`, + `type=sha,format=long` + ] + } as Inputs, + { + main: 'pr-8', + partial: [ + 'sha-f24900b', + 'sha-f24900bfcfee76d8055c8421a164c7e57ad20e68' + ], + latest: false + } as Version, + [ + 'org/app:pr-8', + 'org/app:sha-f24900b', + 'org/app:sha-f24900bfcfee76d8055c8421a164c7e57ad20e68' + ], + [ + "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=pr-8", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=f24900bfcfee76d8055c8421a164c7e57ad20e68", + "org.opencontainers.image.licenses=MIT" + ] ] ])('given %p with %p event', tagsLabelsTest); }); diff --git a/dist/index.js b/dist/index.js index 58f5d80..1cad421 100644 --- a/dist/index.js +++ b/dist/index.js @@ -382,6 +382,11 @@ const fcl = __importStar(__webpack_require__(3716)); const core = __importStar(__webpack_require__(2186)); class Meta { constructor(inputs, context, repo) { + // Needs to override Git reference with pr ref instead of upstream branch ref + // for pull_request_target event + if (/pull_request_target/.test(context.eventName)) { + context.ref = `refs/pull/${context.payload.number}/merge`; + } this.inputs = inputs; this.context = context; this.repo = repo; @@ -531,14 +536,10 @@ class Meta { return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? true : this.flavor.latest == 'true'); } procRefPr(version, tag) { - let ref = this.context.ref; - if (/pull_request_target/.test(this.context.eventName)) { - ref = `refs/pull/${this.context.payload.number}/merge`; - } - if (!/^refs\/pull\//.test(ref)) { + if (!/^refs\/pull\//.test(this.context.ref)) { return version; } - const vraw = this.setValue(ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, ''), tag); + const vraw = this.setValue(this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, ''), tag); return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true'); } procEdge(version, tag) { diff --git a/src/meta.ts b/src/meta.ts index 499fa92..ebcb7a6 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -27,6 +27,12 @@ export class Meta { private readonly date: Date; constructor(inputs: Inputs, context: Context, repo: ReposGetResponseData) { + // Needs to override Git reference with pr ref instead of upstream branch ref + // for pull_request_target event + if (/pull_request_target/.test(context.eventName)) { + context.ref = `refs/pull/${context.payload.number}/merge`; + } + this.inputs = inputs; this.context = context; this.repo = repo; @@ -191,15 +197,11 @@ export class Meta { } private procRefPr(version: Version, tag: tcl.Tag): Version { - let ref = this.context.ref; - if (/pull_request_target/.test(this.context.eventName)) { - ref = `refs/pull/${this.context.payload.number}/merge`; - } - if (!/^refs\/pull\//.test(ref)) { + if (!/^refs\/pull\//.test(this.context.ref)) { return version; } - const vraw = this.setValue(ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, ''), tag); + const vraw = this.setValue(this.context.ref.replace(/^refs\/pull\//g, '').replace(/\/merge$/g, ''), tag); return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? false : this.flavor.latest == 'true'); }