Skip to content

Instantly share code, notes, and snippets.

@siygle
Last active January 12, 2018 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siygle/899e3964cfbc8a4b2909528520a21a28 to your computer and use it in GitHub Desktop.
Save siygle/899e3964cfbc8a4b2909528520a21a28 to your computer and use it in GitHub Desktop.
Initialize comment issue
// 1. npm install sitemapper r2 cheerio p-each-series delay github
// 2. USERNAME=GITHUB_ACCOUNT REPO=GITHUB_REPO TOKEN=GITHUB_TOKEN SITEMAP=YOUR_SITEMAP_URL node initial-comment.js
#!/usr/bin/env node
const util = require('util')
const Sitemapper = require('sitemapper')
const sitemap = new Sitemapper()
const r2 = require('r2')
const cheerio = require('cheerio')
const pEachSeries = require('p-each-series')
const pDelay = require('delay')
const GitHubApi = require('github')
const github = new GitHubApi()
async function fetchPageTitle (url) {
try {
const html = await r2(url).text
const $ = cheerio.load(html)
return $('title').text()
} catch (err) {
throw err
}
}
async function initialIssue (opts) {
try {
const data = await sitemap.fetch(opts.SITEMAP)
if (data && data.sites && Array.isArray(data.sites)) {
github.authenticate({
type: 'token',
token: opts.TOKEN
})
const createGhIssue = util.promisify(github.issues.create)
await pEachSeries(data.sites, async (url, idx) => {
const title = await fetchPageTitle(url)
const ret = await createGhIssue({
owner: opts.USERNAME,
repo: opts.REPO,
title: title,
body: url,
labels: [url, 'Gitalk'],
})
console.log(`Import: ${url}/${title}`)
// Let github api take a rest every ten records ;)
if (idx % 10 === 0 && idx !== 0) {
await pDelay(10000)
}
})
} else {
throw new Error(`Not a valid sitemap format: ${opts.SITEMAP}`)
}
} catch (err) {
throw err
}
}
if (!process.env.USERNAME || !process.env.REPO || !process.env.TOKEN || !process.env.SITEMAP) {
console.log('USERNAME/REPO/TOKEN/SITEMAP are necessary env')
process.exit(1)
} else {
const { USERNAME, REPO, TOKEN, SITEMAP } = process.env
console.log(`Settings: ${USERNAME}/${REPO}/${TOKEN}/${SITEMAP}`)
initialIssue({
USERNAME,
REPO,
TOKEN,
SITEMAP
})
.then(resp => {
console.log('Done!')
})
.catch(err => {
console.error('ERROR: ', err)
process.exit(1)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment