fix(ci): retry GitHub file reads after gateway timeouts (#11987)

Co-authored-by: ShuaixinHuang <197200901+ShuaixinHuang@users.noreply.github.com>
This commit is contained in:
ShuaixinHuangandShuaixinHuang authored and GitHub committed 2026-11-18 14:58:02 -05:00
1 parent e98a767428
commit f4efc293dd
2 files changed
+79

No files matched your search

+30
View File
@@ -81,6 +81,36 @@
// In this case, there is no squashed merge commit. This could be a simple
// merge commit, a rebase merge commit, or a direct commit to the branch.
// Fallback to fetching the list of commits from the REST API. In the future
@@ -995,14 +995,21 @@
*/
async getFileContentsOnBranch(path, branch) {
this.logger.debug(`Fetching ${path} from branch ${branch}`);
- try {
- return await this.fileCache.getFileContents(path, branch);
- }
- catch (e) {
- if (e instanceof git_file_utils_1.FileNotFoundError) {
- throw new errors_1.FileNotFoundError(path);
- }
- throw e;
+ for (let attempt = 0; ; attempt++) {
+ try {
+ return await this.fileCache.getFileContents(path, branch);
+ }
+ catch (e) {
+ if (e instanceof git_file_utils_1.FileNotFoundError) {
+ throw new errors_1.FileNotFoundError(path);
+ }
+ // Retrying this read is safe even when a release proposal is being updated.
+ if (e?.status !== 504 || attempt >= 2) {
+ throw e;
+ }
+ this.logger.info('Received HTTP 504 reading a repository file, retrying');
+ await (0, exports.sleepInMs)(1000 * (attempt + 1));
+ }
}
}
async getFileJson(path, branch) {
@@ -1154,15 +1154,40 @@
* @returns {string} HTML URL of the new file
*/
@@ -138,6 +138,55 @@ function respond(value: unknown, status = 200) {
});
}
it.each([
{ statuses: [504, 200], expectedReads: 2, status: null },
{ statuses: [504, 504, 504], expectedReads: 3, status: 504 },
{ statuses: [403], expectedReads: 1, status: 403 },
])(
"retries release note reads with HTTP responses $statuses",
async ({ statuses, expectedReads, status }) => {
const sleep = vi
.spyOn(githubRuntime, "sleepInMs")
.mockResolvedValue(undefined);
const branch = "proposal--release-notes";
const path = "release-notes.md";
const notes = "Existing release notes";
const reads: string[] = [];
const github = await upstream.GitHub.create({
owner: "fixture",
repo: "rea",
defaultBranch: "main",
logger: silent,
fetch: async (input: string, options: { method?: string }) => {
const { pathname } = new URL(input);
expect(options.method).toBe("GET");
if (pathname === `/repos/fixture/rea/git/trees/${branch}`) {
reads.push(pathname);
const responseStatus = statuses[reads.length - 1];
if (responseStatus !== 200) return respond({}, responseStatus);
return respond({ tree: [{ path, sha: baseline, type: "blob" }] });
}
expect(pathname).toBe(`/repos/fixture/rea/git/blobs/${baseline}`);
return respond({
sha: baseline,
content: Buffer.from(notes).toString("base64"),
encoding: "base64",
});
},
});
const result = github.getFileContentsOnBranch(path, branch);
if (status === null) {
await expect(result).resolves.toMatchObject({ parsedContent: notes });
} else {
await expect(result).rejects.toMatchObject({ status });
}
expect(reads).toHaveLength(expectedReads);
expect(sleep.mock.calls).toEqual(
[[1000], [2000]].slice(0, expectedReads - 1),
);
},
);
function associatedPullRequest(number = 42, mergeCommit = latest) {
return {
number,