microsoft/openvmm
Publicmirrored fromhttps://github.com/microsoft/openvmmAvailable
.github/workflows/unsafe-label.yml
115lines · modecode
| 1 | name: "Unsafe Check" |
| 2 | on: |
| 3 | - pull_request_target |
| 4 | |
| 5 | permissions: |
| 6 | contents: read |
| 7 | pull-requests: write |
| 8 | |
| 9 | jobs: |
| 10 | check-unsafe: |
| 11 | runs-on: ubuntu-latest |
| 12 | steps: |
| 13 | - name: Checkout code |
| 14 | uses: actions/checkout@v4 |
| 15 | with: |
| 16 | fetch-depth: 0 |
| 17 | ref: ${{ github.event.pull_request.head.sha }} |
| 18 | |
| 19 | - name: Check for unsafe code and manage labels |
| 20 | uses: actions/github-script@v7 |
| 21 | with: |
| 22 | script: | |
| 23 | const fs = require('fs'); |
| 24 | |
| 25 | // Get the list of changed files |
| 26 | // TODO: pagination if more than 100 files |
| 27 | const { data: files } = await github.rest.pulls.listFiles({ |
| 28 | owner: context.repo.owner, |
| 29 | repo: context.repo.repo, |
| 30 | pull_number: context.issue.number, |
| 31 | per_page: 100, |
| 32 | }); |
| 33 | |
| 34 | // Filter to just Rust files |
| 35 | const rustFiles = files.filter(file => file.filename.endsWith('.rs')); |
| 36 | |
| 37 | console.log(`Checking ${rustFiles.length} Rust files for unsafe code...`); |
| 38 | |
| 39 | let unsafeFound = false; |
| 40 | |
| 41 | // Check each Rust file for unsafety |
| 42 | for (const file of rustFiles) { |
| 43 | // Don't check deleted files |
| 44 | if (file.status === 'removed') { |
| 45 | continue; |
| 46 | } |
| 47 | |
| 48 | try { |
| 49 | const filePath = file.filename; |
| 50 | const content = fs.readFileSync(filePath, 'utf8'); |
| 51 | |
| 52 | // Look for "unsafe ", the space ensures we don't catch words like the "unsafe_code" lint |
| 53 | const unsafeRegex = /unsafe /; |
| 54 | if (unsafeRegex.test(content)) { |
| 55 | console.log(`Found unsafe code in: ${filePath}`); |
| 56 | unsafeFound = true; |
| 57 | } |
| 58 | } catch (error) { |
| 59 | console.log(`Could not read ${filePath}: ${error.message}`); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Manage the label |
| 64 | if (unsafeFound) { |
| 65 | console.log('Adding unsafe label...'); |
| 66 | await github.rest.issues.addLabels({ |
| 67 | issue_number: context.issue.number, |
| 68 | owner: context.repo.owner, |
| 69 | repo: context.repo.repo, |
| 70 | labels: ['unsafe'] |
| 71 | }); |
| 72 | |
| 73 | // Post a warning comment |
| 74 | const comment = `⚠️ **Unsafe Code Detected** |
| 75 | |
| 76 | This PR modifies files containing \`unsafe\` Rust code. Extra scrutiny is required during review. |
| 77 | |
| 78 | For more on why we check whole files, instead of just diffs, check out [the Rustonomicon](https://doc.rust-lang.org/nomicon/working-with-unsafe.html)`; |
| 79 | |
| 80 | // Check if we already posted this comment |
| 81 | const { data: comments } = await github.rest.issues.listComments({ |
| 82 | issue_number: context.issue.number, |
| 83 | owner: context.repo.owner, |
| 84 | repo: context.repo.repo, |
| 85 | }); |
| 86 | |
| 87 | const botComment = comments.find(c => |
| 88 | c.user.type === 'Bot' && c.body.includes('Unsafe Code Detected') |
| 89 | ); |
| 90 | |
| 91 | if (!botComment) { |
| 92 | console.log('Posting warning comment...'); |
| 93 | await github.rest.issues.createComment({ |
| 94 | issue_number: context.issue.number, |
| 95 | owner: context.repo.owner, |
| 96 | repo: context.repo.repo, |
| 97 | body: comment |
| 98 | }); |
| 99 | } else { |
| 100 | console.log('Warning comment already exists'); |
| 101 | } |
| 102 | } else { |
| 103 | console.log('No unsafe code found, removing label if present...'); |
| 104 | try { |
| 105 | await github.rest.issues.removeLabel({ |
| 106 | issue_number: context.issue.number, |
| 107 | owner: context.repo.owner, |
| 108 | repo: context.repo.repo, |
| 109 | name: 'unsafe' |
| 110 | }); |
| 111 | } catch (error) { |
| 112 | // Label might not exist, that's okay |
| 113 | console.log('Label does not exist or could not be removed:', error.message); |
| 114 | } |
| 115 | } |