Skip to content

Commit

Permalink
✨ Support multiple feeds (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
re-fort authored Sep 21, 2017
1 parent 75004c4 commit 0931c08
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
6 changes: 4 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 33 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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}`)
Expand Down

0 comments on commit 0931c08

Please sign in to comment.