diff --git a/index.js b/index.js index 7979716..33f1e07 100644 --- a/index.js +++ b/index.js @@ -62,6 +62,13 @@ function setupHeadFeeder() { let hash = Utility.extractBasename(item.link) // branch names consisting of 40 hex characters are not allowed let shortHash = hash.substr(0, 8) + const { data: result } = await github.searchIssue(remote, { hash }) + + if (result.total_count === 0) { + let body = `本家のドキュメントに更新がありました:page_facing_up:\r\nOriginal:${item.link}` + const { data: newIssue } = await github.createIssue(remote, { title: `[Doc]: ${item.title}`, body, labels: ['documentation'] }) + Utility.log('S', `Issue created: ${newIssue.html_url}`) + } if (repo.existsRemoteBranch(shortHash)) { Utility.log('W', `Remote branch already exists: ${shortHash}`) diff --git a/lib/github.js b/lib/github.js index 48a5a0f..a695950 100644 --- a/lib/github.js +++ b/lib/github.js @@ -13,6 +13,43 @@ class GitHub { }) } + createIssue(remote, params = {}) { + return new Promise((resolve, reject) => { + this.github.issues.create({ + owner: remote.upstream.owner, + repo: remote.upstream.name, + title: params.title, + body: params.body, + labels: params.labels, + }) + .then(res => resolve(res)) + .catch(err => reject(err)) + }) + } + + closeIssue(remote, params = {}) { + return new Promise((resolve, reject) => { + this.github.issues.edit({ + owner: remote.upstream.owner, + repo: remote.upstream.name, + number: params.number, + state: 'closed', + }) + .then(res => resolve(res)) + .catch(err => reject(err)) + }) + } + + searchIssue(remote, params = {}) { + return new Promise((resolve, reject) => { + this.github.search.issues({ + q: `${params.hash} repo:${remote.upstream.owner}/${remote.upstream.name} type:issue`, + }) + .then(res => resolve(res)) + .catch(err => reject(err)) + }) + } + createPullRequest(remote, params = {}) { return new Promise((resolve, reject) => { this.github.pullRequests.create({ diff --git a/test/lib/github.js b/test/lib/github.js index 76dbbd0..c4c670e 100644 --- a/test/lib/github.js +++ b/test/lib/github.js @@ -16,6 +16,34 @@ describe('Github', function () { }, } + describe('issue', function () { + let issueNumber + let hash = 'c0601c7981c0b6a53373cc70c2a1edb20908da68' + let url = `https://github.com/vuejs-jp-bot/test/commit/${hash}` + + describe('createIssue()', function () { + it('creates new issue', async function () { + const { data: newIssue } = await github.createIssue(remote, { title: 'Test', body: `Test\r\nOriginal:${url}`, labels: ['documentation'] }) + issueNumber = newIssue.number + assert(newIssue.state === 'open') + }) + }) + + describe('searchIssue()', function () { + it('seaches issue', async function () { + const { data: result } = await github.searchIssue(remote, { hash }) + assert(result.total_count >= 1) + }) + }) + + describe('closeIssue()', async function () { + it('closes opened issue', async function () { + const { data: closedIssue } = await github.closeIssue(remote, { number: issueNumber }) + assert(closedIssue.state === 'closed') + }) + }) + }) + describe('pullRequest', function () { let pullRequestNumber