diff --git a/index.js b/index.js index cd4891a..c231e7c 100644 --- a/index.js +++ b/index.js @@ -158,13 +158,17 @@ dotenv.config(); try { const desc = await sourceRepo.getRepositoryDescription(); await targetRepo.createRemoteRepository(desc); - transferRepository(sourceRepoUrl, targetRepoUrl); + const issues = await sourceRepo.getIssues(); - console.log(`${issues.length} issues`); - await targetRepo.setIssues(issues); + await targetRepo.createIssues(issues); + + transferRepository(sourceRepoUrl, targetRepoUrl); + + await targetRepo.changeIssuesToPullRequests(issues); + await targetRepo.closeIssuesAndPullRequests(issues); + const hooks = await sourceRepo.getHooks(); - console.log(`${hooks.length} hooks`); - await targetRepo.setHooks(hooks); + await targetRepo.createHooks(hooks); } catch (e) { console.log(`${e.name}:${e.message}`); } diff --git a/lib/repository.js b/lib/repository.js index 4690375..8449085 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -36,6 +36,7 @@ class Repository { async getRepositoryDescription() { try { const res = await this.got.get(`repos/${this.owner}/${this.repo}`); + console.log("Got repository description"); return res.body.description; } catch (e) { console.log(`Failed to get repository description`); @@ -53,6 +54,7 @@ class Repository { description: description } }); + console.log("Remote repository was created!"); } catch (e) { if (e.response.statusCode === 422) { console.log("Remote repository has already created"); @@ -124,6 +126,7 @@ class Repository { const link = parse(res.headers.link); if (!link || !link.last) break; } + console.log(`Got ${issues.length} issues`); return issues; } catch (e) { console.log(`Failed to get issues`); @@ -131,8 +134,7 @@ class Repository { } } - async setIssues(issues) { - // Create issue and comment + async createIssues(issues) { for (const issue of issues) { try { const body = this.createBodyWithOriginalUserInfo( @@ -144,12 +146,7 @@ class Repository { ); const res = await this.got.post( `repos/${this.owner}/${this.repo}/issues`, - { - json: { - title: issue.title, - body - } - } + { json: { title: issue.title, body } } ); try { const commentsUrl = res.body.comments_url; @@ -176,42 +173,36 @@ class Repository { throw e; } } - // Change issue to PullRequest + } + + async changeIssuesToPullRequests(issues) { for (const issue of issues) { - if (issue.isPullRequest) { - const { number, head, base } = issue; - try { + try { + if (issue.isPullRequest) { + const { number, head, base } = issue; await this.got.post(`repos/${this.owner}/${this.repo}/pulls`, { - json: { - issue: number, - head, - base - } + json: { issue: number, head, base } }); console.log(`Issue ${issue.number} was to be pull request!`); - } catch (e) { - if (e.response.statusCode === 422) { - console.log( - `Issue ${issue.number} has not changed to pull request` - ); - } else { - console.log( - `Failed to change issue to pull request:${issue.number}` - ); - throw e; - } + } + } catch (e) { + if (e.response.statusCode === 422) { + console.log(`Issue ${issue.number} has not changed to pull request`); + } else { + console.log(`Failed to change issue to pull request:${issue.number}`); + throw e; } } } - // Close issue + } + + async closeIssuesAndPullRequests(issues) { for (const issue of issues) { try { if (issue.state === "closed") { await this.got.patch( `repos/${this.owner}/${this.repo}/issues/${issue.number}`, - { - json: { state: "closed" } - } + { json: { state: "closed" } } ); console.log(`Issue ${issue.number} was closed!`); } @@ -241,14 +232,16 @@ class Repository { async getHooks() { try { const res = await this.got.get(`repos/${this.owner}/${this.repo}/hooks`); - return res.body; + const hooks = res.body; + console.log(`Got ${hooks.length} hooks`); + return hooks; } catch (e) { console.log(`Failed to get repository hooks`); throw e; } } - async setHooks(hooks) { + async createHooks(hooks) { for (const hook of hooks) { try { const { name, config, events, active } = hook;