|
| 1 | +import { addError, filterTokens } from 'markdownlint-rule-helpers' |
| 2 | + |
| 3 | +export const thirdPartyActionsReusable = { |
| 4 | + names: ['GHD054', 'third-party-actions-reusable'], |
| 5 | + description: 'Code examples with third-party actions must include disclaimer reusable', |
| 6 | + tags: ['actions', 'reusable', 'third-party'], |
| 7 | + function: (params, onError) => { |
| 8 | + // Find all code fence blocks |
| 9 | + filterTokens(params, 'fence', (token) => { |
| 10 | + // Only check YAML code blocks (GitHub Actions workflows) |
| 11 | + if (token.info !== 'yaml' && token.info !== 'yaml copy') return |
| 12 | + |
| 13 | + const codeContent = token.content |
| 14 | + const lineNumber = token.lineNumber |
| 15 | + |
| 16 | + // Find third-party actions in the code block |
| 17 | + const thirdPartyActions = findThirdPartyActions(codeContent) |
| 18 | + |
| 19 | + if (thirdPartyActions.length === 0) return |
| 20 | + |
| 21 | + // Check if the required disclaimer reusable is present before this code block or inside it |
| 22 | + const hasDisclaimer = checkForDisclaimer(params.lines, lineNumber, codeContent) |
| 23 | + |
| 24 | + if (!hasDisclaimer) { |
| 25 | + const actionList = thirdPartyActions.map((action) => `'${action}'`).join(', ') |
| 26 | + addError( |
| 27 | + onError, |
| 28 | + lineNumber, |
| 29 | + `Code examples with third-party actions must include the disclaimer reusable. Found third-party actions: ${actionList}. Add '{% data reusables.actions.actions-not-certified-by-github-comment %}' before or inside this code block.`, |
| 30 | + token.line, |
| 31 | + null, // No specific range within the line |
| 32 | + null, // No fix possible - requires manual addition of reusable |
| 33 | + ) |
| 34 | + } |
| 35 | + }) |
| 36 | + }, |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Find third-party actions in YAML content |
| 41 | + * Third-party actions are identified by the pattern: owner/action@version |
| 42 | + * where owner is not 'actions' or 'github' |
| 43 | + */ |
| 44 | +function findThirdPartyActions(yamlContent) { |
| 45 | + const thirdPartyActions = [] |
| 46 | + |
| 47 | + // Pattern to match 'uses: owner/action@version' where owner is not actions or github |
| 48 | + const actionPattern = /uses:\s+([^{\s]+\/[^@\s]+@[^\s]+)/g |
| 49 | + |
| 50 | + let match |
| 51 | + while ((match = actionPattern.exec(yamlContent)) !== null) { |
| 52 | + const actionRef = match[1] |
| 53 | + |
| 54 | + // Extract owner from action reference |
| 55 | + const parts = actionRef.split('/') |
| 56 | + if (parts.length >= 2) { |
| 57 | + const owner = parts[0] |
| 58 | + |
| 59 | + // Skip GitHub-owned actions (actions/* and github/*) |
| 60 | + if (owner !== 'actions' && owner !== 'github') { |
| 61 | + thirdPartyActions.push(actionRef) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return thirdPartyActions |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Check if the disclaimer reusable is present before the given line number or inside the code block |
| 71 | + * Looks backward from the code block and also inside the code block content |
| 72 | + */ |
| 73 | +function checkForDisclaimer(lines, codeBlockLineNumber, codeContent) { |
| 74 | + const disclaimerPattern = /{% data reusables\.actions\.actions-not-certified-by-github-comment %}/ |
| 75 | + |
| 76 | + // First, check inside the code block content |
| 77 | + if (disclaimerPattern.test(codeContent)) { |
| 78 | + return true |
| 79 | + } |
| 80 | + |
| 81 | + // Convert from 1-based line number to 0-based array index |
| 82 | + const codeBlockIndex = codeBlockLineNumber - 1 |
| 83 | + |
| 84 | + // Search backwards from the code block (up to 10 lines before) |
| 85 | + // This is reasonable since disclaimers are typically right before code blocks |
| 86 | + const searchStart = Math.max(0, codeBlockIndex - 10) |
| 87 | + |
| 88 | + for (let i = codeBlockIndex - 1; i >= searchStart; i--) { |
| 89 | + const line = lines[i] |
| 90 | + |
| 91 | + if (disclaimerPattern.test(line)) { |
| 92 | + return true |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return false |
| 97 | +} |
0 commit comments