reimplement github action in typescript

This commit is contained in:
Denis Isaev 2020-05-04 16:01:25 +03:00
parent 666cc9164e
commit 853ade09ed
No known key found for this signature in database
GPG Key ID: B1866FBC94B37EDF
22 changed files with 67641 additions and 49 deletions

23
.eslintrc.json Normal file
View File

@ -0,0 +1,23 @@
{
"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",
"prettier/@typescript-eslint"
],
"plugins": ["@typescript-eslint", "simple-import-sort"],
"rules": {
"import/first": "error",
"import/newline-after-import": "error",
"import/no-duplicates": "error",
"simple-import-sort/sort": "error",
"sort-imports": "off"
}
}

View File

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

View File

@ -1,14 +0,0 @@
name: golangci
on: [pull_request]
jobs:
golangci-lint-dockerfile:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: ./
with:
directory: sample
format: colored-line-number
flags: --issues-exit-code 0

27
.github/workflows/test.yml vendored Normal file
View File

@ -0,0 +1,27 @@
name: "build-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@v1
- run: |
npm install
npm run prepare-deps
npm run all
git diff && git diff --cached
test: # make sure the action works on a clean machine without building
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: ./
with:
version: v1.26
args: --issues-exit-code=0 ./sample/...
github-token: ${{ secrets.GOLANGCI_LINT_GITHUB_TOKEN }}

99
.gitignore vendored Normal file
View 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
View File

@ -0,0 +1,9 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 140,
"parser": "typescript"
}

View File

@ -1,22 +1,75 @@
# golangci-lint-action
![docker image](https://github.com/golangci/golangci-lint-action/workflows/docker%20image/badge.svg)
[![Build Status](https://github.com/golangci/golangci-lint-action/workflows/test/badge.svg)](https://github.com/golangci/golangci-lint-action/actions)
Action that runs [golangci-lint](https://github.com/golangci/golangci-lint) and reports issues from linters.
![GitHub Annotations](./static/annotations.png)
The action that runs [golangci-lint](https://github.com/golangci/golangci-lint) and reports issues from linters.
## How to use
1. Create a [GitHub token](https://github.com/settings/tokens/new) with scope `repo.public_repo`.
2. Add it to a [repository secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets): repository -> `Settings` -> `Secrets`.
3. Add `.github/workflows/golangci-lint.yml` with the following contents:
You can put `.github/workflows/lint.yml` with following contents:
```yaml
name: golangci
on: [push]
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v0.0.2
uses: golangci/golangci-lint-action@v1
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.26
# Optional: golangci-lint command line arguments.
# args: ./the-only-dir-to-analyze/...
# Required: GitHub token with scope `repo.public_repo`. Used for fetching a list of releases of golangci-lint.
github-token: ${{ secrets.GOLANGCI_LINT_GITHUB_TOKEN }}
```
Based on [reviewdog action](https://github.com/reviewdog/action-golangci-lint).
## 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).
## 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/cache) until GitHub team hasn't supported reusing actions from actions
Inside our action we perform 3 steps:
1. Setup environment running in parallel:
* restore [cache](https://github.com/actions/cache) of previous analyzes into `$HOME/.cache/golangci-lint`
* list [releases of golangci-lint](https://github.com/golangci/golangci-lint/releases) and find the latest patch version
for needed version (users of this action can specify only minor version). 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 from `$HOME/.cache/golangci-lint` for later builds
## 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. Get a [GitHub token](https://github.com/settings/tokens/new) with the scope `repo.public_repo`. Export it by `export GITHUB_TOKEN=YOUR_TOKEN`.
4. Prepare deps once: `npm run prepare-deps`
5. Run `npm run local` after any change to test it

View File

@ -3,25 +3,21 @@ name: 'Run golangci-lint'
description: 'Run golangci-lint (WIP)'
author: 'golangci'
inputs:
github_token:
description: 'GITHUB_TOKEN'
required: false
flags:
description: 'GolangCI command line flags'
required: false
directory:
description: 'Working directory'
required: false
version:
description: 'version of golangci-lint to use in form of v1.2.3'
required: true
args:
description: 'golangci-lint command line arguments'
default: ''
format:
description: 'Output format of issues'
default: 'github-actions'
required: false
github-token:
description: 'GitHub token with scope `repo.public_repo`. Used for fetching a list of releases of golangci-lint.'
required: true
runs:
using: 'docker'
image: 'Dockerfile'
using: 'node12'
main: 'dist/run/index.js'
post: 'dist/post_run/index.js'
branding:
icon: 'check-circle'
color: 'blue'

16
dist/matchers.json vendored Normal file
View File

@ -0,0 +1,16 @@
{
"problemMatcher": [
{
"owner": "golangci-lint-action",
"pattern": [
{
"regexp": "^\\s*(\\.{0,2}[\\/\\\\].+\\.go):(?:(\\d+):(\\d+):)? (.*)",
"file": 1,
"line": 2,
"column": 3,
"message": 4
}
]
}
]
}

32437
dist/post_run/index.js vendored Normal file

File diff suppressed because it is too large Load Diff

32437
dist/run/index.js vendored Normal file

File diff suppressed because it is too large Load Diff

2162
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

49
package.json Normal file
View File

@ -0,0 +1,49 @@
{
"name": "golanci-lint-action",
"version": "1.1.2",
"private": true,
"description": "golangci-lint github action",
"main": "dist/main.js",
"scripts": {
"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 && npm run prepare-cache",
"build": "tsc && ncc build -o dist/run/ src/main.ts && ncc build -o dist/post_run/ src/post_main.ts",
"type-check": "tsc",
"lint": "eslint **/*.ts --cache",
"lint-fix": "eslint **/*.ts --cache --fix",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"all": "npm run build && npm run format-check && npm run lint",
"local": "npm run build && act -j test -s GOLANGCI_LINT_GITHUB_TOKEN=$GITHUB_TOKEN"
},
"repository": {
"type": "git",
"url": "git+https://github.com/golangci/golangci-lint-action.git"
},
"author": "GitHub",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/exec": "^1.0.1",
"@actions/github": "^2.1.1",
"@actions/tool-cache": "^1.3.4",
"@types/semver": "^7.1.0",
"cache": "git+https://github.com/golangci/cache.git",
"setup-go": "git+https://github.com/actions/setup-go.git"
},
"devDependencies": {
"@types/node": "^12.0.4",
"@types/uuid": "^3.4.5",
"@typescript-eslint/eslint-plugin": "^2.7.0",
"@typescript-eslint/parser": "^2.7.0",
"@zeit/ncc": "^0.20.5",
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-simple-import-sort": "^5.0.2",
"prettier": "^1.19.1",
"typescript": "^3.8.3"
}
}

29
src/cache.ts Normal file
View File

@ -0,0 +1,29 @@
import * as core from "@actions/core"
import restore from "cache/lib/restore"
import save from "cache/lib/save"
// TODO: ensure dir exists, have access, etc
const getCacheDir = (): string => `${process.env.HOME}/.cache/golangci-lint`
const setCacheInputs = (): void => {
process.env.INPUT_KEY = `golangci-lint.analysis-cache`
process.env.INPUT_PATH = getCacheDir()
}
export async function restoreCache(): Promise<void> {
const startedAt = Date.now()
setCacheInputs()
// Tell golangci-lint to use our cache directory.
process.env.GOLANGCI_LINT_CACHE = getCacheDir()
await restore()
core.info(`Restored golangci-lint analysis cache in ${Date.now() - startedAt}ms`)
}
export async function saveCache(): Promise<void> {
const startedAt = Date.now()
setCacheInputs()
await save()
core.info(`Saved golangci-lint analysis cache in ${Date.now() - startedAt}ms`)
}

13
src/deps.d.ts vendored Normal file
View 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
}

27
src/install.ts Normal file
View File

@ -0,0 +1,27 @@
import * as core from "@actions/core"
import * as tc from "@actions/tool-cache"
import path from "path"
import { run as setupGo } from "setup-go/lib/main"
import { stringifyVersion, Version } from "./version"
// The installLint returns path to installed binary of golangci-lint.
export async function installLint(ver: Version): Promise<string> {
core.info(`Installing golangci-lint ${stringifyVersion(ver)}...`)
const startedAt = Date.now()
const dirName = `golangci-lint-${ver.major}.${ver.minor}.${ver.patch}-linux-amd64`
const assetUrl = `https://github.com/golangci/golangci-lint/releases/download/${stringifyVersion(ver)}/${dirName}.tar.gz`
const tarGzPath = await tc.downloadTool(assetUrl)
const extractedDir = await tc.extractTar(tarGzPath, process.env.HOME)
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 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
View File

@ -0,0 +1,3 @@
import { run } from "./run"
run()

3
src/post_main.ts Normal file
View File

@ -0,0 +1,3 @@
import { postRun } from "./run"
postRun()

97
src/run.ts Normal file
View File

@ -0,0 +1,97 @@
import * as core from "@actions/core"
import { exec } from "child_process"
import { promisify } from "util"
import { restoreCache, saveCache } from "./cache"
import { installGo, installLint } from "./install"
import { findLintVersion } from "./version"
const execShellCommand = promisify(exec)
async function prepareLint(): Promise<string> {
const lintVersion = await findLintVersion()
return await installLint(lintVersion)
}
async function prepareEnv(): Promise<string> {
const startedAt = Date.now()
// Prepare cache, lint and go in parallel.
const restoreCachePromise = restoreCache()
const prepareLintPromise = prepareLint()
const installGoPromise = installGo()
const lintPath = await prepareLintPromise
await installGoPromise
await restoreCachePromise
core.info(`Prepared env in ${Date.now() - startedAt}ms`)
return lintPath
}
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): Promise<void> {
const debug = core.getInput(`debug`)
if (debug.split(`,`).includes(`cache`)) {
const res = await execShellCommand(`${lintPath} cache status`)
printOutput(res)
}
const args = core.getInput(`args`)
if (args.includes(`-out-format`)) {
throw new Error(`please, don't change out-format for golangci-lint: it can be broken in a future`)
}
const cmd = `${lintPath} run ${args}`.trimRight()
core.info(`Running [${cmd}] ...`)
const startedAt = Date.now()
try {
const res = await execShellCommand(cmd)
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 = await core.group(`prepare environment`, prepareEnv)
await core.group(`run golangci-lint`, () => runLint(lintPath))
} 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)
}
}

123
src/version.ts Normal file
View File

@ -0,0 +1,123 @@
import * as core from "@actions/core"
import * as github from "@actions/github"
import { Octokit } from "@actions/github/node_modules/@octokit/rest"
async function performResilientGitHubRequest<T>(opName: string, execFn: () => Promise<Octokit.Response<T>>): Promise<T> {
let lastError = ``
for (let i = 0; i < 3; i++) {
// TODO: configurable params, timeouts, random jitters, exponential back-off, etc
try {
const res = await execFn()
if (res.status === 200) {
return res.data
}
lastError = `GitHub returned HTTP code ${res.status}`
} catch (exc) {
lastError = exc.message
}
}
throw new Error(`failed to execute github operation '${opName}': ${lastError}`)
}
// TODO: make a class
export type Version = {
major: number
minor: number
patch: number | null
}
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/
const parseVersion = (s: string): Version => {
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 => `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}`
const minVersion = {
major: 1,
minor: 14,
patch: 0,
}
const isLessVersion = (a: Version, b: Version): boolean => {
if (a.major != b.major) {
return a.major < b.major
}
if (a.minor != b.minor) {
return a.minor < b.minor
}
if (a.patch === null || b.patch === null) {
return true
}
return a.patch < b.patch
}
const getRequestedLintVersion = (): Version => {
const requestedLintVersion = core.getInput(`version`, { required: true })
const parsedRequestedLintVersion = parseVersion(requestedLintVersion)
if (parsedRequestedLintVersion.patch !== null) {
throw new Error(
`requested golangci-lint version '${requestedLintVersion}' was specified with the patch version, need specify only minor version`
)
}
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 async function findLintVersion(): Promise<Version> {
core.info(`Finding needed golangci-lint version...`)
const startedAt = Date.now()
const reqLintVersion = getRequestedLintVersion()
const githubToken = core.getInput(`github-token`, { required: true })
const octokit = new github.GitHub(githubToken)
// TODO: fetch all pages, not only the first one.
const releasesPage = await performResilientGitHubRequest(`fetch releases of golangci-lint`, function() {
return octokit.repos.listReleases({ owner: `golangci`, repo: `golangci-lint`, [`per_page`]: 100 })
})
// TODO: use semver and semver.satisfies
let latestPatchVersion: number | null = null
for (const rel of releasesPage) {
const ver = parseVersion(rel.tag_name)
if (ver.patch === null) {
// < minVersion
continue
}
if (ver.major == reqLintVersion.major && ver.minor == reqLintVersion.minor) {
latestPatchVersion = latestPatchVersion !== null ? Math.max(latestPatchVersion, ver.patch) : ver.patch
}
}
if (latestPatchVersion === null) {
throw new Error(
`requested golangci-lint lint version ${stringifyVersion(reqLintVersion)} doesn't exist in list of golangci-lint releases`
)
}
const neededVersion = { ...reqLintVersion, patch: latestPatchVersion }
core.info(`Calculated needed golangci-lint version ${stringifyVersion(neededVersion)} in ${Date.now() - startedAt}ms`)
return neededVersion
}

BIN
static/annotations.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

14
tsconfig.json Normal file
View 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"]
}