From b15cb68c3ee66e8dc868e7d5716693d91ce2dd0b Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Tue, 25 Oct 2022 19:33:24 +0200 Subject: [PATCH 1/7] Add new workflow based on smart cherry-picking for Meeds Dists --- github/meeds-picks.sh | 128 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100755 github/meeds-picks.sh diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh new file mode 100755 index 00000000..9624e550 --- /dev/null +++ b/github/meeds-picks.sh @@ -0,0 +1,128 @@ +#!/bin/bash -eu + +### Colors +info() { +echo -e "\033[1;34m[Info]\033[0m $1" +} + +error() { +echo -e "\033[1;31m[Error]\033[0m $1" +} + +success() { +echo -e "\033[1;32m[Success]\033[0m $1" +} +### + +export FILTER_BRANCH_SQUELCH_WARNING=1 #filter-branch hide warnings +export DEFAULT_CP_DAYS_BEFORE=3 # default cherry-picks checkpoint in days in case of absence of tag + +isAlreadyBackported() { + local ref="$2" + local TARGET_COMMIT_PATCHID=$( + git show --patch-with-raw "$1" | + git patch-id | + cut -d' ' -f1 + ) + + local MATCHING_COMMIT_SHA="" + for c in $(git rev-list origin/$ref ); do + if [[ $(git show --patch-with-raw "$c" | git patch-id | cut -d' ' -f1) == "${TARGET_COMMIT_PATCHID}" ]]; then + MATCHING_COMMIT_SHA=$c + break; + fi + done + [ ! -z "$MATCHING_COMMIT_SHA" ] +} + +seedfileraw=$(mktemp) +seedfilefiltred=$(mktemp) + +current_date=$(date '+%s') +echo "Parsing Modules repositories from catalog..." +curl -H "Authorization: token ${GIT_TOKEN}" \ + -H 'Accept: application/vnd.github.v3.raw' \ + -L "https://api.github.com/repos/exoplatform/swf-jenkins-pipeline/contents/dsl-jobs/platform/seed_jobs_ci.groovy" --output ${seedfilefiltred} +cat ${seedfilefiltred} | grep "${DIST_BRANCH}" | grep "project:" > ${seedfileraw} +modules_length=$(wc -l ${seedfileraw} | awk '{ print $1 }') +counter=1 +echo "Done. Performing action..." +ret=0 +baseDir=$PWD +while IFS= read -r line; do + item=$(echo $line | awk -F'project:' '{print $2}' | cut -d "," -f 1 | tr -d "'"| xargs) + org=$(echo $line | awk -F'gitOrganization:' '{print $2}' | cut -d "," -f 1 | tr -d "'" | tr -d "]"| xargs) + [ -z "${item}" ] && continue + [ -z "${org}" ] && continue + echo "=================================================================================================" + echo -e " Module (${counter}/${modules_length}): \e]8;;http://github.com/${org}/${item}\a${org}/${item}\e]8;;\a" + echo "=================================================================================================" + cd $baseDir + git init $item &>/dev/null + cd $item &>/dev/null + git remote add origin git@github.com:${org}/${item}.git &>/dev/null + git fetch origin develop ${DIST_BRANCH} --tags &>/dev/null + checkpointTag="@cp-${DIST_BRANCH}" + if ! git rev-parse $checkpointTag 2>/dev/null; then + tag_commit=$(git log --no-merges --pretty=format:"%H" --since="${DEFAULT_CP_DAYS_BEFORE}days" | tail -1) + git tag $checkpointTag $tag_commit + fi + messageCP=$(git show --pretty=format:%s -s $checkpointTag ) + echo "Cherry-pick checkpoint is at: ($(git rev-parse --short $checkpointTag)) $messageCP." + prev_head=$(git rev-parse --short origin/$DIST_BRANCH) + # Applying cherry-picks + commitIds=$(git log --no-merges --pretty=format:"%H" $checkpointTag..develop | xargs) + if [ -z "${commitIds:-}" ]; then + echo "Nothing to backport!" + git push origin ${checkpointTag} -f &>/dev/null + fi + echo "Start backporting..." + echo + git checkout $DIST_BRANCH + for commitId in $commitIds; do + message=$(git show --pretty=format:%s -s $commitId ) + body=$(git show --pretty=format:%b -s $commitId ) + if [[ "${body:-}" =~ "#dontcp" ]]; then + echo "Skipped marked commit: ($(git rev-parse --short $commitId)) $message!" + continue + fi + if isAlreadyBackported $commitId origin/$DIST_BRANCH; then + echo "Skipped already backported commit: ($(git rev-parse --short $commitId)) $message!" + continue + fi + echo "Cherry-picking commit: ($(git rev-parse --short $commitId)) $message..." + if ! git cherry-pick -x $commitId; then + ret=1 + echo "Failed to cherry-pick $commitId! Please fix it manually and update tag: ${checkpointTag} on the source commit: $commitId of develop branch, then relaunch this job!" + continue + fi + done + if [ ! -z "$(git diff origin/${DIST_BRANCH} 2>/dev/null)" ]; then + info "Reseting commits authors..." + git filter-branch --commit-filter 'export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"; git commit-tree "$@"' -- origin/$DIST_BRANCH..HEAD + info "Changes to be pushed:" + echo -e "\033[1;32m****\033[0m" + git log origin/${DIST_BRANCH}..HEAD --oneline --pretty=format:"(%C(yellow)%h%Creset) %s" + echo -e "\n\033[1;32m****\033[0m" + else + info "No changes detected!" + fi + new_head=$(git rev-parse --short HEAD) + if [ "${prev_head}" != "${new_head}" ]; then + info "Previous $DIST_BRANCH HEAD: \033[1;31m${prev_head}\033[0m, New $DIST_BRANCH HEAD: \033[1;32m${new_head}\033[0m." + git push origin HEAD:${DIST_BRANCH} | grep -v remote ||: + fi + info "Previous CP Checkpoint HEAD: \033[1;31m$(git rev-parse --short ${$checkpointTag})\033[0m, New CP Checkpoint HEAD: \033[1;32m$(git rev-parse --short origin/develop)\033[0m." + git tag ${$checkpointTag} origin/develop -f + git push origin ${$checkpointTag} -f +done < ${seedfileraw} +echo "Cleaning up temporary files..." +rm -v ${seedfileraw} +rm -v ${seedfilefiltred} +echo "=================================================================================================" +if [ $ret -eq "0" ]; then + success "Backporting done!" +else + error "Some Backports have failed!" +fi +exit $ret \ No newline at end of file From fa7ff495336c2a8e7ca850a5c2304390b1607f04 Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Tue, 25 Oct 2022 21:46:17 +0200 Subject: [PATCH 2/7] Refactor code --- github/meeds-picks.sh | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh index 9624e550..e8e84db0 100755 --- a/github/meeds-picks.sh +++ b/github/meeds-picks.sh @@ -63,40 +63,46 @@ while IFS= read -r line; do git remote add origin git@github.com:${org}/${item}.git &>/dev/null git fetch origin develop ${DIST_BRANCH} --tags &>/dev/null checkpointTag="@cp-${DIST_BRANCH}" - if ! git rev-parse $checkpointTag 2>/dev/null; then + if ! git rev-parse ${checkpointTag} 2>/dev/null; then tag_commit=$(git log --no-merges --pretty=format:"%H" --since="${DEFAULT_CP_DAYS_BEFORE}days" | tail -1) - git tag $checkpointTag $tag_commit + git tag ${checkpointTag} ${tag_commit} fi - messageCP=$(git show --pretty=format:%s -s $checkpointTag ) - echo "Cherry-pick checkpoint is at: ($(git rev-parse --short $checkpointTag)) $messageCP." + messageCP=$(git show --pretty=format:%s -s ${checkpointTag} ) + info "Cherry-pick checkpoint is at: ($(git rev-parse --short ${checkpointTag})) $messageCP." prev_head=$(git rev-parse --short origin/$DIST_BRANCH) # Applying cherry-picks - commitIds=$(git log --no-merges --pretty=format:"%H" $checkpointTag..develop | xargs) + commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop | xargs) if [ -z "${commitIds:-}" ]; then - echo "Nothing to backport!" + info "Nothing to backport!" git push origin ${checkpointTag} -f &>/dev/null + continue fi echo "Start backporting..." echo - git checkout $DIST_BRANCH + cherryPickFailed=false for commitId in $commitIds; do message=$(git show --pretty=format:%s -s $commitId ) body=$(git show --pretty=format:%b -s $commitId ) if [[ "${body:-}" =~ "#dontcp" ]]; then - echo "Skipped marked commit: ($(git rev-parse --short $commitId)) $message!" + info "Skipped marked commit: ($(git rev-parse --short $commitId)) $message!" continue fi - if isAlreadyBackported $commitId origin/$DIST_BRANCH; then - echo "Skipped already backported commit: ($(git rev-parse --short $commitId)) $message!" + if isAlreadyBackported $commitId $DIST_BRANCH; then + info "Skipped already backported commit: ($(git rev-parse --short $commitId)) $message!" continue fi - echo "Cherry-picking commit: ($(git rev-parse --short $commitId)) $message..." + info "Cherry-picking commit: ($(git rev-parse --short $commitId)) $message..." if ! git cherry-pick -x $commitId; then ret=1 - echo "Failed to cherry-pick $commitId! Please fix it manually and update tag: ${checkpointTag} on the source commit: $commitId of develop branch, then relaunch this job!" - continue + error "Cherry-pick failed! Please fix it manually and update tag: ${checkpointTag} on the source commit: $commitId of develop branch, then relaunch this job!" + cherryPickFailed=true + break fi done + if $cherryPickFailed; then + error "Failed to backport commits for ${org}/${item}!" + continue + fi if [ ! -z "$(git diff origin/${DIST_BRANCH} 2>/dev/null)" ]; then info "Reseting commits authors..." git filter-branch --commit-filter 'export GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; export GIT_COMMITTER_EMAIL="$GIT_AUTHOR_EMAIL"; git commit-tree "$@"' -- origin/$DIST_BRANCH..HEAD @@ -112,9 +118,9 @@ while IFS= read -r line; do info "Previous $DIST_BRANCH HEAD: \033[1;31m${prev_head}\033[0m, New $DIST_BRANCH HEAD: \033[1;32m${new_head}\033[0m." git push origin HEAD:${DIST_BRANCH} | grep -v remote ||: fi - info "Previous CP Checkpoint HEAD: \033[1;31m$(git rev-parse --short ${$checkpointTag})\033[0m, New CP Checkpoint HEAD: \033[1;32m$(git rev-parse --short origin/develop)\033[0m." - git tag ${$checkpointTag} origin/develop -f - git push origin ${$checkpointTag} -f + info "Previous CP Checkpoint HEAD: \033[1;31m$(git rev-parse --short ${checkpointTag})\033[0m, New CP Checkpoint HEAD: \033[1;32m$(git rev-parse --short origin/develop)\033[0m." + git tag ${checkpointTag} origin/develop -f + git push origin ${checkpointTag} -f done < ${seedfileraw} echo "Cleaning up temporary files..." rm -v ${seedfileraw} From 6c56b39e6f908b36cb28ab88ee12e71f49875b9d Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Tue, 25 Oct 2022 21:47:06 +0200 Subject: [PATCH 3/7] Refactor code --- github/meeds-picks.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh index e8e84db0..418e3bc4 100755 --- a/github/meeds-picks.sh +++ b/github/meeds-picks.sh @@ -78,7 +78,6 @@ while IFS= read -r line; do continue fi echo "Start backporting..." - echo cherryPickFailed=false for commitId in $commitIds; do message=$(git show --pretty=format:%s -s $commitId ) From 99eaf11bba57af5590ae91fd4c10ab60f6c78e99 Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Tue, 25 Oct 2022 22:12:49 +0200 Subject: [PATCH 4/7] Invert commits list --- github/meeds-picks.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh index 418e3bc4..c666def2 100755 --- a/github/meeds-picks.sh +++ b/github/meeds-picks.sh @@ -71,7 +71,7 @@ while IFS= read -r line; do info "Cherry-pick checkpoint is at: ($(git rev-parse --short ${checkpointTag})) $messageCP." prev_head=$(git rev-parse --short origin/$DIST_BRANCH) # Applying cherry-picks - commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop | xargs) + commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop | xargs | tac -s ' ' | xargs) if [ -z "${commitIds:-}" ]; then info "Nothing to backport!" git push origin ${checkpointTag} -f &>/dev/null From ed121822621d76b32f7868ab96df301a42e6260d Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Tue, 25 Oct 2022 22:17:00 +0200 Subject: [PATCH 5/7] Use reverse flag --- github/meeds-picks.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh index c666def2..055377d6 100755 --- a/github/meeds-picks.sh +++ b/github/meeds-picks.sh @@ -71,7 +71,7 @@ while IFS= read -r line; do info "Cherry-pick checkpoint is at: ($(git rev-parse --short ${checkpointTag})) $messageCP." prev_head=$(git rev-parse --short origin/$DIST_BRANCH) # Applying cherry-picks - commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop | xargs | tac -s ' ' | xargs) + commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop --reverse | xargs) if [ -z "${commitIds:-}" ]; then info "Nothing to backport!" git push origin ${checkpointTag} -f &>/dev/null From 3844ca7b74e1fd30e9eca9227dd8f44c5bc1a75e Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Tue, 25 Oct 2022 22:32:13 +0200 Subject: [PATCH 6/7] Add Unreferenced case + Add comments + Add logs --- github/meeds-picks.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh index 055377d6..59954973 100755 --- a/github/meeds-picks.sh +++ b/github/meeds-picks.sh @@ -63,12 +63,22 @@ while IFS= read -r line; do git remote add origin git@github.com:${org}/${item}.git &>/dev/null git fetch origin develop ${DIST_BRANCH} --tags &>/dev/null checkpointTag="@cp-${DIST_BRANCH}" - if ! git rev-parse ${checkpointTag} 2>/dev/null; then + # if CP-Checkpoint (cherry-pick checkpoint) is not created; create a new one base on develop with some days delay + # else, check if this checkpoint belongs to the develop history (Opposite can be caused by commits removal on develop branch); OK, else create a new one with some days delay. + if ! git rev-parse ${checkpointTag} 2>/dev/null || ! git rev-list origin/develop | grep -q $(git rev-parse ${checkpointTag}); then + if git rev-parse ${checkpointTag} >/dev/null; then + info "Dangling CP-Checkpoint ($(git rev-parse --short ${checkpointTag})) is not found on base branch!" + echo "Reseting it..." + else + info "CP-Checkpoint is not found" + echo "Generating new one..." + fi tag_commit=$(git log --no-merges --pretty=format:"%H" --since="${DEFAULT_CP_DAYS_BEFORE}days" | tail -1) git tag ${checkpointTag} ${tag_commit} + info "OK" fi messageCP=$(git show --pretty=format:%s -s ${checkpointTag} ) - info "Cherry-pick checkpoint is at: ($(git rev-parse --short ${checkpointTag})) $messageCP." + info "CP-Checkpoint is at: ($(git rev-parse --short ${checkpointTag})) $messageCP." prev_head=$(git rev-parse --short origin/$DIST_BRANCH) # Applying cherry-picks commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop --reverse | xargs) @@ -117,7 +127,7 @@ while IFS= read -r line; do info "Previous $DIST_BRANCH HEAD: \033[1;31m${prev_head}\033[0m, New $DIST_BRANCH HEAD: \033[1;32m${new_head}\033[0m." git push origin HEAD:${DIST_BRANCH} | grep -v remote ||: fi - info "Previous CP Checkpoint HEAD: \033[1;31m$(git rev-parse --short ${checkpointTag})\033[0m, New CP Checkpoint HEAD: \033[1;32m$(git rev-parse --short origin/develop)\033[0m." + info "Previous CP-Checkpoint HEAD: \033[1;31m$(git rev-parse --short ${checkpointTag})\033[0m, New CP-Checkpoint HEAD: \033[1;32m$(git rev-parse --short origin/develop)\033[0m." git tag ${checkpointTag} origin/develop -f git push origin ${checkpointTag} -f done < ${seedfileraw} From f4e456d6428b367b80750b4681603fd2580fd2a9 Mon Sep 17 00:00:00 2001 From: Houssem Ben Ali Date: Thu, 27 Oct 2022 16:19:43 +0200 Subject: [PATCH 7/7] List only first parent for merge commits --- github/meeds-picks.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/meeds-picks.sh b/github/meeds-picks.sh index 59954973..e17c529d 100755 --- a/github/meeds-picks.sh +++ b/github/meeds-picks.sh @@ -81,7 +81,7 @@ while IFS= read -r line; do info "CP-Checkpoint is at: ($(git rev-parse --short ${checkpointTag})) $messageCP." prev_head=$(git rev-parse --short origin/$DIST_BRANCH) # Applying cherry-picks - commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop --reverse | xargs) + commitIds=$(git log --no-merges --pretty=format:"%H" ${checkpointTag}..origin/develop --first-parent --reverse | xargs) if [ -z "${commitIds:-}" ]; then info "Nothing to backport!" git push origin ${checkpointTag} -f &>/dev/null