|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs') |
| 4 | +const path = require('path') |
| 5 | +const github = require('@actions/github') |
| 6 | +const enterpriseDates = require('../../lib/enterprise-dates') |
| 7 | +const { latest, oldestSupported } = require('../../lib/enterprise-server-releases') |
| 8 | +const acceptedMilestones = ['release', 'deprecation'] |
| 9 | +const teamsToCC = '/cc @github/docs-content @github/docs-engineering' |
| 10 | + |
| 11 | +// Adjust these values as needed. |
| 12 | +const numberOfdaysBeforeReleaseToOpenIssue = 30 |
| 13 | +const numberOfdaysBeforeDeprecationToOpenIssue = 15 |
| 14 | + |
| 15 | +// [start-readme] |
| 16 | +// |
| 17 | +// This script runs once per day via a scheduled GitHub Action to check whether |
| 18 | +// an Enterprise release or deprecation milestone is within the specified |
| 19 | +// number of days. |
| 20 | +// |
| 21 | +// When a milestone is within the specified number of days, a new issue is |
| 22 | +// created using the templates in |
| 23 | +// .github/actions-scripts/enterprise-server-issue-templates. |
| 24 | +// |
| 25 | +// Release issues are then added to the docs content squad board for triage. |
| 26 | +// Deprecations issues are owned by docs engineering and are added to the |
| 27 | +// docs engineering squad board automatically when the engineering label is added. |
| 28 | +// |
| 29 | +// [end-readme] |
| 30 | + |
| 31 | +run() |
| 32 | + |
| 33 | +async function run () { |
| 34 | + |
| 35 | + |
| 36 | + const milestone = process.argv[2] |
| 37 | + if (!acceptedMilestones.includes(milestone)) { |
| 38 | + console.log('Please specify either \'release\' or \'deprecation\'\n') |
| 39 | + console.log('Example: script/open-enterprise-issue.js release') |
| 40 | + process.exit(1) |
| 41 | + } |
| 42 | + |
| 43 | + // Milestone-dependent values. |
| 44 | + const numberOfdaysBeforeMilestoneToOpenIssue = milestone === 'release' |
| 45 | + ? numberOfdaysBeforeReleaseToOpenIssue |
| 46 | + : numberOfdaysBeforeDeprecationToOpenIssue |
| 47 | + |
| 48 | + const versionNumber = milestone === 'release' |
| 49 | + ? getNextVersionNumber() |
| 50 | + : oldestSupported |
| 51 | + |
| 52 | + if (!versionNumber) { |
| 53 | + console.log(`Could not find the next version number after ${latest} in enterprise-dates.json. Try running script/udpate-enterprise-dates.js, then rerun this script.`) |
| 54 | + process.exit(0) |
| 55 | + } |
| 56 | + |
| 57 | + const datesForVersion = enterpriseDates[versionNumber] |
| 58 | + |
| 59 | + if (!datesForVersion) { |
| 60 | + console.log(`Could not find ${versionNumber} in enterprise-dates.json. Try running script/udpate-enterprise-dates.js, then rerun this script.`) |
| 61 | + process.exit(0) |
| 62 | + } |
| 63 | + |
| 64 | + const nextMilestoneDate = datesForVersion[`${milestone}Date`] |
| 65 | + const daysUntilMilestone = calculateDaysUntilMilestone(nextMilestoneDate) |
| 66 | + |
| 67 | + // If the milestone is more than the specific days away, exit now. |
| 68 | + if (daysUntilMilestone > numberOfdaysBeforeMilestoneToOpenIssue) { |
| 69 | + console.log(`The ${versionNumber} ${milestone} is not until ${nextMilestoneDate}! An issue will be opened when it is ${numberOfdaysBeforeMilestoneToOpenIssue} days away.`) |
| 70 | + process.exit(0) |
| 71 | + } |
| 72 | + |
| 73 | + const milestoneSteps = fs.readFileSync(path.join(process.cwd(), `.github/actions-scripts/enterprise-server-issue-templates/${milestone}-issue.md`), 'utf8') |
| 74 | + const issueLabels = [`enterprise ${milestone}`, `engineering`] |
| 75 | + const issueTitle = `[${nextMilestoneDate}] Enterprise Server ${versionNumber} ${milestone} (technical steps)` |
| 76 | + |
| 77 | + const issueBody = `GHES ${versionNumber} ${milestone} occurs on ${nextMilestoneDate}. |
| 78 | + \n${milestoneSteps} |
| 79 | + ${teamsToCC}` |
| 80 | + |
| 81 | + const token = process.env.GITHUB_TOKEN |
| 82 | + |
| 83 | + // Create the milestone issue |
| 84 | + const octokit = github.getOctokit(token) |
| 85 | + try { |
| 86 | + issue = await octokit.request('POST /repos/{owner}/{repo}/issues', { |
| 87 | + owner: 'github', |
| 88 | + repo: 'docs-internal', |
| 89 | + title: issueTitle, |
| 90 | + body: issueBody, |
| 91 | + labels: issueLabels |
| 92 | + }) |
| 93 | + if (issue.status === 201) { |
| 94 | + // Write the values to disk for use in the workflow. |
| 95 | + console.log(`Issue #${issue.data.number} for the ${versionNumber} ${milestone} was opened: ${issue.data.html_url}`) |
| 96 | + } |
| 97 | + } catch (error) { |
| 98 | + console.error(`#ERROR# ${error}`) |
| 99 | + console.log(`🛑 There was an error creating the issue.`) |
| 100 | + process.exit(1) |
| 101 | + } |
| 102 | + |
| 103 | + // Add the release issue to the 'Needs triage' column on the |
| 104 | + // docs content squad project board: |
| 105 | + // https://github.com/orgs/github/projects/1773#column-12198119 |
| 106 | + // Deprecation issues are owned by docs engineering only and will |
| 107 | + // be triaged by adding the engineering label to the issue. |
| 108 | + if (milestone === 'release') { |
| 109 | + try { |
| 110 | + const addCard = await octokit.request('POST /projects/columns/{column_id}/cards', { |
| 111 | + column_id: 12198119, |
| 112 | + content_id: issue.data.id, |
| 113 | + content_type: 'Issue', |
| 114 | + mediaType: { |
| 115 | + previews: [ |
| 116 | + 'inertia' |
| 117 | + ] |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + if (addCard.status === 201) { |
| 122 | + // Write the values to disk for use in the workflow. |
| 123 | + console.log(`The issue #${issue.data.number} was added to https://github.com/orgs/github/projects/1773#column-12198119.`) |
| 124 | + } |
| 125 | + } catch(error) { |
| 126 | + console.error(`#ERROR# ${error}`) |
| 127 | + console.log(`🛑 There was an error adding the issue to the project board.`) |
| 128 | + process.exit(1) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +function getNextVersionNumber () { |
| 134 | + const indexOfLatest = Object.keys(enterpriseDates).indexOf(latest) |
| 135 | + const indexOfNext = indexOfLatest + 1 |
| 136 | + return Object.keys(enterpriseDates)[indexOfNext] |
| 137 | +} |
| 138 | + |
| 139 | +function calculateDaysUntilMilestone (nextMilestoneDate) { |
| 140 | + const today = new Date().toISOString().slice(0, 10) |
| 141 | + const differenceInMilliseconds = getTime(nextMilestoneDate) - getTime(today) |
| 142 | + // Return the difference in days |
| 143 | + return Math.floor((differenceInMilliseconds) / (1000 * 60 * 60 * 24)) |
| 144 | +} |
| 145 | + |
| 146 | +function getTime (date) { |
| 147 | + return new Date(date).getTime() |
| 148 | +} |
0 commit comments