|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import fs from 'fs'; |
| 17 | +import path from 'path'; |
| 18 | +import { format as prettierFormat } from 'prettier'; |
| 19 | + |
| 20 | +const workspacesDir = path.resolve('workspaces'); |
| 21 | +const presetsDir = path.resolve('.github', 'renovate-presets', 'workspace'); |
| 22 | +const renovateJsonPath = path.resolve('.github', 'renovate.json'); |
| 23 | + |
| 24 | +function listWorkspaceNames() { |
| 25 | + if (!fs.existsSync(workspacesDir)) return []; |
| 26 | + return fs |
| 27 | + .readdirSync(workspacesDir, { withFileTypes: true }) |
| 28 | + .filter(dirent => dirent.isDirectory()) |
| 29 | + .map(dirent => dirent.name) |
| 30 | + .filter(name => name !== 'noop') |
| 31 | + .sort(); |
| 32 | +} |
| 33 | + |
| 34 | +function isWorkspaceCovered(workspaceName) { |
| 35 | + const presetFile = path.join( |
| 36 | + presetsDir, |
| 37 | + `rhdh-${workspaceName}-presets.json`, |
| 38 | + ); |
| 39 | + return fs.existsSync(presetFile); |
| 40 | +} |
| 41 | + |
| 42 | +function toTitleCase(source) { |
| 43 | + return source |
| 44 | + .split(/[-_ ]+/) |
| 45 | + .filter(Boolean) |
| 46 | + .map(token => token.charAt(0).toLocaleUpperCase('en-US') + token.slice(1)) |
| 47 | + .join(' '); |
| 48 | +} |
| 49 | + |
| 50 | +async function ensureRenovateExtends(presetRef) { |
| 51 | + let content = fs.readFileSync(renovateJsonPath, 'utf8'); |
| 52 | + |
| 53 | + if (content.includes(`"${presetRef}"`)) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + const descIndex = content.indexOf('"all RHDH Plugins workspaces"'); |
| 58 | + if (descIndex === -1) { |
| 59 | + throw new Error('Could not find "all RHDH Plugins workspaces" rule'); |
| 60 | + } |
| 61 | + |
| 62 | + const extendsIndex = content.indexOf('"extends":', descIndex); |
| 63 | + const arrayStart = content.indexOf('[', extendsIndex); |
| 64 | + const arrayEnd = content.indexOf(']', arrayStart); |
| 65 | + |
| 66 | + if (extendsIndex === -1 || arrayStart === -1 || arrayEnd === -1) { |
| 67 | + throw new Error('Could not find extends array'); |
| 68 | + } |
| 69 | + |
| 70 | + const before = content.substring(0, arrayEnd).trimEnd(); |
| 71 | + const after = content.substring(arrayEnd); |
| 72 | + content = `${before},\n "${presetRef}"\n ${after}`; |
| 73 | + |
| 74 | + fs.writeFileSync(renovateJsonPath, content, 'utf8'); |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +if (!fs.existsSync(presetsDir)) fs.mkdirSync(presetsDir, { recursive: true }); |
| 79 | + |
| 80 | +function buildPresetJson(workspaceName) { |
| 81 | + const displayName = toTitleCase(workspaceName.replace(/^rhdh-/, '')); |
| 82 | + return { |
| 83 | + packageRules: [ |
| 84 | + { |
| 85 | + description: `all ${displayName} minor updates`, |
| 86 | + matchFileNames: [`workspaces/${workspaceName}/**`], |
| 87 | + extends: [ |
| 88 | + `github>redhat-developer/rhdh-plugins//.github/renovate-presets/base/rhdh-minor-presets(${displayName})`, |
| 89 | + ], |
| 90 | + addLabels: ['team/rhdh', `${workspaceName}`], |
| 91 | + }, |
| 92 | + { |
| 93 | + description: `all ${displayName} patch updates`, |
| 94 | + matchFileNames: [`workspaces/${workspaceName}/**`], |
| 95 | + extends: [ |
| 96 | + `github>redhat-developer/rhdh-plugins//.github/renovate-presets/base/rhdh-patch-presets(${displayName})`, |
| 97 | + ], |
| 98 | + addLabels: ['team/rhdh', `${workspaceName}`], |
| 99 | + }, |
| 100 | + { |
| 101 | + description: `all ${displayName} dev dependency updates`, |
| 102 | + matchFileNames: [`workspaces/${workspaceName}/**`], |
| 103 | + extends: [ |
| 104 | + `github>redhat-developer/rhdh-plugins//.github/renovate-presets/base/rhdh-devdependency-presets(${displayName})`, |
| 105 | + ], |
| 106 | + addLabels: ['team/rhdh', `${workspaceName}`], |
| 107 | + }, |
| 108 | + ], |
| 109 | + }; |
| 110 | +} |
| 111 | + |
| 112 | +function findUncoveredWorkspaces() { |
| 113 | + return listWorkspaceNames().filter(ws => !isWorkspaceCovered(ws)); |
| 114 | +} |
| 115 | + |
| 116 | +const argv = process.argv.slice(2); |
| 117 | +switch (argv[0]) { |
| 118 | + case '--apply': { |
| 119 | + const ws = argv[1]; |
| 120 | + if (!ws) { |
| 121 | + console.error('ERROR: --apply requires a workspace name'); |
| 122 | + process.exit(2); |
| 123 | + } |
| 124 | + if (isWorkspaceCovered(ws)) { |
| 125 | + console.log(JSON.stringify({ workspace: ws, skipped: true })); |
| 126 | + process.exit(0); |
| 127 | + } |
| 128 | + |
| 129 | + const presetFilePath = path.join(presetsDir, `rhdh-${ws}-presets.json`); |
| 130 | + const presetJson = buildPresetJson(ws); |
| 131 | + const formattedPreset = await prettierFormat(JSON.stringify(presetJson), { |
| 132 | + parser: 'json', |
| 133 | + }); |
| 134 | + fs.writeFileSync(presetFilePath, formattedPreset, 'utf8'); |
| 135 | + const presetRef = `github>redhat-developer/rhdh-plugins//.github/renovate-presets/workspace/rhdh-${ws}-presets`; |
| 136 | + await ensureRenovateExtends(presetRef); |
| 137 | + if (process.env.GITHUB_OUTPUT) { |
| 138 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `workspace=${ws}\n`); |
| 139 | + } |
| 140 | + console.log(JSON.stringify({ workspace: ws })); |
| 141 | + break; |
| 142 | + } |
| 143 | + case '--list': { |
| 144 | + const queue = findUncoveredWorkspaces(); |
| 145 | + console.log(JSON.stringify(queue)); |
| 146 | + break; |
| 147 | + } |
| 148 | + default: { |
| 149 | + console.error( |
| 150 | + `Unknown command: ${argv[0]}. Use --list (default) or --apply <workspace>.`, |
| 151 | + ); |
| 152 | + break; |
| 153 | + } |
| 154 | +} |
0 commit comments