Skip to content

Commit 617b3cc

Browse files
hkernbachKVS85
andauthored
Fixed detection of wrong pull request (#404)
* fixed detection of wrongly picked github pull request urls which are sitting out of our repository - e.g. a fork from an unrealted repository * use short circuit in reverse loop, typecheck of an additional response field * search query repo based now Co-authored-by: Vadim Kondratyev <[email protected]>
1 parent 98ab0ed commit 617b3cc

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

github/updateGitHubStatus.js

+51-25
Original file line numberDiff line numberDiff line change
@@ -239,40 +239,65 @@ const checkPRMethod = (prCheckData, sha) => {
239239
// bool to check whether we won't find expected attributes we need
240240
let parseError = false;
241241

242-
if (prCheckData.hasOwnProperty('total_count')) {
243-
if (prCheckData.total_count >= 1) {
244-
let elementPositionToUse = prCheckData.total_count - 1;
245-
let infoItem = prCheckData.items[elementPositionToUse];
242+
const parsePullRequestItems = (prCheckData) => {
243+
let infoItem;
244+
if (!prCheckData.hasOwnProperty('items')) {
245+
exitAndWriteResultToFile(true, 'Could not parse expected attribute: "items". Format might have been changed.');
246+
}
246247

247-
if (infoItem.hasOwnProperty('state')) {
248-
if (infoItem.state !== 'open') {
249-
// we can abort - we found a PR, but this one is already closed
250-
exitAndWriteResultToFile(true, "We've only found a closed PR. Exiting.", "FAIL_NO_PR");
251-
}
248+
let foundPullRequestsArray = prCheckData.items.slice().reverse();
249+
250+
// iterate in reverse order, as the last items are the most relevant ones
251+
for (let pullRequestItem of foundPullRequestsArray) {
252+
// check if found item is valid (means it must be part of our given repository)
253+
if (pullRequestItem.url && pullRequestItem.url.indexOf(repository) != -1) {
254+
// means we've found a valid repository into our given repository
255+
infoItem = pullRequestItem;
256+
break;
252257
}
258+
};
253259

254-
if (infoItem.hasOwnProperty('body')) {
255-
// just additional information, let's not fail here
256-
pullRequestExtraInformation.info = infoItem.body;
260+
if (!infoItem) {
261+
// We've not found any valid pull request, therefore we cannot continue.
262+
// TODO Future: This is no actual error, we just do not need to continue with that script as it would be useless.
263+
// Check how to handle this case in the future.
264+
exitAndWriteResultToFile(true, "Found no related pull request. Exiting. Nothing to do.", 'FAIL_NO_PR');
265+
}
266+
267+
if (infoItem.hasOwnProperty('state')) {
268+
if (infoItem.state !== 'open') {
269+
// we can abort - we found a PR, but this one is already closed
270+
exitAndWriteResultToFile(true, "We've only found a closed PR. Exiting.", "FAIL_NO_PR");
257271
}
272+
}
258273

259-
if (infoItem.hasOwnProperty('pull_request')) {
260-
let pr = infoItem.pull_request;
261-
if (pr.hasOwnProperty('url')) {
262-
pullRequestExtraInformation.url = infoItem.pull_request.url;
263-
} else {
264-
parseError = true;
265-
}
274+
if (infoItem.hasOwnProperty('body')) {
275+
// just additional information, let's not fail here
276+
pullRequestExtraInformation.info = infoItem.body;
277+
}
278+
279+
if (infoItem.hasOwnProperty('pull_request')) {
280+
let pr = infoItem.pull_request;
281+
if (pr.hasOwnProperty('url')) {
282+
pullRequestExtraInformation.url = infoItem.pull_request.url;
266283
} else {
267284
parseError = true;
268285
}
286+
} else {
287+
parseError = true;
288+
}
269289

270-
if (parseError) {
271-
exitAndWriteResultToFile(true, "Could not parse expected attributes. Format might have been changed.");
272-
}
290+
if (parseError) {
291+
exitAndWriteResultToFile(true, "Could not parse expected attributes. Format might have been changed.");
292+
}
273293

274-
// if all good and PR found
275-
continueWithAction(sha);
294+
// if all good and PR found
295+
continueWithAction(sha);
296+
};
297+
298+
if (prCheckData.hasOwnProperty('total_count')) {
299+
if (prCheckData.total_count >= 1) {
300+
parsePullRequestItems(prCheckData);
276301
} else {
277302
exitAndWriteResultToFile(true, "Could not read pull request details from GitHub Search API");
278303
}
@@ -284,7 +309,8 @@ const checkPRMethod = (prCheckData, sha) => {
284309
}
285310

286311
const checkPRExists = (sha) => {
287-
const checkPRExistencePath = `/search/issues?q=${sha}`;
312+
const queryString = encodeURIComponent(`${sha} repo:${repository}`);
313+
const checkPRExistencePath = `/search/issues?q=${queryString}`;
288314
getRequest(checkPRExistencePath, checkPRMethod, sha);
289315
}
290316

0 commit comments

Comments
 (0)