|
| 1 | +const axios = require('axios'); |
| 2 | + |
| 3 | +const githubToken = process.env.GITHUB_TOKEN; |
| 4 | +const { GITHUB_REPOSITORY, GITHUB_PR_NUMBER } = process.env; |
| 5 | + |
| 6 | +const [owner, repo] = GITHUB_REPOSITORY.split('/'); |
| 7 | + |
| 8 | +async function getPRDetails() { |
| 9 | + const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${GITHUB_PR_NUMBER}`; |
| 10 | + const response = await axios.get(url, { |
| 11 | + headers: { |
| 12 | + Authorization: `token ${githubToken}` |
| 13 | + } |
| 14 | + }); |
| 15 | + return response.data; |
| 16 | +} |
| 17 | + |
| 18 | +async function getIssueDetails(issueNumber) { |
| 19 | + const url = `https://api.github.com/repos/${owner}/${repo}/issues/${issueNumber}`; |
| 20 | + const response = await axios.get(url, { |
| 21 | + headers: { |
| 22 | + Authorization: `token ${githubToken}` |
| 23 | + } |
| 24 | + }); |
| 25 | + return response.data; |
| 26 | +} |
| 27 | + |
| 28 | +async function run() { |
| 29 | + try { |
| 30 | + const pr = await getPRDetails(); |
| 31 | + const { labels: prLabels, milestone: prMilestone, body: prBody } = pr; |
| 32 | + |
| 33 | + if (prLabels.length === 0) { |
| 34 | + throw new Error('The PR has no labels.'); |
| 35 | + } |
| 36 | + if (!prMilestone) { |
| 37 | + throw new Error('The PR has no milestone.'); |
| 38 | + } |
| 39 | + |
| 40 | + const issueNumberMatches = prBody.match(/#(\d+)/g); |
| 41 | + |
| 42 | + if (!issueNumberMatches) { |
| 43 | + console.log('No associated issues found in PR description.'); |
| 44 | + } else { |
| 45 | + for (const match of issueNumberMatches) { |
| 46 | + const issueNumber = match.replace('#', ''); |
| 47 | + const issue = await getIssueDetails(issueNumber); |
| 48 | + const { labels: issueLabels, milestone: issueMilestone } = issue; |
| 49 | + |
| 50 | + if (issueLabels.length === 0) { |
| 51 | + throw new Error(`Associated issue #${issueNumber} has no labels.`); |
| 52 | + } |
| 53 | + if (!issueMilestone) { |
| 54 | + throw new Error(`Associated issue #${issueNumber} has no milestone.`); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + console.log('PR and all associated issues have labels and milestones.'); |
| 60 | + } catch (error) { |
| 61 | + console.error(error.message); |
| 62 | + process.exit(1); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +run(); |
0 commit comments