|
17 | 17 | type: string |
18 | 18 | default: '["ubuntu-latest"]' |
19 | 19 | required: false |
20 | | - directoriesInput: |
21 | | - description: "List of directoriesInput to lint." |
| 20 | + lint-all: |
| 21 | + description: "Run linter on all files, not just the changed ones." |
| 22 | + type: boolean |
| 23 | + required: false |
| 24 | + default: ${{ github.event_name != 'pull_request' }} |
| 25 | + action-files: |
| 26 | + description: "List of files or directories where GitHub Actions are defined. Supports glob patterns." |
22 | 27 | type: string |
23 | 28 | default: | |
24 | | - .github/workflows |
25 | | - actions |
| 29 | + ./action.yml |
| 30 | + ./.github/workflows/**/*.yml |
| 31 | + ./actions/**/*.yml |
| 32 | + linter-env: |
| 33 | + description: | |
| 34 | + Environment variables in multilines format "key=value" to pass to the linter. |
| 35 | + See [linter](./linter.md). |
| 36 | + type: string |
| 37 | + required: false |
26 | 38 |
|
27 | 39 | secrets: |
28 | 40 | github-token: |
@@ -61,55 +73,130 @@ jobs: |
61 | 73 | runs-on: ${{ fromJson(inputs.runs-on) }} |
62 | 74 | steps: |
63 | 75 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 76 | + with: |
| 77 | + fetch-depth: "${{ inputs.lint-all && 1 || 0 }}" |
| 78 | + |
| 79 | + - id: changed-files |
| 80 | + uses: tj-actions/changed-files@823fcebdb31bb35fdf2229d9f769b400309430d0 # v46.0.3 |
| 81 | + if: ${{ inputs.lint-all == false }} |
| 82 | + with: |
| 83 | + files: ${{ inputs.action-files }} |
| 84 | + dir_names_exclude_current_dir: true |
64 | 85 |
|
65 | 86 | - id: get-files-to-lint |
66 | | - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 87 | + uses: actions/github-script@e69ef5462fd455e02edcaf4dd7708eda96b9eda0 # v7.0.0 |
67 | 88 | with: |
68 | 89 | script: | |
69 | 90 | const fs = require("node:fs"); |
70 | 91 | const path = require("node:path"); |
71 | 92 |
|
72 | | - const directoriesInput = ${{ toJson(inputs.directoriesInput) }}; |
73 | | - const directories = []; |
| 93 | + const changedFiles = ${{ toJSON(steps.changed-files.outputs.all_changed_and_modified_files) }}; |
74 | 94 |
|
75 | | - for (const directory of directoriesInput.split("\n")) { |
76 | | - let sanitizedDirectory = directory.trim(); |
77 | | - if (sanitizedDirectory === "") { |
78 | | - continue; |
79 | | - } |
| 95 | + let actionFiles = []; |
| 96 | + if (changedFiles !== null) { |
| 97 | + actionFiles = changedFiles.split(" "); |
| 98 | + } else { |
| 99 | + const actionFilesInput = ${{ toJson(inputs.action-files) }}; |
| 100 | +
|
| 101 | + for (const actionFile of actionFilesInput.split("\n")) { |
| 102 | + let sanitizedActionFile = actionFile.trim(); |
| 103 | + if (sanitizedActionFile === "") { |
| 104 | + continue; |
| 105 | + } |
80 | 106 |
|
81 | | - if (path.isAbsolute(sanitizedDirectory)) { |
82 | | - // Ensure directory is within the workspace |
83 | | - if (!sanitizedDirectory.startsWith(process.env.GITHUB_WORKSPACE)) { |
84 | | - return core.setFailed(`Directory is not within the workspace: ${sanitizedDirectory}`); |
| 107 | + if (path.isAbsolute(sanitizedActionFile)) { |
| 108 | + // Ensure actionFile is within the workspace |
| 109 | + if (!sanitizedActionFile.startsWith(process.env.GITHUB_WORKSPACE)) { |
| 110 | + return core.setFailed(`Action file / directory is not within the workspace: ${sanitizedActionFile}`); |
| 111 | + } |
| 112 | + } else { |
| 113 | + sanitizedActionFile = path.join(process.env.GITHUB_WORKSPACE, sanitizedActionFile); |
85 | 114 | } |
86 | | - } else { |
87 | | - sanitizedDirectory = path.join(process.env.GITHUB_WORKSPACE, sanitizedDirectory); |
| 115 | + actionFiles.push(sanitizedActionFile); |
88 | 116 | } |
89 | 117 |
|
90 | | - // Ensure directory exists |
91 | | - if (!fs.existsSync(sanitizedDirectory)) { |
92 | | - core.setFailed(`Directory does not exist: ${sanitizedDirectory}`); |
| 118 | + if (actionFiles.length === 0) { |
| 119 | + return core.setFailed("No action files to lint."); |
93 | 120 | } |
94 | 121 |
|
95 | | - directories.push(sanitizedDirectory); |
96 | | - } |
| 122 | + async function getActionFiles(actionFile) { |
| 123 | + const globber = await glob.create(actionFile,{ matchactionFilesInput: false }); |
| 124 | + return await globber.glob(); |
| 125 | + } |
| 126 | +
|
| 127 | + actionFiles = (await Promise.all(actionFiles.map(getActionFiles))) |
| 128 | + .flat() |
| 129 | + .map((file) => path.relative(process.env.GITHUB_WORKSPACE, file)); |
97 | 130 |
|
98 | | - if (directories.length === 0) { |
99 | | - return core.setFailed("No directories to lint."); |
| 131 | + if (actionFiles.length === 0) { |
| 132 | + return core.setFailed("No action files to lint."); |
| 133 | + } |
100 | 134 | } |
101 | 135 |
|
102 | | - async function getActionFiles(directory){ |
103 | | - const globber = await glob.create(`${directory}/**/*.yml`,{ matchdirectoriesInput: false }); |
104 | | - const matchingFiles = await globber.glob(); |
| 136 | + const files = actionFiles.map((file) => path.relative(process.env.GITHUB_WORKSPACE, file)); |
105 | 137 |
|
106 | | - return matchingFiles.map(matchingFile => path.relative(process.env.GITHUB_WORKSPACE, matchingFile)); |
107 | | - } |
| 138 | + const filesOutput = [...new Set(files)].join(" ").trim(); |
108 | 139 |
|
109 | | - const files = await Promise.all(directories.map(getActionFiles)); |
110 | | - core.setOutput("files", files.flat().join(" ").trim()); |
| 140 | + core.setOutput("files", filesOutput); |
111 | 141 |
|
112 | | - - uses: "docker://ghcr.io/sethvargo/ratchet:0.10.2@sha256:78f70ed0c85830a78bd9eeb265f49aa375d71887e4245aebc5da9c641d76b245" # v0.10.2 |
| 142 | + - id: ratchet |
| 143 | + uses: "docker://ghcr.io/sethvargo/ratchet:0.10.2@sha256:78f70ed0c85830a78bd9eeb265f49aa375d71887e4245aebc5da9c641d76b245" # v0.10.2 |
113 | 144 | if: ${{ steps.get-files-to-lint.outputs.files }} |
114 | 145 | with: |
115 | 146 | args: "check ${{ steps.get-files-to-lint.outputs.files }}" |
| 147 | + |
| 148 | + - if: ${{ failure() && steps.get-files-to-lint.outputs.files }} |
| 149 | + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 |
| 150 | + with: |
| 151 | + script: | |
| 152 | + const ratchetOutcome = ${{ toJSON(steps.ratchet.outcome) }}; |
| 153 | +
|
| 154 | + if (ratchetOutcome !== "failure") { |
| 155 | + return; |
| 156 | + } |
| 157 | +
|
| 158 | + // FIXME: should retrieve ratchet error output. See https://github.com/sethvargo/ratchet/issues/103. |
| 159 | + const ratchetErrorsOutputs = ${{ toJSON(steps.ratchet.outputs.errors) }}; |
| 160 | + if (!ratchetErrorsOutputs || ratchetErrorsOutputs.length === 0) { |
| 161 | + return; |
| 162 | + } |
| 163 | +
|
| 164 | + // Format the ratchet errors: found x unpinned refs: ... |
| 165 | + const unpinnedRefsError = /found (\d+) unpinned refs: (.*)/g.exec(ratchetErrorsOutputs[0]); |
| 166 | + if (!unpinnedRefsError) { |
| 167 | + return; |
| 168 | + } |
| 169 | +
|
| 170 | + const unpinnedRefs = JSON.parse(unpinnedRefsError[2]); |
| 171 | +
|
| 172 | + async function getFileErrors(file) { |
| 173 | + const fileContent = await fs.promises.readFile(file, "utf8"); |
| 174 | +
|
| 175 | + const errors = []; |
| 176 | +
|
| 177 | + for(const unpinnedRef of unpinnedRefs) { |
| 178 | + if(fileContent.includes(unpinnedRef)) { |
| 179 | + errors.push(file); |
| 180 | + } |
| 181 | + } |
| 182 | +
|
| 183 | + return errors; |
| 184 | + } |
| 185 | +
|
| 186 | + const files = ${{ toJSON(steps.get-files-to-lint.outputs.files) }}.split(" "); |
| 187 | +
|
| 188 | + // Annotate file errors |
| 189 | + await Promise.all(files.map(async (file) => { |
| 190 | + const errors = await getFileErrors(file); |
| 191 | + if (errors.length === 0) { |
| 192 | + return; |
| 193 | + } |
| 194 | +
|
| 195 | + core.error( |
| 196 | + `Found ${errors.length} unpinned refs`, |
| 197 | + { |
| 198 | + title: `Unpinned refs: ${JSON.stringify(errors)}`, |
| 199 | + file: file |
| 200 | + } |
| 201 | + ); |
| 202 | + })); |
0 commit comments