Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto merge all PRs from upstream #7

Open
milahu opened this issue Mar 5, 2022 · 2 comments
Open

auto merge all PRs from upstream #7

milahu opened this issue Mar 5, 2022 · 2 comments

Comments

@milahu
Copy link

milahu commented Mar 5, 2022

so i heard you want to maintain this fork ; )

here is a script to merge all the pull requests from upstream

#! /usr/bin/env bash

# bash script to automatically merge pull-requests
# form the abandoned upstream repo to this active fork

# author: milahu, license: MIT
# https://github.com/avryhof/speech_recognition/issues/7

set -e

upstream_repo_name=speech_recognition
upstream_repo_owner=Uberi
this_repo_owner=avryhof
upstream_main_branch=master
this_main_branch=$upstream_main_branch
committer_main_branch=$upstream_main_branch
committer_repo_owner=milahu # need write access to this fork
# git remote add milahu https://milahu:[email protected]/milahu/speech_recognition

echo "getting open pulls from https://github.com/$upstream_repo_owner/$upstream_repo_name/pulls"

#pulls_state=all
pulls_state=open # default

merge_upstream_main_branch=true
if $merge_upstream_main_branch; then
  echo "merging main branch from upstream"
  (
    set -x
    git remote add "$upstream_repo_owner" "https://github.com/$upstream_repo_owner/$upstream_repo_name" || true
    git fetch "$upstream_repo_owner" "$upstream_main_branch"
    git checkout "$this_main_branch"
  )
  rev_short=$(git rev-parse --short "$upstream_repo_owner/$upstream_main_branch")
  branch_name="merge-$upstream_repo_owner-$upstream_main_branch-$rev_short"
  if (set -x; git branch "$branch_name")
  then
    (set -x; git switch "$branch_name")
    # use "git merge" to preserve commit hashes
    if (set -x; git merge "$upstream_repo_owner/$upstream_main_branch" -m "merge branch $upstream_repo_owner/$upstream_main_branch")
    then
      #git rebase "$upstream_repo_owner/$upstream_main_branch"
      (set -x; git push "$committer_repo_owner")
      echo "create PR at $upstream_repo_owner/$upstream_repo_name: https://github.com/$committer_repo_owner/$upstream_repo_name/pull/new/$branch_name"
      echo "create PR at $committer_repo_owner/$upstream_repo_name: https://github.com/$committer_repo_owner/$upstream_repo_name/compare/$committer_main_branch...$committer_repo_owner:$branch_name"
      (set -x; git checkout "$this_main_branch")
      echo "done main branch"
    else
      (set -x; git merge --abort)
      echo "failed to auto-merge main branch -> skip"
    fi
    echo "hit enter to continue"
    read
  else
    echo "branch $branch_name exists -> skip merging main branch"
  fi
fi

[ ! -e pulls.json ] && curl -o pulls.json "https://api.github.com/repos/$upstream_repo_owner/$upstream_repo_name/pulls?state=$pulls_state"

clean_name() {
  echo "$1" | sed -E 's/[ \t]+/-/g; s/[^a-zA-Z0-9_-]//g;'
}

num_pulls=$(<pulls.json jq length)
last_index=$((num_pulls - 1))
echo "found $num_pulls open pulls"

#for ((i = 0; i < num_pulls; i++))
#for ((i = 6; i < num_pulls; i++))
for ((i = 0; i < 6; i++))
do
  echo "pull index $i of $last_index"
  json="$(<pulls.json jq ".[$i]")"
  number="$(<<<"$json" jq -r .number)"
  title="$(<<<"$json" jq -r .title)"
  author="$(<<<"$json" jq -r .user.login)"
  head_repo_name="$(<<<"$json" jq -r .head.repo.name)"
  head_repo_owner="$(<<<"$json" jq -r .head.repo.owner.login)"
  head_ref="$(<<<"$json" jq -r .head.ref)"
  head_sha="$(<<<"$json" jq -r .head.sha)"
  base_sha="$(<<<"$json" jq -r .base.sha)"
  base_ref="$(<<<"$json" jq -r .base.ref)"
  echo "original PR: https://github.com/$upstream_repo_owner/$upstream_repo_name/pull/$number"
  echo "title: $title"
  echo "author: $author"
  echo "head_repo_name: $head_repo_name"
  echo "head_repo_owner: $head_repo_owner"
  echo "head_ref: $head_ref"
  echo "head_sha: $head_sha"
  echo "base_sha: $base_sha"

  if [[ "$(<<<"$json" jq -r .head.repo)" == "null" ]]
  then
    echo "FIXME head branch was deleted? -> skip PR"
    # example https://github.com/Uberi/speech_recognition/pull/554
    echo "hit enter to continue"
    read
    continue
  fi

  title_clean=$(clean_name "$title")
  author_clean=$(clean_name "$author")

  # check if this PR was already merged
  # TODO check harder. commit hash may be different
  if [ -n "$(git branch "$this_main_branch" --contains "$head_sha" 2>/dev/null)" ]
  then
    echo "PR $number was already merged -> skip"
    continue
  fi

  # TODO check if this PR was already requested

  (
    set -x

    git fetch "$upstream_repo_owner" "$base_ref"

    git remote add "$head_repo_owner" "https://github.com/$head_repo_owner/$head_repo_name" || true
    git fetch "$head_repo_owner" "$head_ref"

    #git checkout "$head_sha"
    git checkout "$this_main_branch"
  )

  rev_short="${head_sha:0:7}"
  branch_name="merge-$upstream_repo_owner-pr$number-$rev_short-$title_clean-by-$author_clean"

  if (set -x; git branch "$branch_name")
  then

    (set -x; git switch "$branch_name")

    # use "git merge" to preserve commit hashes
    if (set -x; git merge "$head_repo_owner/$head_ref" -m "merge branch $head_repo_owner/$head_ref")
    then
      #git rebase "$upstream_repo_owner/$upstream_main_branch"
      (set -x; git push "$committer_repo_owner")
      echo "create PR at $this_repo_owner/$upstream_repo_name: https://github.com/$committer_repo_owner/$upstream_repo_name/pull/new/$branch_name"
      echo "create PR at $committer_repo_owner/$upstream_repo_name: https://github.com/$committer_repo_owner/$upstream_repo_name/compare/$committer_main_branch...$committer_repo_owner:$branch_name"
      echo "original PR: https://github.com/$upstream_repo_owner/$upstream_repo_name/pull/$number"
      (set -x; git checkout "$this_main_branch")
      echo "done PR $number"
    else
      (set -x; git merge --abort)
      echo "failed to auto-merge PR $number -> skip"
    fi
    echo "hit enter to continue"
    read
  else
    echo "branch $branch_name exists -> skip merging PR $number"
  fi

done
avryhof pushed a commit that referenced this issue Mar 9, 2022
…-by-yoe

Merge uberi pr574 e693e43 allow offline tests by yoe
@avryhof
Copy link
Owner

avryhof commented Mar 9, 2022

Thank you! This will certainly be helpful.

@milahu
Copy link
Author

milahu commented Mar 9, 2022

: )

see also

most merge-conflicts are trivial ...
but i only need deepspeech and google

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants