Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 32 additions & 41 deletions lib/node-repo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const LRU = require('lru-cache')
const retry = require('async').retry
const Aigle = require('aigle')
const request = require('request')

Expand All @@ -21,30 +20,22 @@ async function deferredResolveLabelsThenUpdatePr (options) {
return resolveLabelsThenUpdatePr(options)
}

function resolveLabelsThenUpdatePr (options) {
options.logger.debug('Fetching PR files for labelling')

const listFiles = (cb) => {
githubClient.pullRequests.listFiles({
owner: options.owner,
repo: options.repo,
number: options.prId
}, cb)
}
async function resolveLabelsThenUpdatePr (options) {
const times = options.retries || 5
const interval = options.retryInterval || fiveSeconds
const retry = fn => Aigle.retry({ times, interval }, fn)

return new Promise((resolve, reject) => {
retry({ times: 5, interval: fiveSeconds }, listFiles, (err, res) => {
if (err) {
options.logger.error(err, 'Error retrieving files from GitHub')
return reject(err)
}
const filepathsChanged = await retry(() => listFiles({
owner: options.owner,
repo: options.repo,
number: options.prId,
logger: options.logger
}))
options.logger.debug('Fetching PR files for labelling')

const filepathsChanged = res.data.map((fileMeta) => fileMeta.filename)
const resolvedLabels = resolveLabels(filepathsChanged, options.baseBranch)
const resolvedLabels = resolveLabels(filepathsChanged, options.baseBranch)

resolve(fetchExistingThenUpdatePr(options, resolvedLabels))
})
})
return fetchExistingThenUpdatePr(options, resolvedLabels)
}

async function fetchExistingThenUpdatePr (options, labels) {
Expand Down Expand Up @@ -83,28 +74,32 @@ async function updatePrWithLabels (options, labels) {
}
}

function removeLabelFromPR (options, label) {
async function removeLabelFromPR (options, label) {
// no need to request github if we didn't resolve a label
if (!label) {
return
}

options.logger.debug('Trying to remove label: ' + label)

githubClient.issues.removeLabel({
owner: options.owner,
repo: options.repo,
number: options.prId,
name: label
}, (err) => {
if (err) {
if (err.code === 404) return options.logger.info('Label to remove did not exist, bailing ' + label)

return options.logger.error(err, 'Error while removing a label')
try {
await githubClient.issues.removeLabel({
owner: options.owner,
repo: options.repo,
number: options.prId,
name: label
})
} catch (err) {
if (err.code === 404) {
options.logger.info('Label to remove did not exist, bailing ' + label)
throw err
}
options.logger.error(err, 'Error while removing a label')
throw err
}

options.logger.info('Removed a label ' + label)
})
options.logger.info('Removed a label ' + label)
return label
}

async function fetchExistingLabels (options) {
Expand Down Expand Up @@ -154,11 +149,7 @@ function getBotPrLabels (options, cb) {
page: 1,
per_page: 100, // we probably won't hit this
number: options.prId
}, (err, res) => {
if (err) {
return cb(err)
}

}).then(res => {
const events = res.data || []
const ourLabels = []

Expand All @@ -179,7 +170,7 @@ function getBotPrLabels (options, cb) {
}

cb(null, ourLabels)
})
}, cb)
}

function stringsInCommon (arr1, arr2) {
Expand Down
93 changes: 40 additions & 53 deletions lib/push-jenkins-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ function pushStarted (options, build, cb) {
}
}

findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
cb(err)
return
}

findLatestCommitInPr(optsWithPr).then(latestCommit => {
const statusOpts = Object.assign({
sha: latestCommit.sha,
url: build.url,
Expand All @@ -44,7 +38,10 @@ function pushStarted (options, build, cb) {
message: build.message || 'running tests'
}, options)

createGhStatus(statusOpts, logger, cb)
createGhStatus(statusOpts, logger).then(cb).catch(cb)
}, err => {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
return cb(err)
})
}

Expand All @@ -57,13 +54,7 @@ function pushEnded (options, build, cb) {

const optsWithPr = Object.assign({ pr }, options)

findLatestCommitInPr(optsWithPr, (err, latestCommit) => {
if (err) {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
cb(err)
return
}

findLatestCommitInPr(optsWithPr).then(latestCommit => {
const statusOpts = Object.assign({
sha: latestCommit.sha,
url: build.url,
Expand All @@ -72,7 +63,10 @@ function pushEnded (options, build, cb) {
message: build.message || 'all tests passed'
}, options)

createGhStatus(statusOpts, logger, cb)
createGhStatus(statusOpts, logger).then(cb, cb)
}, err => {
logger.error(err, 'Got error when retrieving GitHub commits for PR')
return cb(err)
})
}

Expand All @@ -81,52 +75,45 @@ function findPrInRef (gitRef) {
return parseInt(gitRef.split('/')[2], 10)
}

function findLatestCommitInPr (options, cb, pageNumber = 1) {
githubClient.pullRequests.listCommits({
async function findLatestCommitInPr (options, pageNumber = 1) {
const res = await githubClient.pullRequests.listCommits({
owner: options.owner,
repo: options.repo,
number: options.pr,
page: pageNumber,
per_page: 100
}, (err, res) => {
if (err) {
return cb(err)
}

const commitMetas = res.data || []
const lastPageURL = githubClient.hasLastPage(res)
if (lastPageURL) {
return findLatestCommitInPr(options, cb, pageNumberFromURL(lastPageURL))
}
})

const lastCommitMeta = commitMetas.pop()
const lastCommit = {
sha: lastCommitMeta.sha,
date: lastCommitMeta.commit.committer.date
}
const commitMetas = res.data || []
const lastPageURL = githubClient.hasLastPage(res)
if (lastPageURL) {
return findLatestCommitInPr(options, pageNumberFromURL(lastPageURL))
}
const lastCommitMeta = commitMetas.pop()
const lastCommit = {
sha: lastCommitMeta.sha,
date: lastCommitMeta.commit.committer.date
}

cb(null, lastCommit)
})
return lastCommit
}

function createGhStatus (options, logger, cb) {
githubClient.repos.createStatus({
owner: options.owner,
repo: options.repo,
sha: options.sha,
target_url: options.url,
context: options.context,
state: options.state,
description: options.message
}, (err, res) => {
if (err) {
logger.error(err, 'Error while updating Jenkins / GitHub PR status')
cb(err)
return
}
logger.info('Jenkins / Github PR status updated')
cb(null)
})
async function createGhStatus (options, logger) {
try {
await githubClient.repos.createStatus({
owner: options.owner,
repo: options.repo,
sha: options.sha,
target_url: options.url,
context: options.context,
state: options.state,
description: options.message
})
} catch (err) {
logger.error(err, 'Error while updating Jenkins / GitHub PR status')
throw err
}
logger.info('Jenkins / Github PR status updated')
}

function validate (payload) {
Expand Down
Loading