Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the main -> develop sync job to GitHub Actions #2968

Closed
Tracked by #3318
ankatiyar opened this issue Aug 24, 2023 · 2 comments · Fixed by #3508
Closed
Tracked by #3318

Move the main -> develop sync job to GitHub Actions #2968

ankatiyar opened this issue Aug 24, 2023 · 2 comments · Fixed by #3508
Assignees
Labels
Component: DevOps Issue/PR that addresses automation, CI, GitHub setup

Comments

@ankatiyar
Copy link
Contributor

Description

Part of the migration to GHA

Context

Currently done by CircleCi

hourly_pr_merge:

Possible Implementation

Possible Alternatives

@ankatiyar ankatiyar added the Issue: Feature Request New feature or improvement to existing feature label Aug 24, 2023
@ankatiyar ankatiyar added this to the Github Actions milestone Aug 24, 2023
@ankatiyar
Copy link
Contributor Author

Potential solution on slack by @deepyaman

If need to do this on Kedro/if it helps...
.github/workflows/sync-fork.yml:

name: Sync Fork

on:
  schedule:
    - cron: '*/30 * * * *' # every 30 minutes
  workflow_dispatch: # on button click

jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - name: checkout
        uses: actions/checkout@v3

      - name: set git email and name
        run: |
          git config --global user.email "info@claypot.ai"
          git config --global user.name "Claypot AI"

      - name: update upstream
        run: |
          git remote add upstream https://github.com/some-thing/cool.git
          git fetch upstream master && git checkout -b upstream upstream/master
          git push origin upstream

          # revert side effects
          git checkout -
          git branch -D upstream

      - name: maybe merge upstream into master or raise a PR
        run: ./ci/merge.sh . "upstream" "master" "${{ secrets.GITHUB_TOKEN }}"

ci/merge.sh (should be largely unchanged?):

#!/usr/bin/env bash
# Script to merge a source branch into a target branch and raise a PR on conflict.

# Exit script if you try to use an uninitialized variable.
set -o nounset

# Exit script if a statement returns a non-true return value.
set -o errexit

# The git directory where this script will be invoked
GIT_DIRECTORY=$1
cd $GIT_DIRECTORY

# The source & target branches to perform the merge, i.e.
# git merge source target
SOURCE_BRANCH=$2
TARGET_BRANCH=$3
# A branch created to raise a PR in case SOURCE_BRANCH is push-protected.
PR_BRANCH="merge-${SOURCE_BRANCH}-to-${TARGET_BRANCH}"

# The Github details to raise a PR
GITHUB_TAGGING_TOKEN=$4
GITHUB_USER="claypotai"
GITHUB_REPO="cool"
GITHUB_ENDPOINT="[https://api.github.com/repos/${GITHUB_USER}/${GITHUB_REPO}/pulls](https://api.github.com/repos/$%7BGITHUB_USER%7D/$%7BGITHUB_REPO%7D/pulls)"
PAYLOAD=$(cat <<-END
{
    "title": "[AUTO-MERGE] Merge ${SOURCE_BRANCH} into ${TARGET_BRANCH} via ${PR_BRANCH}",
    "head": "${PR_BRANCH}",
    "base": "${TARGET_BRANCH}",
    "body": "A new change in ${SOURCE_BRANCH} cannot be merged into ${TARGET_BRANCH} as part of the regular sync job, hence this PR. Please resolve the conflicts manually, and make sure to obtain 2 approvals once the builds pass.\\n\\n### IMPORTANT NOTICE\\n\\nPlease let GitHub Actions merge this PR automatically, with merge commit enabled."
}
END
)

# Attempt to merge the source branch into the target branch after updating the target branch
# with latest changes from origin.
MERGE_STATUS=0
# We need to reconfigure origin.fetch because we originally clone with --single-branch
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch origin $SOURCE_BRANCH && git checkout -b $SOURCE_BRANCH "origin/${SOURCE_BRANCH}"
git checkout -
git merge --no-edit --allow-unrelated-histories $SOURCE_BRANCH $TARGET_BRANCH || MERGE_STATUS=1

if [ $MERGE_STATUS -eq 0 ]
then
    # If the merge was successful, attempt to push the target branch to origin.
    # We don't do any error handling here because if this fails, something really wrong is going on,
    # so let's just fail the job and debug it manually.
    echo "Successfully merged ${SOURCE_BRANCH} into ${TARGET_BRANCH}. Now pushing ${TARGET_BRANCH} to origin..."
    git push origin $TARGET_BRANCH
else
    # If the merge was not successful, i.e. there was some conflict between source branch and target branch,
    # abandon the merge and raise a PR instead.
    git merge --abort

    # Check if the PR_BRANCH already exists
    PR_BRANCH_EXIST=$(git ls-remote --heads origin $PR_BRANCH | wc -l)

    # If it doesn't exists, push and raise a PR
    if [ $PR_BRANCH_EXIST -eq 0 ]
    then
        echo "Failed to merge ${SOURCE_BRANCH} into ${TARGET_BRANCH}. Raising a pull request instead..."
        # Create a new branch from which to raise a PR, as ${SOURCE_BRANCH} might be push-protected
        git checkout -b ${PR_BRANCH} ${SOURCE_BRANCH}
        git push origin ${PR_BRANCH}
        STATUS=$(curl -X POST \
          --output /dev/null --location --silent --write-out "%{http_code}\n" --retry 3 \
          --header "Authorization: token ${GITHUB_TAGGING_TOKEN}" \
          --header "Content-Type: application/json" \
          --data "${PAYLOAD}" \
          "${GITHUB_ENDPOINT}")
        [ "${STATUS}" == "201" ]
    else
        echo "Failed to merge ${SOURCE_BRANCH} into ${TARGET_BRANCH} and it seems like another manual merge between ${SOURCE_BRANCH} and ${TARGET_BRANCH} is in progress. Doing nothing here."
    fi
fi

git checkout ${SOURCE_BRANCH}

@ankatiyar ankatiyar added Component: DevOps Issue/PR that addresses automation, CI, GitHub setup and removed Issue: Feature Request New feature or improvement to existing feature labels Aug 24, 2023
@merelcht
Copy link
Member

@lorenabalan has also done something similar, so could be worth reaching out to her.

@merelcht merelcht moved this to To Do in Kedro Framework Oct 16, 2023
@ankatiyar ankatiyar moved this from To Do to In Progress in Kedro Framework Jan 11, 2024
@ankatiyar ankatiyar moved this from In Progress to In Review in Kedro Framework Jan 15, 2024
@github-project-automation github-project-automation bot moved this from In Review to Done in Kedro Framework Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: DevOps Issue/PR that addresses automation, CI, GitHub setup
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants