|
1 | 1 | #!/usr/bin/env node |
2 | 2 |
|
3 | | -const github = require('../lib/github') |
| 3 | +const { getContents } = require('../lib/git-utils') |
4 | 4 | const fs = require('fs') |
5 | 5 | const path = require('path') |
6 | | -const filename = path.join(__dirname, '../lib/enterprise-dates.json') |
7 | | -const jsonFile = require(filename) |
| 6 | +const enterpriseDatesFile = path.join(__dirname, '../lib/enterprise-dates.json') |
| 7 | +const enterpriseDatesString = fs.readFileSync(enterpriseDatesFile, 'utf8') |
8 | 8 |
|
9 | 9 | // [start-readme] |
10 | 10 | // |
11 | | -// Run this script during Enterprise releases and deprecations. |
12 | | -// It uses the GitHub API to get dates from enterprise-releases and updates `lib/enterprise-dates.json`. |
13 | | -// The help site uses this JSON to display dates at the top of some Enterprise versions. |
14 | | -// |
15 | | -// This script requires that you have a GitHub Personal Access Token in a `.env` file. |
16 | | -// If you don't have a token, get one [here](https://github.com/settings/tokens/new?scopes=repo&description=docs-dev). |
17 | | -// If you don't have an `.env` file in your docs checkout, run this command in Terminal: |
18 | | -// |
19 | | -// `cp .env.example .env` |
20 | | -// |
21 | | -// Open the `.env` file in a text editor, and find the `GITHUB_TOKEN=` placeholder. Add your token after the equals sign. |
22 | | -// |
23 | | -// Do not commit the `.env` file; just leave it in your checkout. |
| 11 | +// This script fetches data from https://github.com/github/enterprise-releases/blob/master/releases.json |
| 12 | +// and updates `lib/enterprise-dates.json`, which the site uses for various functionality. |
24 | 13 | // |
25 | 14 | // [end-readme] |
26 | 15 |
|
| 16 | +// check for required PAT |
| 17 | +if (!process.env.GITHUB_TOKEN) { |
| 18 | + console.error('Error! You must have a GITHUB_TOKEN set in an .env file to run this script.') |
| 19 | + process.exit(1) |
| 20 | +} |
| 21 | + |
27 | 22 | main() |
28 | 23 |
|
29 | | -// GHE Release Lifecycle Dates |
30 | 24 | async function main () { |
31 | | - let raw |
32 | | - try { |
33 | | - raw = await getDataFromReleasesRepo() |
34 | | - } catch (err) { |
35 | | - console.log('error getting JSON from enterprise-releases repo') |
36 | | - throw (err) |
37 | | - } |
38 | | - const json = prepareData(raw) |
39 | | - if (json === prettify(jsonFile)) { |
40 | | - console.log('This repo is already in sync with enterprise-releases!') |
41 | | - } else { |
42 | | - fs.writeFileSync(filename, json, 'utf8') |
43 | | - console.log(`${filename} has been updated!`) |
44 | | - } |
45 | | -} |
| 25 | + // send owner, repo, ref, path |
| 26 | + const rawDates = JSON.parse(await getContents('github', 'enterprise-releases', 'master', 'releases.json')) |
46 | 27 |
|
47 | | -// Uses https://octokit.github.io/rest.js/#api-Repos-getContents |
48 | | -async function getDataFromReleasesRepo () { |
49 | | - const octokit = github() |
50 | | - const { data } = await octokit.repos.getContents({ |
51 | | - owner: 'github', |
52 | | - repo: 'enterprise-releases', |
53 | | - path: 'releases.json', |
54 | | - ref: 'master', |
55 | | - headers: { accept: 'application/vnd.github.v3.raw+json' } |
| 28 | + const formattedDates = {} |
| 29 | + Object.entries(rawDates).forEach(([releaseNumber, releaseObject]) => { |
| 30 | + formattedDates[releaseNumber] = { |
| 31 | + releaseDate: releaseObject.release_candidate || releaseObject.start, |
| 32 | + deprecationDate: releaseObject.end |
| 33 | + } |
56 | 34 | }) |
57 | | - return data |
58 | | -} |
59 | 35 |
|
60 | | -// We only need some of the values from the source JSON |
61 | | -// We use https://github.com/zeke/browser-date-formatter on the client side to reformat the dates |
62 | | -function prepareData (raw) { |
63 | | - const data = Object.entries(JSON.parse(raw)) |
64 | | - const obj = {} |
65 | | - data.forEach(versions => { |
66 | | - const datesObj = {} |
67 | | - const version = versions[0] |
68 | | - const releaseDate = versions[1].start |
69 | | - const deprecationDate = versions[1].end |
70 | | - datesObj.releaseDate = releaseDate |
71 | | - datesObj.deprecationDate = deprecationDate |
72 | | - obj[version] = datesObj |
73 | | - }) |
74 | | - return prettify(obj) |
75 | | -} |
| 36 | + const formattedDatesString = JSON.stringify(formattedDates, null, 2) |
76 | 37 |
|
77 | | -function prettify (json) { |
78 | | - return JSON.stringify(json, null, 2) |
| 38 | + if (formattedDatesString === enterpriseDatesString) { |
| 39 | + console.log('This repo is already in sync with enterprise-releases!') |
| 40 | + } else { |
| 41 | + fs.writeFileSync(enterpriseDatesFile, formattedDatesString) |
| 42 | + console.log(`${enterpriseDatesFile} has been updated!`) |
| 43 | + } |
79 | 44 | } |
0 commit comments