-
Notifications
You must be signed in to change notification settings - Fork 2
98 lines (92 loc) · 4.4 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
name: Sync Changes to Farajaland
on:
pull_request:
types:
- closed
jobs:
sync_farajaland:
if: ${{ github.event.pull_request.merged == true }}
runs-on: ubuntu-latest
env:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
GH_TOKEN: ${{ secrets.FORK_ORGANISATION_TOKEN }}
FORK_REPOSITORY_PATH: "${{ secrets.FORK_REPOSITORY_ORGANISATION }}/${{ secrets.FORK_REPOSITORY_NAME }}"
steps:
- name: Checkout OpenCRVS Countryconfig repository
uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.base.ref }}
- 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: Create PR in ${{ secrets.FORK_REPOSITORY_ORGANISATION }}/${{ secrets.FORK_REPOSITORY_NAME }}
if: steps.sync_fork.outcome == 'failure'
run: |
echo "The 'sync_fork' step failed. Creating a PR in ${SYNC_FORK_REPOSITORY_PATH}"
if gh pr create \
--repo ${FORK_REPOSITORY_PATH} \
--base ${BASE_BRANCH} \
--head opencrvs:${BASE_BRANCH} \
--title "Update Farajaland from ${BASE_BRANCH}" \
--body \
"""
This PR updates the ${BASE_BRANCH} branch with the latest changes from
the original repository opencrvs:${BASE_BRANCH}.
""" 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="Exists" >> $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
- name: Send slack notification
run: |
[ "x$STATUS" == "xSynced" ] && \
FORK_STATUS="✅ Changes synced with Farajaland repository https://github.com/${FORK_REPOSITORY_PATH}"
[ "x$STATUS" == "xCreated" ] && \
FORK_STATUS="⚠️ PR $RESULT created in Farajaland repository due to merge conflicts, please resolve conflicts and merge to keep the repo in sync with upstream"
[ "x$STATUS" == "xExists" ] && \
FORK_STATUS="⚠️ PR $RESULT already exists in Farajaland repository, please resolve conflicts and merge to keep the repo in sync with upstream"
[ "x$STATUS" == "xFailed" ] && \
FORK_STATUS="❌ Failed to create PR in Farajaland repository: $RESULT"
BODY="""
Pull request https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}
authored by ${{ github.actor }} from branch ${{ github.head_ref }} was merged into ${BASE_BRANCH}.
$FORK_STATUS
"""
echo "$BODY"
# - name: Slack Notification
# if: steps.sync_fork.outcome == 'failure'
# run: |
# echo "Sending slack notification"
# curl -X POST https://slack.com/api/chat.postMessage \
# -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
# -H 'Content-type: application/json' \
# --data '{
# "channel": "C02LU432JGK",
# "text": "Hello, world!"
# }'