diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9a16cb3069..46092cbc3b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,8 +52,8 @@ jobs: version: - "" - "latest" - - "v1.56" - - "v1.56.1" + - "v1.58" + - "v1.58.0" runs-on: ${{ matrix.os }} permissions: contents: read @@ -63,7 +63,6 @@ jobs: - uses: actions/setup-go@v5 with: go-version: oldstable - cache: false # setup-go v4 caches by default - uses: ./ with: version: ${{ matrix.version }} @@ -81,8 +80,8 @@ jobs: version: - "" - "latest" - - "v1.56.1" - - "bf5008a11acf2da5fe76716eb21d808499e079fa" + - "v1.58.0" + - "4bf574a12bb61234e28e3d6172be6ed95b0e8baf" runs-on: ${{ matrix.os }} permissions: contents: read @@ -92,7 +91,6 @@ jobs: - uses: actions/setup-go@v5 with: go-version: oldstable - cache: false # setup-go v4 caches by default - uses: ./ with: version: ${{ matrix.version }} @@ -116,7 +114,6 @@ jobs: - uses: actions/setup-go@v5 with: go-version: oldstable - cache: false # setup-go v4 caches by default - uses: ./ with: working-directory: sample-go-mod diff --git a/README.md b/README.md index d1754e92ec..5e498a32a7 100644 --- a/README.md +++ b/README.md @@ -43,39 +43,11 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: - go-version: '1.21' + go-version: '1.22' - name: golangci-lint uses: golangci/golangci-lint-action@v5 with: - # Require: The version of golangci-lint to use. - # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. - # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. - version: v1.57 - - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - # - # Note: By default, the `.golangci.yml` file should be at the root of the repository. - # The location of the configuration file can be changed by using `--config=` - # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 - - # Optional: Show only new issues. - # If you are using `merge_group` event (merge queue) you should add the option `fetch-depth: 0` to `actions/checkout` step. - # The default value is `false`. - # only-new-issues: true - - # Optional: if set to true, then all caching functionality will be completely disabled, - # takes precedence over all other caching options. - # skip-cache: true - - # Optional: if set to true, caches will not be saved, but they may still be restored, - # subject to other options - # skip-save-cache: true - - # Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'. - # install-mode: "goinstall" + version: latest ``` We recommend running this action in a job separate from other jobs (`go test`, etc.) @@ -105,7 +77,7 @@ jobs: golangci: strategy: matrix: - go: ['1.21'] + go: ['1.22'] os: [ubuntu-latest, macos-latest, windows-latest] name: lint runs-on: ${{ matrix.os }} @@ -117,35 +89,7 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v5 with: - # Require: The version of golangci-lint to use. - # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. - # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. - version: v1.57 - - # Optional: working directory, useful for monorepos - # working-directory: somedir - - # Optional: golangci-lint command line arguments. - # - # Note: By default, the `.golangci.yml` file should be at the root of the repository. - # The location of the configuration file can be changed by using `--config=` - # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 - - # Optional: Show only new issues. - # If you are using `merge_group` event (merge queue) you should add the option `fetch-depth: 0` to `actions/checkout` step. - # The default value is `false`. - # only-new-issues: true - - # Optional: if set to true, then all caching functionality will be completely disabled, - # takes precedence over all other caching options. - # skip-cache: true - - # Optional: if set to true, caches will not be saved, but they may still be restored, - # subject to other options - # skip-save-cache: true - - # Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'. - # install-mode: "goinstall" + version: latest ``` You will also likely need to add the following `.gitattributes` file to ensure that line endings for Windows builds are properly formatted: @@ -154,7 +98,157 @@ You will also likely need to add the following `.gitattributes` file to ensure t *.go text eol=lf ``` -## Comments and Annotations +## Options + +### `version` + +(required) + +The version of golangci-lint to use. + +* When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. +* When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + version: latest + # ... +``` + +### `install-mode` + +(optional) + +The mode to install golangci-lint: it can be `binary` or `goinstall`. + +The default value is `binary`. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + install-mode: "goinstall" + # ... +``` + +### `only-new-issues` + +(optional) + +Show only new issues. + +The default value is `false`. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + only-new-issues: true + # ... +``` + +* `pull_request` and `pull_request_target`: the action gets the diff of the PR content from the [GitHub API](https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request) and use it with `--new-from-patch`. +* `push`: the action gets the diff of the push content (difference between commits before and after the push) from the [GitHub API](https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#compare-two-commits) and use it with `--new-from-patch`. +* `merge_group`: the action gets the diff by using `--new-from-rev` option (relies on git). + You should add the option `fetch-depth: 0` to `actions/checkout` step. + +### `working-directory` + +(optional) + +Working directory, useful for monorepos. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + working-directory: somedir + # ... +``` + +### `skip-cache` + +(optional) + +If set to `true`, then all caching functionality will be completely disabled, +takes precedence over all other caching options. + +The default value is `false`. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + skip-cache: true + # ... +``` + +### `skip-save-cache` + +(optional) + +If set to `true`, caches will not be saved, but they may still be restored, required `skip-cache: false`. + +The default value is `false`. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + skip-save-cache: true + # ... +``` + +### `cache-invalidation-interval` + +(optional) + +Periodically invalidate the cache every `cache-invalidation-interval` days to ensure that outdated data is removed and fresh data is loaded. + +The default value is `7`. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + cache-invalidation-interval: 15 + # ... +``` + +If set the number is `<= 0`, the cache will be always invalidate (Not recommended). + +### `annotations` + +(optional) + +To enable/disable GitHub Action annotations. + +If disabled (`false`), the output format(s) will follow the golangci-lint configuration file (or CLI flags from `args`) +and use the same default as golangci-lint (i.e. `colored-line-number`). + +https://golangci-lint.run/usage/configuration/#output-configuration + +The default value is `true`. + +```yml +uses: golangci/golangci-lint-action@v5 +with: + annotations: false + # ... +``` + +### `args` + +(optional) + +golangci-lint command line arguments. + +Note: By default, the `.golangci.yml` file should be at the root of the repository. +The location of the configuration file can be changed by using `--config=` + +```yml +uses: golangci/golangci-lint-action@v5 +with: + args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 + # ... +``` + +## Annotations Currently, GitHub parses the action's output and creates [annotations](https://github.blog/2018-12-14-introducing-check-runs-and-annotations/). @@ -163,8 +257,9 @@ 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 the list of comments. If you would like to have comments - please, up-vote [the issue](https://github.com/golangci/golangci-lint-action/issues/5). +3. The number of annotations is [limited](https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md#limitations). -To enable annotations, you need to add the `checks' permission to your action. +To enable annotations, you need to add the `checks` permission to your action. ```yaml annotate permissions: @@ -172,7 +267,7 @@ permissions: contents: read # Optional: allow read access to pull request. Use with `only-new-issues` option. pull-requests: read - # Optional: Allow write access to checks to allow the action to annotate code in the PR. + # Optional: allow write access to checks to allow the action to annotate code in the PR. checks: write ``` @@ -213,10 +308,11 @@ Inside our action, we perform 3 steps: ### Caching internals 1. We save and restore the following directory: `~/.cache/golangci-lint`. -2. The primary caching key looks like `golangci-lint.cache-{interval_number}-{go.mod_hash}`. +2. The primary caching key looks like `golangci-lint.cache-{runner_os}-{working_directory}-{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}-`. +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-{runner_os}-{working_directory}-{interval_number}-`. 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. diff --git a/action.yml b/action.yml index 4f3cb3a4bf..bb4a50870e 100644 --- a/action.yml +++ b/action.yml @@ -1,4 +1,5 @@ -name: "Run golangci-lint" +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions +name: "Golangci-lint" description: "Official golangci-lint action with line-attached annotations for found issues, caching and parallel execution." author: "golangci" inputs: @@ -8,9 +9,9 @@ inputs: When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. required: false - args: - description: "golangci-lint command line arguments" - default: "" + install-mode: + description: "The mode to install golangci-lint. It can be 'binary' or 'goinstall'." + default: "binary" required: false working-directory: description: "golangci-lint working directory, default is project root" @@ -35,9 +36,17 @@ inputs: restore existing caches, subject to other options. default: 'false' required: false - install-mode: - description: "The mode to install golangci-lint. It can be 'binary' or 'goinstall'." - default: "binary" + annotations: + description: "To Enable/disable GitHub Action annotations" + default: 'true' + required: false + args: + description: "golangci-lint command line arguments" + default: "" + required: false + cache-invalidation-interval: + description: "Periodically invalidate a cache because a new code being added. (number of days)" + default: '7' required: false runs: using: "node20" @@ -45,4 +54,4 @@ runs: post: "dist/post_run/index.js" branding: icon: "shield" - color: "yellow" + color: "white" diff --git a/dist/post_run/index.js b/dist/post_run/index.js index 9dd2226db6..87f67815a6 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -88815,18 +88815,26 @@ const getLintCacheDir = () => { }; const getIntervalKey = (invalidationIntervalDays) => { const now = new Date(); + if (invalidationIntervalDays <= 0) { + return `${now.getTime()}`; + } const secondsSinceEpoch = now.getTime() / 1000; const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400)); return intervalNumber.toString(); }; async function buildCacheKeys() { const keys = []; - // Periodically invalidate a cache because a new code being added. - // TODO: configure it via inputs. - let cacheKey = `golangci-lint.cache-${getIntervalKey(7)}-`; - keys.push(cacheKey); + // Cache by OS. + let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-`; // Get working directory from input const workingDirectory = core.getInput(`working-directory`); + if (workingDirectory) { + cacheKey += `${workingDirectory}-`; + } + // Periodically invalidate a cache because a new code being added. + const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()); + cacheKey += `${getIntervalKey(invalidationIntervalDays)}-`; + keys.push(cacheKey); // create path to go.mod prepending the workingDirectory if it exists const goModPath = path_1.default.join(workingDirectory, `go.mod`); core.info(`Checking for go.mod: ${goModPath}`); @@ -88841,7 +88849,7 @@ async function buildCacheKeys() { return keys; } async function restoreCache() { - if (core.getInput(`skip-cache`, { required: true }).trim() == "true") + if (core.getBooleanInput(`skip-cache`, { required: true })) return; if (!utils.isValidEvent()) { utils.logWarning(`Event Validation Error: The event type ${process.env[constants_1.Events.Key]} is not supported because it's not tied to a branch or tag ref.`); @@ -88879,9 +88887,9 @@ async function restoreCache() { } exports.restoreCache = restoreCache; async function saveCache() { - if (core.getInput(`skip-cache`, { required: true }).trim() == "true") + if (core.getBooleanInput(`skip-cache`, { required: true })) return; - if (core.getInput(`skip-save-cache`, { required: true }).trim() == "true") + if (core.getBooleanInput(`skip-save-cache`, { required: true })) return; // Validate inputs, this can cause task failure if (!utils.isValidEvent()) { @@ -89147,11 +89155,7 @@ const execShellCommand = (0, util_1.promisify)(child_process_1.exec); const writeFile = (0, util_1.promisify)(fs.writeFile); const createTempDir = (0, util_1.promisify)(tmp_1.dir); function isOnlyNewIssues() { - 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"`); - } - return onlyNewIssues === `true`; + return core.getBooleanInput(`only-new-issues`, { required: true }); } async function prepareLint() { const mode = core.getInput("install-mode").toLowerCase(); @@ -89221,11 +89225,10 @@ async function fetchPushPatch(ctx) { const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); let patch; try { - const patchResp = await octokit.rest.repos.compareCommits({ + const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({ owner: ctx.repo.owner, repo: ctx.repo.repo, - base: ctx.payload.before, - head: ctx.payload.after, + basehead: `${ctx.payload.before}..${ctx.payload.after}`, mediaType: { format: `diff`, }, @@ -89287,15 +89290,18 @@ async function runLint(lintPath, patchPath) { .map(([key, value]) => [key.toLowerCase(), value ?? ""]); const userArgsMap = new Map(userArgsList); const userArgNames = new Set(userArgsList.map(([key]) => key)); - const formats = (userArgsMap.get("out-format") || "") - .trim() - .split(",") - .filter((f) => f.length > 0) - .filter((f) => !f.startsWith(`github-actions`)) - .concat("github-actions") - .join(","); - addedArgs.push(`--out-format=${formats}`); - userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); + const annotations = core.getBooleanInput(`annotations`); + if (annotations) { + const formats = (userArgsMap.get("out-format") || "") + .trim() + .split(",") + .filter((f) => f.length > 0) + .filter((f) => !f.startsWith(`github-actions`)) + .concat("github-actions") + .join(","); + addedArgs.push(`--out-format=${formats}`); + userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); + } if (isOnlyNewIssues()) { 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`); @@ -89323,8 +89329,8 @@ async function runLint(lintPath, patchPath) { break; } } - const workingDirectory = core.getInput(`working-directory`); const cmdArgs = {}; + const workingDirectory = core.getInput(`working-directory`); if (workingDirectory) { if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) { throw new Error(`working-directory (${workingDirectory}) was not a path`); @@ -89335,7 +89341,7 @@ async function runLint(lintPath, patchPath) { cmdArgs.cwd = path.resolve(workingDirectory); } const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd(); - core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`); + core.info(`Running [${cmd}] in [${cmdArgs.cwd || process.cwd()}] ...`); const startedAt = Date.now(); try { const res = await execShellCommand(cmd, cmdArgs); diff --git a/dist/run/index.js b/dist/run/index.js index b015ef3b3f..11ef531598 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -88815,18 +88815,26 @@ const getLintCacheDir = () => { }; const getIntervalKey = (invalidationIntervalDays) => { const now = new Date(); + if (invalidationIntervalDays <= 0) { + return `${now.getTime()}`; + } const secondsSinceEpoch = now.getTime() / 1000; const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400)); return intervalNumber.toString(); }; async function buildCacheKeys() { const keys = []; - // Periodically invalidate a cache because a new code being added. - // TODO: configure it via inputs. - let cacheKey = `golangci-lint.cache-${getIntervalKey(7)}-`; - keys.push(cacheKey); + // Cache by OS. + let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-`; // Get working directory from input const workingDirectory = core.getInput(`working-directory`); + if (workingDirectory) { + cacheKey += `${workingDirectory}-`; + } + // Periodically invalidate a cache because a new code being added. + const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()); + cacheKey += `${getIntervalKey(invalidationIntervalDays)}-`; + keys.push(cacheKey); // create path to go.mod prepending the workingDirectory if it exists const goModPath = path_1.default.join(workingDirectory, `go.mod`); core.info(`Checking for go.mod: ${goModPath}`); @@ -88841,7 +88849,7 @@ async function buildCacheKeys() { return keys; } async function restoreCache() { - if (core.getInput(`skip-cache`, { required: true }).trim() == "true") + if (core.getBooleanInput(`skip-cache`, { required: true })) return; if (!utils.isValidEvent()) { utils.logWarning(`Event Validation Error: The event type ${process.env[constants_1.Events.Key]} is not supported because it's not tied to a branch or tag ref.`); @@ -88879,9 +88887,9 @@ async function restoreCache() { } exports.restoreCache = restoreCache; async function saveCache() { - if (core.getInput(`skip-cache`, { required: true }).trim() == "true") + if (core.getBooleanInput(`skip-cache`, { required: true })) return; - if (core.getInput(`skip-save-cache`, { required: true }).trim() == "true") + if (core.getBooleanInput(`skip-save-cache`, { required: true })) return; // Validate inputs, this can cause task failure if (!utils.isValidEvent()) { @@ -89147,11 +89155,7 @@ const execShellCommand = (0, util_1.promisify)(child_process_1.exec); const writeFile = (0, util_1.promisify)(fs.writeFile); const createTempDir = (0, util_1.promisify)(tmp_1.dir); function isOnlyNewIssues() { - 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"`); - } - return onlyNewIssues === `true`; + return core.getBooleanInput(`only-new-issues`, { required: true }); } async function prepareLint() { const mode = core.getInput("install-mode").toLowerCase(); @@ -89221,11 +89225,10 @@ async function fetchPushPatch(ctx) { const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); let patch; try { - const patchResp = await octokit.rest.repos.compareCommits({ + const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({ owner: ctx.repo.owner, repo: ctx.repo.repo, - base: ctx.payload.before, - head: ctx.payload.after, + basehead: `${ctx.payload.before}..${ctx.payload.after}`, mediaType: { format: `diff`, }, @@ -89287,15 +89290,18 @@ async function runLint(lintPath, patchPath) { .map(([key, value]) => [key.toLowerCase(), value ?? ""]); const userArgsMap = new Map(userArgsList); const userArgNames = new Set(userArgsList.map(([key]) => key)); - const formats = (userArgsMap.get("out-format") || "") - .trim() - .split(",") - .filter((f) => f.length > 0) - .filter((f) => !f.startsWith(`github-actions`)) - .concat("github-actions") - .join(","); - addedArgs.push(`--out-format=${formats}`); - userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); + const annotations = core.getBooleanInput(`annotations`); + if (annotations) { + const formats = (userArgsMap.get("out-format") || "") + .trim() + .split(",") + .filter((f) => f.length > 0) + .filter((f) => !f.startsWith(`github-actions`)) + .concat("github-actions") + .join(","); + addedArgs.push(`--out-format=${formats}`); + userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); + } if (isOnlyNewIssues()) { 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`); @@ -89323,8 +89329,8 @@ async function runLint(lintPath, patchPath) { break; } } - const workingDirectory = core.getInput(`working-directory`); const cmdArgs = {}; + const workingDirectory = core.getInput(`working-directory`); if (workingDirectory) { if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) { throw new Error(`working-directory (${workingDirectory}) was not a path`); @@ -89335,7 +89341,7 @@ async function runLint(lintPath, patchPath) { cmdArgs.cwd = path.resolve(workingDirectory); } const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd(); - core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`); + core.info(`Running [${cmd}] in [${cmdArgs.cwd || process.cwd()}] ...`); const startedAt = Date.now(); try { const res = await execShellCommand(cmd, cmdArgs); diff --git a/src/cache.ts b/src/cache.ts index 5c73169753..dc6aa06b14 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -25,6 +25,11 @@ const getLintCacheDir = (): string => { const getIntervalKey = (invalidationIntervalDays: number): string => { const now = new Date() + + if (invalidationIntervalDays <= 0) { + return `${now.getTime()}` + } + const secondsSinceEpoch = now.getTime() / 1000 const intervalNumber = Math.floor(secondsSinceEpoch / (invalidationIntervalDays * 86400)) return intervalNumber.toString() @@ -32,28 +37,42 @@ const getIntervalKey = (invalidationIntervalDays: number): string => { async function buildCacheKeys(): Promise { const keys = [] - // Periodically invalidate a cache because a new code being added. - // TODO: configure it via inputs. - let cacheKey = `golangci-lint.cache-${getIntervalKey(7)}-` - keys.push(cacheKey) + + // Cache by OS. + let cacheKey = `golangci-lint.cache-${process.env?.RUNNER_OS}-` + // Get working directory from input const workingDirectory = core.getInput(`working-directory`) + + if (workingDirectory) { + cacheKey += `${workingDirectory}-` + } + + // Periodically invalidate a cache because a new code being added. + const invalidationIntervalDays = parseInt(core.getInput(`cache-invalidation-interval`, { required: true }).trim()) + cacheKey += `${getIntervalKey(invalidationIntervalDays)}-` + + keys.push(cacheKey) + // create path to go.mod prepending the workingDirectory if it exists const goModPath = path.join(workingDirectory, `go.mod`) + core.info(`Checking for go.mod: ${goModPath}`) + if (await pathExists(goModPath)) { // Add checksum to key to invalidate a cache when dependencies change. cacheKey += await checksumFile(`sha1`, goModPath) } else { cacheKey += `nogomod` } + keys.push(cacheKey) return keys } export async function restoreCache(): Promise { - if (core.getInput(`skip-cache`, { required: true }).trim() == "true") return + if (core.getBooleanInput(`skip-cache`, { required: true })) return if (!utils.isValidEvent()) { utils.logWarning( @@ -95,8 +114,8 @@ export async function restoreCache(): Promise { } export async function saveCache(): Promise { - if (core.getInput(`skip-cache`, { required: true }).trim() == "true") return - if (core.getInput(`skip-save-cache`, { required: true }).trim() == "true") return + if (core.getBooleanInput(`skip-cache`, { required: true })) return + if (core.getBooleanInput(`skip-save-cache`, { required: true })) return // Validate inputs, this can cause task failure if (!utils.isValidEvent()) { diff --git a/src/run.ts b/src/run.ts index 60ff43201b..9cffa9c690 100644 --- a/src/run.ts +++ b/src/run.ts @@ -17,13 +17,7 @@ const writeFile = promisify(fs.writeFile) const createTempDir = promisify(dir) function isOnlyNewIssues(): boolean { - 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"`) - } - - return onlyNewIssues === `true` + return core.getBooleanInput(`only-new-issues`, { required: true }) } async function prepareLint(): Promise { @@ -104,11 +98,10 @@ async function fetchPushPatch(ctx: Context): Promise { let patch: string try { - const patchResp = await octokit.rest.repos.compareCommits({ + const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({ owner: ctx.repo.owner, repo: ctx.repo.repo, - base: ctx.payload.before, - head: ctx.payload.after, + basehead: `${ctx.payload.before}..${ctx.payload.after}`, mediaType: { format: `diff`, }, @@ -192,16 +185,20 @@ async function runLint(lintPath: string, patchPath: string): Promise { const userArgsMap = new Map(userArgsList) const userArgNames = new Set(userArgsList.map(([key]) => key)) - const formats = (userArgsMap.get("out-format") || "") - .trim() - .split(",") - .filter((f) => f.length > 0) - .filter((f) => !f.startsWith(`github-actions`)) - .concat("github-actions") - .join(",") + const annotations = core.getBooleanInput(`annotations`) - addedArgs.push(`--out-format=${formats}`) - userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim() + if (annotations) { + const formats = (userArgsMap.get("out-format") || "") + .trim() + .split(",") + .filter((f) => f.length > 0) + .filter((f) => !f.startsWith(`github-actions`)) + .concat("github-actions") + .join(",") + + addedArgs.push(`--out-format=${formats}`) + userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim() + } if (isOnlyNewIssues()) { if (userArgNames.has(`new`) || userArgNames.has(`new-from-rev`) || userArgNames.has(`new-from-patch`)) { @@ -236,8 +233,9 @@ async function runLint(lintPath: string, patchPath: string): Promise { } } - const workingDirectory = core.getInput(`working-directory`) const cmdArgs: ExecOptions = {} + + const workingDirectory = core.getInput(`working-directory`) if (workingDirectory) { if (!fs.existsSync(workingDirectory) || !fs.lstatSync(workingDirectory).isDirectory()) { throw new Error(`working-directory (${workingDirectory}) was not a path`) @@ -250,7 +248,7 @@ async function runLint(lintPath: string, patchPath: string): Promise { const cmd = `${lintPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd() - core.info(`Running [${cmd}] in [${cmdArgs.cwd || ``}] ...`) + core.info(`Running [${cmd}] in [${cmdArgs.cwd || process.cwd()}] ...`) const startedAt = Date.now() try {