Compare commits
138 Commits
Author | SHA1 | Date | |
---|---|---|---|
51485a4001 | |||
a12ae43dd8 | |||
f1dee55574 | |||
23487cb16f | |||
84560243ae | |||
3e54e516ae | |||
2f717c44f2 | |||
4a6ab128b0 | |||
f9b6927981 | |||
41858b8c7c | |||
67236c010b | |||
544d2efb30 | |||
0dd30832fc | |||
e1ae6cf354 | |||
0ef0ffc44e | |||
125ac487c1 | |||
e246bba31d | |||
147f37a473 | |||
a19645e82f | |||
286cf073ea | |||
7641a6707d | |||
e4e910b0ce | |||
89d96d6c20 | |||
45f3a377de | |||
943013ec20 | |||
49a9e469b1 | |||
528677aa24 | |||
85798b53e5 | |||
d164f0d6d8 | |||
9cf34bb2b8 | |||
629aad184c | |||
1bac7d9888 | |||
44fae30515 | |||
689e90e8a0 | |||
d73150ab3c | |||
2c3839ff26 | |||
f881d993a0 | |||
d3f3b8adb7 | |||
e3e2a92cbd | |||
e1bd4355fb | |||
345e5340e3 | |||
13e9e901f4 | |||
771153bfbc | |||
e868220d9f | |||
0348c21999 | |||
a1c5369c06 | |||
90fbd40fc4 | |||
3d2a5790a3 | |||
54a84d46fb | |||
ba0e91ca3a | |||
4957db1776 | |||
2ea701ccbd | |||
b6429a6771 | |||
7ba2fe0d4d | |||
b248ff60d3 | |||
0f7d62b0dc | |||
f16cd8cd00 | |||
f745d2bb6b | |||
9bf04a21dc | |||
273fa7ab10 | |||
18087a51d1 | |||
37037d32f2 | |||
9d250b4a7c | |||
e820a262b2 | |||
3b7ba9cb59 | |||
2efd4a3be8 | |||
0098fd17c9 | |||
863caeb194 | |||
1d3f25bff9 | |||
62252c6349 | |||
9535745fa5 | |||
dc87c27dac | |||
87260465c1 | |||
014f5e25d2 | |||
a520c23e6f | |||
6317259e28 | |||
809d3b078b | |||
b026646c83 | |||
c598686db1 | |||
25d72af787 | |||
79f232513c | |||
f7f5eff206 | |||
485c6a047e | |||
e2ff3f296a | |||
294f27a519 | |||
c238b72278 | |||
ba40dc6b1d | |||
65dc624d8b | |||
466abb7dfd | |||
5c7c749f16 | |||
2f458de87f | |||
94d029dd63 | |||
a5502f9224 | |||
fa6ef09daf | |||
2effa0b58a | |||
3b01eb9dce | |||
0d019f3dae | |||
48e7180822 | |||
8577a0ee23 | |||
cf72500b28 | |||
d737e6d962 | |||
8870cfbcd4 | |||
3395f777a4 | |||
04eca20383 | |||
10cbc929b3 | |||
64c208bfbc | |||
20d5541dab | |||
85a3a6abe4 | |||
b66692b61d | |||
348830fe4b | |||
e4cc61e5b1 | |||
27e14e0f3f | |||
6993abb7cd | |||
ca150a071d | |||
1ef31b642a | |||
fc9d1728df | |||
1ad27ad153 | |||
1778ebaefa | |||
1dd9e0522b | |||
66883b5fcf | |||
8ea3043ee4 | |||
95f6eefffa | |||
977a01f96c | |||
13e2c1f984 | |||
afc07fb790 | |||
b7926cae39 | |||
ce34998f1f | |||
1e4a1137a8 | |||
eda878dc56 | |||
f7d08adb03 | |||
b030249340 | |||
853ade09ed | |||
666cc9164e | |||
24c25982ec | |||
85e31a269e | |||
9c543591de | |||
4cb8a13e45 | |||
67503c7b3d |
32
.eslintrc.json
Normal file
32
.eslintrc.json
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"env": {
|
||||||
|
"node": true,
|
||||||
|
"jest": true
|
||||||
|
},
|
||||||
|
"parser": "@typescript-eslint/parser",
|
||||||
|
"parserOptions": {
|
||||||
|
"ecmaVersion": 2020,
|
||||||
|
"sourceType": "module"
|
||||||
|
},
|
||||||
|
"extends": [
|
||||||
|
"eslint:recommended",
|
||||||
|
"plugin:@typescript-eslint/eslint-recommended",
|
||||||
|
"plugin:@typescript-eslint/recommended",
|
||||||
|
"plugin:import/errors",
|
||||||
|
"plugin:import/warnings",
|
||||||
|
"plugin:import/typescript",
|
||||||
|
"plugin:prettier/recommended"
|
||||||
|
],
|
||||||
|
"plugins": [
|
||||||
|
"@typescript-eslint",
|
||||||
|
"simple-import-sort"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"import/first": "error",
|
||||||
|
"import/newline-after-import": "error",
|
||||||
|
"import/no-duplicates": "error",
|
||||||
|
"simple-import-sort/imports": "error",
|
||||||
|
"simple-import-sort/exports": "error",
|
||||||
|
"sort-imports": "off"
|
||||||
|
}
|
||||||
|
}
|
14
.github/dependabot.yml
vendored
Normal file
14
.github/dependabot.yml
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: github-actions
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
|
reviewers:
|
||||||
|
- "golangci/team"
|
||||||
|
- package-ecosystem: npm
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
|
reviewers:
|
||||||
|
- "golangci/team"
|
44
.github/workflows/codeql.yaml
vendored
Normal file
44
.github/workflows/codeql.yaml
vendored
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
name: "Code Scanning - Action"
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
# The branches below must be a subset of the branches above
|
||||||
|
branches: [ master ]
|
||||||
|
schedule:
|
||||||
|
- cron: '0 17 * * 5'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
codeQL:
|
||||||
|
# CodeQL runs on ubuntu-latest, windows-latest, and macos-latest
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
# Must fetch at least the immediate parents so that if this is
|
||||||
|
# a pull request then we can checkout the head of the pull request.
|
||||||
|
# Only include this option if you are running this workflow on pull requests.
|
||||||
|
fetch-depth: 2
|
||||||
|
|
||||||
|
# If this run was triggered by a pull request event then checkout
|
||||||
|
# the head of the pull request instead of the merge commit.
|
||||||
|
# Only include this step if you are running this workflow on pull requests.
|
||||||
|
- run: git checkout HEAD^2
|
||||||
|
if: ${{ github.event_name == 'pull_request' }}
|
||||||
|
|
||||||
|
# Initializes the CodeQL tools for scanning.
|
||||||
|
- name: Initialize CodeQL
|
||||||
|
uses: github/codeql-action/init@v1
|
||||||
|
# Override language selection by uncommenting this and choosing your languages
|
||||||
|
with:
|
||||||
|
language: 'javascript'
|
||||||
|
|
||||||
|
- run: |
|
||||||
|
npm install
|
||||||
|
npm run all
|
||||||
|
|
||||||
|
- name: Perform CodeQL Analysis
|
||||||
|
uses: github/codeql-action/analyze@v1
|
11
.github/workflows/dockerimage.yml
vendored
11
.github/workflows/dockerimage.yml
vendored
@ -1,11 +0,0 @@
|
|||||||
name: docker image
|
|
||||||
|
|
||||||
on: [push]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v1
|
|
||||||
- name: Build the Docker image
|
|
||||||
run: docker build . --file Dockerfile --tag golangci-lint-action:$(date +%s)
|
|
16
.github/workflows/golangci.yml
vendored
16
.github/workflows/golangci.yml
vendored
@ -1,16 +0,0 @@
|
|||||||
name: golangci
|
|
||||||
on: [pull_request]
|
|
||||||
jobs:
|
|
||||||
golangci-lint-dockerfile:
|
|
||||||
name: lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- name: Check out code into the Go module directory
|
|
||||||
uses: actions/checkout@v1
|
|
||||||
- name: golangci-lint
|
|
||||||
uses: ./
|
|
||||||
with:
|
|
||||||
github_token: ${{ secrets.github_token }}
|
|
||||||
directory: sample
|
|
||||||
format: colored-line-number
|
|
||||||
flags: --issues-exit-code 0
|
|
48
.github/workflows/test.yml
vendored
Normal file
48
.github/workflows/test.yml
vendored
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
name: "build-and-test"
|
||||||
|
on: # rebuild any PRs and main branch changes
|
||||||
|
pull_request:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- "releases/*"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build: # make sure build/ci work properly
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- run: |
|
||||||
|
npm install
|
||||||
|
npm run all
|
||||||
|
test: # make sure the action works on a clean machine without building
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os:
|
||||||
|
- ubuntu-latest
|
||||||
|
- macos-latest
|
||||||
|
- windows-latest
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: ./
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
args: --issues-exit-code=0 ./sample/...
|
||||||
|
only-new-issues: true
|
||||||
|
|
||||||
|
# Test with full version vX.Y.Z
|
||||||
|
test-full-version:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os:
|
||||||
|
- ubuntu-latest
|
||||||
|
- macos-latest
|
||||||
|
- windows-latest
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: ./
|
||||||
|
with:
|
||||||
|
version: v1.28.3
|
||||||
|
args: --issues-exit-code=0 ./sample/...
|
||||||
|
only-new-issues: true
|
99
.gitignore
vendored
Normal file
99
.gitignore
vendored
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
# symlink for `act`
|
||||||
|
/golangci-lint-action
|
||||||
|
|
||||||
|
__tests__/runner/*
|
||||||
|
|
||||||
|
node_modules/
|
||||||
|
lib/
|
||||||
|
|
||||||
|
# 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/
|
||||||
|
|
||||||
|
# Text editor files
|
||||||
|
.vscode/
|
9
.prettierrc.json
Normal file
9
.prettierrc.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"endOfLine": "lf",
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": false,
|
||||||
|
"tabWidth": 2,
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"printWidth": 140,
|
||||||
|
"parser": "typescript"
|
||||||
|
}
|
@ -1,5 +0,0 @@
|
|||||||
FROM golangci/golangci-lint:v1.25
|
|
||||||
|
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
|
||||||
|
|
||||||
ENTRYPOINT ["/entrypoint.sh"]
|
|
165
README.md
165
README.md
@ -1,5 +1,166 @@
|
|||||||
# golangci-lint-action
|
# golangci-lint-action
|
||||||
|
|
||||||
Work in progress
|
[](https://github.com/golangci/golangci-lint-action/actions)
|
||||||
|
|
||||||
Based on [reviewdog action](https://github.com/reviewdog/action-golangci-lint).
|
It's the official GitHub action for [golangci-lint](https://github.com/golangci/golangci-lint) from its authors.
|
||||||
|
The action runs [golangci-lint](https://github.com/golangci/golangci-lint) and reports issues from linters.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Compatibility
|
||||||
|
|
||||||
|
* `v2.0.0+` works with `golangci-lint` version >= `v1.28.3`
|
||||||
|
* `v1.2.2` is deprecated due to we forgot to change the minimum version of `golangci-lint` to `v1.28.3` ([issue](https://github.com/golangci/golangci-lint-action/issues/39))
|
||||||
|
* `v1.2.1` works with `golangci-lint` version >= `v1.14.0` ([issue](https://github.com/golangci/golangci-lint-action/issues/39))
|
||||||
|
|
||||||
|
## How to use
|
||||||
|
|
||||||
|
Add `.github/workflows/golangci-lint.yml` with the following contents:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
name: golangci-lint
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- v*
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
jobs:
|
||||||
|
golangci:
|
||||||
|
name: lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v2
|
||||||
|
with:
|
||||||
|
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
|
||||||
|
version: v1.29
|
||||||
|
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
|
|
||||||
|
# Optional: if set to true then the action will use pre-installed Go.
|
||||||
|
# skip-go-installation: true
|
||||||
|
|
||||||
|
# Optional: if set to true then the action don't cache or restore ~/go/pkg.
|
||||||
|
# skip-pkg-cache: true
|
||||||
|
|
||||||
|
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build.
|
||||||
|
# skip-build-cache: true
|
||||||
|
```
|
||||||
|
|
||||||
|
We recommend running this action in a job separate from other jobs (`go test`, etc)
|
||||||
|
because different jobs [run in parallel](https://help.github.com/en/actions/getting-started-with-github-actions/core-concepts-for-github-actions#job).
|
||||||
|
|
||||||
|
### Multiple OS Support
|
||||||
|
|
||||||
|
If you need to run linters for specific operating systems, you will need to use `v2` of the action. Here is a sample configuration file:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
name: golangci-lint
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- v*
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
- main
|
||||||
|
pull_request:
|
||||||
|
jobs:
|
||||||
|
golangci:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: [1.15.x]
|
||||||
|
os: [macos-latest, windows-latest]
|
||||||
|
name: lint
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- name: golangci-lint
|
||||||
|
uses: golangci/golangci-lint-action@v2
|
||||||
|
with:
|
||||||
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
|
version: v1.29
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
|
||||||
|
# Optional: golangci-lint command line arguments.
|
||||||
|
# args: --issues-exit-code=0
|
||||||
|
|
||||||
|
# Optional: show only new issues if it's a pull request. The default value is `false`.
|
||||||
|
# only-new-issues: true
|
||||||
|
```
|
||||||
|
|
||||||
|
You will also likely need to add the following `.gitattributes` file to ensure that line endings for windows builds are properly formatted:
|
||||||
|
|
||||||
|
```.gitattributes
|
||||||
|
*.go text eol=lf
|
||||||
|
```
|
||||||
|
|
||||||
|
## Comments and Annotations
|
||||||
|
|
||||||
|
Currently, GitHub parses the action's output and creates [annotations](https://github.community/t5/GitHub-Actions/What-are-annotations/td-p/30770).
|
||||||
|
|
||||||
|
The restrictions of annotations are the following:
|
||||||
|
|
||||||
|
1. Currently, they don't support markdown formatting (see the [feature request](https://github.community/t5/GitHub-API-Development-and/Checks-Ability-to-include-Markdown-in-line-annotations/m-p/56704))
|
||||||
|
2. They aren't shown in list of comments like it was with [golangci.com](https://golangci.com). If you would like to have comments - please, up-vote [the issue](https://github.com/golangci/golangci-lint-action/issues/5).
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
|
||||||
|
The action was implemented with performance in mind:
|
||||||
|
|
||||||
|
1. We cache data by [@actions/cache](https://github.com/actions/toolkit/tree/master/packages/cache) between builds: Go build cache, Go modules cache, golangci-lint analysis cache.
|
||||||
|
2. We don't use Docker because image pulling is slow.
|
||||||
|
3. We do as much as we can in parallel, e.g. we download cache, go and golangci-lint binary in parallel.
|
||||||
|
|
||||||
|
For example, in a repository of [golangci-lint](https://github.com/golangci/golangci-lint) running this action without the cache takes 50s, but with cache takes 14s:
|
||||||
|
* in parallel:
|
||||||
|
* 13s to download Go
|
||||||
|
* 4s to restore 50 MB of cache
|
||||||
|
* 1s to find and install `golangci-lint`
|
||||||
|
* 1s to run `golangci-lint` (it takes 35s without cache)
|
||||||
|
|
||||||
|
## Internals
|
||||||
|
|
||||||
|
We use JavaScript-based action. We don't use Docker-based action because:
|
||||||
|
|
||||||
|
1. docker pulling is slow currently
|
||||||
|
2. it's easier to use caching from [@actions/cache](https://github.com/actions/toolkit/tree/master/packages/cache)
|
||||||
|
|
||||||
|
We support different platforms, such as `ubuntu`, `macos` and `windows` with `x32` and `x64` archs.
|
||||||
|
|
||||||
|
Inside our action we perform 3 steps:
|
||||||
|
|
||||||
|
1. Setup environment running in parallel:
|
||||||
|
* restore [cache](https://github.com/actions/cache) of previous analyzes
|
||||||
|
* fetch [action config](https://github.com/golangci/golangci-lint/blob/master/assets/github-action-config.json) and find the latest `golangci-lint` patch version
|
||||||
|
for needed version (users of this action can specify only minor version of `golangci-lint`). After that install [golangci-lint](https://github.com/golangci/golangci-lint) using [@actions/tool-cache](https://github.com/actions/toolkit/tree/master/packages/tool-cache)
|
||||||
|
* install the latest Go 1.x version using [@actions/setup-go](https://github.com/actions/setup-go)
|
||||||
|
2. Run `golangci-lint` with specified by user `args`
|
||||||
|
3. Save cache for later builds
|
||||||
|
|
||||||
|
### Caching internals
|
||||||
|
|
||||||
|
1. We save and restore the following directories: `~/.cache/golangci-lint`, `~/.cache/go-build`, `~/go/pkg`.
|
||||||
|
2. The primary caching key looks like `golangci-lint.cache-{platform-arch}-{interval_number}-{go.mod_hash}`. Interval number ensures that we periodically invalidate
|
||||||
|
our cache (every 7 days). `go.mod` hash ensures that we invalidate the cache early - as soon as dependencies have changed.
|
||||||
|
3. We use [restore keys](https://help.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows#matching-a-cache-key): `golangci-lint.cache-{interval_number}-`, `golangci-lint.cache-`. GitHub matches keys by prefix if we have no exact match for the primary cache.
|
||||||
|
|
||||||
|
This scheme is basic and needs improvements. Pull requests and ideas are welcome.
|
||||||
|
|
||||||
|
## Development of this action
|
||||||
|
|
||||||
|
1. Install [act](https://github.com/nektos/act#installation)
|
||||||
|
2. Make a symlink for `act` to work properly: `ln -s . golangci-lint-action`
|
||||||
|
3. Prepare deps once: `npm run prepare-deps`
|
||||||
|
4. Run `npm run local` after any change to test it
|
||||||
|
60
action.yml
60
action.yml
@ -1,27 +1,41 @@
|
|||||||
---
|
name: "Run golangci-lint"
|
||||||
name: 'Run golangci-lint'
|
description: "Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution."
|
||||||
description: 'Run golangci-lint (WIP)'
|
author: "golangci"
|
||||||
author: 'golangci'
|
|
||||||
inputs:
|
inputs:
|
||||||
github_token:
|
version:
|
||||||
description: 'GITHUB_TOKEN'
|
description: "version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version"
|
||||||
|
required: false
|
||||||
|
args:
|
||||||
|
description: "golangci-lint command line arguments"
|
||||||
|
default: ""
|
||||||
|
required: false
|
||||||
|
working-directory:
|
||||||
|
description: "golangci-lint working directory, default is project root"
|
||||||
|
required: false
|
||||||
|
github-token:
|
||||||
|
description: "the token is used for fetching patch of a pull request to show only new issues"
|
||||||
|
default: ${{ github.token }}
|
||||||
|
required: true
|
||||||
|
only-new-issues:
|
||||||
|
description: "if set to true and the action runs on a pull request - the action outputs only newly found issues"
|
||||||
|
default: false
|
||||||
|
required: true
|
||||||
|
skip-go-installation:
|
||||||
|
description: "if set to true then action uses pre-installed Go"
|
||||||
|
default: false
|
||||||
|
required: true
|
||||||
|
skip-pkg-cache:
|
||||||
|
description: "if set to true then the action don't cache or restore ~/go/pkg."
|
||||||
|
default: false
|
||||||
|
required: true
|
||||||
|
skip-build-cache:
|
||||||
|
description: "if set to true then the action don't cache or restore ~/.cache/go-build."
|
||||||
|
default: false
|
||||||
required: true
|
required: true
|
||||||
flags:
|
|
||||||
description: 'GolangCI command line flags'
|
|
||||||
required: false
|
|
||||||
directory:
|
|
||||||
description: 'Working directory'
|
|
||||||
required: false
|
|
||||||
default: ''
|
|
||||||
format:
|
|
||||||
description: 'Output format of issues'
|
|
||||||
default: 'github-actions'
|
|
||||||
required: false
|
|
||||||
|
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: 'docker'
|
using: "node12"
|
||||||
image: 'Dockerfile'
|
main: "dist/run/index.js"
|
||||||
|
post: "dist/post_run/index.js"
|
||||||
branding:
|
branding:
|
||||||
icon: 'check-circle'
|
icon: "shield"
|
||||||
color: 'blue'
|
color: "yellow"
|
||||||
|
16
dist/matchers.json
vendored
Normal file
16
dist/matchers.json
vendored
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"problemMatcher": [
|
||||||
|
{
|
||||||
|
"owner": "go",
|
||||||
|
"pattern": [
|
||||||
|
{
|
||||||
|
"regexp": "^\\s*(\\.{0,2}[\\/\\\\].+\\.go):(?:(\\d+):(\\d+):)? (.*)",
|
||||||
|
"file": 1,
|
||||||
|
"line": 2,
|
||||||
|
"column": 3,
|
||||||
|
"message": 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
66144
dist/post_run/index.js
vendored
Normal file
66144
dist/post_run/index.js
vendored
Normal file
File diff suppressed because one or more lines are too long
66144
dist/run/index.js
vendored
Normal file
66144
dist/run/index.js
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -1,10 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
echo 'golangci-lint-action: start'
|
|
||||||
echo " flags: ${INPUT_FLAGS}"
|
|
||||||
echo " format: ${INPUT_FORMAT}"
|
|
||||||
|
|
||||||
cd "${GITHUB_WORKSPACE}/${INPUT_DIRECTORY}" || exit 1
|
|
||||||
|
|
||||||
# shellcheck disable=SC2086
|
|
||||||
golangci-lint run --out-format ${INPUT_FORMAT} ${INPUT_FLAGS}
|
|
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module github.com/golangci/golangci-lint-action
|
||||||
|
|
||||||
|
go 1.14
|
5509
package-lock.json
generated
Normal file
5509
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
53
package.json
Normal file
53
package.json
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
{
|
||||||
|
"name": "golanci-lint-action",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"private": true,
|
||||||
|
"description": "golangci-lint github action",
|
||||||
|
"main": "dist/main.js",
|
||||||
|
"scripts": {
|
||||||
|
"prepare-setup-go": "cd node_modules/setup-go && npm run build",
|
||||||
|
"prepare-deps": "npm run prepare-setup-go",
|
||||||
|
"build": "tsc && ncc build -o dist/run/ src/main.ts && ncc build -o dist/post_run/ src/post_main.ts",
|
||||||
|
"watched_build_main": "tsc && ncc build -w -o dist/run/ src/main.ts",
|
||||||
|
"watched_build_post_main": "tsc && ncc build -w -o dist/post_run/ src/post_main.ts",
|
||||||
|
"type-check": "tsc",
|
||||||
|
"lint": "eslint --max-warnings 1 **/*.ts --cache",
|
||||||
|
"lint-fix": "eslint **/*.ts --cache --fix",
|
||||||
|
"format": "prettier --write **/*.ts",
|
||||||
|
"format-check": "prettier --check **/*.ts",
|
||||||
|
"all": "npm run prepare-deps && npm run build && npm run format-check && npm run lint",
|
||||||
|
"local": "npm run build && act -j test -b",
|
||||||
|
"local-full-version": "npm run build && act -j test-full-version -b"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/golangci/golangci-lint-action.git"
|
||||||
|
},
|
||||||
|
"author": "golangci",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@actions/cache": "^1.0.6",
|
||||||
|
"@actions/core": "^1.2.6",
|
||||||
|
"@actions/exec": "^1.0.1",
|
||||||
|
"@actions/github": "^4.0.0",
|
||||||
|
"@actions/tool-cache": "^1.6.1",
|
||||||
|
"@types/semver": "^7.3.4",
|
||||||
|
"@types/tmp": "^0.2.0",
|
||||||
|
"setup-go": "git+https://github.com/actions/setup-go.git#v2.1.3",
|
||||||
|
"tmp": "^0.2.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^14.14.31",
|
||||||
|
"@types/uuid": "^8.3.0",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^4.15.1",
|
||||||
|
"@typescript-eslint/parser": "^4.15.1",
|
||||||
|
"@zeit/ncc": "^0.22.3",
|
||||||
|
"eslint": "^7.20.0",
|
||||||
|
"eslint-config-prettier": "^8.0.0",
|
||||||
|
"eslint-plugin-import": "^2.22.1",
|
||||||
|
"eslint-plugin-prettier": "^3.3.1",
|
||||||
|
"eslint-plugin-simple-import-sort": "^7.0.0",
|
||||||
|
"prettier": "^2.2.1",
|
||||||
|
"typescript": "^4.1.5"
|
||||||
|
}
|
||||||
|
}
|
@ -4,11 +4,23 @@ package sample
|
|||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Hash~
|
// Hash~
|
||||||
func Hash(data string) string {
|
func Hash(data string) string {
|
||||||
|
retError()
|
||||||
|
retError2()
|
||||||
|
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write([]byte(data))
|
h.Write([]byte(data))
|
||||||
return hex.EncodeToString(h.Sum(nil))
|
return hex.EncodeToString(h.Sum(nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func retError() error {
|
||||||
|
return errors.New("err")
|
||||||
|
}
|
||||||
|
|
||||||
|
func retError2() error {
|
||||||
|
return errors.New("err2")
|
||||||
|
}
|
||||||
|
151
src/cache.ts
Normal file
151
src/cache.ts
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
import * as cache from "@actions/cache"
|
||||||
|
import * as core from "@actions/core"
|
||||||
|
import * as crypto from "crypto"
|
||||||
|
import * as fs from "fs"
|
||||||
|
import path from "path"
|
||||||
|
|
||||||
|
import { Events, State } from "./constants"
|
||||||
|
import * as utils from "./utils/actionUtils"
|
||||||
|
|
||||||
|
function checksumFile(hashName: string, path: string): Promise<string> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const hash = crypto.createHash(hashName)
|
||||||
|
const stream = fs.createReadStream(path)
|
||||||
|
stream.on("error", (err) => reject(err))
|
||||||
|
stream.on("data", (chunk) => hash.update(chunk))
|
||||||
|
stream.on("end", () => resolve(hash.digest("hex")))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const pathExists = async (path: string): Promise<boolean> => !!(await fs.promises.stat(path).catch(() => false))
|
||||||
|
|
||||||
|
const getLintCacheDir = (): string => {
|
||||||
|
return path.resolve(`${process.env.HOME}/.cache/golangci-lint`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const getCacheDirs = (): string[] => {
|
||||||
|
// Not existing dirs are ok here: it works.
|
||||||
|
const skipPkgCache = core.getInput(`skip-pkg-cache`, { required: true }).trim()
|
||||||
|
const skipBuildCache = core.getInput(`skip-build-cache`, { required: true }).trim()
|
||||||
|
const dirs = [getLintCacheDir()]
|
||||||
|
|
||||||
|
if (skipBuildCache.toLowerCase() == "true") {
|
||||||
|
core.info(`Omitting ~/.cache/go-build from cache directories`)
|
||||||
|
} else {
|
||||||
|
dirs.push(path.resolve(`${process.env.HOME}/.cache/go-build`))
|
||||||
|
}
|
||||||
|
if (skipPkgCache.toLowerCase() == "true") {
|
||||||
|
core.info(`Omitting ~/go/pkg from cache directories`)
|
||||||
|
} else {
|
||||||
|
dirs.push(path.resolve(`${process.env.HOME}/go/pkg`))
|
||||||
|
}
|
||||||
|
|
||||||
|
return dirs
|
||||||
|
}
|
||||||
|
|
||||||
|
const getIntervalKey = (invalidationIntervalDays: number): string => {
|
||||||
|
const now = new Date()
|
||||||
|
const secondsSinceEpoch = now.getTime() / 1000
|
||||||
|
const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400))
|
||||||
|
return intervalNumber.toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildCacheKeys(): Promise<string[]> {
|
||||||
|
const keys = []
|
||||||
|
let cacheKey = `golangci-lint.cache-`
|
||||||
|
keys.push(cacheKey)
|
||||||
|
|
||||||
|
// Periodically invalidate a cache because a new code being added.
|
||||||
|
// TODO: configure it via inputs.
|
||||||
|
cacheKey += `${getIntervalKey(7)}-`
|
||||||
|
keys.push(cacheKey)
|
||||||
|
|
||||||
|
if (await pathExists(`go.mod`)) {
|
||||||
|
// Add checksum to key to invalidate a cache when dependencies change.
|
||||||
|
cacheKey += await checksumFile(`sha1`, `go.mod`)
|
||||||
|
} else {
|
||||||
|
cacheKey += `nogomod`
|
||||||
|
}
|
||||||
|
keys.push(cacheKey)
|
||||||
|
|
||||||
|
return keys
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function restoreCache(): Promise<void> {
|
||||||
|
if (!utils.isValidEvent()) {
|
||||||
|
utils.logWarning(
|
||||||
|
`Event Validation Error: The event type ${process.env[Events.Key]} is not supported because it's not tied to a branch or tag ref.`
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
|
const keys = await buildCacheKeys()
|
||||||
|
const primaryKey = keys.pop()
|
||||||
|
const restoreKeys = keys.reverse()
|
||||||
|
|
||||||
|
// Tell golangci-lint to use our cache directory.
|
||||||
|
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir()
|
||||||
|
|
||||||
|
if (!primaryKey) {
|
||||||
|
utils.logWarning(`Invalid primary key`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
core.saveState(State.CachePrimaryKey, primaryKey)
|
||||||
|
try {
|
||||||
|
const cacheKey = await cache.restoreCache(getCacheDirs(), primaryKey, restoreKeys)
|
||||||
|
if (!cacheKey) {
|
||||||
|
core.info(`Cache not found for input keys: ${[primaryKey, ...restoreKeys].join(", ")}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Store the matched cache key
|
||||||
|
utils.setCacheState(cacheKey)
|
||||||
|
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`)
|
||||||
|
} catch (error) {
|
||||||
|
if (error.name === cache.ValidationError.name) {
|
||||||
|
throw error
|
||||||
|
} else {
|
||||||
|
core.warning(error.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveCache(): Promise<void> {
|
||||||
|
// Validate inputs, this can cause task failure
|
||||||
|
if (!utils.isValidEvent()) {
|
||||||
|
utils.logWarning(
|
||||||
|
`Event Validation Error: The event type ${process.env[Events.Key]} is not supported because it's not tied to a branch or tag ref.`
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
|
const cacheDirs = getCacheDirs()
|
||||||
|
const primaryKey = core.getState(State.CachePrimaryKey)
|
||||||
|
if (!primaryKey) {
|
||||||
|
utils.logWarning(`Error retrieving key from state.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const state = utils.getCacheState()
|
||||||
|
|
||||||
|
if (utils.isExactKeyMatch(primaryKey, state)) {
|
||||||
|
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await cache.saveCache(cacheDirs, primaryKey)
|
||||||
|
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`)
|
||||||
|
} catch (error) {
|
||||||
|
if (error.name === cache.ValidationError.name) {
|
||||||
|
throw error
|
||||||
|
} else if (error.name === cache.ReserveCacheError.name) {
|
||||||
|
core.info(error.message)
|
||||||
|
} else {
|
||||||
|
core.info(`[warning] ${error.message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
src/constants.ts
Normal file
18
src/constants.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
export enum Inputs {
|
||||||
|
Key = "key",
|
||||||
|
Path = "path",
|
||||||
|
RestoreKeys = "restore-keys",
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum State {
|
||||||
|
CachePrimaryKey = "CACHE_KEY",
|
||||||
|
CacheMatchedKey = "CACHE_RESULT",
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum Events {
|
||||||
|
Key = "GITHUB_EVENT_NAME",
|
||||||
|
Push = "push",
|
||||||
|
PullRequest = "pull_request",
|
||||||
|
}
|
||||||
|
|
||||||
|
export const RefKey = "GITHUB_REF"
|
13
src/deps.d.ts
vendored
Normal file
13
src/deps.d.ts
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
declare module "setup-go/lib/main" {
|
||||||
|
function run(): Promise<void>
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module "cache/lib/restore" {
|
||||||
|
function run(): Promise<void>
|
||||||
|
export default run
|
||||||
|
}
|
||||||
|
|
||||||
|
declare module "cache/lib/save" {
|
||||||
|
function run(): Promise<void>
|
||||||
|
export default run
|
||||||
|
}
|
74
src/install.ts
Normal file
74
src/install.ts
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
import * as core from "@actions/core"
|
||||||
|
import * as tc from "@actions/tool-cache"
|
||||||
|
import os from "os"
|
||||||
|
import path from "path"
|
||||||
|
import { run as setupGo } from "setup-go/lib/main"
|
||||||
|
|
||||||
|
import { VersionConfig } from "./version"
|
||||||
|
|
||||||
|
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download"
|
||||||
|
|
||||||
|
const getAssetURL = (versionConfig: VersionConfig): string => {
|
||||||
|
let ext = "tar.gz"
|
||||||
|
let platform = os.platform().toString()
|
||||||
|
switch (platform) {
|
||||||
|
case "win32":
|
||||||
|
platform = "windows"
|
||||||
|
ext = "zip"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
let arch = os.arch()
|
||||||
|
switch (arch) {
|
||||||
|
case "x64":
|
||||||
|
arch = "amd64"
|
||||||
|
break
|
||||||
|
case "x32":
|
||||||
|
case "ia32":
|
||||||
|
arch = "386"
|
||||||
|
break
|
||||||
|
}
|
||||||
|
const noPrefix = versionConfig.TargetVersion.slice(1)
|
||||||
|
|
||||||
|
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// The installLint returns path to installed binary of golangci-lint.
|
||||||
|
export async function installLint(versionConfig: VersionConfig): Promise<string> {
|
||||||
|
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`)
|
||||||
|
const startedAt = Date.now()
|
||||||
|
const assetURL = getAssetURL(versionConfig)
|
||||||
|
core.info(`Downloading ${assetURL} ...`)
|
||||||
|
const archivePath = await tc.downloadTool(assetURL)
|
||||||
|
let extractedDir = ""
|
||||||
|
let repl = /\.tar\.gz$/
|
||||||
|
if (assetURL.endsWith("zip")) {
|
||||||
|
extractedDir = await tc.extractZip(archivePath, process.env.HOME)
|
||||||
|
repl = /\.zip$/
|
||||||
|
} else {
|
||||||
|
// We want to always overwrite files if the local cache already has them
|
||||||
|
const args = ["xz"]
|
||||||
|
if (process.platform.toString() != "darwin") {
|
||||||
|
args.push("--overwrite")
|
||||||
|
}
|
||||||
|
extractedDir = await tc.extractTar(archivePath, process.env.HOME, args)
|
||||||
|
}
|
||||||
|
|
||||||
|
const urlParts = assetURL.split(`/`)
|
||||||
|
const dirName = urlParts[urlParts.length - 1].replace(repl, ``)
|
||||||
|
const lintPath = path.join(extractedDir, dirName, `golangci-lint`)
|
||||||
|
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
|
||||||
|
return lintPath
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function installGo(): Promise<void> {
|
||||||
|
const skipGoInstallation = core.getInput(`skip-go-installation`, { required: true }).trim()
|
||||||
|
if (skipGoInstallation.toLowerCase() == "true") {
|
||||||
|
core.info(`Skipping the installation of Go`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const startedAt = Date.now()
|
||||||
|
process.env[`INPUT_GO-VERSION`] = `1`
|
||||||
|
await setupGo()
|
||||||
|
core.info(`Installed Go in ${Date.now() - startedAt}ms`)
|
||||||
|
}
|
3
src/main.ts
Normal file
3
src/main.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { run } from "./run"
|
||||||
|
|
||||||
|
run()
|
3
src/post_main.ts
Normal file
3
src/post_main.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import { postRun } from "./run"
|
||||||
|
|
||||||
|
postRun()
|
205
src/run.ts
Normal file
205
src/run.ts
Normal file
@ -0,0 +1,205 @@
|
|||||||
|
import * as core from "@actions/core"
|
||||||
|
import * as github from "@actions/github"
|
||||||
|
import { exec, ExecOptions } from "child_process"
|
||||||
|
import * as fs from "fs"
|
||||||
|
import * as path from "path"
|
||||||
|
import { dir } from "tmp"
|
||||||
|
import { promisify } from "util"
|
||||||
|
|
||||||
|
import { restoreCache, saveCache } from "./cache"
|
||||||
|
import { installGo, installLint } from "./install"
|
||||||
|
import { findLintVersion } from "./version"
|
||||||
|
|
||||||
|
const execShellCommand = promisify(exec)
|
||||||
|
const writeFile = promisify(fs.writeFile)
|
||||||
|
const createTempDir = promisify(dir)
|
||||||
|
|
||||||
|
async function prepareLint(): Promise<string> {
|
||||||
|
const versionConfig = await findLintVersion()
|
||||||
|
return await installLint(versionConfig)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchPatch(): Promise<string> {
|
||||||
|
const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim()
|
||||||
|
if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) {
|
||||||
|
throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`)
|
||||||
|
}
|
||||||
|
if (onlyNewIssues === `false`) {
|
||||||
|
return ``
|
||||||
|
}
|
||||||
|
|
||||||
|
const ctx = github.context
|
||||||
|
if (ctx.eventName !== `pull_request`) {
|
||||||
|
core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`)
|
||||||
|
return ``
|
||||||
|
}
|
||||||
|
const pull = ctx.payload.pull_request
|
||||||
|
if (!pull) {
|
||||||
|
core.warning(`No pull request in context`)
|
||||||
|
return ``
|
||||||
|
}
|
||||||
|
const octokit = github.getOctokit(core.getInput(`github-token`, { required: true }))
|
||||||
|
let patch: string
|
||||||
|
try {
|
||||||
|
const patchResp = await octokit.pulls.get({
|
||||||
|
owner: ctx.repo.owner,
|
||||||
|
repo: ctx.repo.repo,
|
||||||
|
[`pull_number`]: pull.number,
|
||||||
|
mediaType: {
|
||||||
|
format: `diff`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
if (patchResp.status !== 200) {
|
||||||
|
core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`)
|
||||||
|
return `` // don't fail the action, but analyze without patch
|
||||||
|
}
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||||
|
patch = patchResp.data as any
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(`failed to fetch pull request patch:`, err)
|
||||||
|
return `` // don't fail the action, but analyze without patch
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const tempDir = await createTempDir()
|
||||||
|
const patchPath = path.join(tempDir, "pull.patch")
|
||||||
|
core.info(`Writing patch to ${patchPath}`)
|
||||||
|
await writeFile(patchPath, patch)
|
||||||
|
return patchPath
|
||||||
|
} catch (err) {
|
||||||
|
console.warn(`failed to save pull request patch:`, err)
|
||||||
|
return `` // don't fail the action, but analyze without patch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Env = {
|
||||||
|
lintPath: string
|
||||||
|
patchPath: string
|
||||||
|
}
|
||||||
|
|
||||||
|
async function prepareEnv(): Promise<Env> {
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
|
// Prepare cache, lint and go in parallel.
|
||||||
|
const restoreCachePromise = restoreCache()
|
||||||
|
const prepareLintPromise = prepareLint()
|
||||||
|
const installGoPromise = installGo()
|
||||||
|
const patchPromise = fetchPatch()
|
||||||
|
|
||||||
|
const lintPath = await prepareLintPromise
|
||||||
|
await installGoPromise
|
||||||
|
await restoreCachePromise
|
||||||
|
const patchPath = await patchPromise
|
||||||
|
|
||||||
|
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
|
||||||
|
return { lintPath, patchPath }
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExecRes = {
|
||||||
|
stdout: string
|
||||||
|
stderr: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const printOutput = (res: ExecRes): void => {
|
||||||
|
if (res.stdout) {
|
||||||
|
core.info(res.stdout)
|
||||||
|
}
|
||||||
|
if (res.stderr) {
|
||||||
|
core.info(res.stderr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runLint(lintPath: string, patchPath: string): Promise<void> {
|
||||||
|
const debug = core.getInput(`debug`)
|
||||||
|
if (debug.split(`,`).includes(`cache`)) {
|
||||||
|
const res = await execShellCommand(`${lintPath} cache status`)
|
||||||
|
printOutput(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
const userArgs = core.getInput(`args`)
|
||||||
|
const addedArgs: string[] = []
|
||||||
|
|
||||||
|
const userArgNames = new Set<string>()
|
||||||
|
userArgs
|
||||||
|
.split(/\s/)
|
||||||
|
.map((arg) => arg.split(`=`)[0])
|
||||||
|
.filter((arg) => arg.startsWith(`-`))
|
||||||
|
.forEach((arg) => {
|
||||||
|
userArgNames.add(arg.replace(`-`, ``))
|
||||||
|
})
|
||||||
|
|
||||||
|
if (userArgNames.has(`out-format`)) {
|
||||||
|
throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`)
|
||||||
|
}
|
||||||
|
addedArgs.push(`--out-format=github-actions`)
|
||||||
|
|
||||||
|
if (patchPath) {
|
||||||
|
if (userArgNames.has(`new`) || userArgNames.has(`new-from-rev`) || userArgNames.has(`new-from-patch`)) {
|
||||||
|
throw new Error(`please, don't specify manually --new* args when requesting only new issues`)
|
||||||
|
}
|
||||||
|
addedArgs.push(`--new-from-patch=${patchPath}`)
|
||||||
|
|
||||||
|
// Override config values.
|
||||||
|
addedArgs.push(`--new=false`)
|
||||||
|
addedArgs.push(`--new-from-rev=`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const workingDirectory = core.getInput(`working-directory`)
|
||||||
|
const cmdArgs: ExecOptions = {}
|
||||||
|
if (workingDirectory) {
|
||||||
|
if (patchPath) {
|
||||||
|
// TODO: make them compatible
|
||||||
|
throw new Error(`options working-directory and only-new-issues aren't compatible`)
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) {
|
||||||
|
throw new Error(`working-directory (${workingDirectory}) was not a path`)
|
||||||
|
}
|
||||||
|
if (!userArgNames.has(`path-prefix`)) {
|
||||||
|
addedArgs.push(`--path-prefix=${workingDirectory}`)
|
||||||
|
}
|
||||||
|
cmdArgs.cwd = path.resolve(workingDirectory)
|
||||||
|
}
|
||||||
|
|
||||||
|
const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimRight()
|
||||||
|
core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`)
|
||||||
|
const startedAt = Date.now()
|
||||||
|
try {
|
||||||
|
const res = await execShellCommand(cmd, cmdArgs)
|
||||||
|
printOutput(res)
|
||||||
|
core.info(`golangci-lint found no issues`)
|
||||||
|
} catch (exc) {
|
||||||
|
// This logging passes issues to GitHub annotations but comments can be more convenient for some users.
|
||||||
|
// TODO: support reviewdog or leaving comments by GitHub API.
|
||||||
|
printOutput(exc)
|
||||||
|
|
||||||
|
if (exc.code === 1) {
|
||||||
|
core.setFailed(`issues found`)
|
||||||
|
} else {
|
||||||
|
core.setFailed(`golangci-lint exit with code ${exc.code}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
core.info(`Ran golangci-lint in ${Date.now() - startedAt}ms`)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function run(): Promise<void> {
|
||||||
|
try {
|
||||||
|
const { lintPath, patchPath } = await core.group(`prepare environment`, prepareEnv)
|
||||||
|
core.addPath(path.dirname(lintPath))
|
||||||
|
await core.group(`run golangci-lint`, () => runLint(lintPath, patchPath))
|
||||||
|
} catch (error) {
|
||||||
|
core.error(`Failed to run: ${error}, ${error.stack}`)
|
||||||
|
core.setFailed(error.message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function postRun(): Promise<void> {
|
||||||
|
try {
|
||||||
|
await saveCache()
|
||||||
|
} catch (error) {
|
||||||
|
core.error(`Failed to post-run: ${error}, ${error.stack}`)
|
||||||
|
core.setFailed(error.message)
|
||||||
|
}
|
||||||
|
}
|
37
src/utils/actionUtils.ts
Normal file
37
src/utils/actionUtils.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import * as core from "@actions/core"
|
||||||
|
|
||||||
|
import { RefKey, State } from "../constants"
|
||||||
|
|
||||||
|
export function isExactKeyMatch(key: string, cacheKey?: string): boolean {
|
||||||
|
return !!(
|
||||||
|
cacheKey &&
|
||||||
|
cacheKey.localeCompare(key, undefined, {
|
||||||
|
sensitivity: "accent",
|
||||||
|
}) === 0
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setCacheState(state: string): void {
|
||||||
|
core.saveState(State.CacheMatchedKey, state)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getCacheState(): string | undefined {
|
||||||
|
const cacheKey = core.getState(State.CacheMatchedKey)
|
||||||
|
if (cacheKey) {
|
||||||
|
core.debug(`Cache state/key: ${cacheKey}`)
|
||||||
|
return cacheKey
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
export function logWarning(message: string): void {
|
||||||
|
const warningPrefix = "[warning]"
|
||||||
|
core.info(`${warningPrefix}${message}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cache token authorized for all events that are tied to a ref
|
||||||
|
// See GitHub Context https://help.github.com/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context
|
||||||
|
export function isValidEvent(): boolean {
|
||||||
|
return RefKey in process.env && Boolean(process.env[RefKey])
|
||||||
|
}
|
153
src/version.ts
Normal file
153
src/version.ts
Normal file
@ -0,0 +1,153 @@
|
|||||||
|
import * as core from "@actions/core"
|
||||||
|
import * as httpm from "@actions/http-client"
|
||||||
|
import * as fs from "fs"
|
||||||
|
|
||||||
|
// TODO: make a class
|
||||||
|
export type Version = {
|
||||||
|
major: number
|
||||||
|
minor: number
|
||||||
|
patch: number | null
|
||||||
|
} | null
|
||||||
|
|
||||||
|
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/
|
||||||
|
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/
|
||||||
|
|
||||||
|
const parseVersion = (s: string): Version => {
|
||||||
|
if (s == "latest" || s == "") {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const match = s.match(versionRe)
|
||||||
|
if (!match) {
|
||||||
|
throw new Error(`invalid version string '${s}', expected format v1.2 or v1.2.3`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
major: parseInt(match[1]),
|
||||||
|
minor: parseInt(match[2]),
|
||||||
|
patch: match[3] === undefined ? null : parseInt(match[3]),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const stringifyVersion = (v: Version): string => {
|
||||||
|
if (v == null) {
|
||||||
|
return "latest"
|
||||||
|
}
|
||||||
|
return `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}`
|
||||||
|
}
|
||||||
|
|
||||||
|
const minVersion = {
|
||||||
|
major: 1,
|
||||||
|
minor: 28,
|
||||||
|
patch: 3,
|
||||||
|
}
|
||||||
|
|
||||||
|
const isLessVersion = (a: Version, b: Version): boolean => {
|
||||||
|
if (a == null) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (b == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (a.major != b.major) {
|
||||||
|
return a.major < b.major
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do not compare patch parts because if the min version has a non zero value
|
||||||
|
// then it returns false, since the patch version of requested is always zero
|
||||||
|
return a.minor < b.minor
|
||||||
|
}
|
||||||
|
|
||||||
|
const getRequestedLintVersion = (): Version => {
|
||||||
|
let requestedLintVersion = core.getInput(`version`)
|
||||||
|
|
||||||
|
if (requestedLintVersion == "") {
|
||||||
|
const content = fs.readFileSync("go.mod", "utf-8")
|
||||||
|
const match = content.match(modVersionRe)
|
||||||
|
if (match) {
|
||||||
|
requestedLintVersion = match[1]
|
||||||
|
core.info(`Found golangci-lint version '${requestedLintVersion}' in go.mod`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedRequestedLintVersion = parseVersion(requestedLintVersion)
|
||||||
|
if (parsedRequestedLintVersion == null) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
if (isLessVersion(parsedRequestedLintVersion, minVersion)) {
|
||||||
|
throw new Error(
|
||||||
|
`requested golangci-lint version '${requestedLintVersion}' isn't supported: we support only ${stringifyVersion(
|
||||||
|
minVersion
|
||||||
|
)} and later versions`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return parsedRequestedLintVersion
|
||||||
|
}
|
||||||
|
|
||||||
|
export type VersionConfig = {
|
||||||
|
Error?: string
|
||||||
|
TargetVersion: string
|
||||||
|
AssetURL: string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config = {
|
||||||
|
MinorVersionToConfig: {
|
||||||
|
[minorVersion: string]: VersionConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getConfig = async (): Promise<Config> => {
|
||||||
|
const http = new httpm.HttpClient(`golangci/golangci-lint-action`, [], {
|
||||||
|
allowRetries: true,
|
||||||
|
maxRetries: 5,
|
||||||
|
})
|
||||||
|
try {
|
||||||
|
const url = `https://raw.githubusercontent.com/golangci/golangci-lint/master/assets/github-action-config.json`
|
||||||
|
const response: httpm.HttpClientResponse = await http.get(url)
|
||||||
|
if (response.message.statusCode !== 200) {
|
||||||
|
throw new Error(`failed to download from "${url}". Code(${response.message.statusCode}) Message(${response.message.statusMessage})`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const body = await response.readBody()
|
||||||
|
return JSON.parse(body)
|
||||||
|
} catch (exc) {
|
||||||
|
throw new Error(`failed to get action config: ${exc.message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function findLintVersion(): Promise<VersionConfig> {
|
||||||
|
core.info(`Finding needed golangci-lint version...`)
|
||||||
|
const reqLintVersion = getRequestedLintVersion()
|
||||||
|
// if the patched version is passed, just use it
|
||||||
|
if (reqLintVersion?.major !== null && reqLintVersion?.minor != null && reqLintVersion?.patch !== null) {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const versionWithoutV = `${reqLintVersion.major}.${reqLintVersion.minor}.${reqLintVersion.patch}`
|
||||||
|
resolve({
|
||||||
|
TargetVersion: `v${versionWithoutV}`,
|
||||||
|
AssetURL: `https://github.com/golangci/golangci-lint/releases/download/v${versionWithoutV}/golangci-lint-${versionWithoutV}-linux-amd64.tar.gz`,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
const startedAt = Date.now()
|
||||||
|
|
||||||
|
const config = await getConfig()
|
||||||
|
if (!config.MinorVersionToConfig) {
|
||||||
|
core.warning(JSON.stringify(config))
|
||||||
|
throw new Error(`invalid config: no MinorVersionToConfig field`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const versionConfig = config.MinorVersionToConfig[stringifyVersion(reqLintVersion)]
|
||||||
|
if (!versionConfig) {
|
||||||
|
throw new Error(`requested golangci-lint version '${stringifyVersion(reqLintVersion)}' doesn't exist`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (versionConfig.Error) {
|
||||||
|
throw new Error(`failed to use requested golangci-lint version '${stringifyVersion(reqLintVersion)}': ${versionConfig.Error}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
core.info(
|
||||||
|
`Requested golangci-lint '${stringifyVersion(reqLintVersion)}', using '${versionConfig.TargetVersion}', calculation took ${
|
||||||
|
Date.now() - startedAt
|
||||||
|
}ms`
|
||||||
|
)
|
||||||
|
return versionConfig
|
||||||
|
}
|
BIN
static/annotations.png
Normal file
BIN
static/annotations.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 145 KiB |
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"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'. */
|
||||||
|
"tsBuildInfoFile": ".tsbuildinfo",
|
||||||
|
"incremental": true
|
||||||
|
},
|
||||||
|
"exclude": ["node_modules", "**/*.test.ts"]
|
||||||
|
}
|
Reference in New Issue
Block a user