From d3ae78d8551f136960651f44be55e49a3cfca454 Mon Sep 17 00:00:00 2001 From: Maurizio Vitale Date: Tue, 26 Jul 2022 21:24:50 +0200 Subject: [PATCH] [AAE-10077] ADF's cron job, make it smarter by avoiding publishing on NPM and triggering the PR upstream (#7725) * Avoid the publish on NPM and create useless PR upstream if same ADF sha * Remove useless file --- scripts/travis/build/build-libs.sh | 11 ++- .../travis/update/adf-same-commit-verify.js | 71 +++++++++++++++++++ scripts/travis/update/update-project.sh | 12 +++- 3 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 scripts/travis/update/adf-same-commit-verify.js diff --git a/scripts/travis/build/build-libs.sh b/scripts/travis/build/build-libs.sh index 228b0ed32f..05542b590d 100755 --- a/scripts/travis/build/build-libs.sh +++ b/scripts/travis/build/build-libs.sh @@ -13,9 +13,14 @@ then if [[ $TRAVIS_BRANCH =~ ^develop(-patch.*)?$ ]] || [[ $TRAVIS_EVENT_TYPE == "cron" ]] || [[ $TRAVIS_EVENT_TYPE == "api" ]] then - echo "Replace NPM version with new Alpha tag" - NEXT_VERSION=-nextalpha - ./scripts/update-version.sh -gnu $NEXT_VERSION || exit 1; + isSameADFSha=$(node $BUILD_PIPELINE_DIR/adf-same-commit-verify.js --token=$TOKEN --head=$BRANCH_TO_CREATE --repo=$NAME_REPO --commit=$COMMIT ) + if [ "$isSameADFSha" = 'true' ]; then + echo 'ADF sha is the same. No need to publish again on NPM' + else + echo "Replace NPM version with new Alpha tag" + NEXT_VERSION=-nextalpha + ./scripts/update-version.sh -gnu $NEXT_VERSION || exit 1; + fi fi node ./scripts/pre-publish.js diff --git a/scripts/travis/update/adf-same-commit-verify.js b/scripts/travis/update/adf-same-commit-verify.js new file mode 100644 index 0000000000..6882ff5df4 --- /dev/null +++ b/scripts/travis/update/adf-same-commit-verify.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node + +const GitHub = require('github-api'); +let program = require('commander'); + +const ORGANISATION = 'Alfresco'; +const ORIGIN_REPO = 'alfresco-ng2-components'; + +class PrCreator { + constructor(githubUser, githubRepo, token, commit) { + this.github = new GitHub({token}); + this.repoOrigin = this.github.getRepo(githubUser, ORIGIN_REPO); + this.repoDestination = this.github.getRepo(githubUser, githubRepo); + this.commit = commit; + } + + async getShaClosedPr(head, base) { + return this.getShaPr(head, base, 'closed'); + } + + async getShaOpenPr(head, base) { + return this.getShaPr(head, base, 'open'); + } + + async getShaPr(head, base, status) { + const { data: closedUpstreamPRs } = await this.repoDestination.listPullRequests({ state: status, head: `${ORGANISATION}:${head}`, base }); + if (closedUpstreamPRs.length > 0) { + const latestClosedUpstream = closedUpstreamPRs[0]; + return latestClosedUpstream.body.split(':')[1].trim(); + } + return ''; + } + +} + +async function main() { + + program + .version('0.1.0') + .option('--host [type]', 'Remote environment host adf.lab.com ') + .option('-t, --token [type]', 'token') + .option('-h, --head [type]', 'head') + .option('-r, --repo [type]', 'repo') + .option('-c, --commit [type]', 'commit') + .parse(process.argv); + + const { token, head, repo, commit } = program, + prCreator = new PrCreator(ORGANISATION, repo, token, commit); + + + const baseBranchName = 'develop'; + + const shaOpen = await prCreator.getShaOpenPr(head, baseBranchName); + const shaClosed = await prCreator.getShaClosedPr(head, baseBranchName); + if (shaOpen === commit || shaClosed === commit) { + console.log('ADF sha already exist'); + return 'true'; + } + return 'false'; +} + +main() + .then(result => { + process.stdout.write(result); + process.exit(0); + }) + .catch(error => { + console.error(error.response.status); + console.error(error.response.statusText); + process.exit(1); + }); diff --git a/scripts/travis/update/update-project.sh b/scripts/travis/update/update-project.sh index 3130b32b65..4cc73001b3 100755 --- a/scripts/travis/update/update-project.sh +++ b/scripts/travis/update/update-project.sh @@ -126,8 +126,14 @@ fi rm -rf $TEMP_GENERATOR_DIR -update "generator-alfresco-adf-app" -update "alfresco-content-app" -update "alfresco-apps" +isSameADFSha=$(node $BUILD_PIPELINE_DIR/adf-same-commit-verify.js --token=$TOKEN --head=$BRANCH_TO_CREATE --repo=$NAME_REPO --commit=$COMMIT ) +if [ "$isSameADFSha" = 'true' ]; then + echo 'ADF sha is the same. No need to create another pr' + else + update "generator-alfresco-adf-app" + update "alfresco-content-app" + update "alfresco-apps" +fi + exit $?