-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9888 from keymanapp/chore/common/open-pull-reques…
…t-script chore(common): unify pull-request creation scripts
- Loading branch information
Showing
6 changed files
with
199 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# | ||
# Commit and upload files as a PR to s.keyman.com, help.keyman.com, or other | ||
# repositories | ||
# | ||
|
||
# Defaults for CI pull requests | ||
# TODO: this may belong better somewhere else | ||
if [[ -z ${KEYMAN_PR_USERNAME+x} ]]; then | ||
KEYMAN_PR_USERNAME="Keyman Build Server" | ||
fi | ||
|
||
if [[ -z ${KEYMAN_PR_EMAIL+x} ]]; then | ||
KEYMAN_PR_EMAIL="[email protected]" | ||
fi | ||
|
||
# Add files to the target repository. Saves and restores PWD | ||
# | ||
# Usage: | ||
# ci_add_files repo_path pathspec... | ||
# Parameters: | ||
# 1: repo_path local path to root of the target git repository | ||
# 2: pathspec... one or more paths to add | ||
# Requires: | ||
# * git | ||
# | ||
function ci_add_files { | ||
local repo="$1" | ||
shift | ||
pushd "$repo" >/dev/null | ||
# Git will return an error if the pathspec does not exist, but will not return | ||
# an error if the pathspec exists but there are no new files to add. This is | ||
# the behaviour we want. | ||
git add "$*" || builder_die "git returned error $? when attempting to add $*" | ||
popd >/dev/null | ||
} | ||
|
||
# Test if there are any changes to the git cache in the target repository. | ||
# Used in conjunction with `ci_add_files` to verify that files have or have | ||
# not been added, depending on the scenario | ||
# | ||
# Usage: | ||
# if ci_repo_has_cached_changes repo_path; then ... fi | ||
# Parameters: | ||
# 1: repo_path local path to root of the target git repository | ||
# Requires: | ||
# * git | ||
# | ||
function ci_repo_has_cached_changes { | ||
local repo="$1" | ||
local return_code=1 | ||
pushd "$repo" >/dev/null | ||
if ! git diff --cached --no-ext-diff --quiet --exit-code; then | ||
return_code=0 | ||
fi | ||
popd >/dev/null | ||
return $return_code | ||
} | ||
|
||
# Opens a pull request on the target repository, first committing cached files | ||
# to a new branch (with a hopefully-unique branch name). git vars user.name and | ||
# user.email will be configured if not already present. Adds the label 'auto' | ||
# to the pull request. | ||
# | ||
# Usage: | ||
# ci_open_pull_request repo branch_base commit_message | ||
# Parameters: | ||
# 1: repo_path local path to root of the target git repository | ||
# 2: branch_base branch name to use (a unique value `/<unique>` will be | ||
# appended) | ||
# 3: commit_message commit message and pull request title to use | ||
# Requires: | ||
# * git | ||
# * hub | ||
# * uuidgen (or TeamCity `$BUILD_NUMBER` if `uuidgen` not present on system) | ||
# Example: | ||
# ci_open_pull_request "$S_KEYMAN_COM" auto/keymanweb/release "auto: KeymanWeb release $VERSION_RELEASE"" | ||
# | ||
function ci_open_pull_request { | ||
local repo="$1" | ||
local branch_base="$2" | ||
local commit_message="$3" | ||
pushd "$repo" >/dev/null | ||
|
||
if ! git config user.name > /dev/null; then | ||
git config user.name "$KEYMAN_PR_USERNAME" | ||
fi | ||
if ! git config user.email > /dev/null; then | ||
git config user.email "$KEYMAN_PR_EMAIL" | ||
fi | ||
|
||
# We want a unique branch name, so we append either a random or the TeamCity | ||
# build number | ||
local uuid= | ||
if [[ -z ${BUILD_NUMBER+x} ]]; then | ||
uuid=$(uuidgen) | ||
else | ||
uuid=TC-$BUILD_NUMBER | ||
fi | ||
|
||
local branch="$branch_base/$uuid" | ||
local current_branch="$(git branch --show-current)" | ||
|
||
builder_echo "Creating new branch '$branch' on '$repo'" | ||
git switch -c "$branch" | ||
builder_echo "$commit_message" | ||
git commit -m "$commit_message" | ||
git push origin "$branch" | ||
builder_echo "Push complete" | ||
|
||
hub pull-request -f --no-edit -l auto | ||
builder_echo "Pull request created" | ||
|
||
git switch "$current_branch" | ||
builder_echo "Switched back to $current_branch" | ||
|
||
popd >/dev/null | ||
|
||
builder_echo "Pull request successfully created" | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Tests the pull-requests.inc.sh functions. This is not really intended to be | ||
# used as an automated test, but rather only to be run manually after changes to | ||
# pull-requests.inc.sh. | ||
# | ||
|
||
## START STANDARD BUILD SCRIPT INCLUDE | ||
# adjust relative paths as necessary | ||
THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")" | ||
. "${THIS_SCRIPT%/*}/../../../../resources/build/build-utils.sh" | ||
# END STANDARD BUILD SCRIPT INCLUDE | ||
|
||
cd "$THIS_SCRIPT_PATH" | ||
|
||
. "$KEYMAN_ROOT/resources/build/ci/pull-requests.inc.sh" | ||
|
||
test_repo_base=`mktemp -d` | ||
test_repo="$test_repo_base/ci-test-repo" | ||
test_filename="$(uuidgen).md" | ||
|
||
builder_echo "Preparing local repository clone in $test_repo" | ||
|
||
pushd "$test_repo_base" >/dev/null | ||
git clone https://github.com/keymanapp/ci-test-repo | ||
popd >/dev/null | ||
|
||
builder_echo "Creating test file $test_filename" | ||
|
||
echo "This is a test $test_filename" > "$test_repo/$test_filename" | ||
|
||
builder_echo blue "Testing ci_add_files" | ||
|
||
ci_add_files "$test_repo" . | ||
|
||
builder_echo blue "Testing ci_repo_has_cached_changes" | ||
|
||
if ! ci_repo_has_cached_changes "$test_repo"; then | ||
builder_die "Expected to have a file to commit!" | ||
fi | ||
|
||
builder_echo blue "Testing ci_open_pull_request" | ||
|
||
ci_open_pull_request "$test_repo" "test/ci" "chore: testing CI" | ||
|
||
rm -rf "$test_repo_base" | ||
|
||
builder_echo green "Tests passed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,4 @@ | ||
#!/usr/bin/env bash | ||
|
||
# | ||
# Prevents 'clear' on exit of mingw64 bash shell | ||
# | ||
SHLVL=0 | ||
|
||
set -e | ||
set -u | ||
|
||
## START STANDARD BUILD SCRIPT INCLUDE | ||
# adjust relative paths as necessary | ||
THIS_SCRIPT="$(readlink -f "${BASH_SOURCE[0]}")" | ||
|
@@ -135,49 +126,6 @@ function upload_keyman_help { | |
cp -rv "$helppath"/* "$dstpath/" | ||
} | ||
|
||
# | ||
# Commit and push to the help.keyman.com repo | ||
# Creates a pull request with the 'auto' label | ||
# Which a GitHub action will watch for in order | ||
# to automatically process and merge it | ||
# | ||
|
||
function commit_and_push { | ||
echo "Committing and pushing updated Keyman for $platform documentation" | ||
|
||
pushd $HELP_KEYMAN_COM | ||
|
||
if [ ! -z "${TEAMCITY_VERSION-}" ]; then | ||
git config user.name "Keyman Build Server" | ||
git config user.email "[email protected]" | ||
fi | ||
|
||
local branchname="auto/$platform-help-$VERSION_WITH_TAG" | ||
local modifiedfiles=$(help_product_path) | ||
|
||
local basebranch="master" | ||
|
||
git checkout -b $branchname $basebranch | ||
git add $modifiedfiles || return 1 | ||
git diff --cached --no-ext-diff --quiet --exit-code && { | ||
# if no changes then don't do anything. | ||
echo "No changes to commit" | ||
popd | ||
return 0 | ||
} | ||
|
||
echo "changes added to cache...>>>" | ||
local commitmessage="auto: Keyman for $platform help deployment" | ||
git commit -m "$commitmessage" || return 1 | ||
git push origin $branchname || return 1 | ||
hub pull-request -b $basebranch -l auto -m "$commitmessage" || return 1 | ||
popd | ||
|
||
echo "Push to help.keyman.com complete" | ||
|
||
return 0 | ||
} | ||
|
||
# | ||
# Main | ||
# | ||
|
@@ -213,4 +161,13 @@ generate_markdown_help || exit 1 | |
echo "Uploading Keyman for $platform documentation to help.keyman.com" | ||
|
||
upload_keyman_help || exit 1 | ||
commit_and_push || exit 1 | ||
|
||
ci_add_files "$HELP_KEYMAN_COM" "$(help_product_path)" | ||
if ! ci_repo_has_cached_changes "$HELP_KEYMAN_COM"; then | ||
echo "No changes to commit" | ||
exit 0 | ||
fi | ||
|
||
ci_open_pull_request "$HELP_KEYMAN_COM" "auto/$platform-help-$VERSION_WITH_TAG" "auto: Keyman for $platform help deployment" | ||
|
||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.