From e24a4815489364716353b4165d344f138c170213 Mon Sep 17 00:00:00 2001 From: Michele Degges Date: Thu, 22 Dec 2022 13:21:14 -0800 Subject: [PATCH] OSS->ENT merge (#1) --- .github/scripts/merge.sh | 91 +++++++++++++++++++++ .github/workflows/oss-ent-merge-trigger.yml | 25 ++++++ .github/workflows/oss-merge.yml | 24 ++++++ 3 files changed, 140 insertions(+) create mode 100755 .github/scripts/merge.sh create mode 100644 .github/workflows/oss-ent-merge-trigger.yml create mode 100644 .github/workflows/oss-merge.yml diff --git a/.github/scripts/merge.sh b/.github/scripts/merge.sh new file mode 100755 index 000000000..acb8812b1 --- /dev/null +++ b/.github/scripts/merge.sh @@ -0,0 +1,91 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Configure Git +git config --global user.email "hc-github-team-secure-boundary@hashicorp.com" +git config --global user.name "github-team-secure-boundary" +# Fetch branches from ENT +git fetch origin +BRANCH=${1} + +echo "BRANCH to merge is $BRANCH" +git checkout "${BRANCH}" + +# Checkout OSS repo +git remote add oss-remote https://github.com/hashicorp/boundary-ui.git +git fetch oss-remote + +# Merge oss/${BRANCH} into ent/${BRANCH} +git_merge_branch="${2}" +git checkout -b "${git_merge_branch}" +latest_oss_commit="$(git rev-parse --short oss-remote/${BRANCH})" +echo "Trying to merge the latest oss commit - ${latest_oss_commit} from oss/${BRANCH} into ent/${BRANCH}" + +if ! errors=$(git merge -m "Merge Boundary UI OSS branch '${BRANCH}' at commit ${latest_oss_commit}" "${latest_oss_commit}"); then + # Here we can try to auto resolve conflicts + # This logic will vary from project to project. The below is just an example. + # We'll try to resolve package.json and yarn.lock conflicts by taking the OSS changes + conflicts=$(git diff --name-only --diff-filter=U) + printf "Found conflicts:\n${conflicts}\n" + # Resolve package.json conflicts by taking oss + if echo "$conflicts" | grep -q package.json; then + echo "Checking out oss package.json..." + git checkout --theirs package.json + git add package.json + fi + # Resolve yarn.lock conflicts by taking oss + if echo "$conflicts" | grep -q yarn.lock; then + echo "Checking out oss yarn.lock..." + git checkout --theirs yarn.lock + git add yarn.lock + fi + + # If we can commit all the changes without conflict, we have resolved everything automatically + if ! git commit --no-edit; then + echo "Auto Merge failed, creating a draft PR" + git merge --abort + git clean -xfd + git fetch oss-remote + git checkout "${OSS_BRANCH}" + git checkout -b "${OSS_LOCAL_BRANCH}" + git push -f --set-upstream origin "${OSS_LOCAL_BRANCH}" + hub pull-request \ + --draft \ + --no-edit \ + --labels "oss-merge" \ + -b "${BASE_BRANCH}" \ + -h "${OSS_LOCAL_BRANCH}" \ + -a "${ACTOR}" \ + -m "[${BASE_BRANCH/oss\/release\//}] OSS to ENT merge of (${SHA::7})" \ + -m "${BASE_BRANCH/oss\//} upstream merge of (${SHA::7})" \ + -m "Merge failed, Please fix the conflicts using github UI or using the following commands:" \ + -m "\`\`\`bash" \ + -m "git checkout ${BASE_BRANCH}" \ + -m "git pull" \ + -m "git checkout ${OSS_LOCAL_BRANCH}" \ + -m "git merge ${BASE_BRANCH}" \ + -m "# fix conflicts and commit" \ + -m "git push origin ${OSS_LOCAL_BRANCH}" \ + -m "\`\`\`" + exit 0 + fi +fi + +# Check that there are actually changes to merge by comparing the +# merge branch to the current checked out branch +if (git diff ${BRANCH} --exit-code > /dev/null); then + echo "there are no changes to merge" + exit 0 +fi +git push origin "${git_merge_branch}" +hub pull-request \ + --no-edit \ + --labels "oss-merge" \ + -b "${BASE_BRANCH}" \ + -h "${MERGE_BRANCH}" \ + -a ${ACTOR} \ + -m "[${BASE_BRANCH/oss\/release\//}] OSS to ENT merge of (${SHA::7})" \ + -m "${BASE_BRANCH/oss\//} upstream merge of (${SHA::7})" +gh pr merge --auto --merge + +# This PR will be need to be approved manually \ No newline at end of file diff --git a/.github/workflows/oss-ent-merge-trigger.yml b/.github/workflows/oss-ent-merge-trigger.yml new file mode 100644 index 000000000..b8aa222d2 --- /dev/null +++ b/.github/workflows/oss-ent-merge-trigger.yml @@ -0,0 +1,25 @@ +name: Trigger OSS to Enterprise Merge +on: + pull_request_target: + types: + - closed + branches: + - main + +jobs: + trigger-oss-merge: + # Run this only on merge events in OSS repo + if: ${{ github.event.pull_request.merged && github.repository == 'hashicorp/boundary-ui' }} + runs-on: ubuntu-latest + steps: + - name: Trigger Merge + env: + GIT_REF: ${{ github.ref_name }} + GIT_SHA: ${{ github.sha }} + GH_PAT: ${{ secrets.ELEVATED_GITHUB_TOKEN }} + GIT_ACTOR: ${{ github.actor }} + run: | + curl -H "Authorization: token $GH_PAT" \ + -H 'Accept: application/json' \ + -d "{\"event_type\": \"oss-merge\", \"client_payload\": {\"git-ref\": \"${GIT_REF}\", \"git-sha\": \"${GIT_SHA}\", \"git-actor\": \"${GIT_ACTOR}\" }}" \ + "https://api.github.com/repos/hashicorp/boundary-ui-enterprise/dispatches" \ No newline at end of file diff --git a/.github/workflows/oss-merge.yml b/.github/workflows/oss-merge.yml new file mode 100644 index 000000000..5c721674a --- /dev/null +++ b/.github/workflows/oss-merge.yml @@ -0,0 +1,24 @@ +name: OSS → ENT Merge +on: + repository_dispatch: + types: [ oss-merge ] +jobs: + oss-merge: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 #https://github.com/actions/checkout/releases/tag/v3.1.0 + with: + token: ${{ secrets.ELEVATED_GITHUB_TOKEN }} + - run: | + git fetch --prune --unshallow + ./.github/scripts/merge.sh "${BASE_BRANCH}" "${MERGE_BRANCH}" + env: + BASE_BRANCH: ${{ github.event.client_payload.git-ref }} + OSS_BRANCH: oss-remote/${{ github.event.client_payload.git-ref }} + OSS_LOCAL_BRANCH: oss/${{ github.event.client_payload.git-ref }}-${{ github.event.client_payload.git-sha }} + MERGE_BRANCH: oss/merge-${{ github.event.client_payload.git-ref }}-${{github.event.client_payload.git-sha}} + GITHUB_TOKEN: ${{secrets.ELEVATED_GITHUB_TOKEN }} + MERGE_APPROVE_TOKEN: ${{secrets.ELEVATED_GITHUB_TOKEN }} + ACTOR: ${{ github.event.client_payload.git-actor }} + SHA: ${{github.event.client_payload.git-sha}} \ No newline at end of file