|
| 1 | +name: Create Issues from Dependabot Alerts |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run daily at 7 AM UTC |
| 6 | + - cron: '0 7 * * *' |
| 7 | + workflow_dispatch: # Allow manual trigger for urgent situations |
| 8 | + |
| 9 | +permissions: |
| 10 | + issues: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + create-issues: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Create Issues from Dependabot Alerts |
| 17 | + uses: actions/github-script@v8 |
| 18 | + env: |
| 19 | + DEPENDABOT_TOKEN: ${{ secrets.DEPENDABOT_PAT }} |
| 20 | + with: |
| 21 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 22 | + script: | |
| 23 | + // Fetch Dependabot alerts using PAT via fetch API |
| 24 | + const alertsResponse = await fetch( |
| 25 | + `https://api.github.com/repos/${context.repo.owner}/${context.repo.repo}/dependabot/alerts?state=open&per_page=100`, |
| 26 | + { |
| 27 | + headers: { |
| 28 | + 'Authorization': `Bearer ${process.env.DEPENDABOT_TOKEN}`, |
| 29 | + 'Accept': 'application/vnd.github+json', |
| 30 | + 'X-GitHub-Api-Version': '2022-11-28' |
| 31 | + } |
| 32 | + } |
| 33 | + ); |
| 34 | +
|
| 35 | + if (!alertsResponse.ok) { |
| 36 | + throw new Error(`Failed to fetch Dependabot alerts: ${alertsResponse.status} ${alertsResponse.statusText}`); |
| 37 | + } |
| 38 | +
|
| 39 | + const alerts = await alertsResponse.json(); |
| 40 | + console.log(`Found ${alerts.length} open Dependabot alerts`); |
| 41 | +
|
| 42 | + if (alerts.length === 0) { |
| 43 | + console.log('No open alerts to process'); |
| 44 | + return; |
| 45 | + } |
| 46 | +
|
| 47 | + // Get existing issues to avoid duplicates |
| 48 | + const { data: existingIssues } = await github.rest.issues.listForRepo({ |
| 49 | + owner: context.repo.owner, |
| 50 | + repo: context.repo.repo, |
| 51 | + labels: 'Source: Vulnerability Scan', |
| 52 | + state: 'all', |
| 53 | + per_page: 100 |
| 54 | + }); |
| 55 | +
|
| 56 | + // Extract CVE/GHSA identifiers from existing issue titles |
| 57 | + const existingIdentifiers = existingIssues.map(issue => { |
| 58 | + const cveMatch = issue.title.match(/CVE-\d{4}-\d+/); |
| 59 | + const ghsaMatch = issue.title.match(/GHSA-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}/); |
| 60 | + return cveMatch ? cveMatch[0] : (ghsaMatch ? ghsaMatch[0] : null); |
| 61 | + }).filter(Boolean); |
| 62 | +
|
| 63 | + console.log(`Found ${existingIdentifiers.length} existing vulnerability issues`); |
| 64 | +
|
| 65 | + let createdCount = 0; |
| 66 | + let skippedCount = 0; |
| 67 | +
|
| 68 | + for (const alert of alerts) { |
| 69 | + const cve = alert.security_advisory?.cve_id; |
| 70 | + const ghsa = alert.security_advisory?.ghsa_id; |
| 71 | + const identifier = cve || (ghsa ? `GHSA-${ghsa}` : `ALERT-${alert.number}`); |
| 72 | + const severity = (alert.security_vulnerability?.severity || 'unknown').toUpperCase(); |
| 73 | +
|
| 74 | + // Skip if issue already exists for this vulnerability |
| 75 | + if (existingIdentifiers.includes(identifier) || existingIdentifiers.includes(cve) || existingIdentifiers.includes(ghsa)) { |
| 76 | + console.log(`Skipping ${identifier} - issue already exists`); |
| 77 | + skippedCount++; |
| 78 | + continue; |
| 79 | + } |
| 80 | +
|
| 81 | + // Determine SLA based on severity (matches company security policy) |
| 82 | + const slaMap = { |
| 83 | + 'CRITICAL': '30 days', |
| 84 | + 'HIGH': '30 days', |
| 85 | + 'MEDIUM': '60 days', |
| 86 | + 'LOW': '90 days' |
| 87 | + }; |
| 88 | + const sla = slaMap[severity] || '90 days'; |
| 89 | +
|
| 90 | + // Map severity to label format |
| 91 | + const severityLabelMap = { |
| 92 | + 'CRITICAL': 'Severity: Critical', |
| 93 | + 'HIGH': 'Severity: High', |
| 94 | + 'MEDIUM': 'Severity: Medium', |
| 95 | + 'LOW': 'Severity: Low' |
| 96 | + }; |
| 97 | + const severityLabel = severityLabelMap[severity] || 'Severity: Low'; |
| 98 | +
|
| 99 | + // Format the issue body with all vulnerability details |
| 100 | + const packageName = alert.security_vulnerability?.package?.name || 'Unknown package'; |
| 101 | + const ecosystem = alert.security_vulnerability?.package?.ecosystem || 'Unknown'; |
| 102 | + const vulnerableRange = alert.security_vulnerability?.vulnerable_version_range || 'Unknown'; |
| 103 | + const patchedVersion = alert.security_vulnerability?.first_patched_version?.identifier || 'No patch available'; |
| 104 | + const summary = alert.security_advisory?.summary || 'No summary available'; |
| 105 | + const description = alert.security_advisory?.description || 'No description available'; |
| 106 | + const manifestPath = alert.dependency?.manifest_path || 'Unknown'; |
| 107 | + const alertUrl = alert.html_url; |
| 108 | + const cvssScore = alert.security_advisory?.cvss?.score || 'N/A'; |
| 109 | + const cvssVector = alert.security_advisory?.cvss?.vector_string || 'N/A'; |
| 110 | +
|
| 111 | + // Build references list |
| 112 | + const references = (alert.security_advisory?.references || []) |
| 113 | + .map(ref => `- ${ref.url}`) |
| 114 | + .join('\n') || 'No references available'; |
| 115 | +
|
| 116 | + const issueBody = `## Vulnerability Details |
| 117 | +
|
| 118 | + | Field | Value | |
| 119 | + |-------|-------| |
| 120 | + | **Identifier** | ${identifier} | |
| 121 | + | **Severity** | ${severity} | |
| 122 | + | **CVSS Score** | ${cvssScore} | |
| 123 | + | **SLA** | ${sla} | |
| 124 | + | **Package** | ${packageName} (${ecosystem}) | |
| 125 | + | **Vulnerable Version** | ${vulnerableRange} | |
| 126 | + | **Patched Version** | ${patchedVersion} | |
| 127 | + | **Manifest File** | \`${manifestPath}\` | |
| 128 | +
|
| 129 | + ## Summary |
| 130 | +
|
| 131 | + ${summary} |
| 132 | +
|
| 133 | + ## Description |
| 134 | +
|
| 135 | + ${description} |
| 136 | +
|
| 137 | + ## CVSS Vector |
| 138 | +
|
| 139 | + \`${cvssVector}\` |
| 140 | +
|
| 141 | + ## References |
| 142 | +
|
| 143 | + ${references} |
| 144 | +
|
| 145 | + ## Remediation |
| 146 | +
|
| 147 | + Update \`${packageName}\` to version \`${patchedVersion}\` or later. |
| 148 | +
|
| 149 | + ## Links |
| 150 | +
|
| 151 | + - [View Dependabot Alert](${alertUrl}) |
| 152 | +
|
| 153 | + --- |
| 154 | +
|
| 155 | + *Auto-generated from Dependabot alert on ${new Date().toISOString().split('T')[0]}* |
| 156 | + *This issue will sync to Linear for triage and remediation tracking.*`; |
| 157 | +
|
| 158 | + // Create the issue |
| 159 | + const issueTitle = `[${severity}] ${identifier}: ${summary.substring(0, 80)}`; |
| 160 | +
|
| 161 | + try { |
| 162 | + await github.rest.issues.create({ |
| 163 | + owner: context.repo.owner, |
| 164 | + repo: context.repo.repo, |
| 165 | + title: issueTitle, |
| 166 | + body: issueBody, |
| 167 | + labels: ['Source: Vulnerability Scan', 'Flag: security', severityLabel] |
| 168 | + }); |
| 169 | +
|
| 170 | + console.log(`Created issue for ${identifier}: ${packageName}`); |
| 171 | + createdCount++; |
| 172 | +
|
| 173 | + // Add to tracking to prevent duplicates within same run |
| 174 | + existingIdentifiers.push(identifier); |
| 175 | + } catch (error) { |
| 176 | + console.error(`Failed to create issue for ${identifier}: ${error.message}`); |
| 177 | + } |
| 178 | + } |
| 179 | +
|
| 180 | + console.log(`\nSummary: Created ${createdCount} issues, skipped ${skippedCount} (already exist)`); |
0 commit comments