Compare commits
67 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 |
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
|
48
.github/workflows/test.yml
vendored
48
.github/workflows/test.yml
vendored
@ -4,23 +4,45 @@ on: # rebuild any PRs and main branch changes
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
- 'releases/*'
|
- "releases/*"
|
||||||
|
|
||||||
jobs:
|
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 prepare-deps
|
npm run all
|
||||||
npm run all
|
|
||||||
git diff --exit-code
|
|
||||||
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
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os:
|
||||||
|
- ubuntu-latest
|
||||||
|
- macos-latest
|
||||||
|
- windows-latest
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v2
|
||||||
- uses: ./
|
- uses: ./
|
||||||
with:
|
with:
|
||||||
version: v1.26
|
version: latest
|
||||||
args: --issues-exit-code=0 ./sample/...
|
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
|
||||||
|
75
README.md
75
README.md
@ -7,6 +7,12 @@ The action runs [golangci-lint](https://github.com/golangci/golangci-lint) and r
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
## 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
|
## How to use
|
||||||
|
|
||||||
Add `.github/workflows/golangci-lint.yml` with the following contents:
|
Add `.github/workflows/golangci-lint.yml` with the following contents:
|
||||||
@ -19,6 +25,7 @@ on:
|
|||||||
- v*
|
- v*
|
||||||
branches:
|
branches:
|
||||||
- master
|
- master
|
||||||
|
- main
|
||||||
pull_request:
|
pull_request:
|
||||||
jobs:
|
jobs:
|
||||||
golangci:
|
golangci:
|
||||||
@ -27,13 +34,67 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v0.2.0
|
uses: golangci/golangci-lint-action@v2
|
||||||
with:
|
with:
|
||||||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
|
||||||
version: v1.26
|
version: v1.29
|
||||||
|
|
||||||
|
# Optional: working directory, useful for monorepos
|
||||||
|
# working-directory: somedir
|
||||||
|
|
||||||
# Optional: golangci-lint command line arguments.
|
# Optional: golangci-lint command line arguments.
|
||||||
# args: ./the-only-dir-to-analyze/...
|
# 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
|
||||||
|
```
|
||||||
|
|
||||||
|
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
|
## Comments and Annotations
|
||||||
@ -49,7 +110,7 @@ The restrictions of annotations are the following:
|
|||||||
|
|
||||||
The action was implemented with performance in mind:
|
The action was implemented with performance in mind:
|
||||||
|
|
||||||
1. We cache data by [@actions/cache](https://github.com/actions/cache) between builds: Go build cache, Go modules cache, golangci-lint analysis cache.
|
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.
|
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.
|
3. We do as much as we can in parallel, e.g. we download cache, go and golangci-lint binary in parallel.
|
||||||
|
|
||||||
@ -65,7 +126,9 @@ For example, in a repository of [golangci-lint](https://github.com/golangci/gola
|
|||||||
We use JavaScript-based action. We don't use Docker-based action because:
|
We use JavaScript-based action. We don't use Docker-based action because:
|
||||||
|
|
||||||
1. docker pulling is slow currently
|
1. docker pulling is slow currently
|
||||||
2. it's easier to use caching from [@actions/cache](https://github.com/actions/cache) until GitHub team hasn't supported reusing actions from actions
|
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:
|
Inside our action we perform 3 steps:
|
||||||
|
|
||||||
@ -80,7 +143,7 @@ Inside our action we perform 3 steps:
|
|||||||
### Caching internals
|
### Caching internals
|
||||||
|
|
||||||
1. We save and restore the following directories: `~/.cache/golangci-lint`, `~/.cache/go-build`, `~/go/pkg`.
|
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-{interval_number}-{go.mod_hash}`. Interval number ensures that we periodically invalidate
|
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.
|
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.
|
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.
|
||||||
|
|
||||||
|
34
action.yml
34
action.yml
@ -1,23 +1,31 @@
|
|||||||
---
|
---
|
||||||
name: 'Run golangci-lint'
|
name: "Run golangci-lint"
|
||||||
description: 'Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution. Beta version.'
|
description: "Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution."
|
||||||
author: 'golangci'
|
author: "golangci"
|
||||||
inputs:
|
inputs:
|
||||||
version:
|
version:
|
||||||
description: 'version of golangci-lint to use in form of v1.2.3'
|
description: "version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version"
|
||||||
required: true
|
required: false
|
||||||
args:
|
args:
|
||||||
description: 'golangci-lint command line arguments'
|
description: "golangci-lint command line arguments"
|
||||||
default: ''
|
default: ""
|
||||||
|
required: false
|
||||||
|
working-directory:
|
||||||
|
description: "golangci-lint working directory, default is project root"
|
||||||
required: false
|
required: false
|
||||||
github-token:
|
github-token:
|
||||||
description: 'GitHub token with scope `repo.public_repo`. Used for fetching a list of releases of golangci-lint.'
|
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
|
required: true
|
||||||
|
|
||||||
runs:
|
runs:
|
||||||
using: 'node12'
|
using: "node12"
|
||||||
main: 'dist/run/index.js'
|
main: "dist/run/index.js"
|
||||||
post: 'dist/post_run/index.js'
|
post: "dist/post_run/index.js"
|
||||||
branding:
|
branding:
|
||||||
icon: 'shield'
|
icon: "shield"
|
||||||
color: 'yellow'
|
color: "yellow"
|
||||||
|
59000
dist/post_run/index.js
vendored
59000
dist/post_run/index.js
vendored
File diff suppressed because one or more lines are too long
59002
dist/run/index.js
vendored
59002
dist/run/index.js
vendored
File diff suppressed because one or more lines are too long
1063
package-lock.json
generated
1063
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
48
package.json
48
package.json
@ -1,49 +1,53 @@
|
|||||||
{
|
{
|
||||||
"name": "golanci-lint-action",
|
"name": "golanci-lint-action",
|
||||||
"version": "1.1.2",
|
"version": "2.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "golangci-lint github action",
|
"description": "golangci-lint github action",
|
||||||
"main": "dist/main.js",
|
"main": "dist/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepare-setup-go": "cd node_modules/setup-go && npm run build",
|
"prepare-setup-go": "cd node_modules/setup-go && npm run build",
|
||||||
"prepare-cache": "cd node_modules/cache && npm run build",
|
"prepare-deps": "npm run prepare-setup-go",
|
||||||
"prepare-deps": "npm run prepare-setup-go && npm run prepare-cache",
|
|
||||||
"build": "tsc && ncc build -o dist/run/ src/main.ts && ncc build -o dist/post_run/ src/post_main.ts",
|
"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",
|
"type-check": "tsc",
|
||||||
"lint": "eslint --max-warnings 1 **/*.ts --cache",
|
"lint": "eslint --max-warnings 1 **/*.ts --cache",
|
||||||
"lint-fix": "eslint **/*.ts --cache --fix",
|
"lint-fix": "eslint **/*.ts --cache --fix",
|
||||||
"format": "prettier --write **/*.ts",
|
"format": "prettier --write **/*.ts",
|
||||||
"format-check": "prettier --check **/*.ts",
|
"format-check": "prettier --check **/*.ts",
|
||||||
"all": "npm run build && npm run format-check && npm run lint",
|
"all": "npm run prepare-deps && npm run build && npm run format-check && npm run lint",
|
||||||
"local": "npm run build && act -j test -b"
|
"local": "npm run build && act -j test -b",
|
||||||
|
"local-full-version": "npm run build && act -j test-full-version -b"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/golangci/golangci-lint-action.git"
|
"url": "git+https://github.com/golangci/golangci-lint-action.git"
|
||||||
},
|
},
|
||||||
"author": "GitHub",
|
"author": "golangci",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.2.0",
|
"@actions/cache": "^1.0.2",
|
||||||
|
"@actions/core": "^1.2.6",
|
||||||
"@actions/exec": "^1.0.1",
|
"@actions/exec": "^1.0.1",
|
||||||
"@actions/github": "^2.1.1",
|
"@actions/github": "^4.0.0",
|
||||||
"@actions/tool-cache": "^1.3.4",
|
"@actions/tool-cache": "^1.6.0",
|
||||||
"@types/semver": "^7.1.0",
|
"@types/semver": "^7.3.4",
|
||||||
"cache": "git+https://github.com/golangci/cache.git",
|
"@types/tmp": "^0.2.0",
|
||||||
"setup-go": "git+https://github.com/actions/setup-go.git"
|
"setup-go": "git+https://github.com/actions/setup-go.git#v2.1.3",
|
||||||
|
"tmp": "^0.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^12.0.4",
|
"@types/node": "^14.11.10",
|
||||||
"@types/uuid": "^3.4.5",
|
"@types/uuid": "^8.3.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^2.7.0",
|
"@typescript-eslint/eslint-plugin": "^2.34.0",
|
||||||
"@typescript-eslint/parser": "^2.7.0",
|
"@typescript-eslint/parser": "^2.34.0",
|
||||||
"@zeit/ncc": "^0.20.5",
|
"@zeit/ncc": "^0.22.3",
|
||||||
"eslint": "^6.6.0",
|
"eslint": "^6.6.0",
|
||||||
"eslint-config-prettier": "^6.5.0",
|
"eslint-config-prettier": "^6.13.0",
|
||||||
"eslint-plugin-import": "^2.18.2",
|
"eslint-plugin-import": "^2.22.1",
|
||||||
"eslint-plugin-prettier": "^3.1.1",
|
"eslint-plugin-prettier": "^3.1.4",
|
||||||
"eslint-plugin-simple-import-sort": "^5.0.2",
|
"eslint-plugin-simple-import-sort": "^5.0.2",
|
||||||
"prettier": "^1.19.1",
|
"prettier": "^2.1.2",
|
||||||
"typescript": "^3.8.3"
|
"typescript": "^4.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
// Hash~
|
// Hash~
|
||||||
func Hash(data string) string {
|
func Hash(data string) string {
|
||||||
retError()
|
retError()
|
||||||
|
retError2()
|
||||||
|
|
||||||
h := md5.New()
|
h := md5.New()
|
||||||
h.Write([]byte(data))
|
h.Write([]byte(data))
|
||||||
@ -19,3 +20,7 @@ func Hash(data string) string {
|
|||||||
func retError() error {
|
func retError() error {
|
||||||
return errors.New("err")
|
return errors.New("err")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func retError2() error {
|
||||||
|
return errors.New("err2")
|
||||||
|
}
|
||||||
|
87
src/cache.ts
87
src/cache.ts
@ -1,26 +1,31 @@
|
|||||||
|
import * as cache from "@actions/cache"
|
||||||
import * as core from "@actions/core"
|
import * as core from "@actions/core"
|
||||||
import restore from "cache/lib/restore"
|
|
||||||
import save from "cache/lib/save"
|
|
||||||
import * as crypto from "crypto"
|
import * as crypto from "crypto"
|
||||||
import * as fs from "fs"
|
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> {
|
function checksumFile(hashName: string, path: string): Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const hash = crypto.createHash(hashName)
|
const hash = crypto.createHash(hashName)
|
||||||
const stream = fs.createReadStream(path)
|
const stream = fs.createReadStream(path)
|
||||||
stream.on("error", err => reject(err))
|
stream.on("error", (err) => reject(err))
|
||||||
stream.on("data", chunk => hash.update(chunk))
|
stream.on("data", (chunk) => hash.update(chunk))
|
||||||
stream.on("end", () => resolve(hash.digest("hex")))
|
stream.on("end", () => resolve(hash.digest("hex")))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathExists = async (path: string): Promise<boolean> => !!(await fs.promises.stat(path).catch(() => false))
|
const pathExists = async (path: string): Promise<boolean> => !!(await fs.promises.stat(path).catch(() => false))
|
||||||
|
|
||||||
const getLintCacheDir = (): string => `${process.env.HOME}/.cache/golangci-lint`
|
const getLintCacheDir = (): string => {
|
||||||
|
return path.resolve(`${process.env.HOME}/.cache/golangci-lint`)
|
||||||
|
}
|
||||||
|
|
||||||
const getCacheDirs = (): string[] => {
|
const getCacheDirs = (): string[] => {
|
||||||
// Not existing dirs are ok here: it works.
|
// Not existing dirs are ok here: it works.
|
||||||
return [getLintCacheDir(), `${process.env.HOME}/.cache/go-build`, `${process.env.HOME}/go/pkg`]
|
return [getLintCacheDir(), path.resolve(`${process.env.HOME}/.cache/go-build`), path.resolve(`${process.env.HOME}/go/pkg`)]
|
||||||
}
|
}
|
||||||
|
|
||||||
const getIntervalKey = (invalidationIntervalDays: number): string => {
|
const getIntervalKey = (invalidationIntervalDays: number): string => {
|
||||||
@ -52,30 +57,80 @@ async function buildCacheKeys(): Promise<string[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function restoreCache(): Promise<void> {
|
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 startedAt = Date.now()
|
||||||
|
|
||||||
const keys = await buildCacheKeys()
|
const keys = await buildCacheKeys()
|
||||||
const primaryKey = keys.pop()
|
const primaryKey = keys.pop()
|
||||||
const restoreKeys = keys.reverse()
|
const restoreKeys = keys.reverse()
|
||||||
core.info(`Primary analysis cache key is '${primaryKey}', restore keys are '${restoreKeys.join(` | `)}'`)
|
|
||||||
process.env[`INPUT_RESTORE-KEYS`] = restoreKeys.join(`\n`)
|
|
||||||
process.env.INPUT_KEY = primaryKey
|
|
||||||
|
|
||||||
process.env.INPUT_PATH = getCacheDirs().join(`\n`)
|
|
||||||
|
|
||||||
// Tell golangci-lint to use our cache directory.
|
// Tell golangci-lint to use our cache directory.
|
||||||
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir()
|
process.env.GOLANGCI_LINT_CACHE = getLintCacheDir()
|
||||||
|
|
||||||
await restore()
|
if (!primaryKey) {
|
||||||
core.info(`Restored cache for golangci-lint from key '${primaryKey}' in ${Date.now() - startedAt}ms`)
|
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> {
|
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 startedAt = Date.now()
|
||||||
|
|
||||||
const cacheDirs = getCacheDirs()
|
const cacheDirs = getCacheDirs()
|
||||||
process.env.INPUT_PATH = cacheDirs.join(`\n`)
|
const primaryKey = core.getState(State.CachePrimaryKey)
|
||||||
|
if (!primaryKey) {
|
||||||
|
utils.logWarning(`Error retrieving key from state.`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
await save()
|
const state = utils.getCacheState()
|
||||||
core.info(`Saved cache for golangci-lint from paths '${cacheDirs.join(`, `)}' in ${Date.now() - startedAt}ms`)
|
|
||||||
|
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"
|
@ -1,21 +1,55 @@
|
|||||||
import * as core from "@actions/core"
|
import * as core from "@actions/core"
|
||||||
import * as tc from "@actions/tool-cache"
|
import * as tc from "@actions/tool-cache"
|
||||||
|
import os from "os"
|
||||||
import path from "path"
|
import path from "path"
|
||||||
import { run as setupGo } from "setup-go/lib/main"
|
import { run as setupGo } from "setup-go/lib/main"
|
||||||
|
|
||||||
import { VersionConfig } from "./version"
|
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.
|
// The installLint returns path to installed binary of golangci-lint.
|
||||||
export async function installLint(versionConfig: VersionConfig): Promise<string> {
|
export async function installLint(versionConfig: VersionConfig): Promise<string> {
|
||||||
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`)
|
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`)
|
||||||
const startedAt = Date.now()
|
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 {
|
||||||
|
extractedDir = await tc.extractTar(archivePath, process.env.HOME)
|
||||||
|
}
|
||||||
|
|
||||||
core.info(`Downloading ${versionConfig.AssetURL} ...`)
|
const urlParts = assetURL.split(`/`)
|
||||||
const tarGzPath = await tc.downloadTool(versionConfig.AssetURL)
|
const dirName = urlParts[urlParts.length - 1].replace(repl, ``)
|
||||||
const extractedDir = await tc.extractTar(tarGzPath, process.env.HOME)
|
|
||||||
|
|
||||||
const urlParts = versionConfig.AssetURL.split(`/`)
|
|
||||||
const dirName = urlParts[urlParts.length - 1].replace(/\.tar\.gz$/, ``)
|
|
||||||
const lintPath = path.join(extractedDir, dirName, `golangci-lint`)
|
const lintPath = path.join(extractedDir, dirName, `golangci-lint`)
|
||||||
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
|
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`)
|
||||||
return lintPath
|
return lintPath
|
||||||
|
129
src/run.ts
129
src/run.ts
@ -1,5 +1,9 @@
|
|||||||
import * as core from "@actions/core"
|
import * as core from "@actions/core"
|
||||||
import { exec } from "child_process"
|
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 { promisify } from "util"
|
||||||
|
|
||||||
import { restoreCache, saveCache } from "./cache"
|
import { restoreCache, saveCache } from "./cache"
|
||||||
@ -7,26 +11,90 @@ import { installGo, installLint } from "./install"
|
|||||||
import { findLintVersion } from "./version"
|
import { findLintVersion } from "./version"
|
||||||
|
|
||||||
const execShellCommand = promisify(exec)
|
const execShellCommand = promisify(exec)
|
||||||
|
const writeFile = promisify(fs.writeFile)
|
||||||
|
const createTempDir = promisify(dir)
|
||||||
|
|
||||||
async function prepareLint(): Promise<string> {
|
async function prepareLint(): Promise<string> {
|
||||||
const versionConfig = await findLintVersion()
|
const versionConfig = await findLintVersion()
|
||||||
return await installLint(versionConfig)
|
return await installLint(versionConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function prepareEnv(): Promise<string> {
|
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()
|
const startedAt = Date.now()
|
||||||
|
|
||||||
// Prepare cache, lint and go in parallel.
|
// Prepare cache, lint and go in parallel.
|
||||||
const restoreCachePromise = restoreCache()
|
const restoreCachePromise = restoreCache()
|
||||||
const prepareLintPromise = prepareLint()
|
const prepareLintPromise = prepareLint()
|
||||||
const installGoPromise = installGo()
|
const installGoPromise = installGo()
|
||||||
|
const patchPromise = fetchPatch()
|
||||||
|
|
||||||
const lintPath = await prepareLintPromise
|
const lintPath = await prepareLintPromise
|
||||||
await installGoPromise
|
await installGoPromise
|
||||||
await restoreCachePromise
|
await restoreCachePromise
|
||||||
|
const patchPath = await patchPromise
|
||||||
|
|
||||||
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
|
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
|
||||||
return lintPath
|
return { lintPath, patchPath }
|
||||||
}
|
}
|
||||||
|
|
||||||
type ExecRes = {
|
type ExecRes = {
|
||||||
@ -43,23 +111,62 @@ const printOutput = (res: ExecRes): void => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runLint(lintPath: string): Promise<void> {
|
async function runLint(lintPath: string, patchPath: string): Promise<void> {
|
||||||
const debug = core.getInput(`debug`)
|
const debug = core.getInput(`debug`)
|
||||||
if (debug.split(`,`).includes(`cache`)) {
|
if (debug.split(`,`).includes(`cache`)) {
|
||||||
const res = await execShellCommand(`${lintPath} cache status`)
|
const res = await execShellCommand(`${lintPath} cache status`)
|
||||||
printOutput(res)
|
printOutput(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = core.getInput(`args`)
|
const userArgs = core.getInput(`args`)
|
||||||
if (args.includes(`-out-format`)) {
|
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`)
|
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`)
|
||||||
|
|
||||||
const cmd = `${lintPath} run --out-format=github-actions ${args}`.trimRight()
|
if (patchPath) {
|
||||||
core.info(`Running [${cmd}] ...`)
|
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()
|
const startedAt = Date.now()
|
||||||
try {
|
try {
|
||||||
const res = await execShellCommand(cmd)
|
const res = await execShellCommand(cmd, cmdArgs)
|
||||||
printOutput(res)
|
printOutput(res)
|
||||||
core.info(`golangci-lint found no issues`)
|
core.info(`golangci-lint found no issues`)
|
||||||
} catch (exc) {
|
} catch (exc) {
|
||||||
@ -79,8 +186,8 @@ async function runLint(lintPath: string): Promise<void> {
|
|||||||
|
|
||||||
export async function run(): Promise<void> {
|
export async function run(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const lintPath = await core.group(`prepare environment`, prepareEnv)
|
const { lintPath, patchPath } = await core.group(`prepare environment`, prepareEnv)
|
||||||
await core.group(`run golangci-lint`, () => runLint(lintPath))
|
await core.group(`run golangci-lint`, () => runLint(lintPath, patchPath))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
core.error(`Failed to run: ${error}, ${error.stack}`)
|
core.error(`Failed to run: ${error}, ${error.stack}`)
|
||||||
core.setFailed(error.message)
|
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])
|
||||||
|
}
|
@ -6,11 +6,14 @@ export type Version = {
|
|||||||
major: number
|
major: number
|
||||||
minor: number
|
minor: number
|
||||||
patch: number | null
|
patch: number | null
|
||||||
}
|
} | null
|
||||||
|
|
||||||
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/
|
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/
|
||||||
|
|
||||||
const parseVersion = (s: string): Version => {
|
const parseVersion = (s: string): Version => {
|
||||||
|
if (s == "latest" || s == "") {
|
||||||
|
return null
|
||||||
|
}
|
||||||
const match = s.match(versionRe)
|
const match = s.match(versionRe)
|
||||||
if (!match) {
|
if (!match) {
|
||||||
throw new Error(`invalid version string '${s}', expected format v1.2 or v1.2.3`)
|
throw new Error(`invalid version string '${s}', expected format v1.2 or v1.2.3`)
|
||||||
@ -23,37 +26,40 @@ const parseVersion = (s: string): Version => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const stringifyVersion = (v: Version): string => `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}`
|
export const stringifyVersion = (v: Version): string => {
|
||||||
|
if (v == null) {
|
||||||
|
return "latest"
|
||||||
|
}
|
||||||
|
return `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}`
|
||||||
|
}
|
||||||
|
|
||||||
const minVersion = {
|
const minVersion = {
|
||||||
major: 1,
|
major: 1,
|
||||||
minor: 14,
|
minor: 28,
|
||||||
patch: 0,
|
patch: 3,
|
||||||
}
|
}
|
||||||
|
|
||||||
const isLessVersion = (a: Version, b: Version): boolean => {
|
const isLessVersion = (a: Version, b: Version): boolean => {
|
||||||
|
if (a == null) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (b == null) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if (a.major != b.major) {
|
if (a.major != b.major) {
|
||||||
return a.major < b.major
|
return a.major < b.major
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a.minor != b.minor) {
|
// Do not compare patch parts because if the min version has a non zero value
|
||||||
return a.minor < b.minor
|
// then it returns false, since the patch version of requested is always zero
|
||||||
}
|
return a.minor < b.minor
|
||||||
|
|
||||||
if (a.patch === null || b.patch === null) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return a.patch < b.patch
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const getRequestedLintVersion = (): Version => {
|
const getRequestedLintVersion = (): Version => {
|
||||||
const requestedLintVersion = core.getInput(`version`, { required: true })
|
const requestedLintVersion = core.getInput(`version`)
|
||||||
const parsedRequestedLintVersion = parseVersion(requestedLintVersion)
|
const parsedRequestedLintVersion = parseVersion(requestedLintVersion)
|
||||||
if (parsedRequestedLintVersion.patch !== null) {
|
if (parsedRequestedLintVersion == null) {
|
||||||
throw new Error(
|
return null
|
||||||
`requested golangci-lint version '${requestedLintVersion}' was specified with the patch version, need specify only minor version`
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
if (isLessVersion(parsedRequestedLintVersion, minVersion)) {
|
if (isLessVersion(parsedRequestedLintVersion, minVersion)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
@ -98,11 +104,20 @@ const getConfig = async (): Promise<Config> => {
|
|||||||
|
|
||||||
export async function findLintVersion(): Promise<VersionConfig> {
|
export async function findLintVersion(): Promise<VersionConfig> {
|
||||||
core.info(`Finding needed golangci-lint version...`)
|
core.info(`Finding needed golangci-lint version...`)
|
||||||
const startedAt = Date.now()
|
|
||||||
const reqLintVersion = getRequestedLintVersion()
|
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()
|
const config = await getConfig()
|
||||||
|
|
||||||
if (!config.MinorVersionToConfig) {
|
if (!config.MinorVersionToConfig) {
|
||||||
core.warning(JSON.stringify(config))
|
core.warning(JSON.stringify(config))
|
||||||
throw new Error(`invalid config: no MinorVersionToConfig field`)
|
throw new Error(`invalid config: no MinorVersionToConfig field`)
|
||||||
@ -118,8 +133,9 @@ export async function findLintVersion(): Promise<VersionConfig> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
core.info(
|
core.info(
|
||||||
`Requested golangci-lint '${stringifyVersion(reqLintVersion)}', using '${versionConfig.TargetVersion}', calculation took ${Date.now() -
|
`Requested golangci-lint '${stringifyVersion(reqLintVersion)}', using '${versionConfig.TargetVersion}', calculation took ${
|
||||||
startedAt}ms`
|
Date.now() - startedAt
|
||||||
|
}ms`
|
||||||
)
|
)
|
||||||
return versionConfig
|
return versionConfig
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user