From b3c47197236ae0aa65bf9fbcf86901afe56c9fde Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Mon, 29 Jan 2024 04:13:57 +0000 Subject: [PATCH 1/4] perf: remove calc repo size because of github's changes https://github.blog/2024-01-17-github-hosted-runners-double-the-power-for-open-source/ --- .eslintrc.cjs | 1 - action.yml | 19 +------- calc-repo-size/index.ts | 39 ---------------- package.json | 4 +- sources/branches.ts | 21 ++------- sources/commits.ts | 101 +++++++++------------------------------- sources/types.ts | 2 - tsconfig.json | 3 +- 8 files changed, 31 insertions(+), 159 deletions(-) delete mode 100644 calc-repo-size/index.ts diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 38e5dca..f6846c2 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -11,7 +11,6 @@ module.exports = { 'xo-typescript', ], files: [ - 'calc-repo-size/**/*.ts', 'sources/**/*.ts', 'index.ts' ], diff --git a/action.yml b/action.yml index 7462ef2..b2ac29f 100644 --- a/action.yml +++ b/action.yml @@ -16,22 +16,6 @@ runs: npm i shell: bash working-directory: ${{ github.action_path }} - - name: Check if size of the repo exceeds the runner's hardware capacity - id: check_size - env: - GITHUB_TOKEN: ${{ github.token }} - REPO: ${{ github.repository }} - CI_WORKSPACE_PATH: ${{ github.workspace }} - run: | - npm run calc-repo-size -- --gh-token "$GITHUB_TOKEN" --repo "$REPO" --ci-workspace-path "$CI_WORKSPACE_PATH" - shell: bash - working-directory: ${{ github.action_path }} - - name: Clone repo into github.workspace - uses: actions/checkout@v4 - with: - fetch-depth: 0 - ref: ${{ github.event.repository.default_branch }} - if: ${{ steps.check_size.outputs.should_api != 'true' }} - name: Run program env: GITHUB_TOKEN: ${{ github.token }} @@ -40,8 +24,7 @@ runs: WORKFLOWREF: ${{ github.workflow_ref }} CI_WORKSPACE_PATH: ${{ github.workspace }} CI_ACTION_PATH: ${{ github.action_path }} - SHOULD_USE_API: ${{ steps.check_size.outputs.should_use_api }} run: | - npm run ci -- --gh-token "$GITHUB_TOKEN" --repo "$REPO" --workflow-ref "$WORKFLOWREF" --branch "$BRANCHE" --ci-workspace-path "$CI_WORKSPACE_PATH" --ci-action-path "$CI_ACTION_PATH" --should-use-api "$SHOULD_USE_API" + npm run ci -- --gh-token "$GITHUB_TOKEN" --repo "$REPO" --workflow-ref "$WORKFLOWREF" --branch "$BRANCHE" --ci-workspace-path "$CI_WORKSPACE_PATH" --ci-action-path "$CI_ACTION_PATH" shell: bash working-directory: ${{ github.action_path }} \ No newline at end of file diff --git a/calc-repo-size/index.ts b/calc-repo-size/index.ts deleted file mode 100644 index 9e60195..0000000 --- a/calc-repo-size/index.ts +++ /dev/null @@ -1,39 +0,0 @@ -import * as GitHub from '@octokit/rest' -import * as Actions from '@actions/core' -import * as Commander from 'commander' -import * as DiskUsage from 'diskusage' - -const Program = new Commander.Command() - -Program.option('--debug', 'output extra debugging', false) - .option('--gh-token ', 'GitHub token', '') - .option('--repo ', 'A GitHub repository. eg: owner/repo', '') - .option('--ci-workspace-path ', 'A path to the CI workspace.', '') - -Program.parse() - -type ProgramOptionsType = { - // eslint-disable-next-line @typescript-eslint/naming-convention - debug: boolean; - // eslint-disable-next-line @typescript-eslint/naming-convention - ghToken: string; - // eslint-disable-next-line @typescript-eslint/naming-convention - repo: string; - // eslint-disable-next-line @typescript-eslint/naming-convention - ciWorkspacePath: string; -} -const ProgramOptions: ProgramOptionsType = Program.opts() - -const GitHubInstance = new GitHub.Octokit({auth: ProgramOptions.ghToken}) -const RepoSize = GitHubInstance.repos.get({owner: ProgramOptions.repo.split('/')[0], repo: ProgramOptions.repo.split('/')[1]}) - .then(Response => Response.data.size) -const DiskFreeSize = DiskUsage.check(ProgramOptions.ciWorkspacePath).then(DiskInfo => DiskInfo.available) - -await Promise.all([RepoSize, DiskFreeSize]).then(([RepoSizeVaule, DiskFreeSizeVaule]) => { - Actions.info(`calc-repo-size: RepoSize: ${RepoSizeVaule}; DiskFreeSize: ${DiskFreeSizeVaule}`) - if (RepoSizeVaule * 1000 < DiskFreeSizeVaule) { - Actions.setOutput('should_use_api', 'false') - } else { - Actions.setOutput('should_use_api', 'true') - } -}) diff --git a/package.json b/package.json index 9455563..00cc8ca 100644 --- a/package.json +++ b/package.json @@ -17,8 +17,7 @@ "license": "MIT", "type": "module", "scripts": { - "ci": "tsx index.ts", - "calc-repo-size": "tsx calc-repo-size/index.ts" + "ci": "tsx index.ts" }, "dependencies": { "@actions/core": "^1.10.1", @@ -26,7 +25,6 @@ "@types/luxon": "^3.4.2", "@types/node": "^20.11.7", "commander": "^11.1.0", - "diskusage": "^1.2.0", "got": "^14.0.0", "luxon": "^3.4.4", "p-queue": "^8.0.1", diff --git a/sources/branches.ts b/sources/branches.ts index 926a6ad..1002f51 100644 --- a/sources/branches.ts +++ b/sources/branches.ts @@ -23,22 +23,11 @@ function CreateGitInstance(BasePath: string): Git.SimpleGit { */ export async function ListBranches(ProgramOptions: Types.ProgramOptionsType): Promise { const Branches: string[] = ['latest'] - if (ProgramOptions.shouldUseApi) { - const GitHubInstance = CreateGitHubInstance(ProgramOptions) - const [RepoOwner, RepoName] = ProgramOptions.repo.split('/') - Branches.push((await GitHubInstance.repos.get({owner: RepoOwner, repo: RepoName})).data.default_branch) - const OtherBranches = (await GitHubInstance.repos.listBranches({owner: RepoOwner, repo: RepoName}).then(Branches => Branches.data)) - .map(Item => Item.name) - OtherBranches.forEach(Item => Branches.push(ProgramOptions.branch.split(' ').find(Branch => Branch === Item))) - } - - if (!ProgramOptions.shouldUseApi) { - const GitInstance = CreateGitInstance(ProgramOptions.ciWorkspacePath) - Branches.push(await GitInstance.branchLocal().then(Branches => Branches.current)) - // Branches[1] is always the current/default branch. - const OtherBranches = (await GitInstance.branchLocal().then(Branches => Branches.all)).filter(Branch => Branch !== Branches[1]) - OtherBranches.forEach(Item => Branches.push(ProgramOptions.branch.split(' ').find(Branch => Branch === Item))) - } + const GitInstance = CreateGitInstance(ProgramOptions.ciWorkspacePath) + Branches.push(await GitInstance.branchLocal().then(Branches => Branches.current)) + // Branches[1] is always the current/default branch. + const OtherBranches = (await GitInstance.branchLocal().then(Branches => Branches.all)).filter(Branch => Branch !== Branches[1]) + OtherBranches.forEach(Item => Branches.push(ProgramOptions.branch.split(' ').find(Branch => Branch === Item))) if (IsDebug(ProgramOptions)) { Actions.debug(`ListBranches in branches.ts called: ${JSON.stringify(Branches)}`) diff --git a/sources/commits.ts b/sources/commits.ts index 4681774..ae9753d 100644 --- a/sources/commits.ts +++ b/sources/commits.ts @@ -26,57 +26,27 @@ export class CommitManager { */ async GetCommitSHAFromLatestWorkflowTime(LatestWorkflowRunTime: number, Branch: string): Promise { var MatchedCommitTimeAddress = 0 - if (this.ProgramOptions.shouldUseApi) { - const GitHubInstance = CreateGitHubInstance(this.ProgramOptions) - const GitHubListCommits = await GitHubInstance.repos.listCommits({ - owner: this.ProgramOptions.repo.split('/')[0], - repo: this.ProgramOptions.repo.split('/')[1], - sha: Branch === 'latest' ? this.Branches[1] : Branch, - }).then(Response => Response.data) - for (const CommitRaw of GitHubListCommits) { - if (DateTime.fromISO(CommitRaw.commit.author.date).toMillis() < LatestWorkflowRunTime) { - break - } - - MatchedCommitTimeAddress++ - } - - // If any commit is pushed after the latest workflow time, skip the branch. - if (MatchedCommitTimeAddress === 0) { - return {sha: '', length: 0} - } - - // If the wokflow had not executed before, return SHA of the oldest commit. - if (LatestWorkflowRunTime === 0) { - return {sha: GitHubListCommits[GitHubListCommits.length - 1].sha, length: GitHubListCommits.length} + const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath) + const GitLog = (await GitInstance.log(['--date=iso-strict'])).all + for (const CommitRaw of GitLog) { + if (DateTime.fromISO(CommitRaw.date).toMillis() < LatestWorkflowRunTime) { + break } - return {sha: GitHubListCommits[MatchedCommitTimeAddress].sha, length: GitHubListCommits.length} + MatchedCommitTimeAddress++ } - if (!this.ProgramOptions.shouldUseApi) { - const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath) - const GitLog = (await GitInstance.log(['--date=iso-strict'])).all - for (const CommitRaw of GitLog) { - if (DateTime.fromISO(CommitRaw.date).toMillis() < LatestWorkflowRunTime) { - break - } - - MatchedCommitTimeAddress++ - } - - // If any commit is pushed after the latest workflow time, skip the branch. - if (MatchedCommitTimeAddress === 0) { - return {sha: '', length: 0} - } - - // If the wokflow had not executed before, return SHA of the oldest commit. - if (LatestWorkflowRunTime === 0) { - return {sha: GitLog[GitLog.length - 1].hash, length: GitLog.length} - } + // If any commit is pushed after the latest workflow time, skip the branch. + if (MatchedCommitTimeAddress === 0) { + return {sha: '', length: 0} + } - return {sha: GitLog[MatchedCommitTimeAddress].hash, length: GitLog.length} + // If the wokflow had not executed before, return SHA of the oldest commit. + if (LatestWorkflowRunTime === 0) { + return {sha: GitLog[GitLog.length - 1].hash, length: GitLog.length} } + + return {sha: GitLog[MatchedCommitTimeAddress].hash, length: GitLog.length} } /** @@ -87,22 +57,9 @@ export class CommitManager { * @returns {Promise} A list of changed files. */ async GetChangedFilesFromSHAToHead(CommitSHA: string, Branch: string): Promise { - if (this.ProgramOptions.shouldUseApi) { - const GitHubInstance = CreateGitHubInstance(this.ProgramOptions) - const GitHubComparingRaw = await GitHubInstance.repos.compareCommits({ - owner: this.ProgramOptions.repo.split('/')[0], - repo: this.ProgramOptions.repo.split('/')[1], - head: Branch === 'latest' ? this.Branches[1] : Branch, - base: CommitSHA, - }).then(Response => Response.data) - return GitHubComparingRaw.files.map(File => File.filename) - } - - if (!this.ProgramOptions.shouldUseApi) { - const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath) - const ChangedFiles = (await GitInstance.diff(['--name-only', `${CommitSHA}...${Branch === 'latest' ? this.Branches[1] : Branch}`])).split('\n') - return ChangedFiles[ChangedFiles.length - 1] === '' ? ChangedFiles.slice(0, ChangedFiles.length - 1) : ChangedFiles - } + const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath) + const ChangedFiles = (await GitInstance.diff(['--name-only', `${CommitSHA}...${Branch === 'latest' ? this.Branches[1] : Branch}`])).split('\n') + return ChangedFiles[ChangedFiles.length - 1] === '' ? ChangedFiles.slice(0, ChangedFiles.length - 1) : ChangedFiles } /** @@ -112,22 +69,10 @@ export class CommitManager { * @returns {Promise} A list of changed files. */ async GetChangedFilesFromACommit(CommitSHA: string): Promise { - if (this.ProgramOptions.shouldUseApi) { - const GitHubInstance = CreateGitHubInstance(this.ProgramOptions) - const GitHubComparingRaw = await GitHubInstance.repos.getCommit({ - owner: this.ProgramOptions.repo.split('/')[0], - repo: this.ProgramOptions.repo.split('/')[1], - ref: CommitSHA, - }).then(Response => Response.data) - return GitHubComparingRaw.files.map(File => `/gh/${File.filename}`) - } - - if (!this.ProgramOptions.shouldUseApi) { - const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath) - const ChangedFiles = (await GitInstance.show(['--pretty=format:"%f"', '--name-only', CommitSHA])).split('\n') - ChangedFiles.shift() // Remove the commit message. - ChangedFiles.pop() - return ChangedFiles - } + const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath) + const ChangedFiles = (await GitInstance.show(['--pretty=format:"%f"', '--name-only', CommitSHA])).split('\n') + ChangedFiles.shift() // Remove the commit message. + ChangedFiles.pop() + return ChangedFiles } } diff --git a/sources/types.ts b/sources/types.ts index 4244b47..038646a 100644 --- a/sources/types.ts +++ b/sources/types.ts @@ -7,7 +7,6 @@ export type ProgramOptionsRawType = { branch: string; ciWorkspacePath: string; ciActionPath: string; - shouldUseApi: 'true' | 'false'; } export type ProgramOptionsType = { @@ -18,7 +17,6 @@ export type ProgramOptionsType = { branch: string; ciWorkspacePath: string; ciActionPath: string; - shouldUseApi: boolean; } export type CDNStatusResponseType = { diff --git a/tsconfig.json b/tsconfig.json index 52dbd43..7bfa7a9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,6 @@ }, "include": [ "index.ts", - "sources/**/*.ts", - "calc-repo-size/**/*.ts" + "sources/**/*.ts" ] } \ No newline at end of file From 1cc8c09cf839c8da62453e16e0570c135c976297 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Mon, 29 Jan 2024 04:29:42 +0000 Subject: [PATCH 2/4] chore: add CPU checker --- index.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.ts b/index.ts index 580f505..5543e4a 100644 --- a/index.ts +++ b/index.ts @@ -6,6 +6,10 @@ import {GetLatestWorkflowTime} from './sources/actions.js' import {ListBranches} from './sources/branches.js' import {CommitManager} from './sources/commits.js' import {PurgeRequestManager} from './sources/requests.js' +import * as Actions from '@actions/core' +import * as Os from 'node:os' + +Actions.info(`Running on ${Os.cpus()[0].model} with ${Os.cpus().length} threads/vCPUs.`) const Program = new Commander.Command() From 4f26caaee8edbc7eaf1e517b2148a89fa61e1b14 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Mon, 29 Jan 2024 04:30:12 +0000 Subject: [PATCH 3/4] chore: update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 00cc8ca..96e6346 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jsdelivr-purge", - "version": "5.1.1", + "version": "5.3.0", "author": { "name": "PiQuark6046", "email": "piquark6046@proton.me", From 9c2c8f7a84d637c97398bdf7eedd9f7f3793e307 Mon Sep 17 00:00:00 2001 From: piquark6046 Date: Mon, 29 Jan 2024 04:33:19 +0000 Subject: [PATCH 4/4] docs: update readme --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fe157b4..2236641 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,12 @@ jobs: uses: actions/setup-node@v4 with: node-version: 'lts/*' + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Run jsDelivr-Purge - uses: List-KR/jsdelivr-purge@5.1.0 + uses: List-KR/jsdelivr-purge@5.3.0 ``` The jsDelivr-Purge supports `workflow_dispatch`, `schedule` and `push` event.