Skip to content

Commit

Permalink
ossm: add merge_upstream.sh to do merges of upstream commits
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Grimm <dgrimm@redhat.com>
  • Loading branch information
dgn committed Apr 23, 2024
1 parent b3767cc commit cfceff3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile.core.mk
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ bundle: gen helm operator-sdk ## Generate bundle manifests and metadata, then va
if (git ls-files --error-unmatch "$$csvPath" &>/dev/null); then \
if ! (git diff "$$csvPath" | grep '^[+-][^+-][^+-]' | grep -v "createdAt:" >/dev/null); then \
echo "reverting timestamp change in $$csvPath"; \
git checkout "$$csvPath"; \
git checkout "$$csvPath" || echo "failed to revert timestamp change. assuming we're in the middle of a merge"; \
fi \
fi
$(OPERATOR_SDK) bundle validate ./bundle
Expand Down
75 changes: 75 additions & 0 deletions ossm/merge_upstream.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# Copyright Istio Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

if [ -e "$GITHUB_TOKEN_PATH" ]; then
GITHUB_TOKEN=${GITHUB_TOKEN:-"$(cat "$GITHUB_TOKEN_PATH")"}
fi

GITHUB_TOKEN=${GITHUB_TOKEN:-""}

if [ -z "${GIT_USERNAME:-}" ]; then
GIT_USERNAME="$(curl -sSfLH "Authorization: token $GITHUB_TOKEN" "https://api.github.com/user" | jq --raw-output ".login")"
fi

if [ -z "${GIT_EMAIL:-}" ]; then
GIT_EMAIL="$(curl -sSfLH "Authorization: token $GITHUB_TOKEN" "https://api.github.com/user" | jq --raw-output ".email")"
fi

GIT_COMMIT_MESSAGE=${GIT_COMMIT_MESSAGE:-"Automated merge"}

MERGE_STRATEGY=${MERGE_STRATEGY:-"merge"}
MERGE_REPOSITORY=${MERGE_REPOSITORY:-"git@github.com:istio-ecosystem/sail-operator.git"}
MERGE_BRANCH=${MERGE_BRANCH:-"main"}

print_error() {
local last_return="$?"

{
echo
echo "${1:-unknown error}"
echo
} >&2

return "${2:-$last_return}"
}

merge() {
git remote add -f -t "$MERGE_BRANCH" upstream "$MERGE_REPOSITORY"
echo "Using branch $MERGE_BRANCH"

set +e # git returns a non-zero exit code on merge failure, which fails the script
if [ "${MERGE_STRATEGY}" == "merge" ]; then
git -c "user.name=$GIT_USERNAME" -c "user.email=$GIT_EMAIL" merge --no-ff -m "$GIT_COMMIT_MESSAGE" --log upstream/"$MERGE_BRANCH"
else
git -c "user.name=$GIT_USERNAME" -c "user.email=$GIT_EMAIL" rebase upstream/"$MERGE_BRANCH"
fi
return $?
}

main () {
if ! merge; then
set -e
echo "Conflicts detected, attempting to run 'make gen' to resolve."
rm -rf bundle resources
make gen
git add bundle resources chart
git -c "user.name=$GIT_USERNAME" -c "user.email=$GIT_EMAIL" commit --no-edit
fi
}

main

0 comments on commit cfceff3

Please sign in to comment.