From 4313535ee3be6398df7673c492dc82bacbb93d35 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Tue, 9 Feb 2021 12:36:00 +0000 Subject: [PATCH] Update children script (#6650) * Update children script version 2 * save exact --- .travis.yml | 19 ++- package-lock.json | 35 ++++++ package.json | 1 + scripts/travis/update/pr-creator.js | 55 +++++++++ .../travis/update/trigger-travis-children.sh | 68 ----------- scripts/travis/update/update-children.sh | 24 ---- scripts/travis/update/update-project.sh | 111 +++++++++++++----- 7 files changed, 184 insertions(+), 129 deletions(-) create mode 100644 scripts/travis/update/pr-creator.js delete mode 100755 scripts/travis/update/trigger-travis-children.sh delete mode 100755 scripts/travis/update/update-children.sh diff --git a/.travis.yml b/.travis.yml index 15184c95cb..9b8a0a436b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,13 +34,13 @@ stages: if: tag IS blank - name: Check bundle if: type = push AND tag IS blank - - name: Trigger ADF child build - if: (branch = develop AND type = push) OR type = api - name: Unit test if: (branch != master AND type != cron AND tag IS blank) OR type = api - name: e2e Test if: (branch != master AND type != cron AND tag IS blank) OR type = api - - name: Update Children Projects + - name: Trigger Alpha ADF child build + if: (branch = develop AND type = push) OR type = api + - name: Trigger Beta ADF child build if: tag =~ .*beta.* - name: Release tag if: type = cron OR branch = master @@ -84,9 +84,13 @@ jobs: name: Unit test process-cloud script: ./scripts/travis/unit-test/process-cloud.sh + - stage: Trigger Alpha ADF child build + name: Trigger Alpha ADF child build + script: ./scripts/travis/update/update-project.sh -p $TRAVIS_BUILD_NUMBER -t $GITHUB_TOKEN -v alpha - - stage: Update Children Projects - script: ./scripts/travis/update/update-children.sh + - stage: Trigger Beta ADF child build + name: Trigger Beta ADF child build + script: ./scripts/travis/update/update-project.sh -p $TRAVIS_BUILD_NUMBER -t $GITHUB_TOKEN -v beta - stage: Release tag script: ./scripts/travis/release/git-tag.sh @@ -139,11 +143,6 @@ jobs: - ADF_VERSION=$(npm view @alfresco/adf-core@${TAG_NPM} version) - ./scripts/travis/build/npm-check-bundles.sh -v ${ADF_VERSION} - - - stage: Trigger ADF child build - name: Trigger ADF child build - script: ./scripts/travis/update/trigger-travis-children.sh --branch $TRAVIS_BRANCH Alfresco $TRAVIS_ACCESS_TOKEN - notifications: slack: on_pull_requests: false diff --git a/package-lock.json b/package-lock.json index 9751137732..d1827a3b8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12547,6 +12547,35 @@ "assert-plus": "^1.0.0" } }, + "github-api": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/github-api/-/github-api-3.4.0.tgz", + "integrity": "sha512-2yYqYS6Uy4br1nw0D3VrlYWxtGTkUhIZrumBrcBwKdBOzMT8roAe8IvI6kjIOkxqxapKR5GkEsHtz3Du/voOpA==", + "dev": true, + "requires": { + "axios": "^0.21.1", + "debug": "^2.2.0", + "js-base64": "^2.1.9", + "utf8": "^2.1.1" + }, + "dependencies": { + "debug": { + "version": "2.6.9", + "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", + "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", + "dev": true, + "requires": { + "ms": "2.0.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", + "dev": true + } + } + }, "github-slugger": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/github-slugger/-/github-slugger-1.3.0.tgz", @@ -26772,6 +26801,12 @@ "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==", "dev": true }, + "utf8": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/utf8/-/utf8-2.1.2.tgz", + "integrity": "sha1-H6DZJw6b6FDZsFAn9jUZv0ZFfZY=", + "dev": true + }, "util": { "version": "0.10.3", "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", diff --git a/package.json b/package.json index 027c3f2a7e..460248de70 100644 --- a/package.json +++ b/package.json @@ -127,6 +127,7 @@ "css-loader": "^4.3.0", "dotenv": "^8.2.0", "ejs": "^3.1.6", + "github-api": "^3.4.0", "graphql": "^15.4.0", "graphql-request": "^3.1.0", "husky": "^4.3.8", diff --git a/scripts/travis/update/pr-creator.js b/scripts/travis/update/pr-creator.js new file mode 100644 index 0000000000..c6f8086ba4 --- /dev/null +++ b/scripts/travis/update/pr-creator.js @@ -0,0 +1,55 @@ +const GitHub = require('github-api'); +let program = require('commander'); + +const ORGANISATION = 'Alfresco'; + +class PrCreator { + constructor(githubUser, githubRepo, token) { + this.github = new GitHub({token}); + this.repo = this.github.getRepo(githubUser, githubRepo); + } + + async create(title, head, base) { + const { data: prs } = await this.repo.listPullRequests({ state: 'open', head: `${ORGANISATION}:${head}`, base }); + + if (prs.length < 1) { + const { data: pr } = await this.repo.createPullRequest({ title, head, base }); + return pr.number; + } + + return prs[0].number; + } +} + +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('-title, --title [type]', 'title') + .parse(process.argv); + + console.log('process.argv ', program); + + const { token, title, head, repo } = program, + prCreator = new PrCreator(ORGANISATION, repo, token); + + if (!token || !head || !title) { + throw new Error('Each of the parameters have to be provided. --token, --title, --head'); + } + + return prCreator.create(title, head, 'develop'); +} + +main() + .then(prNumber => { + console.log(prNumber) + process.exit(0); + }) + .catch(error => { + console.error(error); + process.exit(1); + }); diff --git a/scripts/travis/update/trigger-travis-children.sh b/scripts/travis/update/trigger-travis-children.sh deleted file mode 100755 index 0c75247992..0000000000 --- a/scripts/travis/update/trigger-travis-children.sh +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/env bash - -eval repos=( - "alfresco-apps" - "alfresco-content-app" - "generator-alfresco-adf-app" - "alfresco-process-workspace-app") - - -if [ "$#" -lt 3 ] || [ "$#" -ge 7 ]; then - echo "Wrong number of arguments $# to trigger-travis.sh; run like:" - echo " trigger-travis.sh [--branch BRANCH] GITHUBPROJECT TRAVIS_ACCESS_TOKEN [MESSAGE]" >&2 - exit 1 -fi - -if [ "$1" = "--branch" ] ; then - shift - BRANCH="$1" - shift -else - BRANCH=master -fi - -USER=$1 -TOKEN=$2 -if [ $# -eq 4 ] ; then - MESSAGE=",\"message\": \"$4\"" -elif [ -n "$TRAVIS_REPO_SLUG" ] ; then - MESSAGE=",\"message\": \"Triggered by upstream build of $TRAVIS_REPO_SLUG commit "`git log --oneline -n 1 HEAD`"\"" -else - MESSAGE="" -fi -## For debugging: -# echo "USER=$USER" -# echo "REPO=$REPO" -# echo "TOKEN=$TOKEN" -# echo "MESSAGE=$MESSAGE" - -body="{ -\"request\": { - \"branch\":\"$BRANCH\" - $MESSAGE -}}" - - -for REPO in ${repos[@]} -do - - # "%2F" creates a literal "/" in the URL, that is not interpreted as a - # segment or directory separator. - curl -s -X POST \ - -H "Content-Type: application/json" \ - -H "Accept: application/json" \ - -H "Travis-API-Version: 3" \ - -H "Authorization: token ${TOKEN}" \ - -d "$body" \ - https://api.travis-ci.com/repo/${USER}%2F${REPO}/requests \ - | tee /tmp/travis-request-output.$$.txt - - if grep -q '"@type": "error"' /tmp/travis-request-output.$$.txt; then - exit 1 - fi - - if grep -q 'access denied' /tmp/travis-request-output.$$.txt; then - exit 1 - fi - -done diff --git a/scripts/travis/update/update-children.sh b/scripts/travis/update/update-children.sh deleted file mode 100755 index b2f453f0f0..0000000000 --- a/scripts/travis/update/update-children.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - - -if ([ "$TRAVIS_BRANCH" = "master" ]); then - VERSION=$(npm view @alfresco/adf-core version) - JS_VERSION=$(npm view @alfresco/js-api version) -else - VERSION=$(npm view @alfresco/adf-core@beta version) - JS_VERSION=$(npm view @alfresco/js-api@alpha version) -fi; - -echo "Update Generator" -./scripts/travis/update/update-project.sh -t $GITHUB_TOKEN -n 'Alfresco/generator-alfresco-adf-app' -v $VERSION -vjs $JS_VERSION -echo "Update ACA" -./scripts/travis/update/update-project.sh -t $GITHUB_TOKEN -n 'Alfresco/alfresco-content-app' -v $VERSION -vjs $JS_VERSION -echo "Update AMA" -./scripts/travis/update/update-project.sh -t $GITHUB_ENTERPRISE_TOKEN -n 'Alfresco/alfresco-apps' -v $VERSION -vjs $JS_VERSION -echo "Update Workspace" -./scripts/travis/update/update-project.sh -t $GITHUB_ENTERPRISE_TOKEN -n 'Alfresco/alfresco-process-workspace-app' -v $VERSION -vjs $JS_VERSION -echo "Update Digital Workspace" -./scripts/travis/update/update-project.sh -t $GITHUB_ENTERPRISE_TOKEN -n 'Alfresco/alfresco-digital-workspace-app' -v $VERSION -vjs $JS_VERSION - diff --git a/scripts/travis/update/update-project.sh b/scripts/travis/update/update-project.sh index 35ea4e20f8..8723e987f0 100755 --- a/scripts/travis/update/update-project.sh +++ b/scripts/travis/update/update-project.sh @@ -1,61 +1,118 @@ #!/usr/bin/env bash -set -e +BUILD_PIPELINE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +REPO_DIR="$BUILD_PIPELINE_DIR/../.." + TEMP_GENERATOR_DIR=".tmp-generator"; -VERSION=$(npm view @alfresco/adf-core@beta version) -JS_VERSION=$(npm view @alfresco/js-api@alpha version) +BRANCH_TO_CREATE="update-alfresco-dependencies" +TOKEN="" +PR_NUMBER="" show_help() { - echo "Usage: update-project.sh" + echo "Usage: create-updatebranch.sh" echo "" - echo "-t or --token Github ouath token" - echo "-n or --name Github name of the project" - echo "-v or --version ADF version if not passed will use the beta" - echo "-vjs or --vjs JS API version if not passed will use the beta" + echo "-t or --token: Github ouath token" + echo "-p or --pr: Originating jsapi PR number" + echo "-v or --version version to update" } -token() { +set_token() { TOKEN=$1 } -vjs() { - JS_VERSION=$1 +set_pr() { + PR_NUMBER=$1 } version() { VERSION=$1 } -name_repo() { +update_dependency() { + PKG=$1 + PKG_VERSION=$(npm view $PKG@$VERSION version) + echo "Update $PKG to $PKG_VERSION in $NAME_REPO" + + for i in $(find . ! -path "*/node_modules/*" -name "package-lock.json" | xargs grep -l $PKG); do + directory=$(dirname $i) + echo "Update $PKG in $directory" + ( cd $directory ; npm i --ignore-scripts $PKG@$PKG_VERSION --save-exact) + done + + git add . + git commit -n -m "[auto-commit] Update $PKG to $PKG_VERSION for branch: $BRANCH_TO_CREATE originated from $PKG PR: $PR_NUMBER" +} + +update_js_dependency() { + PKG=$1 + PKG_VERSION=$2 + echo "Update $PKG to $PKG_VERSION in $NAME_REPO" + + for i in $(find . ! -path "*/node_modules/*" -name "package-lock.json" | xargs grep -l $PKG); do + directory=$(dirname $i) + echo "Update $PKG in $directory" + ( cd $directory ; npm i --ignore-scripts $PKG@$PKG_VERSION --save-exact) + done + + git add . + git commit -n -m "[auto-commit] Update $PKG to $PKG_VERSION for branch: $BRANCH_TO_CREATE originated from $PKG PR: $PR_NUMBER" +} + +update() { NAME_REPO=$1 + echo "Update dependencies $NAME_REPO" + + git clone https://$TOKEN@github.com/Alfresco/$NAME_REPO.git $TEMP_GENERATOR_DIR + cd $TEMP_GENERATOR_DIR + + git fetch + + # Checkout branch if exist, otherwise create it + git checkout $BRANCH_TO_CREATE 2>/dev/null || git checkout -b $BRANCH_TO_CREATE origin/develop + + update_js_dependency "@alfresco/js-api" $JS_API_INSTALLED + update_dependency "@alfresco/adf-extensions" + update_dependency "@alfresco/adf-core" + update_dependency "@alfresco/adf-content-services" + update_dependency "@alfresco/adf-process-services" + update_dependency "@alfresco/adf-process-services-cloud" + + git push origin $BRANCH_TO_CREATE + + node $BUILD_PIPELINE_DIR/pr-creator.js --token=$TOKEN --title="Update branch for ADF and JS-API" --head=$BRANCH_TO_CREATE --repo=$NAME_REPO + + cd .. + rm -rf $TEMP_GENERATOR_DIR } while [[ $1 == -* ]]; do case "$1" in -h|--help|-\?) show_help; exit 0;; - -n|--name|-\?) name_repo $2; shift 2;; - -t|--token) token $2; shift 2;; + -t|--token) set_token $2; shift; shift;; + -p|--pr) set_pr $2; shift; shift;; -v|--version) version $2; shift 2;; - -vjs|--vjs) vjs $2; shift 2;; -*) echo "invalid option: $1" 1>&2; show_help; exit 1;; esac done -rm -rf $TEMP_GENERATOR_DIR; +cd "$REPO_DIR" -git clone https://$TOKEN@github.com/$NAME_REPO.git $TEMP_GENERATOR_DIR -cd $TEMP_GENERATOR_DIR -git checkout develop +JS_API_INSTALLED=$(npm list @alfresco/js-api --depth=0 --json | jq -r '.dependencies["@alfresco/js-api"].version') -BRANCH="ADF-update-$VERSION" -git checkout -b $BRANCH +echo "Current installed JS-API $JS_API_INSTALLED" -./scripts/update-version.sh -gnu -v $VERSION -vj $JS_VERSION +if [[ (-z "$TOKEN") || (-z "$VERSION") ]] + then + echo "Each of 'branch name' (-b) token (-t) and pr number (-p) have to be set. See -help." + exit 1; +fi -git add . -git commit -m "Update ADF packages version $VERSION" -git push -u origin $BRANCH +rm -rf $TEMP_GENERATOR_DIR -curl -H "Authorization: token $TOKEN" -X POST -d '{"body":"Update ADF packages version '$VERSION'","head":"'$BRANCH'","base":"develop","title":"Update ADF packages version '$VERSION'"}' https://api.github.com/repos/$NAME_REPO/pulls +update "generator-alfresco-adf-app" +update "alfresco-content-app" +update "alfresco-apps" +update "alfresco-digital-workspace-app" -rm -rf $TEMP_GENERATOR_DIR; + +exit $?