Skip to content

Instantly share code, notes, and snippets.

@eriwen
Created October 30, 2017 21:51
Show Gist options
  • Save eriwen/b833129e1eaeb8de237d6d9cb868b505 to your computer and use it in GitHub Desktop.
Save eriwen/b833129e1eaeb8de237d6d9cb868b505 to your computer and use it in GitHub Desktop.
Automatically fix Kotlin version declared in build.gradle.kts
#!/usr/bin/env zsh
OAUTH_HEADER="Authorization: token ${GITHUB_OAUTH_TOKEN}"
GITHUB_API_URL="https://api.github.com"
# List Repos that have a .kts file containing 'kotlin("jvm")'
AFFECTED_REPOS=$(curl -H "${OAUTH_HEADER}" "${GITHUB_API_URL}/search/code?q=kotlin%28jvm%29+extension%3Akts" | jq '.items[].repository.full_name' | sed -e 's/"//g' | sort | uniq)
typeset -A version_hash
version_hash=(4.3 1.1.51 4.2.1 1.1.4-3 4.2 1.1.4-3 4.1 1.1.3-2 4.0 1.1.0 3.5 1.1.0)
# Fork and clone all repos
for repo_full_name in $(printf "%s\n" "${AFFECTED_REPOS[@]}"); do
echo "Cloning ${repo_full_name}"
git clone "https://github.com/${repo_full_name}.git" ${repo_full_name}
if [[ $(find ${repo_full_name} -name build.gradle.kts -exec egrep "kotlin\(\"jvm\"\)\s*$" {} \;) ]]; then
# Parse Gradle version from wrapper properties so we know which version of kotlin to use
wrapper_properties_file="${repo_full_name}/gradle/wrapper/gradle-wrapper.properties"
if [[ -e ${wrapper_properties_file} ]]; then
gradle_version=$(grep distributionUrl ${wrapper_properties_file} | egrep -o "\d\.\d(\.\d)?")
echo "Detected Gradle version ${gradle_version} for ${repo_full_name}."
forked_repo_name=$(echo ${repo_full_name} | sed -e 's/^[[:alnum:]\-]*\//eriwen\//g')
echo "Forking ${repo_full_name} to ${forked_repo_name}"
# See https://developer.github.com/v3/repos/forks/#create-a-fork
curl -X POST -H "${OAUTH_HEADER}" "${GITHUB_API_URL}/repos/${repo_full_name}/forks"
git clone "git@github.com:${forked_repo_name}.git" ${forked_repo_name}
# Declare kotlin version in build file
build_files=$(find ${forked_repo_name} -name build.gradle.kts)
for build_file in $(printf "%s\n" "${build_files[@]}"); do
kotlin_version=${version_hash[$gradle_version]}
echo "Declaring Kotlin version ${kotlin_version} for ${build_file}"
sed -i '' -e "s/kotlin(\"jvm\")\s*$/kotlin(\"jvm\") version \"${kotlin_version}\"/g" ${build_file}
done
echo "Committing to ${forked_repo_name}"
cd ${forked_repo_name}
git add -A
git commit -m 'Declare Kotlin version in Gradle build scripts
See https://github.com/gradle/kotlin-dsl/releases/tag/v0.12.0'
git push
# NOTE: must create JSON for PR in /tmp/postdata
curl -X POST -H "${OAUTH_HEADER}" "${GITHUB_API_URL}/repos/${repo_full_name}/pulls" -d @/tmp/postdata
echo "Cleaning up ${repo_full_name}"
cd -
rm -rf ${repo_full_name}
else
echo "Undeclared Kotlin version not found. Removing ${repo_full_name}"
rm -rf ${repo_full_name}
fi
else
echo "NOT FOUND: ${wrapper_properties_file}. Removing ${repo_full_name}"
rm -rf ${repo_full_name}
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment