1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/lib/release-notes-utils.js
Kevin Heis 42e785b0a8 Migrate CommonJS to ESM (#20301)
* First run of script

* Get the app running --- ish

* Get NextJS working

* Remove `node:`

* Get more tests passing in unit directory

* Update FailBot test to use nock

* Update test.yml

* Update Dockerfile

* tests/content fixes

* Update page.js

* Update build-changelog.js

* updating tests/routing

* Update orphan-tests.js

* updating tests/rendering

* Update .eslintrc.js

* Update .eslintrc.js

* Install jest/globals

* "linting" tests

* staging update to server.mjs

* Change '.github/allowed-actions.js' to a ESM export

* Lint

* Fixes for the main package.json

* Move Jest to be last in the npm test command so we can pass args

* Just use 'npm run lint' in the npm test command

* update algolia label script

* update openapi script

* update require on openapi

* Update enterprise-algolia-label.js

* forgot JSON.parse

* Update lunr-search-index.js

* Always explicitly include process.cwd() for JSON file reads pathed from project root

* update graphql/update-files.js script

* Update other npm scripts using jest to pass ESM NODE_OPTIONS

* Update check-for-enterprise-issues-by-label.js for ESM

* Update create-enterprise-issue.js for ESM

* Import jest global for browser tests

* Convert 'script/deploy' to ESM

Co-authored-by: Grace Park <gracepark@github.com>
Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-14 13:49:18 -07:00

105 lines
3.2 KiB
JavaScript

import semver from 'semver'
import renderContent from './render-content/index.js'
/**
* Turn { [key]: { notes, intro, date } }
* into [{ version, notes, intro, date }]
*/
export function sortPatchKeys (release, version, options = {}) {
const keys = Object.keys(release)
.map(key => {
const keyWithDots = key.replace(/-/g, '.')
return {
version: `${version}.${keyWithDots}`,
patchVersion: keyWithDots,
downloadVersion: `${version}.${keyWithDots.replace(/\.rc\d*$/, '')}`,
release: version, // TODO this naming :/ we are not currently using this value, but we may want to.
...release[key]
}
})
// Filter out any deprecated patches
.filter(key => !key.deprecated)
// Versions with numbered releases like GHES 2.22, 3.0, etc. need additional semver sorting;
// Versions with date releases need to be sorted by date.
return options.semverSort
? semverSort(keys)
: keys.sort((a, b) => new Date(b.date) - new Date(a.date))
}
export function semverSort (keys) {
return keys
.sort((a, b) => {
let aTemp = a.version
let bTemp = b.version
// There's an RC version here, so doing regular semver
// comparisons won't work. So, we'll convert the incompatible version
// strings to real semver strings, then compare.
const [aBase, aRc] = a.version.split('.rc')
if (aRc) aTemp = `${aBase}-rc.${aRc}`
const [bBase, bRc] = b.version.split('.rc')
if (bRc) bTemp = `${bBase}-rc.${bRc}`
if (semver.gt(aTemp, bTemp)) return -1
if (semver.lt(aTemp, bTemp)) return 1
return 0
})
}
/**
* Render each note in the given patch, by looping through the
* sections and rendering either `note` or `note.notes` in the
* case of a sub-section
*/
export async function renderPatchNotes (patch, ctx) {
// Run the notes through the markdown rendering pipeline
for (const key in patch.sections) {
await Promise.all(patch.sections[key].map(async (noteOrHeading, index) => {
patch.sections[key][index] = typeof noteOrHeading === 'string'
? await renderContent(noteOrHeading, ctx)
: {
...noteOrHeading,
notes: await Promise.all(noteOrHeading.notes.map(note => renderContent(note, ctx)))
}
}))
}
// Also render the patch's intro
if (patch.intro) {
patch.intro = await renderContent(patch.intro, ctx)
}
return patch
}
export function sortReleasesByDate (releaseNotes) {
return Object.keys(releaseNotes)
.map(release => {
const [year, month] = release.split('-')
return {
name: release,
date: new Date(`20${year}`, month - 1, '1')
}
})
.sort((releaseEntry1, releaseEntry2) => releaseEntry2.date - releaseEntry1.date)
.map(releaseEntry => releaseEntry.name)
}
export function getAllReleases (releases, releaseNotesPerPlan, hasNumberedReleases) {
return releases.map(version => {
const release = releaseNotesPerPlan[version.replace(/\./g, '-')]
if (!release) return { version }
const patches = sortPatchKeys(release, version, { semverSort: hasNumberedReleases })
return { version, patches }
})
}
export default {
sortReleasesByDate,
sortPatchKeys,
renderPatchNotes,
getAllReleases
}