-
Notifications
You must be signed in to change notification settings - Fork 1
/
entrypoint.sh
executable file
·117 lines (87 loc) · 2.97 KB
/
entrypoint.sh
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
#!/usr/bin/env bash
set -x
# Set variables
UPSTREAM_REPO=$1
UPSTREAM_BRANCH=$2
DOWNSTREAM_BRANCH=$3
GITHUB_TOKEN=$4
FETCH_ARGS=$5
MERGE_ARGS=$6
PUSH_ARGS=$7
SPAWN_LOGS=$8
if [[ -z "$UPSTREAM_REPO" ]]; then
echo "Missing \$UPSTREAM_REPO"
exit 1
fi
if [[ -z "$DOWNSTREAM_BRANCH" ]]; then
echo "Missing \$DOWNSTREAM_BRANCH"
echo "Default to ${UPSTREAM_BRANCH}"
DOWNSTREAM_BREANCH=UPSTREAM_BRANCH
fi
if ! echo "$UPSTREAM_REPO" | grep '\.git'; then
UPSTREAM_REPO="https://github.com/${UPSTREAM_REPO_PATH}.git"
fi
echo "UPSTREAM_REPO=$UPSTREAM_REPO"
# Checkout repository working and add upstream remote
git clone -b $DOWNSTREAM_BRANCH "https://github.com/${GITHUB_REPOSITORY}.git" work
cd work || { echo "Missing work dir" && exit 2 ; }
git config user.name "${GITHUB_ACTOR}"
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
git config --local user.password ${GITHUB_TOKEN}
git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git remote add upstream "$UPSTREAM_REPO"
git fetch ${FETCH_ARGS} upstream
git remote -v
git checkout ${DOWNSTREAM_BRANCH}
# Get percentage files modified
RST_FILES=$(find ./ -name "*.rst")
N_RST_FILES=$(find ./ -name "*.rst"|wc -l)
COUNT_DIFF=0
for i in $RST_FILES
do
if [[ $(git --no-pager diff upstream/${UPSTREAM_BRANCH} -- $i) ]]
then
COUNT_DIFF=$((COUNT_DIFF + 1))
fi
done
PERCENTAGE_DIFF_FILES=$(awk "BEGIN { print ${COUNT_DIFF} / ${N_RST_FILES} }")
# Merge and get files with conflict
MERGE_RESULT=$(git merge ${MERGE_ARGS} upstream/${UPSTREAM_BRANCH})
CONFLICTS=$(git ls-files -u | awk '{print $4}' | sort | uniq)
if [[ $MERGE_RESULT == "" ]]
then
exit 1
elif [[ $MERGE_RESULT != *"Already up to date."* ]]
then
git commit -m "Merged upstream"
git push ${PUSH_ARGS} origin ${DOWNSTREAM_BRANCH} || exit $?
fi
cd ..
rm -rf work
echo "merge_result<<EOF" >> $GITHUB_OUTPUT
echo "${MERGE_RESULT}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "conflicts<<EOF" >> $GITHUB_OUTPUT
echo "${CONFLICTS}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "completed<<EOF" >> $GITHUB_OUTPUT
echo "${PERCENTAGE_DIFF_FILES}" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
if [[ $CONFLICTS ]]
then
echo "Please resolve conflicts in next files:" > bodyfile
echo "${CONFLICTS}" >> bodyfile
echo >> bodyfile
echo "Complete message: " >> bodyfile
echo "${MERGE_RESULT}" >> bodyfile
touch placeholder.txt
GH_TOKEN=${GITHUB_TOKEN} gh issue list --label automatic-sync-merge --repo https://github.com/${GITHUB_REPOSITORY}.git >> placeholder.txt
AUTOMATIC_ISSUE_LIST=`cat placeholder.txt`
#echo "$AUTOMATIC_ISSUE_LIST"
#avoid created duplicated issue
#create the issue if the previous automatic-sync is no longer open
if [[ $AUTOMATIC_ISSUE_LIST = "" ]]
then
GH_TOKEN=${GITHUB_TOKEN} gh issue create --repo https://github.com/${GITHUB_REPOSITORY}.git --title "$UPSTREAM_BRANCH Fix merging conflict" --body-file bodyfile --label "automatic-sync-merge"
fi
fi