|
| 1 | +// [start-readme] |
| 2 | +// |
| 3 | +// This script adds and removes placeholder data files in the |
| 4 | +// automation pipelines data directories and |
| 5 | +// data/release-notes/enterprise-server directories. This script |
| 6 | +// uses the supported and deprecated versions to determine what |
| 7 | +// directories should exist. This script also modifies the `api-versions` |
| 8 | +// key if it exists in a pipeline's lib/config.json file. |
| 9 | +// |
| 10 | +// [end-readme] |
| 11 | + |
| 12 | +import { existsSync } from 'fs' |
| 13 | +import { readFile, readdir, writeFile, cp } from 'fs/promises' |
| 14 | +import { rimrafSync } from 'rimraf' |
| 15 | +import { difference, intersection } from 'lodash-es' |
| 16 | +import { mkdirp } from 'mkdirp' |
| 17 | + |
| 18 | +import { deprecated, supported } from '#src/versions/lib/enterprise-server-releases.js' |
| 19 | + |
| 20 | +const [currentReleaseNumber, previousReleaseNumber] = supported |
| 21 | +const pipelines = JSON.parse(await readFile('src/automated-pipelines/lib/config.json', 'utf-8'))[ |
| 22 | + 'automation-pipelines' |
| 23 | +] |
| 24 | + |
| 25 | +// If the config file for a pipeline includes `api-versions` update that list |
| 26 | +// based on the supported and deprecated releases. |
| 27 | +export async function updateAutomatedConfigFiles() { |
| 28 | + for (const pipeline of pipelines) { |
| 29 | + const configFilepath = `src/${pipeline}/lib/config.json` |
| 30 | + const configData = JSON.parse(await readFile(configFilepath, 'utf-8')) |
| 31 | + const apiVersions = configData['api-versions'] |
| 32 | + if (!apiVersions) continue |
| 33 | + for (const key of Object.keys(apiVersions)) { |
| 34 | + // Copy the previous release's calendar date versions to the new release |
| 35 | + if (key.endsWith(previousReleaseNumber)) { |
| 36 | + const newKey = key.replace(previousReleaseNumber, currentReleaseNumber) |
| 37 | + apiVersions[newKey] = apiVersions[key] |
| 38 | + } |
| 39 | + // Remove any deprecated versions |
| 40 | + for (const deprecatedRelease of deprecated) { |
| 41 | + if (key.endsWith(deprecatedRelease)) { |
| 42 | + delete apiVersions[key] |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + const newConfigData = Object.assign({}, configData) |
| 47 | + newConfigData['api-versions'] = apiVersions |
| 48 | + await writeFile(configFilepath, JSON.stringify(newConfigData, null, 2)) |
| 49 | + } |
| 50 | + await updateAutomatedPipelines() |
| 51 | +} |
| 52 | + |
| 53 | +export async function updateAutomatedPipelines() { |
| 54 | + // The allVersions object uses the 'api-versions' data stored in the |
| 55 | + // src/rest/lib/config.json file. We want to update 'api-versions' |
| 56 | + // before the allVersions object is created so we need to import it |
| 57 | + // after calling updateAutomatedConfigFiles. |
| 58 | + const { allVersions } = await import('#src/versions/lib/all-versions.js') |
| 59 | + |
| 60 | + // Gets all of the base names (e.g., ghes-) in the allVersions object |
| 61 | + // Currently, this is only ghes- but if we had more than one type of |
| 62 | + // numbered release it would get all of them. |
| 63 | + const numberedReleaseBaseNames = Array.from( |
| 64 | + new Set( |
| 65 | + Object.values(allVersions) |
| 66 | + .filter((version) => version.hasNumberedReleases) |
| 67 | + .map((version) => version.openApiBaseName), |
| 68 | + ), |
| 69 | + ) |
| 70 | + |
| 71 | + // A list of currently supported versions (calendar date inclusive) |
| 72 | + // in the format using the short name rather than full format |
| 73 | + // (e.g., enterprise-server@). The list is filtered |
| 74 | + // to only include versions that have numbered releases (e.g. ghes-). |
| 75 | + // The list is generated from the `apiVersions` key in allVersions. |
| 76 | + // This is currently only needed for the rest and github-apps pipelines. |
| 77 | + const versionNamesCalDate = Object.values(allVersions) |
| 78 | + .filter((version) => version.hasNumberedReleases) |
| 79 | + .map((version) => |
| 80 | + version.apiVersions.length |
| 81 | + ? version.apiVersions.map((apiVersion) => `${version.openApiVersionName}-${apiVersion}`) |
| 82 | + : version.openApiVersionName, |
| 83 | + ) |
| 84 | + .flat() |
| 85 | + // A list of currently supported versions in the format using the short name |
| 86 | + // rather than the full format (e.g., enterprise-server@). The list is filtered |
| 87 | + // to only include versions that have numbered releases (e.g. ghes-). |
| 88 | + // Currently, this is used for the graphql and webhooks pipelines. |
| 89 | + const versionNames = Object.values(allVersions) |
| 90 | + .filter((version) => version.hasNumberedReleases) |
| 91 | + .map((version) => version.openApiVersionName) |
| 92 | + |
| 93 | + for (const pipeline of pipelines) { |
| 94 | + if (!existsSync(`src/${pipeline}/data`)) continue |
| 95 | + const isCalendarDateVersioned = JSON.parse( |
| 96 | + await readFile(`src/${pipeline}/lib/config.json`, 'utf-8'), |
| 97 | + )['api-versions'] |
| 98 | + |
| 99 | + const directoryListing = await readdir(`src/${pipeline}/data`) |
| 100 | + // filter the directory list to only include directories that start with |
| 101 | + // basenames with numbered releases (e.g., ghes-). |
| 102 | + const existingDataDir = directoryListing.filter((directory) => |
| 103 | + numberedReleaseBaseNames.some((basename) => directory.startsWith(basename)), |
| 104 | + ) |
| 105 | + const expectedDirectory = isCalendarDateVersioned ? versionNamesCalDate : versionNames |
| 106 | + |
| 107 | + // Get a list of data directories to remove (deprecate) and remove them |
| 108 | + // This should only happen if a release is being deprecated. |
| 109 | + const removeFiles = difference(existingDataDir, expectedDirectory) |
| 110 | + for (const directory of removeFiles) { |
| 111 | + console.log(`Removing src/${pipeline}/data/${directory}`) |
| 112 | + rimrafSync(`src/${pipeline}/data/${directory}`) |
| 113 | + } |
| 114 | + |
| 115 | + // Get a list of data directories to create (release) and create them |
| 116 | + // This should only happen if a relase is being added. |
| 117 | + const addFiles = difference(expectedDirectory, existingDataDir) |
| 118 | + if (addFiles.length > numberedReleaseBaseNames.length) { |
| 119 | + throw new Error( |
| 120 | + 'Only one new release per numbered release version should be added at a time. Check that the lib/enterprise-server-releases.js is correct.', |
| 121 | + ) |
| 122 | + } |
| 123 | + |
| 124 | + for (const base of numberedReleaseBaseNames) { |
| 125 | + const dirToAdd = addFiles.find((item) => item.startsWith(base)) |
| 126 | + if (!dirToAdd) continue |
| 127 | + // The suppported array is ordered from most recent (index 0) to oldest |
| 128 | + // Index 1 will be the release prior to the most recent release |
| 129 | + const lastRelease = supported[1] |
| 130 | + const previousDirName = existingDataDir.filter((directory) => directory.includes(lastRelease)) |
| 131 | + |
| 132 | + console.log( |
| 133 | + `Copying src/${pipeline}/data/${previousDirName} to src/${pipeline}/data/${dirToAdd}`, |
| 134 | + ) |
| 135 | + await cp(`src/${pipeline}/data/${previousDirName}`, `src/${pipeline}/data/${dirToAdd}`, { |
| 136 | + recursive: true, |
| 137 | + }) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // Add and remove the GHES release note data. Once we create an automation |
| 142 | + // pipeline for release notes, we can remove this because it will use the |
| 143 | + // same directory structure as the other pipeline data directories. |
| 144 | + const ghesReleaseNotesDirs = await readdir('data/release-notes/enterprise-server') |
| 145 | + const supportedHyphenated = supported.map((version) => version.replace('.', '-')) |
| 146 | + const deprecatedHyphenated = deprecated.map((version) => version.replace('.', '-')) |
| 147 | + const addRelNoteDirs = difference(supportedHyphenated, ghesReleaseNotesDirs) |
| 148 | + const removeRelNoteDirs = intersection(deprecatedHyphenated, ghesReleaseNotesDirs) |
| 149 | + for (const directory of removeRelNoteDirs) { |
| 150 | + console.log(`Removing data/release-notes/enterprise-server/${directory}`) |
| 151 | + rimrafSync(`data/release-notes/enterprise-server/${directory}`) |
| 152 | + } |
| 153 | + for (const directory of addRelNoteDirs) { |
| 154 | + console.log(`Create new directory data/release-notes/enterprise-server/${directory}`) |
| 155 | + await mkdirp(`data/release-notes/enterprise-server/${directory}`) |
| 156 | + await cp( |
| 157 | + `data/release-notes/PLACEHOLDER-TEMPLATE.yml`, |
| 158 | + `data/release-notes/enterprise-server/${directory}/PLACEHOLDER.yml`, |
| 159 | + ) |
| 160 | + } |
| 161 | +} |
0 commit comments