1
0
mirror of https://github.com/benjlevesque/short-sha.git synced 2025-12-06 01:37:51 +01:00

short-sha

This commit is contained in:
Benjamin LEVESQUE
2020-03-09 21:40:47 +01:00
parent 6e8dcce3fd
commit 9f96a4f558
13 changed files with 29878 additions and 6881 deletions

View File

@@ -10,14 +10,21 @@ jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- run: |
npm install
npm run all
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- uses: ./
id: short-sha
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 }}

View File

@@ -1,11 +1,24 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
}
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"overrides": [
{
"files": "*.md",
"options": {
"parser": "markdown"
}
},
{
"files": "*.ts",
"options": {
"parser": "typescript"
}
}
]
}

116
README.md
View File

@@ -1,101 +1,29 @@
<p align="center">
<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>
# Short SHA
# 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:
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)])
## Usage
```yaml
uses: ./
with:
milliseconds: 1000
name: 'build-test'
on: [push]
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:
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
| Name | Required | Default | Description |
| ------ | -------- | ------- | ---------------------------------------- |
| length | `false` | 8 | the expected length of the shortened SHA |

View File

@@ -1,27 +1,15 @@
import {wait} from '../src/wait'
import * as process from 'process'
import * as cp from 'child_process'
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
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 options: cp.ExecSyncOptions = {
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
View 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')
})

View File

@@ -1,10 +1,14 @@
name: 'Your name here'
name: 'short-sha'
description: 'Provide a description here'
author: 'Your name or organization here'
author: 'benjlevesque'
inputs:
myInput: # change this
description: 'input description here'
default: 'default value if applicable'
length:
description: 'length of the sha1'
default: '7'
required: false
outputs:
sha:
description: 'shortened SHA'
runs:
using: 'node12'
main: 'dist/index.js'

25426
dist/index.js vendored

File diff suppressed because it is too large Load Diff

6523
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,31 +1,30 @@
{
"name": "typescript-action",
"name": "short-sha",
"version": "0.0.0",
"private": true,
"description": "TypeScript template action",
"description": "Github Action to shorten the git SHA1 and make it accessible in outputs",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
"build": "tsc && ncc build",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"lint": "eslint src/**/*.ts",
"pack": "ncc build",
"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": {
"type": "git",
"url": "git+https://github.com/actions/typescript-action.git"
"url": "git+https://github.com/benjlevesque/short-sha.git"
},
"keywords": [
"actions",
"node",
"setup"
"git"
],
"author": "YourNameOrOrganization",
"author": "benjlevesque",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.0"
"@actions/core": "^1.2.0",
"@actions/github": "^2.1.1"
},
"devDependencies": {
"@types/jest": "^24.0.23",

View File

@@ -1,16 +1,17 @@
import * as core from '@actions/core'
import {wait} from './wait'
import {context} from '@actions/github'
import {shorten} from './shorten'
async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')
core.debug(`Waiting ${ms} milliseconds ...`)
const sha = context.sha
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())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())
core.setOutput('time', new Date().toTimeString())
core.setOutput('sha', shortSha)
} catch (error) {
core.setFailed(error.message)
}

12
src/shorten.ts Normal file
View 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)
}

View File

@@ -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)
})
}

4518
yarn.lock Normal file

File diff suppressed because it is too large Load Diff