aboutsummaryrefslogtreecommitdiff
path: root/.github/scripts/checkDependencies.mjs
blob: 2e351bbe6daab1a12681a4dc395dc28e1b401b11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
});