This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
forked from kbrashears5/github-action-file-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
200 lines (168 loc) · 7.01 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/bin/bash
STATUS=0
# remember last error code
trap 'STATUS=$?' ERR
# problem matcher must exist in workspace
cp /error-matcher.json $HOME/file-sync-error-matcher.json
echo "::add-matcher::$HOME/file-sync-error-matcher.json"
echo "Repository: [$GITHUB_REPOSITORY]"
# log inputs
echo "Inputs"
echo "---------------------------------------------"
RAW_REPOSITORIES="$INPUT_REPOSITORIES"
RAW_FILES="$INPUT_FILES"
GITHUB_TOKEN="$INPUT_TOKEN"
REPOSITORIES=($RAW_REPOSITORIES)
echo "Repositories : $REPOSITORIES"
FILES=($RAW_FILES)
echo "Files : $FILES"
PULL_REQUEST_BRANCH_NAME="$INPUT_PULL_REQUEST_BRANCH_NAME"
echo "Pull request : $PULL_REQUEST_BRANCH_NAME"
PULL_REQUEST_LABEL="$INPUT_PULL_REQUEST_LABEL"
echo "PR label : $PULL_REQUEST_LABEL"
GIT_EMAIL="$INPUT_GIT_EMAIL"
echo "Git email : $GIT_EMAIL"
GIT_USERNAME="$INPUT_GIT_USERNAME"
echo "Git username : $GIT_USERNAME"
# set temp path
TEMP_PATH="/ghafs/"
cd /
mkdir "$TEMP_PATH"
cd "$TEMP_PATH"
echo "Temp Path : $TEMP_PATH"
echo "---------------------------------------------"
echo " "
# initalize git
echo "Initializing git with github-actions[bot]"
git config --system core.longpaths true
git config --global core.longpaths true
git config --global user.email "$GIT_EMAIL" && git config --global user.name "$GIT_USERNAME"
echo "Git initialized"
echo " "
# loop through all the repos
for repository in "${REPOSITORIES[@]}"; do
echo "::group::$repository"
# extra arguments to use when pushing changes
PUSH_ARGS=""
# determine repo name
REPO_INFO=($(echo $repository | tr "@" "\n"))
REPO_NAME=${REPO_INFO[0]}
echo "Repository name: [$REPO_NAME]"
REPO_NAME_SPLIT=($(echo $REPO_NAME | tr "/" "\n"))
ORG_NAME=${REPO_NAME_SPLIT[0]}
echo "Determining default branch name"
DEFAULT_BRANCH_NAME=$(curl -X GET -H "Accept: application/vnd.github.v3+json" -u ${USERNAME}:${GITHUB_TOKEN} --silent "${GITHUB_API_URL}/repos/${REPO_NAME}" | jq -r '.default_branch')
echo "Default branch name: [$DEFAULT_BRANCH_NAME]"
# determine branch name
echo "Determining instructed branch name"
if [ ${REPO_INFO[1]+yes} ]; then
BRANCH_NAME="${REPO_INFO[1]}"
else
BRANCH_NAME="$DEFAULT_BRANCH_NAME"
fi
echo "Branch: [$BRANCH_NAME]"
# clone the repo
REPO_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${REPO_NAME}.git"
GIT_PATH="${TEMP_PATH}${REPO_NAME}"
echo "Cloning [$REPO_URL] to [$GIT_PATH]"
git clone --quiet --no-hardlinks --no-tags $REPO_URL $GIT_PATH
cd $GIT_PATH
# checkout the branch, if specified
if [ "$BRANCH_NAME" != "$DEFAULT_BRANCH_NAME" ]; then
# try to check out the origin, if fails, then create the local branch
git fetch && git checkout -b "$BRANCH_NAME" origin/"$BRANCH_NAME" || git checkout -b "$BRANCH_NAME"
if [ ! -z "$PULL_REQUEST_BRANCH_NAME" ]; then
echo "Making hard reset back to Pull Requset target branch before applying sync"
git reset --hard origin/$PULL_REQUEST_BRANCH_NAME
PUSH_ARGS="--force"
fi
fi
echo " "
need_push=false
# loop through all files
for file in "${FILES[@]}"; do
echo "File: [${file}]"
# split and trim
FILE_TO_SYNC=($(echo $file | tr "=" "\n"))
SOURCE_PATH=${FILE_TO_SYNC[0]}
echo "Source path: [$SOURCE_PATH]"
# initialize the full path
SOURCE_FULL_PATH="${GITHUB_WORKSPACE}/${SOURCE_PATH}"
echo "Source full path: [$SOURCE_FULL_PATH]"
# set the default of source and destination path the same
SOURCE_FILE_NAME=$(basename "$SOURCE_PATH")
echo "Source file name: [$SOURCE_FILE_NAME]"
DEST_PATH="${SOURCE_FILE_NAME}"
echo "Destination file path: [$DEST_PATH]"
# if destination is different, then set it
if [ ${FILE_TO_SYNC[1]+yes} ]; then
DEST_PATH="${FILE_TO_SYNC[1]}"
echo "Destination file path specified: [$DEST_PATH]"
fi
# check that source full path isn't null
if [ "$SOURCE_FULL_PATH" != "" ]; then
# test path to copy to
DEST_FULL_PATH="${GIT_PATH}/${DEST_PATH}"
DEST_FOLDER_PATH=$(dirname "$DEST_FULL_PATH")
if [ ! -d "$DEST_FOLDER_PATH" ]; then
echo "Creating [$DEST_FOLDER_PATH]"
mkdir -p $DEST_FOLDER_PATH
fi
# copy file
echo "Copying: [$SOURCE_FULL_PATH] to [$DEST_FULL_PATH]"
cp "${SOURCE_FULL_PATH}" "${DEST_FULL_PATH}" -r
# add file
git add "${DEST_FULL_PATH}" -f
# check if anything is new
if [ "$(git status --porcelain)" != "" ]; then
echo "Committing changes"
git commit -m "File sync from ${GITHUB_REPOSITORY}"
need_push=true
else
echo "Files not changed: [${SOURCE_FILE_NAME}]"
fi
else
echo "[${SOURCE_FULL_PATH}] not found in [${GITHUB_REPOSITORY}]"
fi
echo " "
done
if [ "$need_push" = true ] ; then
cd ${GIT_PATH}
# push changes
echo "Push changes to [${REPO_URL}]"
git push $PUSH_ARGS $REPO_URL
if [ ! -z "$PULL_REQUEST_BRANCH_NAME" -a "$BRANCH_NAME" != "$PULL_REQUEST_BRANCH_NAME" ]; then
echo "Creating pull request from [$BRANCH_NAME] into [$PULL_REQUEST_BRANCH_NAME]"
jq -n --arg title "File sync from ${GITHUB_REPOSITORY}" --arg head "$BRANCH_NAME" --arg base $PULL_REQUEST_BRANCH_NAME '{title:$title,head:$head,base:$base}' | curl -d @- \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-u ${USERNAME}:${GITHUB_TOKEN} \
--silent \
${GITHUB_API_URL}/repos/${REPO_NAME}/pulls
if [ ! -z "$PULL_REQUEST_LABEL" ]; then
# Find the newly created or preexisting pull request
EXISTING_PULL_REQUEST=$(curl \
-H "Accept: application/vnd.github.v3+json" \
-u ${USERNAME}:${GITHUB_TOKEN} \
--silent \
"${GITHUB_API_URL}/repos/${REPO_NAME}/pulls?head=${ORG_NAME}:${BRANCH_NAME}&base=${PULL_REQUEST_BRANCH_NAME}" | jq '.[0]')
if [ ! -z "$EXISTING_PULL_REQUEST" ]; then
# Use the pull request's number to add the PR label to it
PR_NUMBER=$(echo $EXISTING_PULL_REQUEST | jq '.number')
echo "Applying label [$PULL_REQUEST_LABEL] to created PR #$PR_NUMBER"
curl -X POST \
-H "Accept: application/vnd.github.v3+json" \
-u ${USERNAME}:${GITHUB_TOKEN} \
--silent \
--data '{"labels": ["'${PULL_REQUEST_LABEL}'"]}' \
${GITHUB_API_URL}/repos/${REPO_NAME}/issues/${PR_NUMBER}/labels
fi
fi
fi
fi
cd $TEMP_PATH
rm -rf $REPO_NAME
echo "Completed [${REPO_NAME}]"
echo "::endgroup::"
done
exit $STATUS