CI: revise change detection logic ... (#5943)

... now determines whether the last commit was push during the 24 hour
time-frame
This commit is contained in:
liushuyu 2022-02-04 22:53:33 -07:00 committed by GitHub
parent 2fc7def6f6
commit 327d07d66f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,19 +6,30 @@ const fs = require("fs");
const DETECTION_TIME_FRAME = (parseInt(process.env.DETECTION_TIME_FRAME)) || (24 * 3600 * 1000); const DETECTION_TIME_FRAME = (parseInt(process.env.DETECTION_TIME_FRAME)) || (24 * 3600 * 1000);
async function checkBaseChanges(github, context) { async function checkBaseChanges(github, context) {
// a special robustness handling for when GHA did not pass the repository info // query the commit date of the latest commit on this branch
if (!context.payload.repository) { const query = `query($owner:String!, $name:String!, $ref:String!) {
const result = await github.rest.repos.get({ repository(name:$name, owner:$owner) {
owner: context.repo.owner, ref(qualifiedName:$ref) {
repo: context.repo.repo, target {
}); ... on Commit { id pushedDate oid }
context.payload.repository = result.data;
} }
const delta = new Date() - new Date(context.payload.repository.pushed_at); }
}
}`;
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) { if (delta <= DETECTION_TIME_FRAME) {
console.info('New changes detected, triggering a new build.'); console.info('New changes detected, triggering a new build.');
return true; return true;
} }
console.info('No new changes detected.');
return false; return false;
} }