mirror of
https://github.com/benjlevesque/short-sha.git
synced 2025-12-06 17:57:50 +01:00
short-sha
This commit is contained in:
13
.github/workflows/test.yml
vendored
13
.github/workflows/test.yml
vendored
@@ -10,14 +10,21 @@ jobs:
|
|||||||
build: # make sure build/ci work properly
|
build: # make sure build/ci work properly
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- run: |
|
- run: |
|
||||||
npm install
|
npm install
|
||||||
npm run all
|
npm run all
|
||||||
test: # make sure the action works on a clean machine without building
|
test: # make sure the action works on a clean machine without building
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- uses: ./
|
- uses: ./
|
||||||
|
id: short-sha
|
||||||
with:
|
with:
|
||||||
milliseconds: 1000
|
length: 6
|
||||||
|
- name: validate output
|
||||||
|
run: |
|
||||||
|
echo $SHA;
|
||||||
|
[ ${#SHA} -eq 6 ] && exit 0 || exit 1;
|
||||||
|
env:
|
||||||
|
SHA: ${{ steps.short-sha.outputs.sha }}
|
||||||
|
|||||||
@@ -1,11 +1,24 @@
|
|||||||
{
|
{
|
||||||
"printWidth": 80,
|
"printWidth": 80,
|
||||||
"tabWidth": 2,
|
"tabWidth": 2,
|
||||||
"useTabs": false,
|
"useTabs": false,
|
||||||
"semi": false,
|
"semi": false,
|
||||||
"singleQuote": true,
|
"singleQuote": true,
|
||||||
"trailingComma": "none",
|
"trailingComma": "none",
|
||||||
"bracketSpacing": false,
|
"bracketSpacing": false,
|
||||||
"arrowParens": "avoid",
|
"arrowParens": "avoid",
|
||||||
"parser": "typescript"
|
"overrides": [
|
||||||
}
|
{
|
||||||
|
"files": "*.md",
|
||||||
|
"options": {
|
||||||
|
"parser": "markdown"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"files": "*.ts",
|
||||||
|
"options": {
|
||||||
|
"parser": "typescript"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
116
README.md
116
README.md
@@ -1,101 +1,29 @@
|
|||||||
<p align="center">
|
# Short SHA
|
||||||
<a href="https://github.com/actions/typescript-action/actions"><img alt="typescript-action status" src="https://github.com/actions/typescript-action/workflows/build-test/badge.svg"></a>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
# Create a JavaScript Action using TypeScript
|
`short-sha` is a GitHub Action than provides an output `sha` with the shortened commit SHA.
|
||||||
|
|
||||||
Use this template to bootstrap the creation of a JavaScript action.:rocket:
|
## Usage
|
||||||
|
|
||||||
This template includes compilication support, tests, a validation workflow, publishing, and versioning guidance.
|
|
||||||
|
|
||||||
If you are new, there's also a simpler introduction. See the [Hello World JavaScript Action](https://github.com/actions/hello-world-javascript-action)
|
|
||||||
|
|
||||||
## Create an action from this template
|
|
||||||
|
|
||||||
Click the `Use this Template` and provide the new repo details for your action
|
|
||||||
|
|
||||||
## Code in Master
|
|
||||||
|
|
||||||
Install the dependencies
|
|
||||||
```bash
|
|
||||||
$ npm install
|
|
||||||
```
|
|
||||||
|
|
||||||
Build the typescript and package it for distribution
|
|
||||||
```bash
|
|
||||||
$ npm run build && npm run pack
|
|
||||||
```
|
|
||||||
|
|
||||||
Run the tests :heavy_check_mark:
|
|
||||||
```bash
|
|
||||||
$ npm test
|
|
||||||
|
|
||||||
PASS ./index.test.js
|
|
||||||
✓ throws invalid number (3ms)
|
|
||||||
✓ wait 500 ms (504ms)
|
|
||||||
✓ test runs (95ms)
|
|
||||||
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
## Change action.yml
|
|
||||||
|
|
||||||
The action.yml contains defines the inputs and output for your action.
|
|
||||||
|
|
||||||
Update the action.yml with your name, description, inputs and outputs for your action.
|
|
||||||
|
|
||||||
See the [documentation](https://help.github.com/en/articles/metadata-syntax-for-github-actions)
|
|
||||||
|
|
||||||
## Change the Code
|
|
||||||
|
|
||||||
Most toolkit and CI/CD operations involve async operations so the action is run in an async function.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import * as core from '@actions/core';
|
|
||||||
...
|
|
||||||
|
|
||||||
async function run() {
|
|
||||||
try {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
catch (error) {
|
|
||||||
core.setFailed(error.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
run()
|
|
||||||
```
|
|
||||||
|
|
||||||
See the [toolkit documentation](https://github.com/actions/toolkit/blob/master/README.md#packages) for the various packages.
|
|
||||||
|
|
||||||
## Publish to a distribution branch
|
|
||||||
|
|
||||||
Actions are run from GitHub repos so we will checkin the packed dist folder.
|
|
||||||
|
|
||||||
Then run [ncc](https://github.com/zeit/ncc) and push the results:
|
|
||||||
```bash
|
|
||||||
$ npm run pack
|
|
||||||
$ git add dist
|
|
||||||
$ git commit -a -m "prod dependencies"
|
|
||||||
$ git push origin releases/v1
|
|
||||||
```
|
|
||||||
|
|
||||||
Your action is now published! :rocket:
|
|
||||||
|
|
||||||
See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
|
|
||||||
|
|
||||||
## Validate
|
|
||||||
|
|
||||||
You can now validate the action by referencing `./` in a workflow in your repo (see [test.yml](.github/workflows/test.yml)])
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
uses: ./
|
name: 'build-test'
|
||||||
with:
|
on: [push]
|
||||||
milliseconds: 1000
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- uses: benjlevesque/short-sha
|
||||||
|
id: short-sha
|
||||||
|
with:
|
||||||
|
length: 6
|
||||||
|
- run: echo $SHA
|
||||||
|
env:
|
||||||
|
SHA: ${{ steps.short-sha.outputs.sha }}
|
||||||
```
|
```
|
||||||
|
|
||||||
See the [actions tab](https://github.com/actions/javascript-action/actions) for runs of this action! :rocket:
|
## Options
|
||||||
|
|
||||||
## Usage:
|
| Name | Required | Default | Description |
|
||||||
|
| ------ | -------- | ------- | ---------------------------------------- |
|
||||||
After testing you can [create a v1 tag](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md) to reference the stable and latest V1 action
|
| length | `false` | 8 | the expected length of the shortened SHA |
|
||||||
|
|||||||
@@ -1,27 +1,15 @@
|
|||||||
import {wait} from '../src/wait'
|
|
||||||
import * as process from 'process'
|
import * as process from 'process'
|
||||||
import * as cp from 'child_process'
|
import * as cp from 'child_process'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
|
|
||||||
test('throws invalid number', async () => {
|
|
||||||
const input = parseInt('foo', 10)
|
|
||||||
await expect(wait(input)).rejects.toThrow('milliseconds not a number')
|
|
||||||
})
|
|
||||||
|
|
||||||
test('wait 500 ms', async () => {
|
|
||||||
const start = new Date()
|
|
||||||
await wait(500)
|
|
||||||
const end = new Date()
|
|
||||||
var delta = Math.abs(end.getTime() - start.getTime())
|
|
||||||
expect(delta).toBeGreaterThan(450)
|
|
||||||
})
|
|
||||||
|
|
||||||
// shows how the runner will run a javascript action with env / stdout protocol
|
// shows how the runner will run a javascript action with env / stdout protocol
|
||||||
test('test runs', () => {
|
test('test runs', () => {
|
||||||
process.env['INPUT_MILLISECONDS'] = '500'
|
process.env['GITHUB_SHA'] = '6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1'
|
||||||
|
process.env['INPUT_LENGTH'] = '6'
|
||||||
const ip = path.join(__dirname, '..', 'lib', 'main.js')
|
const ip = path.join(__dirname, '..', 'lib', 'main.js')
|
||||||
const options: cp.ExecSyncOptions = {
|
const options: cp.ExecSyncOptions = {
|
||||||
env: process.env
|
env: process.env
|
||||||
}
|
}
|
||||||
console.log(cp.execSync(`node ${ip}`, options).toString())
|
const output = cp.execSync(`node ${ip}`, options).toString()
|
||||||
|
expect(output).toContain('::set-output name=sha::6e8dcc')
|
||||||
})
|
})
|
||||||
|
|||||||
39
__tests__/shorten.test.ts
Normal file
39
__tests__/shorten.test.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import {shorten} from '../src/shorten'
|
||||||
|
|
||||||
|
test('normal case', () => {
|
||||||
|
expect(shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', 7)).toBe('6e8dcce')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('already short', () => {
|
||||||
|
expect(shorten('6e8dcce', 7)).toBe('6e8dcce')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('too short', () => {
|
||||||
|
expect(() => shorten('6e8dcc', 7)).toThrowError('input is too short')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('undefined', () => {
|
||||||
|
expect(() => shorten(undefined as any, 7)).toThrowError('sha must be defined')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('null', () => {
|
||||||
|
expect(() => shorten(null as any, 7)).toThrowError('sha must be defined')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('invalid length', () => {
|
||||||
|
expect(() =>
|
||||||
|
shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', 'x' as any)
|
||||||
|
).toThrowError('length is invalid')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('negative length', () => {
|
||||||
|
expect(() =>
|
||||||
|
shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', -1)
|
||||||
|
).toThrowError('length is invalid')
|
||||||
|
})
|
||||||
|
|
||||||
|
test('zero length', () => {
|
||||||
|
expect(() =>
|
||||||
|
shorten('6e8dcce3fd71cfe9aca3e18c82255dd1e4052aa1', 0)
|
||||||
|
).toThrowError('length is invalid')
|
||||||
|
})
|
||||||
14
action.yml
14
action.yml
@@ -1,10 +1,14 @@
|
|||||||
name: 'Your name here'
|
name: 'short-sha'
|
||||||
description: 'Provide a description here'
|
description: 'Provide a description here'
|
||||||
author: 'Your name or organization here'
|
author: 'benjlevesque'
|
||||||
inputs:
|
inputs:
|
||||||
myInput: # change this
|
length:
|
||||||
description: 'input description here'
|
description: 'length of the sha1'
|
||||||
default: 'default value if applicable'
|
default: '7'
|
||||||
|
required: false
|
||||||
|
outputs:
|
||||||
|
sha:
|
||||||
|
description: 'shortened SHA'
|
||||||
runs:
|
runs:
|
||||||
using: 'node12'
|
using: 'node12'
|
||||||
main: 'dist/index.js'
|
main: 'dist/index.js'
|
||||||
|
|||||||
25426
dist/index.js
vendored
25426
dist/index.js
vendored
File diff suppressed because it is too large
Load Diff
6523
package-lock.json
generated
6523
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@@ -1,31 +1,30 @@
|
|||||||
{
|
{
|
||||||
"name": "typescript-action",
|
"name": "short-sha",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "TypeScript template action",
|
"description": "Github Action to shorten the git SHA1 and make it accessible in outputs",
|
||||||
"main": "lib/main.js",
|
"main": "lib/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc && ncc build",
|
||||||
"format": "prettier --write **/*.ts",
|
"format": "prettier --write **/*.ts",
|
||||||
"format-check": "prettier --check **/*.ts",
|
"format-check": "prettier --check **/*.ts",
|
||||||
"lint": "eslint src/**/*.ts",
|
"lint": "eslint src/**/*.ts",
|
||||||
"pack": "ncc build",
|
|
||||||
"test": "jest",
|
"test": "jest",
|
||||||
"all": "npm run build && npm run format && npm run lint && npm run pack && npm test"
|
"all": "yarn build && yarn format && yarn lint && yarn pack && yarn test"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/actions/typescript-action.git"
|
"url": "git+https://github.com/benjlevesque/short-sha.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"actions",
|
"actions",
|
||||||
"node",
|
"git"
|
||||||
"setup"
|
|
||||||
],
|
],
|
||||||
"author": "YourNameOrOrganization",
|
"author": "benjlevesque",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.0"
|
"@actions/core": "^1.2.0",
|
||||||
|
"@actions/github": "^2.1.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/jest": "^24.0.23",
|
"@types/jest": "^24.0.23",
|
||||||
|
|||||||
17
src/main.ts
17
src/main.ts
@@ -1,16 +1,17 @@
|
|||||||
import * as core from '@actions/core'
|
import * as core from '@actions/core'
|
||||||
import {wait} from './wait'
|
import {context} from '@actions/github'
|
||||||
|
import {shorten} from './shorten'
|
||||||
|
|
||||||
async function run(): Promise<void> {
|
async function run(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const ms: string = core.getInput('milliseconds')
|
const sha = context.sha
|
||||||
core.debug(`Waiting ${ms} milliseconds ...`)
|
core.debug(`Sha: ${sha}`)
|
||||||
|
const length = Number(core.getInput('length'))
|
||||||
|
core.debug(`Length: ${length}`)
|
||||||
|
const shortSha = shorten(sha, length)
|
||||||
|
core.debug(`Output: ${shortSha}`)
|
||||||
|
|
||||||
core.debug(new Date().toTimeString())
|
core.setOutput('sha', shortSha)
|
||||||
await wait(parseInt(ms, 10))
|
|
||||||
core.debug(new Date().toTimeString())
|
|
||||||
|
|
||||||
core.setOutput('time', new Date().toTimeString())
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.setFailed(error.message)
|
core.setFailed(error.message)
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/shorten.ts
Normal file
12
src/shorten.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
export function shorten(sha: string, length: number): string {
|
||||||
|
if (!sha) {
|
||||||
|
throw new Error('sha must be defined')
|
||||||
|
}
|
||||||
|
if (length <= 0 || !Number.isInteger(length)) {
|
||||||
|
throw new Error('length is invalid')
|
||||||
|
}
|
||||||
|
if (sha.length < length) {
|
||||||
|
throw new Error('input is too short')
|
||||||
|
}
|
||||||
|
return sha.substring(0, length)
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
export async function wait(milliseconds: number): Promise<string> {
|
|
||||||
return new Promise(resolve => {
|
|
||||||
if (isNaN(milliseconds)) {
|
|
||||||
throw new Error('milliseconds not a number')
|
|
||||||
}
|
|
||||||
|
|
||||||
setTimeout(() => resolve('done!'), milliseconds)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user