Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/githubHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,11 +729,32 @@ export class GithubHelper {
);

let result = null;
let consecutivePollErrors = 0;
const maxPollRetries = 3;
while (true) {
await utils.sleep(this.delayInMs);
result = await this.githubApi.request(
`GET /repos/${settings.github.owner}/${settings.github.repo}/import/issues/${pending.data.id}`
);
try {
result = await this.githubApi.request(
`GET /repos/${settings.github.owner}/${settings.github.repo}/import/issues/${pending.data.id}`
);
consecutivePollErrors = 0;
} catch (err) {
// Retry transient status-read failures, never the already accepted POST.
// A failed POST is deliberately not retried: it may have been accepted
// by GitHub even if its response did not reach us.
const status = err.status || (err.response && err.response.status);
if (status < 500 || status > 599 || !status ||
consecutivePollErrors >= maxPollRetries) {
throw err;
}
consecutivePollErrors++;
console.log(
`\tRetrying status check for import ${pending.data.id} ` +
`(${consecutivePollErrors}/${maxPollRetries}) after HTTP ${status}.`
);
await utils.sleep(this.delayInMs * consecutivePollErrors);
continue;
}
if (
result.data.status === 'imported' ||
result.data.status === 'failed'
Expand Down
16 changes: 16 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Focused import polling regression tests

After `npm ci`, run on Node.js 18 or newer:

```sh
node --test test/import-polling.test.cjs
```

The tests transpile the actual `GithubHelper` with the existing TypeScript dependency
and replace its runtime imports with isolated test doubles. They do not load local
settings, contact GitHub/GitLab, or execute a migration. They exercise successful,
pending, failed, transient and permanent-error responses, bounded retries, and
verify that the accepted import is never POSTed again within this method.

These are behavioral regression tests, not a project-wide typecheck or live API
compatibility test. Existing caller-level replacement-issue behavior is unchanged.
126 changes: 126 additions & 0 deletions test/import-polling.test.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const ts = require('typescript');

// Execute the real helper with isolated imports: no user settings, credentials,
// service clients or network requests are loaded by this regression suite.
const sourcePath = path.join(__dirname, '../src/githubHelper.ts');
const source = fs.readFileSync(process.env.IMPORT_HELPER_SOURCE || sourcePath, 'utf8');
const compiled = ts.transpileModule(source, {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2019 },
}).outputText;
const sleeps = [];
const exportsObject = {};
vm.runInNewContext(compiled, {
exports: exportsObject,
require(id) {
if (id === '../settings') return { default: { github: { owner: 'example', repo: 'migration' } } };
if (id === './utils') return { sleep: async ms => { sleeps.push(ms); } };
throw new Error(`Unexpected runtime import: ${id}`);
},
console: { log() {}, error() {} },
});
const { GithubHelper } = exportsObject;
const imported = { data: { status: 'imported', issue_url: 'https://api.github.com/repos/example/migration/issues/42' } };
const pending = { data: { status: 'pending' } };
const error = status => Object.assign(new Error('request failed'), { status });

function fixture(polls, postError) {
const calls = [];
const helper = Object.create(GithubHelper.prototype);
helper.delayInMs = 2;
helper.githubApi = { request: async (route, payload) => {
calls.push({ route, payload });
if (route.startsWith('POST ')) {
if (postError) throw postError;
return { data: { id: 123 } };
}
assert.equal(route, 'GET /repos/example/migration/import/issues/123');
assert.ok(polls.length, 'unexpected extra poll');
const next = polls.shift();
if (next instanceof Error) throw next;
return next;
} };
const issue = { title: 'Example', body: 'Original body', closed: false };
return { calls, issue, run: () => helper.requestImportIssue(issue, []) };
}
function assertOnePost(calls) {
assert.equal(calls.filter(call => call.route.startsWith('POST ')).length, 1);
}

test('normal pending/imported flow preserves payload and returned issue number', async () => {
const f = fixture([pending, imported]);
assert.equal(await f.run(), '42');
assert.equal(f.calls[0].payload.issue, f.issue);
assert.equal(f.calls[0].payload.comments.length, 0);
assertOnePost(f.calls);
});

test('transient 500 and 503 status failures retry only GET on the accepted import', async () => {
sleeps.length = 0;
const f = fixture([error(500), error(503), imported]);
assert.equal(await f.run(), '42');
assertOnePost(f.calls);
assert.equal(f.calls.length, 4);
assert.deepEqual(sleeps, [2, 2, 2, 4, 2]);
});

test('retry budget resets after a successful pending response', async () => {
const f = fixture([error(502), error(502), error(502), pending, error(503), imported]);
assert.equal(await f.run(), '42');
assertOnePost(f.calls);
});

test('healthy pending polls do not consume the error retry budget', async () => {
const f = fixture([...Array(12).fill(pending), imported]);
assert.equal(await f.run(), '42');
assertOnePost(f.calls);
});

test('four consecutive status errors exhaust three retries and preserve the error', async () => {
const last = error(503);
const f = fixture([error(500), error(502), error(503), last]);
await assert.rejects(f.run(), e => e === last);
assert.equal(f.calls.length, 5);
assertOnePost(f.calls);
});

test('permanent HTTP and unknown network errors retain upstream fail-fast behavior', async () => {
for (const status of [401, 403, 404, 422, 429, undefined]) {
const failure = error(status);
const f = fixture([failure]);
await assert.rejects(f.run(), e => e === failure);
assert.equal(f.calls.length, 2);
assertOnePost(f.calls);
}
});

test('response.status is recognized for transient errors', async () => {
const failure = Object.assign(new Error('bad gateway'), { response: { status: 502 } });
const f = fixture([failure, imported]);
assert.equal(await f.run(), '42');
assertOnePost(f.calls);
});

test('failed import returns null without resubmitting', async () => {
const f = fixture([{ data: { status: 'failed', errors: ['invalid issue'] } }]);
assert.equal(await f.run(), null);
assertOnePost(f.calls);
});

test('ambiguous POST failure is never automatically resubmitted', async () => {
const failure = error(502);
const f = fixture([], failure);
await assert.rejects(f.run(), e => e === failure);
assert.equal(f.calls.length, 1);
});

test('oversized body is rejected before any request', async () => {
const f = fixture([]);
f.issue.body = 'x'.repeat(65537);
await assert.rejects(f.run(), e => String(e).includes('longer than 65536'));
assert.equal(f.calls.length, 0);
});