diff --git a/.travis.yml b/.travis.yml index 2b277d92e8..f84865a1de 100644 --- a/.travis.yml +++ b/.travis.yml @@ -161,7 +161,7 @@ jobs: - 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 + script: ./scripts/travis/update/update-project.sh -p $TRAVIS_BUILD_NUMBER -t $GITHUB_TOKEN -v alpha -c $TRAVIS_COMMIT workspaces: use: built_libs_cache diff --git a/scripts/travis/update/pr-creator.js b/scripts/travis/update/pr-creator.js index 1768085803..acd307ec48 100644 --- a/scripts/travis/update/pr-creator.js +++ b/scripts/travis/update/pr-creator.js @@ -2,25 +2,94 @@ const GitHub = require('github-api'); let program = require('commander'); const ORGANISATION = 'Alfresco'; +const ORIGIN_REPO = 'alfresco-ng2-components'; +const ATTEMPT_MSG = [ + `Could you check it please? 🤖`, + `Emm did you forget? 🤡`, + `Where are you? 🤷`, + `We are going to die!! 👻`, + `I guess the Apocalypse happened and I am alone 👽` +]; + +GIVE_UP_MSG = `I gave up, it will be fix eventually 🔴`; class PrCreator { - constructor(githubUser, githubRepo, token) { + constructor(githubUser, githubRepo, token, commit) { this.github = new GitHub({token}); - this.repo = this.github.getRepo(githubUser, githubRepo); + this.repoOrigin = this.github.getRepo(githubUser, ORIGIN_REPO); + this.repoDestination = this.github.getRepo(githubUser, githubRepo); + this.issue = this.github.getIssues(githubUser, githubRepo); + this.commit = commit; } - async create(title, head, base) { - const { data: prs } = await this.repo.listPullRequests({ state: 'open', head: `${ORGANISATION}:${head}`, base }); + async createOrUpdate(title, head, base, commit) { + const { data: prs } = await this.repoDestination.listPullRequests({ state: 'open', head: `${ORGANISATION}:${head}`, base }); if (prs.length < 1) { - const { data: pr } = await this.repo.createPullRequest({ title, head, base }); + const { data: pr } = await this.repoDestination.createPullRequest({ title, head, base, body: `sha:${commit}` }); return pr.number; } else { + const upstreamPrOpen = prs[0]; // override the title to contains the latest adf dep number - prs[0].title = title; + await this.repoDestination.updatePullRequest(upstreamPrOpen.number, { title, body: `sha:${commit}` }); + return upstreamPrOpen.number; } - return prs[0].number; + } + + async fetchContributors(shaFrom, shaTo) { + const mapAuthors = new Map(); + let upstreamShaFound = true; + const listCommits = await this.repoOrigin.listCommits(({sha: shaFrom})) + let index = 0; + while(upstreamShaFound) { + if (listCommits.data[index].sha === shaTo ) { + upstreamShaFound = false; + } else { + mapAuthors.set(listCommits.data[index].author.login, listCommits.data[index].commit.author.name); + } + index++; + } + return mapAuthors; + } + + async createComment(issueOrPrNumber, head, base, shaOriginHead ) { + const shaTo = await this.getShaTo(head, base); + const contributors = await this.fetchContributors(shaOriginHead, shaTo); + const attemptCount = await this.getCommentAmount(issueOrPrNumber); + const commentMsg = this.createCommentBody(contributors, attemptCount); + await this.issue.createIssueComment(issueOrPrNumber, commentMsg); + } + + createCommentBody(contributors, attemptCount) { + const flattenedContributors = this.flattenContributors(contributors); + const attemptMsg = attemptCount <= 5 ? ATTEMPT_MSG[attemptCount] : GIVE_UP_MSG + const tmpl = ` Attempt: ${attemptCount+1} + you are part of the contributors: + ${flattenedContributors} + ${attemptMsg} + `; + return tmpl; + } + + flattenContributors(contributors) { + let names = []; + for (let key of contributors.keys()) { + names.push(`@${key}`) + } + return names.join(', '); + } + + async getShaTo(head, base) { + const { data: closedUpstreamPRs } = await this.repoDestination.listPullRequests({ state: 'closed', head: `${ORGANISATION}:${head}`, base }); + const latestClosedUpstream = closedUpstreamPRs[0]; + const shaTo = latestClosedUpstream.body.split(':')[1].trim(); + return shaTo; + } + + async getCommentAmount(issueOrPrNumber) { + const { data: listComments } = await this.issue.listIssueComments(issueOrPrNumber); + return listComments.length; } } @@ -32,17 +101,22 @@ async function main() { .option('-t, --token [type]', 'token') .option('-h, --head [type]', 'head') .option('-r, --repo [type]', 'repo') + .option('-c, --commit [type]', 'commit') .option('-title, --title [type]', 'title') .parse(process.argv); - const { token, title, head, repo } = program, - prCreator = new PrCreator(ORGANISATION, repo, token); + const { token, title, head, repo, commit } = program, + prCreator = new PrCreator(ORGANISATION, repo, token, commit); if (!token || !head || !title) { throw new Error('Each of the parameters have to be provided. --token, --title, --head'); } + const baseBranchName = 'develop'; - return prCreator.create(title, head, 'develop'); + const prNumber = await prCreator.createOrUpdate(title, head, baseBranchName, commit); + await prCreator.createComment(prNumber, head, baseBranchName, commit); + + return prNumber; } main() diff --git a/scripts/travis/update/update-project.sh b/scripts/travis/update/update-project.sh index f5f7669209..3130b32b65 100755 --- a/scripts/travis/update/update-project.sh +++ b/scripts/travis/update/update-project.sh @@ -14,6 +14,7 @@ show_help() { echo "-t or --token: Github ouath token" echo "-p or --pr: Originating jsapi PR number" echo "-v or --version version to update" + echo "-c or --commit The commit that the current build is testing" } set_token() { @@ -28,6 +29,10 @@ version() { VERSION=$1 } +set_commit() { + COMMIT=$1 +} + update_dependency() { PKG=$1 PKG_VERSION=$(npm view $PKG@$VERSION version) @@ -91,7 +96,7 @@ update() { git push --force origin $BRANCH_TO_CREATE fi - node $BUILD_PIPELINE_DIR/pr-creator.js --token=$TOKEN --title="Update branch for ADF ${PR_NUMBER} and JS-API ${JS_API_INSTALLED} [ci:force]" --head=$BRANCH_TO_CREATE --repo=$NAME_REPO + node $BUILD_PIPELINE_DIR/pr-creator.js --token=$TOKEN --title="Update branch for ADF ${PR_NUMBER} and JS-API ${JS_API_INSTALLED} [ci:force]" --head=$BRANCH_TO_CREATE --repo=$NAME_REPO --commit=$COMMIT cd .. rm -rf $TEMP_GENERATOR_DIR @@ -103,6 +108,7 @@ while [[ $1 == -* ]]; do -t|--token) set_token $2; shift; shift;; -p|--pr) set_pr $2; shift; shift;; -v|--version) version $2; shift 2;; + -c|--commit) set_commit $2; shift 2;; -*) echo "invalid option: $1" 1>&2; show_help; exit 1;; esac done