From becae464a4010301123d5b69a6965d383f11ebd8 Mon Sep 17 00:00:00 2001 From: Alex Szabo Date: Wed, 29 Nov 2023 14:08:24 +0100 Subject: [PATCH] [Ops] Fix deployment/project purging logic (#172082) ## Summary We've noticed that on certain PRs, the deployments are cleaned up right after they're creating, with the indication that it was stale. (https://github.com/elastic/kibana/pull/170579 - cleaned up in https://buildkite.com/elastic/kibana-purge-cloud-deployments/builds/15440#018c1605-144b-4db5-b29a-e3af3c9b1c70) Turns out, the logic that acquires the last commit's timestamp is relying on the `gh pr` api, and commits are limited there to 100. It's easier to look at the pr's `updatedAt` value. - change purge logic to be based on the PRs updatedAt label instead of the max-100th commit's timestamp See: https://elastic.slack.com/archives/C5UDAFZQU/p1701093461763989 --- .buildkite/scripts/steps/cloud/purge_deployments.ts | 5 ++--- .buildkite/scripts/steps/cloud/purge_projects.ts | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/.buildkite/scripts/steps/cloud/purge_deployments.ts b/.buildkite/scripts/steps/cloud/purge_deployments.ts index a166f09b73d6a..845e6740cd48e 100644 --- a/.buildkite/scripts/steps/cloud/purge_deployments.ts +++ b/.buildkite/scripts/steps/cloud/purge_deployments.ts @@ -23,12 +23,11 @@ const DAY_IN_SECONDS = 60 * 60 * 24; for (const deployment of prDeployments) { try { const prNumber = deployment.name.match(/^kibana-pr-([0-9]+)$/)[1]; - const prJson = execSync(`gh pr view '${prNumber}' --json state,labels,commits`).toString(); + const prJson = execSync(`gh pr view '${prNumber}' --json state,labels,updatedAt`).toString(); const pullRequest = JSON.parse(prJson); const prOpen = pullRequest.state === 'OPEN'; - const lastCommit = pullRequest.commits.slice(-1)[0]; - const lastCommitTimestamp = new Date(lastCommit.committedDate).getTime() / 1000; + const lastCommitTimestamp = new Date(pullRequest.updatedAt).getTime() / 1000; const persistDeployment = Boolean( pullRequest.labels.filter((label: any) => label.name === 'ci:cloud-persist-deployment').length diff --git a/.buildkite/scripts/steps/cloud/purge_projects.ts b/.buildkite/scripts/steps/cloud/purge_projects.ts index f89aa65fa855a..c3c427c6a3885 100644 --- a/.buildkite/scripts/steps/cloud/purge_projects.ts +++ b/.buildkite/scripts/steps/cloud/purge_projects.ts @@ -66,12 +66,11 @@ async function purgeProjects() { const NOW = new Date().getTime() / 1000; const DAY_IN_SECONDS = 60 * 60 * 24; const prJson = execSync( - `gh pr view '${project.prNumber}' --json state,labels,commits` + `gh pr view '${project.prNumber}' --json state,labels,updatedAt` ).toString(); const pullRequest = JSON.parse(prJson); const prOpen = pullRequest.state === 'OPEN'; - const lastCommit = pullRequest.commits.slice(-1)[0]; - const lastCommitTimestamp = new Date(lastCommit.committedDate).getTime() / 1000; + const lastCommitTimestamp = new Date(pullRequest.updatedAt).getTime() / 1000; const persistDeployment = Boolean( pullRequest.labels.filter((label: any) => label.name === 'ci:project-persist-deployment')