-
Notifications
You must be signed in to change notification settings - Fork 13
Added automatic updates for version fields in plugin metadata files #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| const pluginDirectories = core.getInput('plugin_directories'); | ||
| const allowWorkspaceAddition = core.getInput('allow_workspace_addition'); | ||
| const prToUpdate = core.getInput('pr_to_update'); | ||
| const pluginVersions = JSON.parse(core.getInput('plugin_versions') || '{}'); | ||
|
|
||
| const updateCommitLabel = 'needs-commit-update'; | ||
|
|
||
|
|
@@ -134,6 +135,97 @@ | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Read metadata/*.yaml files from the overlay repo via GraphQL, | ||
| * update spec.version and spec.dynamicArtifact (oci://ghcr.io) to match | ||
| * the plugin versions discovered from NPM. | ||
| * @param {string} branchName | ||
| * @returns {Promise<Array<{path: string, mode: string, content: string}>>} | ||
| */ | ||
| async function updateMetadataFiles(branchName) { | ||
|
Check failure on line 145 in update-overlay/create-pr-if-necessary.js
|
||
| if (Object.keys(pluginVersions).length === 0) { | ||
| return []; | ||
| } | ||
|
|
||
| /** @type {{ repository: { metadataTree: { entries: Array<{name: string, object: {text: string} | null}> } | null } }} */ | ||
| const metadataResponse = await github.graphql(` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already run into problems with rate limiting at the moment, so it would be best to roll this up into the existing query, and then pass it along to the |
||
| query GetMetadataFiles($owner: String!, $repo: String!) { | ||
| repository(owner: $owner, name: $repo) { | ||
| metadataTree: object(expression: "${branchName}:${workspacePath}/metadata") { | ||
| ... on Tree { | ||
| entries { | ||
| name | ||
| object { | ||
| ... on Blob { | ||
| text | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }`, { | ||
| owner: overlayRepoOwner, | ||
| repo: overlayRepoName | ||
| }); | ||
|
|
||
| const entries = metadataResponse.repository?.metadataTree?.entries; | ||
| if (!entries) { | ||
| return []; | ||
| } | ||
|
|
||
| /** @type {Array<{path: string, mode: string, content: string}>} */ | ||
| const treeEntries = []; | ||
|
|
||
| for (const entry of entries) { | ||
| if (!entry.name.endsWith('.yaml') && !entry.name.endsWith('.yml')) continue; | ||
| if (!entry.object?.text) continue; | ||
|
|
||
| let content = entry.object.text; | ||
| const packageNameMatch = content.match(/^\s+packageName:\s*"?([^"\n]+)"?\s*$/m); | ||
|
Check warning on line 185 in update-overlay/create-pr-if-necessary.js
|
||
| if (!packageNameMatch) continue; | ||
|
|
||
| const packageName = packageNameMatch[1].trim(); | ||
| const newVersion = pluginVersions[packageName]; | ||
| if (!newVersion) continue; | ||
|
|
||
| let modified = false; | ||
|
|
||
| // Update spec.version | ||
| const versionRegex = /^(\s+version:\s*).+$/m; | ||
| const versionMatch = content.match(versionRegex); | ||
|
Check warning on line 196 in update-overlay/create-pr-if-necessary.js
|
||
| if (versionMatch && versionMatch[0].trim() !== `version: ${newVersion}`) { | ||
| content = content.replace(versionRegex, `$1${newVersion}`); | ||
| core.info(` Updated version to ${newVersion} in ${entry.name}`); | ||
| modified = true; | ||
| } | ||
|
|
||
| // Update spec.dynamicArtifact tag for oci://ghcr.io references | ||
| const artifactRegex = /^(\s+dynamicArtifact:\s*oci:\/\/ghcr\.io\/[^:]+:)[^\s!]+(![^\s]+)$/m; | ||
| const artifactMatch = content.match(artifactRegex); | ||
|
Check warning on line 205 in update-overlay/create-pr-if-necessary.js
|
||
| if (artifactMatch) { | ||
| const newTag = `bs_${backstageVersion}__${newVersion}`; | ||
| content = content.replace(artifactRegex, `$1${newTag}$2`); | ||
| core.info(` Updated dynamicArtifact tag in ${entry.name}`); | ||
| modified = true; | ||
| } | ||
|
|
||
| if (modified) { | ||
| treeEntries.push({ | ||
| path: `${workspacePath}/metadata/${entry.name}`, | ||
| mode: '100644', | ||
| content, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| if (treeEntries.length > 0) { | ||
| core.info(`Updated ${treeEntries.length} metadata file(s)`); | ||
| } | ||
|
|
||
| return treeEntries; | ||
| } | ||
|
|
||
| const workspaceCheck = await checkWorkspace(overlayRepoBranchName); | ||
| if (workspaceCheck.status === 'sourceEqual') { | ||
| core.info( | ||
|
|
@@ -379,6 +471,11 @@ | |
| core.info(`Deleting the overridden \`backstage.json\` because it's out-of-sync (\`${workspaceCheck.backstageVersionOverride}\`) with the backstage version of the new source commit (\`${backstageVersion}\`)`); | ||
| } | ||
|
|
||
| // Update metadata files with current plugin versions | ||
| const metadataTreeEntries = await updateMetadataFiles( | ||
| prBranchExists ? targetPRBranchName : overlayRepoBranchName | ||
| ); | ||
|
|
||
| /** @type { Parameters<typeof githubClient.git.createTree>[0] } */ | ||
| const createTreeOptions = { | ||
| owner: overlayRepoOwner, | ||
|
|
@@ -387,6 +484,7 @@ | |
| tree: [ | ||
| { path: `${workspacePath}/plugins-list.yaml`, mode: '100644', content: updatedPluginsYamlContent }, | ||
| { path: `${workspacePath}/source.json`, mode: '100644', content: newSourceJsonContent }, | ||
| ...metadataTreeEntries, | ||
| ] | ||
| }; | ||
| if (deleteBackstageJson) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering we are likely to use other workspace metadata, we might consider simply passing in the entire workspace JSON.
@davidfestal @rostalan wdyt?