Skip to content

Instantly share code, notes, and snippets.

@kphrx
Last active April 15, 2019 15:23
Show Gist options
  • Save kphrx/66ba1ccea2bc484e02ff80b5f8b5cdde to your computer and use it in GitHub Desktop.
Save kphrx/66ba1ccea2bc484e02ff80b5f8b5cdde to your computer and use it in GitHub Desktop.
#!/bin/bash
# Need scope: public_repo
GITHUB_TOKEN=<token>
GITHUB_USER=<username>
# Get latest version from git repository
#cd $(dirname $0)
#
#git checkout master
#git config --get remote.upstream.url
#if [ $? = 1 ]; then
# git remote add upstream https://github.com/h3poteto/whalebird-desktop
#fi
#git pull upstream master
#git pull upstream --tags
#git push origin master
#git push --tags
#
#new_version=$(git describe --abbrev=0 --tags)
# Get latest version from github api
new_version=$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/h3poteto/whalebird-desktop/releases/latest | jq -r '.tag_name')
#brew tap $(echo "$GITHUB_USER" | tr '[:upper:]' '[:lower:]')/cask
cd /usr/local/Homebrew/Library/Taps/$(echo "$GITHUB_USER" | tr '[:upper:]' '[:lower:]')/homebrew-cask
git checkout master
git config --get remote.upstream.url
if [ $? = 1 ]; then
git remote add upstream https://github.com/Homebrew/homebrew-cask
fi
git pull upstream master
git fetch --all --prune
git push origin master
# Remove deleted branch
# Use https://github.com/eed3si9n/git-gone
# git gone -D
master_version=$(cat Casks/whalebird.rb | grep 'version ' | sed -e "s/.*version '\(.*\)'/\1/g")
git for-each-ref --format='%(refname:short)' 'refs/heads/update-whalebird-to-*' | grep -v $new_version | xargs git branch -D
new_branch=update-whalebird-to-$new_version
git rev-parse --verify --quiet $new_branch
exists_branch=$?
if [[ $master_version == $new_version ]]; then
echo "Whalebird cask already up to date"
if [ $exists_branch != 1 ]; then
git branch -D $new_branch
fi
exit 1
fi
if [ $exists_branch = 1 ]; then
echo "Not exists branch for update whalebird to $new_version"
git branch $new_branch
fi
git checkout $new_branch
echo "$master_version to $new_version"
old_version=$(cat Casks/whalebird.rb | grep 'version ' | sed -e "s/.*version '\(.*\)'/\1/g")
new_commit="Update whalebird to $new_version"
if [[ $old_version = $new_version ]]; then
echo "Already committed"
else
sed -i -e "s/version '.*'/version '$new_version'/g" Casks/whalebird.rb
dl_link=$(cat Casks/whalebird.rb | grep 'url' | sed -e "s/.*url \"\(.*\)\"/\1/g" | sed -e "s/#{version}/$new_version/g")
checksum=$(wget -nc -O - $dl_link | shasum -a 256 | awk '{print $1}')
echo "Update checksum: $checksum"
sed -i -e "s/sha256 '.*'/sha256 '$checksum'/g" Casks/whalebird.rb
git add Casks/whalebird.rb
rm -f Casks/whalebird.rb-e
git commit -m "$new_commit"
fi
git push -u origin $new_branch
check_pr=$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/Homebrew/homebrew-cask/pulls?per_page=100)
check_pr_result=$(echo "$check_pr" | jq -r '.[] | select(.title == "'"$new_commit"'")')
check_pr_title=$(echo "$check_pr_result" | jq -r '.title')
if [[ $check_pr_title = $new_commit ]]; then
check_pr_url=$(echo "$check_pr_result" | jq -r '.html_url')
check_pr_date=$(echo "$check_pr_result" | jq -r '.created_at' | sed -e 's/Z/+0000/g')
echo "Already open pull requests"
echo -e "Title: $check_pr_title"
echo -e "Date: $(date -jf "%FT%T%z" "$check_pr_date" "+%F %T")"
echo -e "URL: $check_pr_url"
git checkout master
exit 3;
fi
brew cask audit --download $(echo "$GITHUB_USER" | tr '[:upper:]' '[:lower:]')/cask/whalebird
success_audit=$?
brew cask style --fix $(echo "$GITHUB_USER" | tr '[:upper:]' '[:lower:]')/cask/whalebird
success_style=$?
git checkout master
if [[ $success_audit != 0 && $success_style != 0 ]]; then
echo "Test failed"
exit 2;
fi
result=$(curl -s -H "Authorization: token $GITHUB_TOKEN" https://api.github.com/repos/Homebrew/homebrew-cask/pulls \
-d '{
"title": "'"$new_commit"'",
"body": "After making all changes to the cask:\n\n- [x] `brew cask audit --download {{cask_file}}` is error-free.\n- [x] `brew cask style --fix {{cask_file}}` reports no offenses.\n- [x] The commit message includes the cask’s name and version.\n- [x] The submission is for [a stable version](https://github.com/Homebrew/homebrew-cask/blob/master/doc/development/adding_a_cask.md#finding-a-home-for-your-cask) or [documented exception](https://github.com/Homebrew/homebrew-cask/blob/master/doc/development/adding_a_cask.md#but-there-is-no-stable-version).",
"head": "'"$GITHUB_USER"':'"$new_branch"'",
"base": "master"
}' | sed -e 's/\\r\\n/\\n/g' -e 's/\\r/\\n/g' -e 's/\\n/\\\\n/g')
result_title=$(echo "$result" | jq -r '.title')
result_url=$(echo "$result" | jq -r '.html_url')
result_date=$(echo "$result" | jq -r '.created_at' | sed -e 's/Z/+0000/g')
result_body=$(echo "$result" | jq -r '.body')
result_head=$(echo "$result" | jq -r '.head.label')
result_base=$(echo "$result" | jq -r '.base.label')
echo -e "Title: $result_title"
echo -e "Date: $(date -jf "%FT%T%z" "$result_date" "+%F %T")"
echo -e "URL: $result_url"
echo "Branch:"
echo -e " Base: $result_base"
echo -e " Head: $result_head"
echo "Description:"
echo -e "$result_body" | sed -e 's/^\(.*\)/ \1/g'
exit 0
@kphrx
Copy link
Author

kphrx commented Oct 26, 2018

Revisions 13の変更

  • jsonを読むためにjqを利用するように変更
  • brew cask audit --downloadbrew cask style --fixが成功した際にPull Requestを開くように変更
    • GITHUB_TOKENにトークンを入れる

Revisions 14,15の変更

変数名のミスやコミットメッセージがコマンドとして扱われていたので修正

Revisions 16の変更

Revisions 13でPRを開くようにしたのですでにPRが開いているときにはスキップするように変更

Revisions 17の変更

他人が開いたPRでもスキップ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment