diff --git a/.github/workflows/setStableTag.yml b/.github/workflows/setStableTag.yml new file mode 100644 index 000000000..efb30500b --- /dev/null +++ b/.github/workflows/setStableTag.yml @@ -0,0 +1,38 @@ +# +# This workflow scans a dedicated list of adapters (hardcode at setStableTag.js) and ensures that the release number listed +# at sources-dist-stable.json and the release tagged as 'stable' at npm are identical. If a difference is detected, the dist-tag +# at npmjs is changed to meet the contents of sources-dist-stable.json. +# +# This workflow is triggered by any push and additionally runs once a day +# + +name: Set stable tag + +on: + workflow_dispatch: + + push: + branches: [ "master" ] + + schedule: + # * is a special character in YAML, so you have to quote this string + # every day at 1:00:00 + - cron: '0 1 * * *' + +jobs: + set-stable-tag: + name: set stable tag + if: | + github.repository == 'ioBroker/ioBroker.repositories' + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: 18 + - run: npm i + - run: npm run setStableTag + env: + OWN_GITHUB_TOKEN: ${{ secrets.OWN_GITHUB_TOKEN }} + NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/lib/setStableTag.js b/lib/setStableTag.js new file mode 100644 index 000000000..a475e0fb9 --- /dev/null +++ b/lib/setStableTag.js @@ -0,0 +1,110 @@ +'use strict'; +//const fs = require('fs'); +const execSync = require('child_process').execSync; + +const { +// addComment, +// addLabel, +// getGithub, + getUrl, +// getAllComments, +// deleteComment +} = require('./common'); + +const STABLE_JSON = 'sources-dist-stable.json'; +const ADAPTER_LIST = [ + 'admin', + 'discovery', + 'js-controller', + 'backitup' +]; + +async function getStableRepo() { + return await getUrl('http://repo.iobroker.live/sources-dist.json'); +} + +async function getNpmMeta( pAdapter ) { + return await getUrl(`https://registry.npmjs.org/iobroker.${pAdapter}`); +}; + +async function getNpmDisttags(pAdapter) { + const npmMeta = await getNpmMeta( pAdapter ); + return npmMeta['dist-tags']; +}; + +function getPullRequestNumber() { + if (process.env.GITHUB_REF && process.env.GITHUB_REF.match(/refs\/pull\/\d+\/merge/)) { + const result = /refs\/pull\/(\d+)\/merge/g.exec(process.env.GITHUB_REF); + if (!result) { + console.log ('GITHUB_REF not found or not parseable.'); + return null; + } + return result[1]; + } else if (process.env.GITHUB_EVENT_PATH) { + const event = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')); + return event.pull_request ? event.pull_request.number : (event.issue ? event.issue.number : ''); + } else { + console.log ('GITHUB_REF and process.env.GITHUB_EVENT_PATH are not set!'); + return null; + } +} + +async function doIt() { + + const prID = getPullRequestNumber(); + console.log(`Process PR ${prID}`); + + if (prID) { + const files = await getGithub(`https://api.github.com/repos/ioBroker/ioBroker.repositories/pulls/${prID}/files`); + console.log('Files changed:'); + files.forEach( f=>{console.log(` ${f}`)}); + const isStable = files.includes(`${STABLE_JSON}`) + if (!isStable) + { + return 'No changes to stable repository detected'; + } + } else { + console.log(`scanning ${STABLE_JSON} ...`); + } + + const stable=await getStableRepo(); + for (const adapter in stable) { + if (ADAPTER_LIST.includes( adapter )) { + console.log (`\nchecking ${adapter} ...`); + + const npmTags=await getNpmDisttags(adapter); + const repoRelease = stable[adapter].version; + const npmRelease = npmTags.stable; + //console.log( `repo: ${repoRelease} - npm: ${npmRelease}`); + if (repoRelease === npmRelease) { + console.log (`${adapter} ${repoRelease} correctly tagged as stable`) + } else { + console.log (`${adapter} ${repoRelease} need to be tagged as stable`); + console.log( `executing npm --//registry.npmjs.org/:_authToken=*** dist-tag add iobroker.${adapter}@${repoRelease} stable`); + const cmd = `npm --//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN} dist-tag add iobroker.${adapter}@${repoRelease} stable`; + try { + const result=execSync( cmd, { encoding: 'utf-8' }); + console.log(result); + } catch (_e) { + // console.log (JSON.stringify(e)); + } + } + } + } + + return 'done'; +} + +// activate for debugging purposes +//process.env.GITHUB_REF = 'refs/pull/2298/merge'; +//process.env.OWN_GITHUB_TOKEN = 'add-token-here'; +//process.env.GITHUB_EVENT_PATH = __dirname + '/../event.json'; + +console.log(`process.env.GITHUB_REF = ${process.env.GITHUB_REF}`); +console.log(`process.env.GITHUB_EVENT_PATH = ${process.env.GITHUB_EVENT_PATH}`); +console.log(`process.env.OWN_GITHUB_TOKEN = ${(process.env.OWN_GITHUB_TOKEN || '').length}`); +console.log(`process.env.NPM_TOKEN = ${(process.env.NPM_TOKEN || '').length}`); + +doIt() + .then(result => console.log(result)) + .catch(e => console.error(e)); diff --git a/package.json b/package.json index 2dc65a61b..d322115f4 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "checkNpm": "node lib/checkNpm.js", "manualAction": "node lib/manualAction.js", "setLabels": "node lib/setLabels.js", + "setStableTag": "node lib/setStableTag.js", "stable": "node lib/readyForStable.js", "stableBrandNewInfo": "node lib/stableBrandNewInfo.js", "stableBrandNewReminder": "node lib/stableBrandNewReminder.js"