chore(release): v1.0.0

This commit is contained in:
seepine 2023-08-02 00:39:45 +08:00
commit 8e2d3a87e1
29 changed files with 7411 additions and 0 deletions

16
.editorconfig Normal file
View File

@ -0,0 +1,16 @@
# Editor configuration, see http://editorconfig.org
# 表示是最顶层的 EditorConfig 配置文件
root = true
[*] # 表示所有文件适用
charset = utf-8 # 设置文件字符集为 utf-8
indent_style = space # 缩进风格tab | space
indent_size = 2 # 缩进大小
end_of_line = lf # 控制换行类型(lf | cr | crlf)
trim_trailing_whitespace = true # 去除行首的任意空白字符
insert_final_newline = true # 始终在文件末尾插入一个新行
[*.md] # 表示仅 md 文件适用以下规则
max_line_length = off
trim_trailing_whitespace = false

5
.eslintignore Normal file
View File

@ -0,0 +1,5 @@
dist/
lib/
node_modules/
jest.config.js
__tests__/

55
.eslintrc.json Normal file
View File

@ -0,0 +1,55 @@
{
"plugins": ["jest", "@typescript-eslint"],
"extends": ["plugin:github/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"i18n-text/no-en": "off",
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {"allowExpressions": true}],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
},
"env": {
"node": true,
"es6": true,
"jest/globals": true
}
}

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
dist/** -diff linguist-generated=true

11
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,11 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: daily
- package-ecosystem: npm
directory: /
schedule:
interval: daily

53
.github/workflows/check-dist.yml vendored Normal file
View File

@ -0,0 +1,53 @@
# `dist/index.js` is a special file in Actions.
# When you reference an action with `uses:` in a workflow,
# `index.js` is the code that will run.
# For our project, we generate this file through a build process from other source files.
# We need to make sure the checked-in `index.js` actually matches what we expect it to be.
name: Check dist/
on:
push:
branches:
- main
paths-ignore:
- '**.md'
pull_request:
paths-ignore:
- '**.md'
workflow_dispatch:
jobs:
check-dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set Node.js 16.x
uses: actions/setup-node@v3.6.0
with:
node-version: 16.x
- name: Install dependencies
run: npm ci
- name: Rebuild the dist/ directory
run: |
npm run build
npm run package
- name: Compare the expected and actual dist/ directories
run: |
if [ "$(git diff --ignore-space-at-eol dist/ | wc -l)" -gt "0" ]; then
echo "Detected uncommitted changes after build. See status below:"
git diff
exit 1
fi
id: diff
# If index.js was different than expected, upload the expected version as an artifact
- uses: actions/upload-artifact@v3
if: ${{ failure() && steps.diff.conclusion == 'failure' }}
with:
name: dist
path: dist/

71
.github/workflows/codeql-analysis.yml vendored Normal file
View File

@ -0,0 +1,71 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"
on:
push:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '31 7 * * 3'
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'TypeScript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
source-root: src
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
# Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language
#- run: |
# make bootstrap
# make release
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2

31
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,31 @@
name: 'build-test'
on: # rebuild any PRs and main branch changes
pull_request:
push:
branches:
- main
- 'releases/*'
jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: |
npm install
- run: |
npm run all
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ./
id: get-hash
with:
patterns: |
**/package-lock.json
- name: Echo hash
run: echo ${{ steps.get-hash.outputs.hash }}

99
.gitignore vendored Normal file
View File

@ -0,0 +1,99 @@
# Dependency directory
node_modules
# Rest pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# OS metadata
.DS_Store
Thumbs.db
# Ignore built ts files
__tests__/runner/*
lib/**/*

3
.prettierignore Normal file
View File

@ -0,0 +1,3 @@
dist/
lib/
node_modules/

10
.prettierrc.json Normal file
View File

@ -0,0 +1,10 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid"
}

12
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,12 @@
{
// eslint
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
//
"emmet.showExpandedAbbreviation": "never",
//
"editor.minimap.enabled": false,
//
"editor.wordWrap": "on"
}

1
CODEOWNERS Normal file
View File

@ -0,0 +1 @@
* @actions/actions-runtime

22
LICENSE Normal file
View File

@ -0,0 +1,22 @@
The MIT License (MIT)
Copyright (c) 2018 GitHub, Inc. and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

57
README.md Normal file
View File

@ -0,0 +1,57 @@
# hash-files
This action is to compute the SHA256 hash of specified files.
The hash function is based on [nektos/act](https://github.com/nektos/act/blob/ac5dd8feb876d37ae483376a137c57383577dace/pkg/exprparser/functions.go#L183). Thanks!
**NOTE:** This action is written in Go. Please setup the Go environment before running this action or use a runner with Go environment installed.
## Usage
``` yml
- uses: seepine/hash-files@v1
with:
# The working dir for the action.
# Default: ${{ github.workspace }}
workdir: ''
# The patterns used to match files.
patterns: '**/package-lock.json'
# Multiple patterns should be seperated by `\n`
patterns: |
**/package-lock.json
**/yarn.lock
```
## Input
|Output Item|Description|Required|Default|
|---|---|---|---|
|workdir|The working dir for the action|false|${{ github.workspace }}|
|patterns|The patterns used to match files|true||
|gitignore|Respect ignore patterns in .gitignore files that apply to the globbed files.|false|true
|ignoreFiles|Glob patterns to look for ignore files, which are then used to ignore globbed files.|false|
## Output
|Output Item|Description|
|---|---|
|hash|The computed hash result|
|matched-files|The files matched by the patterns|
## Example
``` yml
# Setup the Node environment. This step can be skipped if Node has been installed.
- uses: actions/setup-node@v3
- uses: seepine/hash-files@v1
id: get-hash
with:
patterns: |
**/package-lock.json
**/yarn.lock
- name: Echo hash
run: echo ${{ steps.get-hash.outputs.hash }}
```

38
__tests__/main.test.ts Normal file
View File

@ -0,0 +1,38 @@
import {getFiles, readFile} from '../src/files'
import {hashHex} from '../src/utils'
import * as process from 'process'
import * as cp from 'child_process'
import * as path from 'path'
import {test} from '@jest/globals'
test('test sha256', async () => {
console.log(hashHex('this is content'))
})
test('test sha512', async () => {
console.log(hashHex('this is content', 'sha512'))
})
test('test readFile', async () => {
const content = await readFile('./.prettierignore')
console.log(content)
})
test('test getFiles', async () => {
const paths = await getFiles('./', ['**/*.ts', '**/package-lock.json'], {
gitignore: true
})
console.log(paths)
})
test('test runs', () => {
process.env['INPUT_WORKDIR'] = './'
process.env['INPUT_PATTERNS'] = '**/*.ts\n**/package-lock.json'
process.env['INPUT_GITIGNORE'] = 'true'
const np = process.execPath
const ip = path.join(__dirname, '..', 'lib', 'main.js')
const options: cp.ExecFileSyncOptions = {
env: process.env
}
console.log(cp.execFileSync(np, [ip], options).toString())
})

33
action.yml Normal file
View File

@ -0,0 +1,33 @@
name: 'hash-files'
description: 'Compute the SHA256 hash of specified files'
author: 'seepine'
inputs:
workdir:
description: >
The working directory for the action.
default: ${{ github.workspace }}
required: false
patterns:
description: >
The patterns used to match files. You can input multiple patterns seperated by `\n`.
We recommand using `|` to concat the patterns in `.yml` files.
required: true
gitignore:
description: >
Respect ignore patterns in .gitignore files that apply to the globbed files.
default: true
required: false
ignoreFiles:
descruption: >
Glob patterns to look for ignore files, which are then used to ignore globbed files.
This is a more generic form of the gitignore option, allowing you to find ignore files with a compatible syntax.
For instance, this works with Babel's .babelignore, Prettier's .prettierignore, or ESLint's .eslintignore files.
required: false
outputs:
hash:
description: 'The computed hash result'
matched-files:
description: 'The files matched by the patterns'
runs:
using: 'node16'
main: 'dist/index.js'

BIN
dist/index.js generated vendored Normal file

Binary file not shown.

BIN
dist/index.js.map generated vendored Normal file

Binary file not shown.

BIN
dist/licenses.txt generated vendored Normal file

Binary file not shown.

BIN
dist/sourcemap-register.js generated vendored Normal file

Binary file not shown.

9
jest.config.js Normal file
View File

@ -0,0 +1,9 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
}

6687
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

45
package.json Normal file
View File

@ -0,0 +1,45 @@
{
"name": "hash-files",
"version": "1.0.0",
"private": true,
"description": "Actions of Compute the SHA256 hash of specified files",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts",
"package": "ncc build --source-map --license licenses.txt",
"test": "jest",
"all": "npm run build && npm run format && npm run lint && npm run package && npm test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/seepine/hash-files.git"
},
"keywords": [
"actions",
"node",
"hash-files"
],
"author": "seepine",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.10.0",
"@cjs-exporter/globby": "^13.1.3",
"crypto": "^1.0.1"
},
"devDependencies": {
"@types/node": "^18.16.3",
"@typescript-eslint/parser": "^5.59.2",
"@vercel/ncc": "^0.36.1",
"eslint": "^8.39.0",
"eslint-plugin-github": "^4.7.0",
"eslint-plugin-jest": "^27.2.1",
"jest": "^29.5.0",
"js-yaml": "^4.1.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
}
}

12
src/constants.ts Normal file
View File

@ -0,0 +1,12 @@
/* eslint-disable no-shadow */
export enum Inputs {
Workdir = 'workdir', // Input for cache, restore, save action
Patterns = 'patterns', // Input for cache, restore, save action
Gitignore = 'gitignore', // Input for cache, restore action
IgnoreFiles = 'ignoreFiles' // Input for cache, save action
}
export enum Outputs {
Hash = 'hash', // Output from cache, restore action
MatchedFiles = 'matched-files' // Output from restore action
}

22
src/files.ts Normal file
View File

@ -0,0 +1,22 @@
import {Options, globby} from '@cjs-exporter/globby'
import * as fs from 'fs'
export async function getFiles(
workdir: string,
patterns: string[],
options?: Options
): Promise<string[]> {
return new Promise(async RES => {
const paths = await globby(
patterns.map(item => {
return workdir + item
}),
options
)
RES(paths)
})
}
export async function readFile(path: string): Promise<string> {
return fs.promises.readFile(path, 'utf-8')
}

57
src/main.ts Normal file
View File

@ -0,0 +1,57 @@
import * as core from '@actions/core'
import {Inputs, Outputs} from './constants'
import * as utils from './utils'
import {getFiles, readFile} from './files'
import {hashHexAsync} from './utils'
async function run(): Promise<void> {
try {
let workdir: string = utils.getInput(Inputs.Workdir, {required: true})
const patterns = utils.getInputAsArray(Inputs.Patterns, {
required: true
})
const gitignore = utils.getInputAsBool(Inputs.Gitignore) || true
const ignoreFiles = utils.getInputAsArray(Inputs.IgnoreFiles)
if (!workdir.endsWith('/')) {
workdir += '/'
}
core.debug(`workdir: ${workdir}`)
core.debug(`patterns: ${patterns}`)
core.debug(`gitignore: ${gitignore}`)
core.debug(`ignoreFiles: ${ignoreFiles}`)
const files = await getFiles(workdir, patterns, {gitignore, ignoreFiles})
let hash = ''
const reads = files.map(async file => readFile(file))
const fileContents = await Promise.all(reads)
const contents = await Promise.all(
fileContents.map(async fileContent => hashHexAsync(fileContent))
)
if (contents.length === 1) {
hash = contents[0]
} else if (contents.length > 1) {
let hashStr = ''
for (const content of contents) {
hashStr += content
}
hash = utils.hashHex(hashStr)
}
core.info('')
core.info('MatchedFiles:')
core.info(` ${files.toString()}`)
core.info(`Hash: ${hash}`)
core.setOutput(Outputs.Hash, hash)
core.setOutput(Outputs.MatchedFiles, files)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
}
run()

49
src/utils.ts Normal file
View File

@ -0,0 +1,49 @@
import * as core from '@actions/core'
import crypto, {BinaryLike} from 'crypto'
export function getInput(name: string, options?: core.InputOptions): string {
return core.getInput(name, options)
}
export function getInputAsArray(
name: string,
options?: core.InputOptions
): string[] {
return core
.getInput(name, options)
.split('\n')
.map(s => s.replace(/^!\s+/, '!').trim())
.filter(x => x !== '')
}
export function getInputAsInt(
name: string,
options?: core.InputOptions
): number | undefined {
const value = parseInt(core.getInput(name, options))
if (isNaN(value) || value < 0) {
return undefined
}
return value
}
export function getInputAsBool(
name: string,
options?: core.InputOptions
): boolean {
const result = core.getInput(name, options)
return result.toLowerCase() === 'true'
}
export function hashHex(content: BinaryLike, shaAlgorithm = 'sha256'): string {
return crypto.createHash(shaAlgorithm).update(content).digest('hex')
}
export async function hashHexAsync(
content: BinaryLike,
shaAlgorithm = 'sha256'
): Promise<string> {
return new Promise(RES => {
RES(hashHex(content, shaAlgorithm))
})
}

12
tsconfig.json Normal file
View File

@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./lib", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
},
"exclude": ["node_modules", "**/*.test.ts"]
}