From 327d07d66f1ed13187cdf50284e69b1121a3918c Mon Sep 17 00:00:00 2001 From: liushuyu Date: Fri, 4 Feb 2022 22:53:33 -0700 Subject: [PATCH] CI: revise change detection logic ... (#5943) ... now determines whether the last commit was push during the 24 hour time-frame --- .github/workflows/ci-merge.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/.github/workflows/ci-merge.js b/.github/workflows/ci-merge.js index 44e385d16..63483a739 100644 --- a/.github/workflows/ci-merge.js +++ b/.github/workflows/ci-merge.js @@ -6,19 +6,30 @@ const fs = require("fs"); const DETECTION_TIME_FRAME = (parseInt(process.env.DETECTION_TIME_FRAME)) || (24 * 3600 * 1000); async function checkBaseChanges(github, context) { - // a special robustness handling for when GHA did not pass the repository info - if (!context.payload.repository) { - const result = await github.rest.repos.get({ - owner: context.repo.owner, - repo: context.repo.repo, - }); - context.payload.repository = result.data; - } - const delta = new Date() - new Date(context.payload.repository.pushed_at); + // query the commit date of the latest commit on this branch + const query = `query($owner:String!, $name:String!, $ref:String!) { + repository(name:$name, owner:$owner) { + ref(qualifiedName:$ref) { + target { + ... on Commit { id pushedDate oid } + } + } + } + }`; + const variables = { + owner: context.repo.owner, + name: context.repo.repo, + ref: 'refs/heads/master', + }; + const result = await github.graphql(query, variables); + const pushedAt = result.repository.ref.target.pushedDate; + console.log(`Last commit pushed at ${pushedAt}.`); + const delta = new Date() - new Date(pushedAt); if (delta <= DETECTION_TIME_FRAME) { console.info('New changes detected, triggering a new build.'); return true; } + console.info('No new changes detected.'); return false; }