Skip to content

Commit

Permalink
5.6.0 (#32)
Browse files Browse the repository at this point in the history
* style: Make return value of ListBranches object

* perf: remove checking hash

In many cases jsDelivr backend clones a repo to purge the files on GitHub.

* style: rewrite only Branches.Branches array only

* chore: remove latest tag in action.yml

* chore: add user-agent into PostPurgeRequest

* build: remove noble/hashes

* chore: update regex that uses to find the workflow path

* feat: print warning message when rate-limit about a file is applied

* chore: remove unnecessary imports

* build: dependencies

* build: update dependencies

* chore: update package version
  • Loading branch information
piquark6046 committed Mar 1, 2024
1 parent 0903a96 commit fbb7f9e
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 120 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
fetch-depth: 0
- name: Run jsDelivr-Purge
uses: List-KR/jsdelivr-purge@5.5.0
uses: List-KR/jsdelivr-purge@5.6.0
```
The jsDelivr-Purge supports `workflow_dispatch`, `schedule` and `push` event.
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ inputs:
branches:
description: 'The your branches that you want to purge cache existing in jsDelivr'
required: false
default: 'latest'
default: ''

runs:
using: 'composite'
Expand Down
16 changes: 4 additions & 12 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {ListBranches} from './sources/branches.js'
import {CommitManager} from './sources/commits.js'
import {PurgeRequestManager} from './sources/requests.js'
import {GetIPAddress} from './sources/ipcheck.js'
import {GitHubRAWHash} from './sources/hash.js'
import * as Actions from '@actions/core'
import * as Os from 'node:os'

Expand Down Expand Up @@ -49,8 +48,8 @@ const Branches = await ListBranches(ProgramOptions).then(Branches => Branches)

// Get changed files.
var ChangedFiles: Array<{Branch: string; Filename: string}> = []
for (const Branch of Branches) {
const CommitManagerInstance = new CommitManager(ProgramOptions, Branches)
for (const Branch of Branches.Branches) {
const CommitManagerInstance = new CommitManager(ProgramOptions)
// eslint-disable-next-line no-await-in-loop
const CommitSHA = await CommitManagerInstance.GetCommitSHAFromLatestWorkflowTime(LatestWorkflowRunTime, Branch).then(CommitSHA => CommitSHA)
if (CommitSHA.length === 0) {
Expand All @@ -66,17 +65,10 @@ for (const Branch of Branches) {
}
}

// Hold until checking hash is done.
performance.mark('githubrawhash')
const GitHubRAWHashInstance = new GitHubRAWHash(ProgramOptions, ChangedFiles, Branches)
await GitHubRAWHashInstance.Register()
await GitHubRAWHashInstance.Check()
Actions.info(`Checking hashes took ${Math.floor(performance.measure('githubrawhash-duration', 'githubrawhash').duration)} ms.`)

performance.mark('purge')
const PurgeRequest = new PurgeRequestManager(ProgramOptions)
PurgeRequest.AddURLs(ChangedFiles.filter(ChangedFile => ChangedFile.Branch === 'latest').map(ChangedFile => ChangedFile.Filename), 'latest')
for (const Branch of Branches.filter(Branch => Branch !== 'latest')) {
PurgeRequest.AddURLs(ChangedFiles.filter(ChangedFile => ChangedFile.Branch === Branches.Default).map(ChangedFile => ChangedFile.Filename), 'latest')
for (const Branch of Branches.Branches) {
PurgeRequest.AddURLs(ChangedFiles.filter(ChangedFile => ChangedFile.Branch === Branch).map(ChangedFile => ChangedFile.Filename), Branch)
}

Expand Down
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsdelivr-purge",
"version": "5.5.0",
"version": "5.6.0",
"author": {
"name": "PiQuark6046",
"email": "piquark6046@proton.me",
Expand All @@ -22,10 +22,9 @@
},
"dependencies": {
"@actions/core": "^1.10.1",
"@noble/hashes": "^1.3.3",
"@octokit/rest": "^20.0.2",
"@types/luxon": "^3.4.2",
"@types/node": "^20.11.19",
"@types/node": "^20.11.24",
"commander": "^12.0.0",
"got": "^14.2.0",
"ip-regex": "^5.0.0",
Expand All @@ -36,10 +35,10 @@
"typescript": "^5.3.3"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.0.1",
"@typescript-eslint/parser": "^7.0.1",
"eslint": "^8.56.0",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"eslint": "^8.57.0",
"eslint-config-xo": "^0.44.0",
"eslint-config-xo-typescript": "^2.0.0"
"eslint-config-xo-typescript": "^3.0.0"
}
}
2 changes: 1 addition & 1 deletion sources/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function GetLatestWorkflowTime(ProgramOptions: ProgramOptionsType):
var LatestWorkflowRunTime = 0
const WorkflowRuns = await GitHubInstance.actions.listWorkflowRuns({
owner: RepoOwner, repo: RepoName,
workflow_id: /(?<=^[A-z0-9-_]+\/[A-z0-9-_]+\/\.github\/workflows\/).+\.yml(?=@refs\/)/.exec(ProgramOptions.workflowRef)[0],
workflow_id: /(?<=^[A-Za-z0-9-_.]+\/[A-Za-z0-9-_.]+\/\.github\/workflows\/).+\.yml(?=@refs\/)/.exec(ProgramOptions.workflowRef)[0],
}).then(WorkflowRuns => WorkflowRuns.data.workflow_runs)
for (const WorkflowRun of WorkflowRuns) {
if (WorkflowRun.status === 'completed' && WorkflowRun.conclusion === 'success'
Expand Down
21 changes: 13 additions & 8 deletions sources/branches.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as Git from 'simple-git'
import * as GitHub from '@octokit/rest'
import * as Actions from '@actions/core'
import * as Os from 'node:os'
import type * as Types from './types.js'
Expand All @@ -14,19 +13,25 @@ function CreateGitInstance(BasePath: string): Git.SimpleGit {
* @name ListBranches
* @description List all branches that should be purged.
* @param {Types.ProgramOptions} ProgramOptions The program options.
* @returns {string[]} A list of branches. The list always contains 'latest' and the current/default branch.
* @returns {Promise<{Branches: string[]; Default: string}>} A list of branches and the default branch.
*/
export async function ListBranches(ProgramOptions: Types.ProgramOptionsType): Promise<string[]> {
const Branches: string[] = ['latest']
export async function ListBranches(ProgramOptions: Types.ProgramOptionsType): Promise<{Branches: string[]; Default: string}> {
var Branches: {Branches: string[]; Default: string} = {
Branches: [],
Default: '',
}
const GitInstance = CreateGitInstance(ProgramOptions.ciWorkspacePath)
Branches.push(await GitInstance.branchLocal().then(Branches => Branches.current))
// Branches[1] is always the current/default branch.
Branches.Default = await GitInstance.branchLocal().then(Branches => Branches.current)
Branches.Branches.push(Branches.Default)
// Branches[0] 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)))
OtherBranches.forEach(Item => Branches.Branches.push(ProgramOptions.branch.split(' ').find(Branch => Branch === Item)))

if (IsDebug(ProgramOptions)) {
Actions.debug(`ListBranches in branches.ts called: ${JSON.stringify(Branches)}`)
}

return Branches.filter(Branch => Branch !== undefined && Branch !== null)
Branches.Branches = Branches.Branches.filter(Branch => Branch !== undefined && Branch !== null)

return Branches
}
5 changes: 2 additions & 3 deletions sources/commits.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as Git from 'simple-git'
import * as GitHub from '@octokit/rest'
import * as Os from 'node:os'
import {DateTime} from 'luxon'
import type * as Types from './types.js'
Expand All @@ -10,7 +9,7 @@ function CreateGitInstance(BasePath: string): Git.SimpleGit {
}

export class CommitManager {
constructor(private readonly ProgramOptions: Types.ProgramOptionsType, private readonly Branches: string[]) {}
constructor(private readonly ProgramOptions: Types.ProgramOptionsType) {}

/**
* @name GetCommitSHAFromLatestWorkflowTime
Expand Down Expand Up @@ -53,7 +52,7 @@ export class CommitManager {
*/
async GetChangedFilesFromSHAToHead(CommitSHA: string, Branch: string): Promise<string[]> {
const GitInstance = CreateGitInstance(this.ProgramOptions.ciWorkspacePath)
const ChangedFiles = (await GitInstance.diff(['--name-only', `${CommitSHA}...${Branch === 'latest' ? this.Branches[1] : Branch}`])).split('\n')
const ChangedFiles = (await GitInstance.diff(['--name-only', `${CommitSHA}...${Branch}`])).split('\n')
return ChangedFiles[ChangedFiles.length - 1] === '' ? ChangedFiles.slice(0, ChangedFiles.length - 1) : ChangedFiles
}

Expand Down
87 changes: 0 additions & 87 deletions sources/hash.ts

This file was deleted.

8 changes: 8 additions & 0 deletions sources/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ async function GetCDNResponse(ProgramOptions: Types.ProgramOptionsType, ID: stri
'user-agent': 'jsdelivr-purge',
},
}).json()

for (const [Key, Value] of Object.entries(ResponseRaw.paths)) {
if (Value.throttled) {
Actions.warning(`Throttled: ${Key.replace(/^\/gh\/[A-Za-z0-9-._]+\/[A-Za-z0-9-._]+(?=@)/, '')}`)
}
}

Actions.startGroup(`GetCDNResponse called: ${ID}`)
Actions.info(JSON.stringify(ResponseRaw))
Actions.endGroup()
Expand All @@ -27,6 +34,7 @@ async function PostPurgeRequest(ProgramOptions: Types.ProgramOptionsType, Branch
const ResponseRaw: Types.CDNPostResponseType = await got.post('https://purge.jsdelivr.net/', {
headers: {
'cache-control': 'no-cache',
'user-agent': 'jsdelivr-purge',
},
json: {
path: new Array(Filenames.length).fill(null, 0, Filenames.length).map((Filename, Index) => `/gh/${ProgramOptions.repo}@${BranchOrTag[Index]}/${Filenames[Index]}`),
Expand Down

0 comments on commit fbb7f9e

Please sign in to comment.