Skip to content

Commit 8334127

Browse files
hulkobajanl
authored andcommitted
feat(sync): paginate github results
1 parent 14dab58 commit 8334127

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

jobs/sync-repos.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const redis = require('redis')
22
const { promisify } = require('bluebird')
33
const Log = require('gk-log')
4+
const { flatten } = require('lodash')
45

56
const dbs = require('../lib/dbs')
67
const githubQueue = require('../lib/github-queue')
@@ -31,13 +32,21 @@ module.exports = async function ({ accountId }) {
3132
}
3233

3334
try {
34-
gitHubRepos = await githubQueue(installationId).read(github => github.apps.listRepos({
35+
const options = await githubQueue(installationId).read(github => github.apps.listRepos.endpoint.merge({
3536
headers: {
3637
accept: 'application/vnd.github.machine-man-preview+json'
3738
},
3839
org: 'neighbourhoodie',
3940
per_page: 100
4041
}))
42+
43+
// Paginate does not actually flatten results into a single result array
44+
// as it should, according to the docs, possibly due to these:
45+
// https://github.com/octokit/rest.js/issues/1161
46+
// https://github.com/octokit/routes/issues/329
47+
const results = await githubQueue(installationId).read(github => github.paginate(options))
48+
// So we flatten them ourselves
49+
gitHubRepos = flatten(results.map((result) => result.repositories))
4150
} catch (error) {
4251
log.error('Could not get repos from Github', { error: error.message })
4352
}
@@ -46,8 +55,8 @@ module.exports = async function ({ accountId }) {
4655

4756
// create missing repositories
4857
let reposToCreate = []
49-
gitHubRepos.repositories.map(ghRepo => {
50-
if (!ghRepo.name.includes(dbRepos)) {
58+
gitHubRepos.map(ghRepo => {
59+
if (!dbRepos.includes(ghRepo.name)) {
5160
reposToCreate.push(ghRepo)
5261
}
5362
})

0 commit comments

Comments
 (0)