@@ -12,6 +12,54 @@ print_debug_info() {
12
12
fi
13
13
}
14
14
15
+ fetchBranchFromFork () {
16
+ local fork_remote=" $1 "
17
+ local source_branch=" $2 "
18
+ local target_branch=" $3 "
19
+
20
+ if [ " $INPUT_DEBUG " = " true" ]; then
21
+ echo " Available branches in ${fork_remote} remote:"
22
+ git ls-remote --heads " ${fork_remote} "
23
+ fi
24
+
25
+ if git fetch " ${fork_remote} " " ${source_branch} :${target_branch} " 2> /dev/null; then
26
+ echo " Successfully fetched branch from fork"
27
+ return 0
28
+ fi
29
+
30
+ if git fetch " ${fork_remote} " " refs/heads/${source_branch} :refs/heads/${target_branch} " 2> /dev/null; then
31
+ echo " Successfully fetched branch using refs/heads prefix"
32
+ return 0
33
+ fi
34
+
35
+ echo " Failed to fetch branch ${source_branch} from ${fork_remote} . Aborting."
36
+ return 1
37
+ }
38
+
39
+ setupForkRemote () {
40
+ local remote_name=" $1 "
41
+ local remote_url=" $2 "
42
+
43
+ echo " Setting up ${remote_name} remote with URL: ${remote_url} "
44
+
45
+ if git remote | grep -q " ^${remote_name} $" ; then
46
+ local current_url
47
+ current_url=$( git remote get-url " ${remote_name} " 2> /dev/null)
48
+
49
+ if [ " $current_url " != " $remote_url " ]; then
50
+ echo " Remote ${remote_name} exists with different URL, updating it"
51
+ git remote remove " ${remote_name} "
52
+ git remote add " ${remote_name} " " ${remote_url} " || { echo " Failed to add ${remote_name} as remote" ; return 1; }
53
+ else
54
+ echo " Remote ${remote_name} already exists with correct URL"
55
+ fi
56
+ else
57
+ git remote add " ${remote_name} " " ${remote_url} " || { echo " Failed to add ${remote_name} as remote" ; return 1; }
58
+ fi
59
+
60
+ return 0
61
+ }
62
+
15
63
print_debug_info
16
64
17
65
setGithubPullRequestId () {
@@ -107,38 +155,100 @@ fi
107
155
if [ " ${pull_request_id} " ]; then
108
156
if [ " $GITLAB_CI " = " true" ]; then
109
157
if [ " $CI_MERGE_REQUEST_SOURCE_PROJECT_URL " = " $CI_PROJECT_URL " ]; then
158
+ echo " Same-project merge request"
110
159
git fetch origin " ${base_branch} :${base_branch} " " ${head_branch} :${head_branch} " || { echo " Failed to fetch branches from the repository" ; exit 1; }
111
160
else
161
+ echo " Processing cross-project merge request from fork"
162
+ echo " Base branch: ${base_branch} , Head branch: ${head_branch} "
163
+ echo " Source project URL: ${CI_MERGE_REQUEST_SOURCE_PROJECT_URL} "
164
+
112
165
git fetch origin " ${base_branch} :${base_branch} " || { echo " Failed to fetch base branch ${base_branch} from upstream" ; exit 1; }
113
- git remote add fork " ${CI_MERGE_REQUEST_SOURCE_PROJECT_URL} " || { echo " Failed to add fork as remote" ; exit 1; }
114
- git fetch fork " ${head_branch} :${head_branch} " || { echo " Failed to fetch head branch ${head_branch} from fork" ; exit 1; }
166
+
167
+ if ! setupForkRemote " fork" " ${CI_MERGE_REQUEST_SOURCE_PROJECT_URL} " ; then
168
+ exit 1
169
+ fi
170
+
171
+ if ! fetchBranchFromFork " fork" " ${head_branch} " " ${head_branch} " ; then
172
+ exit 1
173
+ fi
115
174
fi
116
175
elif [ " $GITHUB_ACTIONS " = " true" ]; then
117
176
if [ -f " $GITHUB_EVENT_PATH " ]; then
118
- head_repo_url=$( cat " $GITHUB_EVENT_PATH " | grep ' "clone_url" ' | head -n 1 | awk -F ' " clone_url": ' ' {print $2} ' | tr -d ' ", ' )
119
- base_repo_url=$( cat " $GITHUB_EVENT_PATH " | grep ' "repo" ' | head -n 1 | awk -F ' "url": ' ' {print $2} ' | tr -d ' ", ' )
177
+ head_repo_url=$( cat " $GITHUB_EVENT_PATH " | jq -r ' .pull_request.head.repo. clone_url' )
178
+ base_repo_url=$( cat " $GITHUB_EVENT_PATH " | jq -r ' .pull_request.base.repo.clone_url ' )
120
179
180
+ if [ " $INPUT_DEBUG " = " true" ]; then
181
+ echo " head_repo_url: $head_repo_url "
182
+ echo " base_repo_url: $base_repo_url "
183
+ cat " $GITHUB_EVENT_PATH "
184
+ fi
185
+
121
186
if [ " $head_repo_url " != " $base_repo_url " ]; then
122
187
echo " PR is from a forked repository: $head_repo_url "
188
+
123
189
git fetch origin " ${base_branch} :${base_branch} " || { echo " Failed to fetch base branch ${base_branch} from upstream" ; exit 1; }
124
190
125
- git remote add fork " $head_repo_url " || { echo " Failed to add fork as remote" ; exit 1; }
126
- git fetch fork " ${head_branch} :${head_branch} " || { echo " Failed to fetch head branch ${head_branch} from fork" ; exit 1; }
191
+ if ! setupForkRemote " fork" " $head_repo_url " ; then
192
+ exit 1
193
+ fi
194
+
195
+ if ! fetchBranchFromFork " fork" " ${head_branch} " " ${head_branch} " ; then
196
+ exit 1
197
+ fi
198
+ else
199
+ git fetch origin " ${base_branch} :${base_branch} " " ${head_branch} :${head_branch} " || { echo " Failed to fetch branches from origin" ; exit 1; }
127
200
fi
201
+ else
202
+ git fetch origin " ${base_branch} :${base_branch} " " ${head_branch} :${head_branch} " || { echo " Failed to fetch branches from origin" ; exit 1; }
128
203
fi
129
204
elif [ " $CI " = " true" ] && [ -n " $BITBUCKET_BUILD_NUMBER " ]; then
130
205
origin_url=$( git remote get-url origin)
131
206
upstream_url=$( git remote get-url upstream 2> /dev/null || echo " " )
132
207
if [ " $origin_url " != " $upstream_url " ] && [ " $upstream_url " != " " ] ; then
208
+ echo " Processing Bitbucket cross-repository PR"
209
+
133
210
git fetch upstream " ${base_branch} :${base_branch} " || { echo " Failed to fetch base branch ${base_branch} from upstream" ; exit 1; }
134
- git fetch origin " ${head_branch} :${head_branch} " || { echo " Failed to fetch head branch ${head_branch} from fork" ; exit 1; }
211
+
212
+ # Bitbucket Pipelines automatically configures origin to point to the fork
213
+ echo " Using existing origin as fork remote"
214
+
215
+ if ! fetchBranchFromFork " origin" " ${head_branch} " " ${head_branch} " ; then
216
+ exit 1
217
+ fi
135
218
else
136
219
git checkout --detach || { echo " Failed to detach HEAD" ; exit 1; }
220
+ git fetch origin " ${base_branch} :${base_branch} " " ${head_branch} :${head_branch} " || { echo " Failed to fetch branches from origin" ; exit 1; }
137
221
fi
138
222
fi
139
- git fetch origin " ${base_branch} : ${base_branch} " " ${head_branch} : ${head_branch} " || { echo " Failed to fetch branches " ; exit 1 ; }
223
+
140
224
if [ -f " $( git rev-parse --git-dir) /shallow" ]; then
141
- git fetch --unshallow || { echo " Failed to unshallow the repository" ; exit 1; }
225
+ if [ -n " ${base_branch} " ] && [ -n " ${head_branch} " ]; then
226
+ echo " Repository is shallow, attempting to fetch history..."
227
+
228
+ # Function to unshallow the repository
229
+ unshallow_repo () {
230
+ echo " Unshallowing the entire repository..."
231
+ git fetch --unshallow || { echo " Failed to unshallow the repository" ; exit 1; }
232
+ echo " Repository fully unshallowed."
233
+ }
234
+
235
+ # Try to fetch recent history first
236
+ if ! months_ago=$( cd /code && npm run get:dateMonthsAgo --silent -- 3) ; then
237
+ echo " Failed to get date, falling back to full unshallow"
238
+ unshallow_repo
239
+ elif ! git fetch --shallow-since=" $months_ago " ; then
240
+ echo " Failed to fetch commits for the last 3 months, falling back to full unshallow"
241
+ unshallow_repo
242
+ elif ! git diff --quiet " ${base_branch} " " ${head_branch} " 2> /dev/null; then
243
+ echo " History not sufficient. Falling back to full unshallow."
244
+ unshallow_repo
245
+ else
246
+ echo " Successfully fetched necessary history."
247
+ fi
248
+ else
249
+ echo " Repository is shallow but branch info unavailable. Unshallowing the entire repository..."
250
+ git fetch --unshallow || { echo " Failed to unshallow the repository" ; exit 1; }
251
+ fi
142
252
fi
143
253
if [ -n " ${base_branch} " ] && [ -n " ${head_branch} " ]; then
144
254
changed_files=$( git diff --name-only " ${base_branch} " " ${head_branch} " ) || { echo " Failed to get changed files: ${base_branch} ..${head_branch} " ; exit 1; }
0 commit comments