Skip to content

Commit

Permalink
Support latest tag for golangci-lint version (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Vilgelm committed Aug 2, 2020
1 parent b026646 commit 809d3b0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ jobs:
- uses: actions/checkout@v2
- uses: ./
with:
version: v1.29
version: latest
args: --issues-exit-code=0 ./sample/...
only-new-issues: true
30 changes: 15 additions & 15 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
---
name: 'Run golangci-lint'
description: 'Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution.'
author: 'golangci'
name: "Run golangci-lint"
description: "Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution."
author: "golangci"
inputs:
version:
description: 'version of golangci-lint to use in form of v1.2'
required: true
description: "version of golangci-lint to use in form of v1.2 or `latest` to use the latest version"
required: false
args:
description: 'golangci-lint command line arguments'
default: ''
description: "golangci-lint command line arguments"
default: ""
required: false
working-directory:
description: 'golangci-lint working directory, default is project root'
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'
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'
description: "if set to true and the action runs on a pull request - the action outputs only newly found issues"
default: false
required: true

runs:
using: 'node12'
main: 'dist/run/index.js'
post: 'dist/post_run/index.js'
using: "node12"
main: "dist/run/index.js"
post: "dist/post_run/index.js"
branding:
icon: 'shield'
color: 'yellow'
icon: "shield"
color: "yellow"
23 changes: 20 additions & 3 deletions dist/post_run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,9 @@ const core = __importStar(__webpack_require__(470));
const httpm = __importStar(__webpack_require__(539));
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
const parseVersion = (s) => {
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`);
Expand All @@ -2029,13 +2032,24 @@ const parseVersion = (s) => {
patch: match[3] === undefined ? null : parseInt(match[3]),
};
};
exports.stringifyVersion = (v) => `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}`;
exports.stringifyVersion = (v) => {
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, b) => {
if (a == null) {
return true;
}
if (b == null) {
return false;
}
if (a.major != b.major) {
return a.major < b.major;
}
Expand All @@ -2044,8 +2058,11 @@ const isLessVersion = (a, b) => {
return a.minor < b.minor;
};
const getRequestedLintVersion = () => {
const requestedLintVersion = core.getInput(`version`, { required: true });
const requestedLintVersion = core.getInput(`version`);
const parsedRequestedLintVersion = parseVersion(requestedLintVersion);
if (parsedRequestedLintVersion == null) {
return null;
}
if (parsedRequestedLintVersion.patch !== null) {
throw new Error(`requested golangci-lint version '${requestedLintVersion}' was specified with the patch version, need specify only minor version`);
}
Expand Down Expand Up @@ -2076,12 +2093,12 @@ function findLintVersion() {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Finding needed golangci-lint version...`);
const startedAt = Date.now();
const reqLintVersion = getRequestedLintVersion();
const config = yield getConfig();
if (!config.MinorVersionToConfig) {
core.warning(JSON.stringify(config));
throw new Error(`invalid config: no MinorVersionToConfig field`);
}
const reqLintVersion = getRequestedLintVersion();
const versionConfig = config.MinorVersionToConfig[exports.stringifyVersion(reqLintVersion)];
if (!versionConfig) {
throw new Error(`requested golangci-lint version '${exports.stringifyVersion(reqLintVersion)}' doesn't exist`);
Expand Down
23 changes: 20 additions & 3 deletions dist/run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2019,6 +2019,9 @@ const core = __importStar(__webpack_require__(470));
const httpm = __importStar(__webpack_require__(539));
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/;
const parseVersion = (s) => {
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`);
Expand All @@ -2029,13 +2032,24 @@ const parseVersion = (s) => {
patch: match[3] === undefined ? null : parseInt(match[3]),
};
};
exports.stringifyVersion = (v) => `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}`;
exports.stringifyVersion = (v) => {
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, b) => {
if (a == null) {
return true;
}
if (b == null) {
return false;
}
if (a.major != b.major) {
return a.major < b.major;
}
Expand All @@ -2044,8 +2058,11 @@ const isLessVersion = (a, b) => {
return a.minor < b.minor;
};
const getRequestedLintVersion = () => {
const requestedLintVersion = core.getInput(`version`, { required: true });
const requestedLintVersion = core.getInput(`version`);
const parsedRequestedLintVersion = parseVersion(requestedLintVersion);
if (parsedRequestedLintVersion == null) {
return null;
}
if (parsedRequestedLintVersion.patch !== null) {
throw new Error(`requested golangci-lint version '${requestedLintVersion}' was specified with the patch version, need specify only minor version`);
}
Expand Down Expand Up @@ -2076,12 +2093,12 @@ function findLintVersion() {
return __awaiter(this, void 0, void 0, function* () {
core.info(`Finding needed golangci-lint version...`);
const startedAt = Date.now();
const reqLintVersion = getRequestedLintVersion();
const config = yield getConfig();
if (!config.MinorVersionToConfig) {
core.warning(JSON.stringify(config));
throw new Error(`invalid config: no MinorVersionToConfig field`);
}
const reqLintVersion = getRequestedLintVersion();
const versionConfig = config.MinorVersionToConfig[exports.stringifyVersion(reqLintVersion)];
if (!versionConfig) {
throw new Error(`requested golangci-lint version '${exports.stringifyVersion(reqLintVersion)}' doesn't exist`);
Expand Down
26 changes: 21 additions & 5 deletions src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ export type Version = {
major: number
minor: number
patch: number | null
}
} | null

const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/

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`)
Expand All @@ -23,7 +26,12 @@ 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 = {
major: 1,
Expand All @@ -32,6 +40,12 @@ const minVersion = {
}

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
}
Expand All @@ -42,8 +56,11 @@ const isLessVersion = (a: Version, b: Version): boolean => {
}

const getRequestedLintVersion = (): Version => {
const requestedLintVersion = core.getInput(`version`, { required: true })
const requestedLintVersion = core.getInput(`version`)
const parsedRequestedLintVersion = parseVersion(requestedLintVersion)
if (parsedRequestedLintVersion == null) {
return null
}
if (parsedRequestedLintVersion.patch !== null) {
throw new Error(
`requested golangci-lint version '${requestedLintVersion}' was specified with the patch version, need specify only minor version`
Expand Down Expand Up @@ -93,15 +110,14 @@ const getConfig = async (): Promise<Config> => {
export async function findLintVersion(): Promise<VersionConfig> {
core.info(`Finding needed golangci-lint version...`)
const startedAt = Date.now()
const reqLintVersion = getRequestedLintVersion()

const config = await getConfig()

if (!config.MinorVersionToConfig) {
core.warning(JSON.stringify(config))
throw new Error(`invalid config: no MinorVersionToConfig field`)
}

const reqLintVersion = getRequestedLintVersion()
const versionConfig = config.MinorVersionToConfig[stringifyVersion(reqLintVersion)]
if (!versionConfig) {
throw new Error(`requested golangci-lint version '${stringifyVersion(reqLintVersion)}' doesn't exist`)
Expand Down

0 comments on commit 809d3b0

Please sign in to comment.