-
Notifications
You must be signed in to change notification settings - Fork 2
137 lines (126 loc) · 5.7 KB
/
on-pull-request.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# name: Sync Changes to Farajaland
#
# This GitHub Actions workflow automatically synchronizes changes from an
# OpenCRVS Countryconfig repository to a forked Farajaland repository whenever
# a pull request is merged. The workflow performs the following steps:
#
# 1. Checkout the base branch of the pull request from the OpenCRVS Countryconfig repository.
# 2. Check if the pull request base branch exists in the Farajaland repository.
# 3. If the branch exists, attempt to sync the Farajaland fork with the latest changes.
# 4. If syncing fails due to conflicts, create a pull request in the Farajaland repository to manually merge the changes.
# 5. Send a Slack notification with the status of the sync operation.
#
# Trigger:
# - This workflow is triggered by a `pull_request` event with the `closed` type, specifically when a pull request is merged.
#
# Job:
# - sync_farajaland: Executes the steps to sync changes and send notifications. Only runs if the pull request is merged.
#
# Action secrets:
# - `FORK_REPOSITORY_ORGANISATION`: The organisation name of the Farajaland repository.
# - `FORK_REPOSITORY_NAME`: The name of the Farajaland repository.
# - `SLACK_BOT_TOKEN`: A Slack bot token for sending notifications.
name: Sync Changes to Farajaland
on:
pull_request:
types:
- closed
permissions:
contents: write
jobs:
sync_farajaland:
if: ${{ github.event.pull_request.merged == true }}
runs-on: ubuntu-latest
env:
UPSTREAM_REPOSITORY: ${{ github.repository }}
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
# Check token under Olli's account
GH_TOKEN: ${{ secrets.FORK_ORGANISATION_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
FORK_REPOSITORY_PATH: "${{ vars.FORK_REPOSITORY_ORGANISATION }}/${{ vars.FORK_REPOSITORY_NAME }}"
steps:
- name: Check if PR branch exists in Farajaland repository
id: check_branch
run: |
if git ls-remote --heads https://${GH_TOKEN}@github.com/${FORK_REPOSITORY_PATH}.git "$BASE_BRANCH" | grep "$BASE_BRANCH"; then
echo "branch_exists=true" >> $GITHUB_ENV
else
echo "=============================================="
echo " 🚀 Branch $BASE_BRANCH doesn't exist in repository ${FORK_REPOSITORY_PATH}"
echo " 🚪 Doing exit"
echo "=============================================="
echo "branch_exists=false" >> $GITHUB_ENV
fi
- name: Sync Farajaland Fork repository
id: sync_fork
if: env.branch_exists == 'true'
continue-on-error: true
run: |
gh repo sync ${FORK_REPOSITORY_PATH} --branch ${BASE_BRANCH}
echo STATUS="Synced" >> $GITHUB_ENV
- name: Checkout OpenCRVS Farajaland repository
if: steps.sync_fork.outcome == 'failure'
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: '${{ vars.FORK_REPOSITORY_ORGANISATION }}/${{ vars.FORK_REPOSITORY_NAME }}'
# path: '${{ env.FORK_REPOSITORY_NAME }}'
- if: steps.sync_fork.outcome == 'failure'
name: Set up Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Set up authentication
run: |
git config --global url."https://${{ secrets.FORK_ORGANISATION_TOKEN }}:x-oauth-basic@github.com/".insteadOf "https://github.com/"
git config -l | grep 'http\..*\.extraheader' | cut -d= -f1 | \
xargs -L1 git config --unset-all
- if: steps.sync_fork.outcome == 'failure'
name: Merge changes from upstream
id: merge
continue-on-error: true
run: |
git remote add upstream https://github.com/${UPSTREAM_REPOSITORY}.git
git fetch upstream
git checkout -b sync-with-${BASE_BRANCH}
git merge upstream/${BASE_BRANCH}
- if: steps.merge.outcome == 'failure'
name: Handle merge conflicts
run: |
git add -A
echo "Commit everything"
git commit -m "Merge upstream/${BASE_BRANCH} into sync-with-${BASE_BRANCH} with conflicts"
- name: Push Branch
run: |
git remote -v
git pull origin sync-with-${BASE_BRANCH}
git push origin sync-with-${BASE_BRANCH}
- name: Create PR in ${{ env.FORK_REPOSITORY_ORGANISATION }}/${{ env.FORK_REPOSITORY_NAME }}
if: steps.sync_fork.outcome == 'failure'
run: |
if gh pr create \
--repo ${FORK_REPOSITORY_PATH} \
--base ${BASE_BRANCH} \
--head sync-with-${BASE_BRANCH} \
--title "Update Farajaland from ${BASE_BRANCH}" \
--body \
"""
This PR updates the ${BASE_BRANCH} branch with the latest changes from
the original repository https://github.com/${UPSTREAM_REPOSITORY}.
""" 1>result.txt 2>&1; then
printf "PR created successfully: $(grep ${FORK_REPOSITORY_PATH} result.txt)\n"
echo RESULT=$(grep ${FORK_REPOSITORY_PATH} result.txt) >> $GITHUB_ENV
echo STATUS="Created" >> $GITHUB_ENV
else
if grep -q "already exists" result.txt; then
printf "PR already exists in Farajaland repo: $(grep ${FORK_REPOSITORY_PATH} result.txt)\n"
echo RESULT=$(grep ${FORK_REPOSITORY_PATH} result.txt) >> $GITHUB_ENV
echo STATUS="Created" >> $GITHUB_ENV
else
echo "Failed to create PR: $(cat result.txt)"
echo RESULT=$(cat result.txt) >> $GITHUB_ENV
echo STATUS="Failed" >> $GITHUB_ENV
fi
fi
echo $RESULT
echo $STATUS