5
0
Fork 0
mirror of https://github.com/pre-commit/action.git synced 2025-11-07 10:46:56 +00:00

Initial commit

This commit is contained in:
Anthony Sottile 2019-11-24 19:27:05 -08:00
parent 1dde72fd16
commit 614487b962
10 changed files with 4545 additions and 0 deletions

19
.github/workflows/deploy.yml vendored Normal file
View file

@ -0,0 +1,19 @@
name: deploy
on:
pull_request:
push:
branches: [master]
jobs:
build:
name: pr
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/setup-python@v1
- run: pip install virtualenv
- run: make
- run: make push
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
/.cache
/dist
/node_modules
/venv

13
.pre-commit-config.yaml Normal file
View file

@ -0,0 +1,13 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: check-json
- id: check-yaml
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v6.7.0
hooks:
- id: eslint
args: [--fix]

24
Makefile Normal file
View file

@ -0,0 +1,24 @@
.PHONY: all
all: dist/index.js
venv: Makefile
rm -rf venv
virtualenv venv -ppython3
venv/bin/pip install markdown-to-presentation
node_modules: package.json
npm install --silent
test -d node_modules
touch node_modules
dist/index.js: index.js node_modules
node_modules/.bin/webpack --config webpack.config.js
# terrible hack to prevent lookup of `navigator`
# if someone knows the correct way to use webpack, PRs welcome!
sed -i 's/\bnavigator\b/({})/g' $@
.PHONY: push
push: venv
venv/bin/markdown-to-presentation push \
--pages-branch release \
README.md action.yml dist/index.js

63
README.md Normal file
View file

@ -0,0 +1,63 @@
[![Build Status](https://github.com/pre-commit/action/workflows/deploy/badge.svg)](https://github.com/pre-commit/action/actions)
pre-commit/action
=================
a GitHub action to run [pre-commit](https://pre-commit.com)
### using this action
To use this action, make a file `.github/workflows/pre-commit.yml`. Here's a
template to get started:
```yaml
on:
pull_request:
push:
branches: [master]
jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-python@v1
- name: set PY
run: echo "::set-env name=PY::$(python --version --version | sha256sum | cut -d' ' -f1)"
- uses: actions/cache@v1
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ env.PY }}|${{ hashFiles('.pre-commit-config.yaml') }}
- uses: pre-commit/action@v1.0.0
```
This does a few things:
- clones the code
- installs python
- sets up the `pre-commit` cache
Hopefully in the future when `actions` matures the yaml can be simplified.
### using this action in private repositories
this action also provides an additional behaviour when used in private
repositories. when configured with a github token, the action will push back
fixes to the pull request branch.
here's an example configuration for that (use the template above except for the
`pre-commit` action):
```yaml
- uses: pre-commit/action@v1.0.0
with:
token: ${{ secrets.GITHUB_TOKEN }}
```
note that `secrets.GITHUB_TOKEN` is automatically provisioned and will not
require any special configuration.
while you could _technically_ configure this for a public repository (using a
personal access token), I can't think of a way to do this safely without
exposing a privileged token to pull requests -- if you have any ideas, please
leave an issue!

9
action.yml Normal file
View file

@ -0,0 +1,9 @@
name: pre-commit
description: run pre-commit and optionally commit back to the pull request
inputs:
token:
description: github token to clone / push with
required: false
runs:
using: 'node12'
main: 'dist/index.js'

51
index.js Normal file
View file

@ -0,0 +1,51 @@
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
const ARGS = [
'run', '--all-files', '--show-diff-on-failure', '--color=always'
];
function addToken(url, token) {
return url.replace(/^https:\/\//, `https://x-access-token:${token}@`)
}
async function main() {
await core.group('install pre-commit', async () => {
await exec.exec('pip', ['install', 'pre-commit']);
await exec.exec('pip', ['freeze', '--local']);
});
const token = core.getInput('token');
const push = !!token;
const ret = await exec.exec('pre-commit', ARGS, {ignoreReturnCode: push});
if (ret && push) {
// actions do not run on pushes made by actions.
// need to make absolute sure things are good before pushing
// TODO: is there a better way around this limitation?
await exec.exec('pre-commit', ARGS);
const diff = await exec.exec(
'git', ['diff', '--quiet'], {ignoreReturnCode: true}
);
if (diff) {
await core.group('push fixes', async () => {
await exec.exec('git', ['config', 'user.name', 'pre-commit']);
await exec.exec(
'git', ['config', 'user.email', 'pre-commit@example.com']
);
const branch = github.context.payload.pull_request.head.ref;
await exec.exec('git', ['checkout', 'HEAD', '-b', branch]);
await exec.exec('git', ['commit', '-am', 'pre-commit fixes']);
const pr = github.context.payload.pull_request;
const url = addToken(pr.head.repo.clone_url, token);
await exec.exec('git', ['remote', 'set-url', 'origin', url]);
await exec.exec('git', ['push', 'origin', 'HEAD']);
});
}
}
}
main().catch((e) => core.setFailed(e.message));

4331
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

21
package.json Normal file
View file

@ -0,0 +1,21 @@
{
"private": true,
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/exec": "^1.0.1",
"@actions/github": "^1.1.0"
},
"devDependencies": {
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10"
},
"eslintConfig": {
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2017
},
"env": {
"node": true
}
}
}

10
webpack.config.js Normal file
View file

@ -0,0 +1,10 @@
const path = require('path');
module.exports = {
target: 'node',
entry: './index.js',
output: {
filename: 'index.js',
path: path.resolve(__dirname, 'dist'),
},
};