diff --git a/.github/workflows/upstream-js.yml b/.github/workflows/upstream-js.yml new file mode 100644 index 0000000000..343d4b1ae4 --- /dev/null +++ b/.github/workflows/upstream-js.yml @@ -0,0 +1,193 @@ +name: Upstream js +on: + schedule: + - cron: '0 */3 * * *' # “At minute 0 past every 3rd hour.” + workflow_dispatch: + inputs: + tag_version: + description: image tag + type: choice + required: true + options: + - alpha + - latest + default: alpha + +jobs: + upstream: + runs-on: ubuntu-latest + outputs: + hasAtLeastOneNewVersion: ${{ steps.fetchLatestPkg.outputs.hasAtLeastOneNewVersion }} + hasNewVersionAvailableJS: ${{ steps.fetchLatestPkg.outputs.hasVersionNewJS }} + latestVersionAvailableJS: ${{ steps.fetchLatestPkg.outputs.latestVersionJS }} + hasAtLeastOneNewVersionWithoutPR: ${{ steps.checkPrAlreadyExist.outputs.hasAtLeastOneNewVersionWithoutPR }} + steps: + - id: checkoutRepo + name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 1 + - id: fetchLatestPkg + name: Fetch the latest package version + uses: actions/github-script@v6 + env: + TAG_VERSION: ${{ inputs.repo_to_update }} + with: + github-token: ${{ secrets.PAT_WRITE_PKG }} + script: | + const tagVersion = process.env.TAG_VERSION; + const getLatestVersionOf = require('./scripts/github/update/latest-version-of.js'); + + const { hasVersionNew: hasVersionNewJS, latestVersion: latestVersionJS } = await getLatestVersionOf({github, context, dependencyName: 'js-api', tagVersion}); + console.log('hasVersionNewJS', hasVersionNewJS) + console.log('latestVersionJS', latestVersionJS) + + if (hasVersionNewJS === 'true' ) { + core.setOutput('hasAtLeastOneNewVersion', 'true'); + core.setOutput('hasVersionNewJS', 'true'); + core.setOutput('latestVersionJS', latestVersionJS); + } else { + core.setOutput('hasAtLeastOneNewVersion', 'false'); + console.log('No new version available, skipping upstream!') + } + + - name: Check value after + run: | + echo "The value hasAtLeastOneNewVersion is: ${{ steps.fetchLatestPkg.outputs.hasAtLeastOneNewVersion }}" + echo "The value hasVersionNewJS is: ${{ steps.fetchLatestPkg.outputs.hasVersionNewJS }}" + echo "The value latestVersionJS is: ${{ steps.fetchLatestPkg.outputs.latestVersionJS }}" + + - id: checkPrAlreadyExist + name: Check PR with latest already exist + if: ${{ steps.fetchLatestPkg.outputs.hasAtLeastOneNewVersion == 'true' }} + uses: actions/github-script@v6 + env: + HAS_NEW_JS_VERSION: ${{ steps.fetchLatestPkg.outputs.hasVersionNewJS }} + LATEST_JS_VERSION: ${{ steps.fetchLatestPkg.outputs.latestVersionJS }} + with: + github-token: ${{ secrets.BOT_GITHUB_TOKEN }} + script: | + const hasVersionNewJS = process.env.HAS_NEW_JS_VERSION; + const latestVersionJS = process.env.LATEST_JS_VERSION; + + const checkPRAlreadyExist = require('./scripts/ci/jobs/check-pr-already-exist.js'); + let isPRWithLatestJSAlreadyAvailable = false; + + if (hasVersionNewJS === 'true') { + isPRWithLatestJSAlreadyAvailable = await checkPRAlreadyExist({github, context, version: latestVersionJS}); + console.log('isPRWithLatestJSAlreadyAvailable', isPRWithLatestJSAlreadyAvailable); + } + + if (isPRWithLatestJSAlreadyAvailable) { + console.log('Warning: Upstream PR already exist, stop the migration execution!'); + core.setOutput('hasAtLeastOneNewVersionWithoutPR', 'false'); + } else { + core.setOutput('hasAtLeastOneNewVersionWithoutPR', 'true'); + } + + migrate: + if: ${{ needs.upstream.outputs.hasAtLeastOneNewVersionWithoutPR == 'true' }} + runs-on: ubuntu-latest + needs: upstream + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + token: ${{ secrets.BOT_GITHUB_TOKEN }} + fetch-depth: 1 + - name: setup NPM + uses: actions/setup-node@v3 + with: + node-version: 14.15.4 + cache-dependency-path: package-lock.json + - name: "General :: dbpci-install" + run: | + npm ci --ignore-scripts + - name: Migration + shell: bash + env: + IS_JS_AFFECTED: ${{ needs.upstream.outputs.hasNewVersionAvailableJS }} + PACKAGE_VERSION_JS: ${{ needs.upstream.outputs.latestVersionAvailableJS }} + BRANCH_TO_CREATE: "upstream-dependencies" + run: | + + migrateDependenciesJS() { + echo "Update JS dependencies to: ${PACKAGE_VERSION_JS}" + echo "Calling migration JS" + npx nx migrate @alfresco/js-api@${PACKAGE_VERSION_JS} + echo "Migration JS done" + } + + regeneratePackageLock() { + echo "Regenerate lock" + npm i --package-lock-only + echo "Package-lock done." + } + + if git checkout ${BRANCH_TO_CREATE} 2>/dev/null ; then + git reset --hard origin/develop + echo "Reset branch" + fi + + if [[ "$IS_JS_AFFECTED" == "true" ]]; then + migrateDependenciesJS + fi + + regeneratePackageLock + + - name: Commit Code + if: ${{ needs.upstream.outputs.hasAtLeastOneNewVersion == 'true' }} + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "[ci:force][auto-commit] Update dependencies JS:${{ needs.upstream.outputs.latestVersionAvailableJS }}" + branch: upstream-dependencies + push_options: '--force' + create_branch: true + + - name: Create a Pull request + uses: actions/github-script@v6 + env: + PACKAGE_VERSION_JS: ${{ needs.upstream.outputs.latestVersionAvailableJS }} + with: + github-token: ${{ secrets.BOT_GITHUB_TOKEN }} + script: | + const { PACKAGE_VERSION_JS } = process.env + const BRANCH_TO_CREATE = 'upstream-dependencies'; + + const { data: prs } = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${context.repo.owner}:${BRANCH_TO_CREATE}`, + base: 'develop' + }); + + if (prs.length < 1) { + const payloadPullRequest = { + owner: context.repo.owner, + repo: context.repo.repo, + title: `GH Auto: Upstream dependencies JS-API:${PACKAGE_VERSION_JS}`, + head: `${context.repo.owner}:${BRANCH_TO_CREATE}`, + base: 'develop', + body: `Automatic PR` + }; + + console.log('Payload: ',payloadPullRequest); + + const { data: pr } = await github.rest.pulls.create(payloadPullRequest); + return pr.number; + } else { + const upstreamPrOpen = prs[0]; + + // override the title to contains the latest js dep number + const payloadUpdatePullRequest = { + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: upstreamPrOpen.number, + title: `GH Auto: Upstream dependencies JS-API:${PACKAGE_VERSION_JS}`, + }; + + await github.rest.pulls.update(payloadUpdatePullRequest); + return upstreamPrOpen.number; + } + console.log(`Trigger a dispatch event for the monorepo`); diff --git a/scripts/github/update/latest-version-of.js b/scripts/github/update/latest-version-of.js new file mode 100755 index 0000000000..a839fa9755 --- /dev/null +++ b/scripts/github/update/latest-version-of.js @@ -0,0 +1,45 @@ +module.exports = async ({github, context, dependencyName, tagVersion = 'alpha' }) => { + + console.log('owner', context.repo.owner) + const organization = 'alfresco'; + const dependencyFullName = `@${organization}/${dependencyName}`; + + console.log('Looking versions for: ', dependencyFullName); + + const pjson = require('../../../package.json'); + const currentDependency = pjson?.dependencies[dependencyFullName]; + console.log('current from package.json:', currentDependency); + + const { data: availablePakages } = await github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({ + package_type: 'npm', + package_name: dependencyName, + org: organization + }); + + let latestPkgToUpdate = null; + if (tagVersion === 'alpha') { + console.log('alpha: taking most recent') + const filteredAlphaPkgs = availablePakages.filter( (item) => item.name.match('^[0-9]*.[0-9]*.[0-9]*.A.[0-9].[0-9]*$') ) + latestPkgToUpdate = filteredAlphaPkgs[0]; + } else { + console.log('release: taking most recent') + const filteredReleasePkgs = availablePakages.filter( (item) => item.name.match('^[0-9]*.[0-9]*.[0-9]*.A.[0-9]*$') || item.name.match('^[0-9]*.[0-9]*.[0-9]*$') ) + latestPkgToUpdate = filteredReleasePkgs[0]; + } + + if (latestPkgToUpdate === null) { + console.log(`Something went wrong. Not able to find any version.`); + return { hasVersionNew: 'false' }; + } else { + console.log(`latest tag:${tagVersion} from NPM: `, latestPkgToUpdate.name); + + if (currentDependency === latestPkgToUpdate?.name) { + console.log(`There is no new version published for ${dependencyFullName}.`); + return { hasVersionNew: 'false' }; + } else { + return { hasVersionNew: 'true', latestVersion: latestPkgToUpdate?.name }; + } + } + +} + \ No newline at end of file diff --git a/scripts/github/update/useme-to-test-restokit.js b/scripts/github/update/useme-to-test-restokit.js new file mode 100755 index 0000000000..0fb17506ff --- /dev/null +++ b/scripts/github/update/useme-to-test-restokit.js @@ -0,0 +1,72 @@ + +const { Octokit } = require("@octokit/rest"); +const octokit = new Octokit({ + auth: "", + userAgent: 'myApp v1.2.3', + baseUrl: 'https://api.github.com', + log: { + debug: () => {}, + info: () => {}, + warn: console.warn, + error: console.error + }, + request: { + agent: undefined, + fetch: undefined, + timeout: 0 + } + }); + + + async function asyncCall() { + const organization = 'alfresco'; + + const { data: availablePakages } = await octokit.rest.packages.getAllPackageVersionsForPackageOwnedByOrg({ + package_type: 'npm', + package_name: 'adf-core', + org: organization + }); + + // console.log(availablePakages[0]) + + availablePakages.push({ + id: 123, + name: '6.0.0-A.3', + metadata: { package_type: 'npm' } + }) + availablePakages.push({ + id: 222, + name: '6.0.1', + metadata: { package_type: 'npm' } + }) + + const filteredReleasePkgs = availablePakages.filter( (item) => item.name.match('^[0-9]*.[0-9]*.[0-9]*.A.[0-9]*$') || item.name.match('^[0-9]*.[0-9]*.[0-9]*$') ) + console.log(filteredReleasePkgs) + + // console.log('alpha') + // const filteredAlphaPkgs = availablePakages.filter( (item) => item.name.match('^[0-9]*\.[0-9]*\.[0-9]*.A\.[0-9]\.[0-9]*$') ) + // console.log(filteredAlphaPkgs) + + + + // const { data: info } = await octokit.rest.packages.getPackageForOrganization({ + // package_type: 'npm', + // package_name: 'adf-core', + // org: organization + // }); + + // console.log(info) + + // const { data: infos } = await octokit.rest.packages.getPackageVersionForOrganization({ + // package_type: 'npm', + // package_name: 'adf-core', + // org: organization, + // package_version_id: 85591610 + // }); + + // console.log(infos) + + } + + + asyncCall(); \ No newline at end of file