From 41bdfdf78fef3701930a67a3128901f060dc207c Mon Sep 17 00:00:00 2001 From: Paulo Santana <30875229+hikinine@users.noreply.github.com> Date: Sat, 25 May 2024 09:43:49 -0300 Subject: [PATCH] feat: (#107) webhook listener filter docker events based on image tag. Fixes #107 --- pages/api/deploy/[refreshToken].ts | 49 +++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/pages/api/deploy/[refreshToken].ts b/pages/api/deploy/[refreshToken].ts index 5f88decdc..47cdbda7d 100644 --- a/pages/api/deploy/[refreshToken].ts +++ b/pages/api/deploy/[refreshToken].ts @@ -35,7 +35,20 @@ export default async function handler( const deploymentTitle = extractCommitMessage(req.headers, req.body); const sourceType = application.sourceType; - if (sourceType === "github") { + + if (sourceType === "docker") { + const applicationDockerTag = extractImageTagFromApplication(application.dockerImage); + if (applicationDockerTag) { + const webhookDockerTag = extractImageTagFromRequestEventPayload(req.headers, req.body); + if (webhookDockerTag && (webhookDockerTag !== applicationDockerTag)) { + res.status(301).json({ + message: `Application Image Tag(${applicationDockerTag}) doesn't match request event payload Image Tag(${webhookDockerTag}).` + }); + return; + } + } + } + else if (sourceType === "github") { const branchName = extractBranchName(req.headers, req.body); if (!branchName || branchName !== application.branch) { res.status(301).json({ message: "Branch Not Match" }); @@ -79,6 +92,40 @@ export default async function handler( res.status(400).json({ message: "Error To Deploy Application", error }); } } + +/** + * Return the last part of the image name, which is the tag + * Example: "my-image" => null + * Example: "my-image:latest" => "latest" + * Example: "my-image:1.0.0" => "1.0.0" + * Example: "myregistryhost:5000/fedora/httpd:version1.0" => "version1.0" + * @link https://docs.docker.com/reference/cli/docker/image/tag/ + */ +function extractImageTagFromApplication(dockerImage: string | null) { + if (!dockerImage || typeof dockerImage !== "string") { + return null; + } + + const partsOfDockerImageName = dockerImage.split(':') + if (partsOfDockerImageName.length === 1) { + return null; + } + + return partsOfDockerImageName[partsOfDockerImageName.length - 1]; +} + +/** + * @link https://docs.docker.com/docker-hub/webhooks/#example-webhook-payload + */ +function extractImageTagFromRequestEventPayload(headers: any, body: any) { + if (headers["user-agent"]?.includes("Go-http-client")) { + if (body.push_data && body.repository) { + return body.push_data.tag; + } + } + return null; +} + function extractCommitMessage(headers: any, body: any) { // GitHub if (headers["x-github-event"]) {