From 27a3f1d37a37380b3263472ceae57f065caf81f6 Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Wed, 22 Mar 2017 21:07:27 +0100 Subject: [PATCH] labels: retry request for PR files if GitHub request fails Trying to get around some intermittent GitHub API 404 failures, probably due to a race condition where the API hasn't yet got to update its PR cache or something similar. --- lib/node-repo.js | 17 ++++++++++++----- package.json | 1 + 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib/node-repo.js b/lib/node-repo.js index e133bec0..24dcf50f 100644 --- a/lib/node-repo.js +++ b/lib/node-repo.js @@ -1,11 +1,14 @@ 'use strict' const LRU = require('lru-cache') +const retry = require('async').retry const githubClient = require('./github-client') const resolveLabels = require('./node-labels').resolveLabels const existingLabelsCache = new LRU({ max: 1, maxAge: 1000 * 60 * 60 }) +const fiveSeconds = 5 * 1000 + function deferredResolveLabelsThenUpdatePr (options) { const timeoutMillis = (options.timeoutInSec || 0) * 1000 setTimeout(resolveLabelsThenUpdatePr, timeoutMillis, options) @@ -14,11 +17,15 @@ function deferredResolveLabelsThenUpdatePr (options) { function resolveLabelsThenUpdatePr (options) { options.logger.debug('Fetching PR files for labelling') - githubClient.pullRequests.getFiles({ - owner: options.owner, - repo: options.repo, - number: options.prId - }, (err, res) => { + const getFiles = (cb) => { + githubClient.pullRequests.getFiles({ + owner: options.owner, + repo: options.repo, + number: options.prId + }, cb) + } + + retry({ times: 5, interval: fiveSeconds }, getFiles, (err, res) => { if (err) { return options.logger.error(err, 'Error retrieving files from GitHub') } diff --git a/package.json b/package.json index ba78b5f4..80c42890 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "private": true, "license": "MIT", "dependencies": { + "async": "2.1.5", "basic-auth": "^1.0.4", "body-parser": "^1.15.0", "bunyan": "^1.8.1",