-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: update dependencies * build: add eslint * build: update dependencies * build: Move Typescript declaration into dependencies * build: update package.json * chore: add paths into gitignore * chore: add eslintrc.cjs * build: update tsconfig.json * feat: modularize source files * fix: delete unexpected value * fix: delete lines * fix: update repo and username pattern accepted by GitHub * fix: update git repo path * fix: update minimum number of LatestWorkflowRunTime * fix: selecting wrong array index * fix: fatal error during searching git latest * fix: add some debugging info * chore: add some debugging info * fix: actions/checkout does not clone all history * fix: check if the cache is up to date * fix: return the previous commit * docs: update README * fix: when a workflow run is triggered in non-default branch
- Loading branch information
1 parent
bffbf65
commit 649dd65
Showing
17 changed files
with
621 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module.exports = { | ||
env: { | ||
es2022: true | ||
}, | ||
extends: [ | ||
'xo' | ||
], | ||
overrides: [ | ||
{ | ||
extends: [ | ||
'xo-typescript', | ||
], | ||
files: [ | ||
'calc-repo-size/**/*.ts', | ||
'sources/**/*.ts', | ||
'index.ts' | ||
], | ||
rules: | ||
{ | ||
'@typescript-eslint/naming-convention': ['error', { | ||
selector: ['variableLike', 'parameterProperty', 'classProperty', 'typeProperty'], | ||
format: ['PascalCase'] | ||
}], | ||
'@typescript-eslint/semi': ['error', 'never'], | ||
'@typescript-eslint/prefer-nullish-coalescing': 'off', | ||
'new-cap': 'off' | ||
} | ||
} | ||
], | ||
parserOptions: { | ||
ecmaVersion: 'latest', | ||
sourceType: 'module' | ||
}, | ||
rules: { | ||
'no-var': 'off', | ||
'comma-dangle': 'off', | ||
indent: ['off', 'tab'], | ||
semi: 'off' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
node_modules | ||
.env | ||
.env | ||
dist | ||
.vscode/launch.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as GitHub from '@octokit/rest' | ||
import * as Actions from '@actions/core' | ||
import * as Commander from 'commander' | ||
import checkDiskSpace from 'check-disk-space' | ||
|
||
const Program = new Commander.Command() | ||
|
||
Program.option('--debug', 'output extra debugging', false) | ||
.option('--gh-token <TOKEN>', 'GitHub token', '') | ||
.option('--repo <REPO>', 'A GitHub repository. eg: owner/repo', '') | ||
.option('--ci-workspace-path <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 = checkDiskSpace(ProgramOptions.ciWorkspacePath).then(DiskInfo => DiskInfo.free) | ||
|
||
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') | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as Commander from 'commander' | ||
import type * as Types from './sources/types' | ||
import {ExportArgs, IsDebug} from './sources/debug' | ||
import {ReplaceStringWithBooleanInObject} from './sources/utility' | ||
import {GetLatestWorkflowTime} from './sources/actions' | ||
import {ListBranches} from './sources/branches' | ||
import {GetChangedFilesFromSHAToHead, GetCommitSHAFromLatestWorkflowTime} from './sources/commits' | ||
import {PurgeRequestManager} from './sources/requests' | ||
|
||
const Program = new Commander.Command() | ||
|
||
// Set options. | ||
Program.option('--debug', 'output extra debugging', false) | ||
.option('--gh-token <TOKEN>', 'GitHub token', '') | ||
.option('--repo <REPO>', 'A GitHub repository. eg: owner/repo', '') | ||
.option('--workflow-ref <WORKFLOW_REF>', 'A GitHub workflow ref. eg: refs/heads/master', '') | ||
.option('--branch <BRANCH>', 'A GitHub branch. eg: master', '') | ||
.option('--ci-workspace-path <PATH>', 'A path to the CI workspace.', '') | ||
.option('--ci-action-path <PATH>', 'A path to the CI action.', '') | ||
.option('--should-use-api <TRUE_OR_FALSE>', 'Should use GitHub API?', 'false') | ||
|
||
// Initialize Input of the options and export them. | ||
Program.parse() | ||
|
||
// Declare the options and print them if the debugging mode is enabled. | ||
const ProgramRawOptions: Types.ProgramOptionsRawType = Program.opts() | ||
if (IsDebug(ProgramRawOptions)) { | ||
ExportArgs(ProgramRawOptions) | ||
} | ||
|
||
// Redefine with boolean. | ||
const ProgramOptions = ReplaceStringWithBooleanInObject(ProgramRawOptions) as Types.ProgramOptionsType | ||
|
||
// Workflow | ||
const LatestWorkflowRunTime = await GetLatestWorkflowTime(ProgramOptions).then(LatestWorkflowRunTime => LatestWorkflowRunTime) | ||
const Branches = await ListBranches(ProgramOptions).then(Branches => Branches) | ||
const PurgeRequest = new PurgeRequestManager(ProgramOptions) | ||
for (const Branch of Branches) { | ||
// eslint-disable-next-line no-await-in-loop | ||
const CommitSHA = await GetCommitSHAFromLatestWorkflowTime(ProgramOptions, LatestWorkflowRunTime, Branch).then(CommitSHA => CommitSHA) | ||
if (CommitSHA === null) { | ||
continue | ||
} | ||
|
||
// eslint-disable-next-line no-await-in-loop | ||
const ChangedFiles = await GetChangedFilesFromSHAToHead(ProgramOptions, CommitSHA, Branch, Branches[1]).then(ChangedFiles => ChangedFiles) | ||
PurgeRequest.AddURLs(ChangedFiles, Branch) | ||
} | ||
|
||
PurgeRequest.Start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
{ | ||
"name": "jsdelivr-purge", | ||
"version": "5.0.0", | ||
"author": { | ||
"name": "PiQuark6046", | ||
"email": "piquark6046@proton.me", | ||
"url": "https://github.com/piquark6046" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/List-KR/jsdelivr-purge" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/List-KR/jsdelivr-purge/issues" | ||
}, | ||
"homepage": "https://github.com/List-KR/jsdelivr-purge", | ||
"license": "MIT", | ||
"type": "module", | ||
"scripts": { | ||
"ci": "tsx index.ts", | ||
"calc-repo-size": "tsx calc-repo-size/index.ts" | ||
}, | ||
"dependencies": { | ||
"@actions/core": "^1.10.0", | ||
"@octokit/rest": "^20.0.1", | ||
"@types/luxon": "^3.2.0", | ||
"dotenv": "^16.0.3", | ||
"@types/luxon": "^3.3.3", | ||
"@types/node": "^20.8.10", | ||
"check-disk-space": "^3.4.0", | ||
"commander": "^11.1.0", | ||
"got": "^12.6.1", | ||
"luxon": "^3.3.0", | ||
"p-queue": "^7.4.1", | ||
"simple-git": "^3.20.0", | ||
"tsx": "^3.14.0", | ||
"typescript": "^5.0.2" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.1.0" | ||
"@typescript-eslint/eslint-plugin": "^6.9.1", | ||
"@typescript-eslint/parser": "^6.9.1", | ||
"eslint": "^8.52.0", | ||
"eslint-config-xo": "^0.43.1", | ||
"eslint-config-xo-typescript": "^1.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as GitHub from '@octokit/rest' | ||
import {DateTime} from 'luxon' | ||
import type {ProgramOptionsType} from './types' | ||
|
||
/** | ||
* @name GetLatestWorkflowTime | ||
* @description Get the latest workflow time. | ||
* @param {ProgramOptionsType} ProgramOptions The program options. | ||
* @returns {Promise<number>} The latest workflow time in milliseconds. | ||
*/ | ||
export async function GetLatestWorkflowTime(ProgramOptions: ProgramOptionsType): Promise<number> { | ||
const GitHubInstance = new GitHub.Octokit({auth: ProgramOptions.ghToken}) | ||
const [RepoOwner, RepoName] = ProgramOptions.repo.split('/') | ||
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], | ||
}).then(WorkflowRuns => WorkflowRuns.data.workflow_runs) | ||
for (const WorkflowRun of WorkflowRuns) { | ||
if (WorkflowRun.status === 'completed' && WorkflowRun.conclusion === 'success' | ||
&& DateTime.fromISO(WorkflowRun.updated_at).toMillis() > LatestWorkflowRunTime) { | ||
LatestWorkflowRunTime = DateTime.fromISO(WorkflowRun.updated_at).toMillis() | ||
} | ||
} | ||
|
||
return LatestWorkflowRunTime | ||
} |
Oops, something went wrong.