|
| 1 | +name: "Open plugin XML update PR" |
| 2 | + |
| 3 | +# Synced files only reach the default branch, where workflow_run workflows are read from, unlike tag pushes from X.Y/bugfixes |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: |
| 7 | + - "Automatically tag new version" |
| 8 | + types: |
| 9 | + - "completed" |
| 10 | + |
| 11 | +# The pull request is opened with the bot token |
| 12 | +permissions: |
| 13 | + contents: "read" |
| 14 | + |
| 15 | +jobs: |
| 16 | + open-xml-update-pr: |
| 17 | + if: github.repository == 'pluginsGLPI/fields' && github.event.workflow_run.conclusion == 'success' |
| 18 | + name: "Open plugin XML update PR" |
| 19 | + runs-on: "ubuntu-latest" |
| 20 | + steps: |
| 21 | + - name: "Generate bot token" |
| 22 | + id: "bot-app-token" |
| 23 | + uses: "actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510" # v3.3.0 |
| 24 | + with: |
| 25 | + client-id: "${{ vars.GLPI_PROJECT_BOT_CLIENT_ID }}" |
| 26 | + private-key: "${{ secrets.GLPI_PROJECT_BOT_PRIVATE_KEY }}" |
| 27 | + permission-contents: "write" |
| 28 | + permission-pull-requests: "write" |
| 29 | + - name: "Copy the tag version block to the default branch XML" |
| 30 | + uses: "actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3" # v9.0.0 |
| 31 | + env: |
| 32 | + HEAD_SHA: "${{ github.event.workflow_run.head_sha }}" |
| 33 | + DEFAULT_BRANCH: "${{ github.event.repository.default_branch }}" |
| 34 | + with: |
| 35 | + github-token: "${{ steps.bot-app-token.outputs.token }}" |
| 36 | + script: | |
| 37 | + const { owner, repo } = context.repo; |
| 38 | + const base = process.env.DEFAULT_BRANCH |
| 39 | + || (await github.rest.repos.get({ owner, repo })).data.default_branch; |
| 40 | +
|
| 41 | + const readFile = async (path, ref) => { |
| 42 | + try { |
| 43 | + const { data } = await github.rest.repos.getContent({ owner, repo, path, ref }); |
| 44 | + return Buffer.from(data.content, 'base64').toString('utf8'); |
| 45 | + } catch (error) { |
| 46 | + if (error.status !== 404) { |
| 47 | + throw error; |
| 48 | + } |
| 49 | + return undefined; |
| 50 | + } |
| 51 | + }; |
| 52 | +
|
| 53 | + // Same version extraction as the auto tag workflow, whose tag is named after it |
| 54 | + const tag = (await readFile('setup.php', process.env.HEAD_SHA))?.match(/define\s*\(\s*['"]PLUGIN_[A-Z]+_VERSION['"]\s*,\s*['"]([^'"]+)['"]\s*\)/)?.[1]; |
| 55 | + if (tag === undefined) { |
| 56 | + core.info('No plugin version found in setup.php, skipping update.'); |
| 57 | + return; |
| 58 | + } |
| 59 | + const tagRef = `refs/tags/${tag}`; |
| 60 | + let ref; |
| 61 | + try { |
| 62 | + ({ data: ref } = await github.rest.git.getRef({ owner, repo, ref: `tags/${tag}` })); |
| 63 | + } catch (error) { |
| 64 | + if (error.status !== 404) { |
| 65 | + throw error; |
| 66 | + } |
| 67 | + core.info(`No ${tag} tag, skipping update.`); |
| 68 | + return; |
| 69 | + } |
| 70 | + // Any setup.php push succeeds the auto tag run, only a tag on the triggering commit is a fresh one |
| 71 | + const tagSha = ref.object.type === 'tag' |
| 72 | + ? (await github.rest.git.getTag({ owner, repo, tag_sha: ref.object.sha })).data.object.sha |
| 73 | + : ref.object.sha; |
| 74 | + if (tagSha !== process.env.HEAD_SHA) { |
| 75 | + core.info(`${tag} was not created by this run, skipping update.`); |
| 76 | + return; |
| 77 | + } |
| 78 | +
|
| 79 | + let xmlPath = 'plugin.xml'; |
| 80 | + let tagXml = await readFile(xmlPath, tagRef); |
| 81 | + if (tagXml === undefined) { |
| 82 | + const pluginKey = (await readFile('setup.php', tagRef))?.match(/function plugin_version_([a-z0-9_]+)/)?.[1]; |
| 83 | + if (pluginKey !== undefined) { |
| 84 | + xmlPath = `${pluginKey}.xml`; |
| 85 | + tagXml = await readFile(xmlPath, tagRef); |
| 86 | + } |
| 87 | + } |
| 88 | + if (tagXml === undefined) { |
| 89 | + core.info('No plugin XML file found in the tag, skipping update.'); |
| 90 | + return; |
| 91 | + } |
| 92 | +
|
| 93 | + const escapedTag = tag.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
| 94 | + const existingRe = new RegExp(`[ \\t]*<version>\\s*<num>\\s*${escapedTag}\\s*</num>[\\s\\S]*?</version>`); |
| 95 | + const block = tagXml.match(existingRe)?.[0]; |
| 96 | + if (block === undefined) { |
| 97 | + core.info(`No <version> block for ${tag} in the tag's ${xmlPath}, skipping update.`); |
| 98 | + return; |
| 99 | + } |
| 100 | +
|
| 101 | + const xml = await readFile(xmlPath, base); |
| 102 | + if (xml === undefined) { |
| 103 | + core.info(`No ${xmlPath} on ${base}, skipping update.`); |
| 104 | + return; |
| 105 | + } |
| 106 | +
|
| 107 | + let updated; |
| 108 | + if (existingRe.test(xml)) { |
| 109 | + updated = xml.replace(existingRe, () => block); |
| 110 | + } else if (/<versions>[ \t]*\r?\n/.test(xml)) { |
| 111 | + // Newest first: the block goes right after <versions>. |
| 112 | + updated = xml.replace(/<versions>[ \t]*(\r?\n)/, (match, eol) => `${match}${block}${eol}`); |
| 113 | + } else { |
| 114 | + core.warning(`No <versions> element found in ${xmlPath} on ${base}, skipping update.`); |
| 115 | + return; |
| 116 | + } |
| 117 | + if (updated === xml) { |
| 118 | + core.info(`${xmlPath} on ${base} already contains version ${tag}.`); |
| 119 | + return; |
| 120 | + } |
| 121 | +
|
| 122 | + const branch = `release-xml/${tag}`; |
| 123 | + const baseSha = (await github.rest.git.getRef({ owner, repo, ref: `heads/${base}` })).data.object.sha; |
| 124 | + try { |
| 125 | + await github.rest.git.createRef({ owner, repo, ref: `refs/heads/${branch}`, sha: baseSha }); |
| 126 | + } catch (error) { |
| 127 | + // 422: branch left by a previous run, reset it onto the default branch. |
| 128 | + if (error.status !== 422) { |
| 129 | + throw error; |
| 130 | + } |
| 131 | + await github.rest.git.updateRef({ owner, repo, ref: `heads/${branch}`, sha: baseSha, force: true }); |
| 132 | + } |
| 133 | + const { data: file } = await github.rest.repos.getContent({ owner, repo, path: xmlPath, ref: branch }); |
| 134 | + await github.rest.repos.createOrUpdateFileContents({ |
| 135 | + owner, |
| 136 | + repo, |
| 137 | + path: xmlPath, |
| 138 | + branch, |
| 139 | + sha: file.sha, |
| 140 | + message: `Add version ${tag} to ${xmlPath}`, |
| 141 | + content: Buffer.from(updated, 'utf8').toString('base64'), |
| 142 | + }); |
| 143 | +
|
| 144 | + const { data: openPulls } = await github.rest.pulls.list({ owner, repo, head: `${owner}:${branch}`, base, state: 'open' }); |
| 145 | + if (openPulls.length > 0) { |
| 146 | + core.info(`Updated existing PR: ${openPulls[0].html_url}`); |
| 147 | + return; |
| 148 | + } |
| 149 | + const { data: pull } = await github.rest.pulls.create({ |
| 150 | + owner, |
| 151 | + repo, |
| 152 | + head: branch, |
| 153 | + base, |
| 154 | + title: `Add version ${tag} to ${xmlPath}`, |
| 155 | + body: `Copies the \`${tag}\` version block from the tag's \`${xmlPath}\` to \`${base}\`, so that the plugins catalog lists it.`, |
| 156 | + }); |
| 157 | + core.info(`Opened PR: ${pull.html_url}`); |
0 commit comments