mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-18 23:02:31 +03:00
Add retries to loading pull request data
- github can respond with 422 right after pushing - make three attempts to load data
This commit is contained in:
parent
7c41b56222
commit
b75d03b267
@ -56,7 +56,7 @@
|
|||||||
detailsError = undefined;
|
detailsError = undefined;
|
||||||
isFetchingDetails = true;
|
isFetchingDetails = true;
|
||||||
try {
|
try {
|
||||||
detailedPr = await githubService.getDetailedPullRequest(targetBranch, skipCache);
|
detailedPr = await githubService.getDetailedPr(targetBranch, skipCache);
|
||||||
mergeableState = detailedPr?.mergeableState;
|
mergeableState = detailedPr?.mergeableState;
|
||||||
lastDetailsFetch = createTimeAgoStore(new Date(), true);
|
lastDetailsFetch = createTimeAgoStore(new Date(), true);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
@ -172,37 +172,50 @@ export class GitHubService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDetailedPullRequest(
|
async getDetailedPr(
|
||||||
branch: string | undefined,
|
branch: string | undefined,
|
||||||
skipCache: boolean
|
skipCache: boolean
|
||||||
): Promise<DetailedPullRequest | undefined> {
|
): Promise<DetailedPullRequest | undefined> {
|
||||||
if (!branch) return;
|
if (!branch) return;
|
||||||
|
|
||||||
// We should remove this cache when `list_virtual_branches` no longer triggers
|
const cachedPr = this.prCache.get(branch);
|
||||||
// immedate updates on the subscription.
|
if (cachedPr && !skipCache) {
|
||||||
const cacheHit = this.prCache.get(branch);
|
if (new Date().getTime() - cachedPr.fetchedAt.getTime() < 1000 * 2)
|
||||||
if (cacheHit && !skipCache) {
|
return await cachedPr.value;
|
||||||
if (new Date().getTime() - cacheHit.fetchedAt.getTime() < 1000 * 5) {
|
|
||||||
return await cacheHit.value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const pr = this.getPr(branch);
|
const prNumber = this.getListedPr(branch)?.number;
|
||||||
if (!pr) {
|
if (!prNumber) {
|
||||||
toasts.error('Failed to get pull request data'); // TODO: Notify user
|
toasts.error('No pull request number for branch ' + branch);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resp = await this.octokit.pulls.get({
|
const request = async () => {
|
||||||
headers: DEFAULT_HEADERS,
|
const resp = await this.octokit.pulls.get({
|
||||||
owner: this.owner,
|
headers: DEFAULT_HEADERS,
|
||||||
repo: this.repo,
|
owner: this.owner,
|
||||||
pull_number: pr.number
|
repo: this.repo,
|
||||||
});
|
pull_number: prNumber
|
||||||
const detailedPr = Promise.resolve(parsePullRequestResponse(resp.data));
|
});
|
||||||
|
return parsePullRequestResponse(resp.data);
|
||||||
|
};
|
||||||
|
|
||||||
if (detailedPr) this.prCache.set(branch, { value: detailedPr, fetchedAt: new Date() });
|
// Right after pushing GitHub will respond with status 422, retries are necessary
|
||||||
return await detailedPr;
|
let attempt = 0;
|
||||||
|
let pr = await request();
|
||||||
|
|
||||||
|
while (!pr && attempt < 3) {
|
||||||
|
attempt++;
|
||||||
|
try {
|
||||||
|
pr = await request();
|
||||||
|
if (pr) this.prCache.set(branch, { value: Promise.resolve(pr), fetchedAt: new Date() });
|
||||||
|
return pr;
|
||||||
|
} catch (err: any) {
|
||||||
|
if (err.status != 422) throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!pr) throw 'Failed to fetch pull request details';
|
||||||
|
return pr;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPreviousChecksCount(ref: string) {
|
async getPreviousChecksCount(ref: string) {
|
||||||
@ -212,7 +225,7 @@ export class GitHubService {
|
|||||||
return items.map((suite) => suite.count || 0).reduce((a, b) => a + b, 0);
|
return items.map((suite) => suite.count || 0).reduce((a, b) => a + b, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
getPr(branch: string | undefined): PullRequest | undefined {
|
getListedPr(branch: string | undefined): PullRequest | undefined {
|
||||||
if (!branch) return;
|
if (!branch) return;
|
||||||
return this.prs?.find((pr) => pr.targetBranch == branch);
|
return this.prs?.find((pr) => pr.targetBranch == branch);
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
$: ({ error, branches } = data.remoteBranchService);
|
$: ({ error, branches } = data.remoteBranchService);
|
||||||
|
|
||||||
$: branch = $branches?.find((b) => b.sha == $page.params.sha);
|
$: branch = $branches?.find((b) => b.sha == $page.params.sha);
|
||||||
$: pr = githubService.getPr(branch?.displayName);
|
$: pr = githubService.getListedPr(branch?.displayName);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
{#if $error}
|
{#if $error}
|
||||||
|
Loading…
Reference in New Issue
Block a user