diff options
author | J10a1n15 <45315647+j10a1n15@users.noreply.github.com> | 2024-08-14 11:35:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-14 11:35:48 +0200 |
commit | 06894504573465b410758d0636238b3560bd7da8 (patch) | |
tree | 258bfdb9fbd1164a9bb9c4e17cb463bedc0cf346 | |
parent | 67dd38b9d76eab01f0359b6c9d627618111be07f (diff) | |
download | skyhanni-06894504573465b410758d0636238b3560bd7da8.tar.gz skyhanni-06894504573465b410758d0636238b3560bd7da8.tar.bz2 skyhanni-06894504573465b410758d0636238b3560bd7da8.zip |
Backend: Dependency Label Workflow (#2341)
-rw-r--r-- | .github/scripts/checkDependencies.mjs | 71 | ||||
-rw-r--r-- | .github/workflows/check_dependencies.yml | 26 |
2 files changed, 97 insertions, 0 deletions
diff --git a/.github/scripts/checkDependencies.mjs b/.github/scripts/checkDependencies.mjs new file mode 100644 index 000000000..2e351bbe6 --- /dev/null +++ b/.github/scripts/checkDependencies.mjs @@ -0,0 +1,71 @@ +import {Octokit} from "@octokit/rest"; +import fetch from "node-fetch"; + +const labelName = "Waiting on Dependency PR"; + +async function run() { + const octokit = new Octokit({ + auth: process.env.GITHUB_TOKEN, + request: { + fetch: fetch, + }, + }); + + const context = JSON.parse(process.env.GITHUB_CONTEXT); + + const pull_request = context.event.pull_request; + + const owner = context.repository_owner; + const name = context.repository.split("/")[1]; + + const prNumber = pull_request.number; + const prBody = pull_request.body || ""; + + const dependencyRegex = /## Dependencies/; + const match = prBody.match(dependencyRegex); + + if (match) { + const prLinks = prBody.match(/- https:\/\/github\.com\/[\w-]+\/[\w-]+\/pull\/\d+/g); + + if (prLinks && prLinks.length > 0) { + let hasOpenDependencies = false; + + for (const link of prLinks) { + const [, depOwner, depRepo, depNumber] = link.match(/github\.com\/([\w-]+)\/([\w-]+)\/pull\/(\d+)/); + const {data: dependencyPr} = await octokit.pulls.get({ + owner: depOwner, + repo: depRepo, + pull_number: depNumber, + }); + + if (dependencyPr.state === "open") { + hasOpenDependencies = true; + break; + } + } + + const labels = pull_request.labels.map(label => label.name); + + if (hasOpenDependencies && !labels.includes(labelName)) { + await octokit.issues.addLabels({ + owner, + repo: name, + issue_number: prNumber, + labels: [labelName], + }); + } else if (!hasOpenDependencies && labels.includes(labelName)) { + await octokit.issues.removeLabel({ + owner, + repo: name, + issue_number: prNumber, + name: labelName, + }); + } + } + } +} + +run().catch(error => { + console.error(error); + process.exit(1); +}); diff --git a/.github/workflows/check_dependencies.yml b/.github/workflows/check_dependencies.yml new file mode 100644 index 000000000..5f80c46f7 --- /dev/null +++ b/.github/workflows/check_dependencies.yml @@ -0,0 +1,26 @@ +name: Check PR Dependencies + +on: + pull_request_target: + types: [ opened, edited ] + +jobs: + check-dependencies: + runs-on: ubuntu-latest + steps: + - name: Check out the repository + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: '16' + + - name: Install dependencies + run: npm install @octokit/rest node-fetch + + - name: Run dependency check script + run: node .github/scripts/checkDependencies.mjs + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_CONTEXT: ${{ toJson(github) }} |