Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: gha-workflow-validator action reference validation bug #758

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/sweet-pens-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"gha-workflow-validator": patch
---

fix: action reference validation bug producing false positives for lines which
contain "uses:" substring, but is not an action reference
8 changes: 5 additions & 3 deletions actions/gha-workflow-validator/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24174,9 +24174,11 @@ function extractActionReferenceFromLine(line) {
if (trimmedLine.startsWith("#")) {
return;
}
const trimSubString = "uses:";
const usesIndex = trimmedLine.indexOf(trimSubString);
if (usesIndex === -1) {
const possibleTrimmedPrefixes = ["- uses: ", "uses: "];
const trimSubString = possibleTrimmedPrefixes.find(
(prefix) => trimmedLine.startsWith(prefix)
);
if (!trimSubString) {
return;
}
const trimmedUses = line.substring(line.indexOf(trimSubString) + trimSubString.length).trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ describe(ActionReferenceValidation.name, () => {
expect(result).toEqual({ filename: "foo.yml", lineValidations: [] });
});

it("should validate no action references", async () => {
it("should validate no action references (statuses:write) ", async () => {
const octokit = getTestOctokit(nockBack.currentMode);
const subject = new ActionReferenceValidation(octokit);
const noWorkflowChanges: ParsedFile = {
filename: ".github/workflows/test.yml",
lines: [
{ lineNumber: 1, content: "line 1", operation: "add", ignored: false },
{
lineNumber: 1,
content: " statuses: write",
operation: "add",
ignored: false,
},
{ lineNumber: 2, content: "line 2", operation: "add", ignored: false },
],
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,16 @@ export function extractActionReferenceFromLine(
return;
}

// example line:
// example line (after trimming):
// - uses: actions/[email protected]
const trimSubString = "uses:";
const usesIndex = trimmedLine.indexOf(trimSubString);
// or
// uses: actions/[email protected]
const possibleTrimmedPrefixes = ["- uses: ", "uses: "];
const trimSubString = possibleTrimmedPrefixes.find((prefix) =>
trimmedLine.startsWith(prefix),
);

if (usesIndex === -1) {
if (!trimSubString) {
// Not an action reference
return;
}
Expand Down
Loading