|
| 1 | +name: License Audit |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + paths: |
| 7 | + - "yarn.lock" |
| 8 | + - "package.json" |
| 9 | + - "packages/*/package.json" |
| 10 | + - "scripts/fetchLicenses.mjs" |
| 11 | + - "scripts/summarizeLicenses.mjs" |
| 12 | + - "scripts/npmLicenseMap.json" |
| 13 | + - "scripts/licenseAuditPrompt.txt" |
| 14 | + - "scripts/runLicenseAudit.sh" |
| 15 | + workflow_dispatch: |
| 16 | + |
| 17 | +jobs: |
| 18 | + license-audit: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + pull-requests: write |
| 23 | + id-token: write |
| 24 | + steps: |
| 25 | + - name: Checkout repository |
| 26 | + uses: actions/checkout@v4 |
| 27 | + with: |
| 28 | + fetch-depth: 1 |
| 29 | + |
| 30 | + - name: Setup Node.js |
| 31 | + uses: actions/setup-node@v4 |
| 32 | + with: |
| 33 | + node-version: "20" |
| 34 | + cache: "yarn" |
| 35 | + |
| 36 | + - name: Install dependencies |
| 37 | + run: yarn install --frozen-lockfile |
| 38 | + |
| 39 | + - name: Fetch licenses |
| 40 | + run: node scripts/fetchLicenses.mjs |
| 41 | + |
| 42 | + - name: Summarize licenses |
| 43 | + run: node scripts/summarizeLicenses.mjs |
| 44 | + |
| 45 | + - name: Read audit prompt |
| 46 | + id: read-prompt |
| 47 | + run: | |
| 48 | + { |
| 49 | + echo 'PROMPT<<PROMPT_EOF' |
| 50 | + cat scripts/licenseAuditPrompt.txt |
| 51 | + echo 'PROMPT_EOF' |
| 52 | + } >> "$GITHUB_OUTPUT" |
| 53 | +
|
| 54 | + - name: Audit licenses with Claude |
| 55 | + uses: anthropics/claude-code-action@v1 |
| 56 | + with: |
| 57 | + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} |
| 58 | + claude_args: '--allowedTools "Bash,Read,Write,Glob,Grep,WebFetch"' |
| 59 | + prompt: ${{ steps.read-prompt.outputs.PROMPT }} |
| 60 | + |
| 61 | + - name: Validate audit result |
| 62 | + run: | |
| 63 | + if [ ! -f license-audit-result.json ]; then |
| 64 | + echo "::error::license-audit-result.json was not created by the audit step" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +
|
| 68 | + STATUS=$(node -e "const r = require('./license-audit-result.json'); console.log(r.status)") |
| 69 | + UNRESOLVED=$(node -e "const r = require('./license-audit-result.json'); console.log(r.summary.unresolvedCount)") |
| 70 | + STRONG=$(node -e "const r = require('./license-audit-result.json'); console.log(r.summary.strongCopyleftCount)") |
| 71 | + WEAK=$(node -e "const r = require('./license-audit-result.json'); console.log(r.summary.weakCopyleftCount)") |
| 72 | + RESOLVED=$(node -e "const r = require('./license-audit-result.json'); console.log(r.summary.resolvedCount)") |
| 73 | +
|
| 74 | + echo "## License Audit Result: $STATUS" |
| 75 | + echo "" |
| 76 | + echo "- Resolved: $RESOLVED" |
| 77 | + echo "- Unresolved: $UNRESOLVED" |
| 78 | + echo "- Strong copyleft: $STRONG" |
| 79 | + echo "- Weak copyleft: $WEAK" |
| 80 | +
|
| 81 | + if [ "$STATUS" = "FAIL" ]; then |
| 82 | + echo "" |
| 83 | + echo "::error::License audit failed. See details below:" |
| 84 | + node -e "const r = require('./license-audit-result.json'); r.failReasons.forEach(r => console.log(' - ' + r))" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +
|
| 88 | + - name: Comment on PR |
| 89 | + if: always() && github.event_name == 'pull_request' |
| 90 | + uses: actions/github-script@v7 |
| 91 | + with: |
| 92 | + script: | |
| 93 | + const fs = require('fs'); |
| 94 | + const resultPath = 'license-audit-result.json'; |
| 95 | +
|
| 96 | + // Determine if we should comment |
| 97 | + let shouldComment = false; |
| 98 | + let body = ''; |
| 99 | +
|
| 100 | + if (!fs.existsSync(resultPath)) { |
| 101 | + shouldComment = true; |
| 102 | + body = `## License Audit\n\n:x: Audit failed to produce results. Check the workflow logs for details.`; |
| 103 | + } else { |
| 104 | + const result = JSON.parse(fs.readFileSync(resultPath, 'utf-8')); |
| 105 | + const hasWeakCopyleft = result.summary.weakCopyleftCount > 0; |
| 106 | + const isFail = result.status === 'FAIL'; |
| 107 | +
|
| 108 | + if (!isFail && !hasWeakCopyleft) { |
| 109 | + return; |
| 110 | + } |
| 111 | +
|
| 112 | + shouldComment = true; |
| 113 | + const icon = isFail ? ':x:' : ':warning:'; |
| 114 | +
|
| 115 | + body = `## License Audit\n\n`; |
| 116 | + body += `${icon} **Status: ${result.status}**\n\n`; |
| 117 | + body += `| Metric | Count |\n|---|---|\n`; |
| 118 | + body += `| Total packages | ${result.summary.totalPackages} |\n`; |
| 119 | + body += `| Resolved (non-standard) | ${result.summary.resolvedCount} |\n`; |
| 120 | + body += `| Unresolved | ${result.summary.unresolvedCount} |\n`; |
| 121 | + body += `| Strong copyleft | ${result.summary.strongCopyleftCount} |\n`; |
| 122 | + body += `| Weak copyleft | ${result.summary.weakCopyleftCount} |\n`; |
| 123 | +
|
| 124 | + if (result.failReasons && result.failReasons.length > 0) { |
| 125 | + body += `\n### Fail Reasons\n\n`; |
| 126 | + for (const reason of result.failReasons) { |
| 127 | + body += `- ${reason}\n`; |
| 128 | + } |
| 129 | + } |
| 130 | +
|
| 131 | + if (result.unresolved && result.unresolved.length > 0) { |
| 132 | + body += `\n### Unresolved Packages\n\n`; |
| 133 | + body += `| Package | Version | License | Reason |\n|---|---|---|---|\n`; |
| 134 | + for (const pkg of result.unresolved) { |
| 135 | + body += `| ${pkg.name} | ${pkg.version} | \`${pkg.license}\` | ${pkg.reason} |\n`; |
| 136 | + } |
| 137 | + } |
| 138 | +
|
| 139 | + if (result.copyleft && result.copyleft.strong && result.copyleft.strong.length > 0) { |
| 140 | + body += `\n### Strong Copyleft Packages\n\n`; |
| 141 | + body += `| Package | Version | License |\n|---|---|---|\n`; |
| 142 | + for (const pkg of result.copyleft.strong) { |
| 143 | + body += `| ${pkg.name} | ${pkg.version} | \`${pkg.license}\` |\n`; |
| 144 | + } |
| 145 | + } |
| 146 | +
|
| 147 | + if (result.copyleft && result.copyleft.weak && result.copyleft.weak.length > 0) { |
| 148 | + body += `\n### Weak Copyleft Packages (informational)\n\n`; |
| 149 | + body += `| Package | Version | License |\n|---|---|---|\n`; |
| 150 | + for (const pkg of result.copyleft.weak) { |
| 151 | + body += `| ${pkg.name} | ${pkg.version} | \`${pkg.license}\` |\n`; |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + if (result.resolved && result.resolved.length > 0) { |
| 156 | + body += `\n<details><summary>Resolved Packages (${result.resolved.length})</summary>\n\n`; |
| 157 | + body += `| Package | Version | Original | Resolved | Source |\n|---|---|---|---|---|\n`; |
| 158 | + for (const pkg of result.resolved) { |
| 159 | + body += `| ${pkg.name} | ${pkg.version} | \`${pkg.originalLicense}\` | \`${pkg.resolvedLicense}\` | ${pkg.source} |\n`; |
| 160 | + } |
| 161 | + body += `\n</details>\n`; |
| 162 | + } |
| 163 | + } |
| 164 | +
|
| 165 | + if (!shouldComment) return; |
| 166 | +
|
| 167 | + const comments = await github.rest.issues.listComments({ |
| 168 | + owner: context.repo.owner, |
| 169 | + repo: context.repo.repo, |
| 170 | + issue_number: context.issue.number, |
| 171 | + }); |
| 172 | + const existing = comments.data.find(c => c.body.startsWith('## License Audit')); |
| 173 | + if (existing) { |
| 174 | + await github.rest.issues.updateComment({ |
| 175 | + owner: context.repo.owner, |
| 176 | + repo: context.repo.repo, |
| 177 | + comment_id: existing.id, |
| 178 | + body, |
| 179 | + }); |
| 180 | + } else { |
| 181 | + await github.rest.issues.createComment({ |
| 182 | + owner: context.repo.owner, |
| 183 | + repo: context.repo.repo, |
| 184 | + issue_number: context.issue.number, |
| 185 | + body, |
| 186 | + }); |
| 187 | + } |
| 188 | +
|
| 189 | + - name: Upload artifacts |
| 190 | + if: always() |
| 191 | + uses: actions/upload-artifact@v4 |
| 192 | + with: |
| 193 | + name: license-audit |
| 194 | + path: | |
| 195 | + oss-licenses.json |
| 196 | + oss-license-summary.json |
| 197 | + license-audit-result.json |
0 commit comments