Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.3.0 #21

Merged
merged 4 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports = {
'xo-typescript',
],
files: [
'calc-repo-size/**/*.ts',
'sources/**/*.ts',
'index.ts'
],
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 1 addition & 18 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -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 }}
39 changes: 0 additions & 39 deletions calc-repo-size/index.ts

This file was deleted.

4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsdelivr-purge",
"version": "5.1.1",
"version": "5.3.0",
"author": {
"name": "PiQuark6046",
"email": "[email protected]",
Expand All @@ -17,16 +17,14 @@
"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",
"@octokit/rest": "^20.0.2",
"@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",
Expand Down
21 changes: 5 additions & 16 deletions sources/branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,11 @@ function CreateGitInstance(BasePath: string): Git.SimpleGit {
*/
export async function ListBranches(ProgramOptions: Types.ProgramOptionsType): Promise<string[]> {
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)}`)
Expand Down
101 changes: 23 additions & 78 deletions sources/commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,57 +26,27 @@ export class CommitManager {
*/
async GetCommitSHAFromLatestWorkflowTime(LatestWorkflowRunTime: number, Branch: string): Promise<Types.CommitSHA> {
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}
}

/**
Expand All @@ -87,22 +57,9 @@ export class CommitManager {
* @returns {Promise<string[]>} A list of changed files.
*/
async GetChangedFilesFromSHAToHead(CommitSHA: string, Branch: string): Promise<string[]> {
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
}

/**
Expand All @@ -112,22 +69,10 @@ export class CommitManager {
* @returns {Promise<string[]>} A list of changed files.
*/
async GetChangedFilesFromACommit(CommitSHA: string): Promise<string[]> {
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
}
}
2 changes: 0 additions & 2 deletions sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export type ProgramOptionsRawType = {
branch: string;
ciWorkspacePath: string;
ciActionPath: string;
shouldUseApi: 'true' | 'false';
}

export type ProgramOptionsType = {
Expand All @@ -18,7 +17,6 @@ export type ProgramOptionsType = {
branch: string;
ciWorkspacePath: string;
ciActionPath: string;
shouldUseApi: boolean;
}

export type CDNStatusResponseType = {
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"include": [
"index.ts",
"sources/**/*.ts",
"calc-repo-size/**/*.ts"
"sources/**/*.ts"
]
}