Skip to content

Commit

Permalink
#6 Push after creating issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fuku710 committed Mar 16, 2020
1 parent 4527017 commit c04515a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
14 changes: 9 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
61 changes: 27 additions & 34 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -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");
Expand Down Expand Up @@ -124,15 +126,15 @@ 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`);
throw e;
}
}

async setIssues(issues) {
// Create issue and comment
async createIssues(issues) {
for (const issue of issues) {
try {
const body = this.createBodyWithOriginalUserInfo(
Expand All @@ -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;
Expand All @@ -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!`);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c04515a

Please sign in to comment.