From b1e95b4bbc19f829e0e3c262163efb9847a3e1ea Mon Sep 17 00:00:00 2001 From: Dmitri Dolguikh Date: Wed, 24 Mar 2021 12:15:50 -0700 Subject: [PATCH 1/2] added a merge workflow to automator.sh script Signed-off-by: Dmitri Dolguikh --- tools/automator/automator.sh | 55 ++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 9 deletions(-) diff --git a/tools/automator/automator.sh b/tools/automator/automator.sh index 7d39db53d79..c57569a981f 100755 --- a/tools/automator/automator.sh +++ b/tools/automator/automator.sh @@ -27,7 +27,7 @@ cleanup() { } get_opts() { - if opt="$(getopt -o '' -l branch:,org:,repo:,title:,match-title:,body:,labels:,user:,email:,modifier:,script-path:,cmd:,token-path:,token:,strict,dry-run,verbose -n "$(basename "$0")" -- "$@")"; then + if opt="$(getopt -o '' -l branch:,org:,repo:,title:,match-title:,body:,labels:,user:,email:,modifier:,script-path:,cmd:,token-path:,token:,merge-repository:,merge-branch:,strict,dry-run,verbose -n "$(basename "$0")" -- "$@")"; then eval set -- "$opt" else print_error_and_exit "unable to parse options" @@ -97,6 +97,14 @@ get_opts() { token_path="$tmp_token" shift 2 ;; + --merge-repository) + merge_repository="$2" + shift 2 + ;; + --merge-branch) + merge_branch="$2" + shift 2 + ;; --verbose) shell_args+=("-x") shift @@ -125,6 +133,7 @@ validate_opts() { sha="$(current_sha)" sha_short="$(current_sha --short)" commit_date="$(commit_date)" + merge=false if [ -z "${strict:-}" ]; then strict=false @@ -138,6 +147,10 @@ validate_opts() { branch="$(current_branch)" fi + if [ ! -z "${merge_repository:-}" ] && [ ! -z "${merge_branch:-}" ]; then + merge=true + fi + if [ -z "${title_tmpl:-}" ]; then title_tmpl='Automator: update $AUTOMATOR_ORG/$AUTOMATOR_REPO@$AUTOMATOR_BRANCH-$AUTOMATOR_MODIFIER' fi @@ -162,8 +175,8 @@ validate_opts() { print_error_and_exit "token_path or token is a required option" fi - if [ ! -f "${script_path:-}" ]; then - print_error_and_exit "script-path or cmd is a required option" + if [ ! -f "${script_path:-}" ] && ! $merge; then + print_error_and_exit "either script-path, cmd, or merge-repository and merge-branch are required" fi if [ -z "${modifier:-}" ]; then @@ -229,6 +242,26 @@ commit() { add_labels } +merge() { + local src_branch="${AUTOMATOR_SRC_BRANCH:-none}" + fork_name="$src_branch-$branch-$modifier-$(hash "$title")" + git remote add -f -t "$merge_branch" upstream "$merge_repository" + git -c "user.name=$user" -c "user.email=$email" merge --no-ff -m "$title" --log upstream/"$merge_branch" + local code=$? + if [ "$code" -ne 0 ]; then + print_error "$(git status)" 1 + else + if [[ "$(git show --shortstat)" =~ "$title" ]]; then + git show --shortstat + git push --force "https://$user:$token@github.com/$user/$repo.git" "HEAD:$fork_name" + pull_request="$(create_pr)" + add_labels + else + print_error "No changes to merge" 0 + fi + fi +} + work() { ( set -e @@ -247,14 +280,18 @@ work() { ( AUTOMATOR_REPO_DIR="$(pwd)" - GOPATH="${gopath}" bash "${shell_args[@]}" "$script_path" "${script_args[@]}" + if $merge; then + merge + else + GOPATH="${gopath}" bash "${shell_args[@]}" "$script_path" "${script_args[@]}" - git add --all + git add --all - if ! git diff --cached --quiet --exit-code; then - commit - elif $strict; then - print_error "no diff for $repo" 1 + if ! git diff --cached --quiet --exit-code; then + commit + elif $strict; then + print_error "no diff for $repo" 1 + fi fi popd From 878c95bfcd1064f570010fb3c1c98baf6f28061d Mon Sep 17 00:00:00 2001 From: Dmitri Dolguikh Date: Wed, 24 Mar 2021 12:29:18 -0700 Subject: [PATCH 2/2] Fixed issues picked up by the linter --- tools/automator/automator.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/automator/automator.sh b/tools/automator/automator.sh index c57569a981f..aba86a3759c 100755 --- a/tools/automator/automator.sh +++ b/tools/automator/automator.sh @@ -147,7 +147,7 @@ validate_opts() { branch="$(current_branch)" fi - if [ ! -z "${merge_repository:-}" ] && [ ! -z "${merge_branch:-}" ]; then + if [ -n "${merge_repository:-}" ] && [ -n "${merge_branch:-}" ]; then merge=true fi @@ -251,7 +251,7 @@ merge() { if [ "$code" -ne 0 ]; then print_error "$(git status)" 1 else - if [[ "$(git show --shortstat)" =~ "$title" ]]; then + if [[ "$(git show --shortstat)" =~ $title ]]; then git show --shortstat git push --force "https://$user:$token@github.com/$user/$repo.git" "HEAD:$fork_name" pull_request="$(create_pr)"