From 5f3ac211ca397fcb3f1c998d384927cc773a2933 Mon Sep 17 00:00:00 2001 From: Mutahhir Hayat Date: Tue, 2 Apr 2024 10:19:41 +0200 Subject: [PATCH] fix: Randomize locking job schedule to prevent running into github rate limits --- .github/workflows/lock.yml | 2 +- src/lock-issues.ts | 9 ++++++++- test/__snapshots__/index.test.ts.snap | 10 +++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/.github/workflows/lock.yml b/.github/workflows/lock.yml index 15d9ca5e..902f1d40 100644 --- a/.github/workflows/lock.yml +++ b/.github/workflows/lock.yml @@ -3,7 +3,7 @@ name: lock on: schedule: - - cron: 20 2 * * * + - cron: 2 4 * * * jobs: lock: runs-on: ubuntu-latest diff --git a/src/lock-issues.ts b/src/lock-issues.ts index d45cbacf..3e8356d4 100644 --- a/src/lock-issues.ts +++ b/src/lock-issues.ts @@ -3,6 +3,7 @@ * SPDX-License-Identifier: MPL-2.0 */ +import { createHash } from "crypto"; import { javascript } from "projen"; import { JobPermission } from "projen/lib/github/workflows-model"; @@ -17,8 +18,14 @@ export class LockIssues { if (!workflow) throw new Error("no workflow defined"); + const projectNameHash = createHash("md5") + .update(project.name) + .digest("hex"); + const scheduleHour = parseInt(projectNameHash.slice(0, 2), 16) % 24; + const scheduleMinute = parseInt(projectNameHash.slice(2, 4), 16) % 24; + workflow.on({ - schedule: [{ cron: "20 2 * * *" }], + schedule: [{ cron: `${scheduleHour} ${scheduleMinute} * * *` }], }); workflow.addJob("lock", { diff --git a/test/__snapshots__/index.test.ts.snap b/test/__snapshots__/index.test.ts.snap index 110737f0..c4b46853 100644 --- a/test/__snapshots__/index.test.ts.snap +++ b/test/__snapshots__/index.test.ts.snap @@ -412,7 +412,7 @@ jobs: name: lock on: schedule: - - cron: 20 2 * * * + - cron: 17 16 * * * jobs: lock: runs-on: ubuntu-latest @@ -2642,7 +2642,7 @@ jobs: name: lock on: schedule: - - cron: 20 2 * * * + - cron: 17 16 * * * jobs: lock: runs-on: ubuntu-latest @@ -5386,7 +5386,7 @@ jobs: name: lock on: schedule: - - cron: 20 2 * * * + - cron: 17 16 * * * jobs: lock: runs-on: ubuntu-latest @@ -8127,7 +8127,7 @@ jobs: name: lock on: schedule: - - cron: 20 2 * * * + - cron: 17 16 * * * jobs: lock: runs-on: ubuntu-latest @@ -10850,7 +10850,7 @@ jobs: name: lock on: schedule: - - cron: 20 2 * * * + - cron: 7 12 * * * jobs: lock: runs-on: ubuntu-latest