From 0931c08101dc660ccf011ab10cf7a441d13b060b Mon Sep 17 00:00:00 2001 From: re-fort Date: Fri, 22 Sep 2017 00:38:31 +0900 Subject: [PATCH] :sparkles: Support multiple feeds (#2) --- .env.sample | 6 ++++-- index.js | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/.env.sample b/.env.sample index dd42ce3..9992589 100644 --- a/.env.sample +++ b/.env.sample @@ -11,8 +11,10 @@ UPSTREAM_REPO_DEFAULT_BRANCH=lang-ja HEAD_REPO_URL=https://github.com/vuejs/vuejs.org.git HEAD_REPO_DEFAULT_BRANCH=master -FEED_URL=https://github.com/vuejs/vuejs.org/commits/master.atom -FEED_REFRESH=60000 +UPSTREAM_FEED_URL=https://github.com/vuejs/jp.vuejs.org/commits/lang-ja.atom +UPSTREAM_FEED_REFRESH=600 +HEAD_FEED_URL=https://github.com/vuejs/vuejs.org/commits/master.atom +HEAD_FEED_REFRESH=60000 GITHUB_ACCESS_TOKEN=XXXXXXXX SLACK_TOKEN=XXXXXXXX diff --git a/index.js b/index.js index 2b91bf3..76a3526 100644 --- a/index.js +++ b/index.js @@ -4,10 +4,13 @@ const Repo = require('./lib/repository') const Slack = require('./lib/slack') const Utility = require('./lib/utility') -let feeder = new RssFeedEmitter() +let upstreamFeeder = new RssFeedEmitter() +let headFeeder = new RssFeedEmitter() let github = new Github() let slack = new Slack({ token: process.env.SLACK_TOKEN }) +let startUpTime = new Date().toISOString() + let remote = { origin: { url: process.env.ORIGIN_REPO_URL, @@ -42,17 +45,18 @@ let repo = new Repo( function setup() { repo.setup() github.authenticate({ type: 'token', token: process.env.GITHUB_ACCESS_TOKEN }) - setupFeeder() + setupUpstreamFeeder() + setupHeadFeeder() } -function setupFeeder() { - feeder.add({ - url: process.env.FEED_URL, - refresh: Number(process.env.FEED_REFRESH), +function setupHeadFeeder() { + headFeeder.add({ + url: process.env.HEAD_FEED_URL, + refresh: Number(process.env.HEAD_FEED_REFRESH), }) - feeder.on('new-item', function(item) { - Utility.log('I', `New commit: ${item.title}`) + headFeeder.on('new-item', function(item) { + Utility.log('I', `New commit on head repo: ${item.title}`) let hash = Utility.extractBasename(item.link) // branch names consisting of 40 hex characters are not allowed let shortHash = hash.substr(0, 8) @@ -77,6 +81,27 @@ function setupFeeder() { }) } +function setupUpstreamFeeder() { + upstreamFeeder.add({ + url: process.env.UPSTREAM_FEED_URL, + refresh: Number(process.env.UPSTREAM_FEED_REFRESH), + }) + + upstreamFeeder.on('new-item', function(item) { + if (startUpTime < item.date.toISOString()) { + Utility.log('I', `New commit on upstream repo: ${item.title}`) + removeHeadFeeder() + setupHeadFeeder() + } + }) +} + +function removeHeadFeeder() { + headFeeder.off('new-item') + headFeeder.destroy() +} + + async function after(item, hash, shortHash) { const { data: pullRequest } = await github.createPullRequest(remote, { title: item.title, body: `Cherry picked from ${item.link}`, branch: shortHash }) Utility.log('S', `Created new pull request: ${pullRequest.html_url}`)