Skip to content
This repository has been archived by the owner on Feb 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #31 from mapbox/shaerror
Browse files Browse the repository at this point in the history
do not error if sha cannot be found on github
  • Loading branch information
vsmart authored Nov 9, 2017
2 parents 529e7f6 + 12578b1 commit 20c3136
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 6 deletions.
38 changes: 32 additions & 6 deletions lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,24 @@ stork.status = (event, context, callback) => {
.catch((err) => Traceable.promise(err))
];

let token;
let sha;
let owner;
let repo;

Promise.all(requests)
.then((results) => {
const token = results[0];
token = results[0];
const data = results[1];

const build = data.builds[0];
if (!build) return;

const logs = build.logs.deepLink;
const sha = build.sourceVersion;
sha = build.sourceVersion;
const source = url.parse(build.source.location);
const owner = source.pathname.split('/')[1];
const repo = source.pathname.split('/')[2].replace(/.git$/, '');
owner = source.pathname.split('/')[1];
repo = source.pathname.split('/')[2].replace(/.git$/, '');

const uri = `https://api.github.com/repos/${owner}/${repo}/statuses/${sha}`;
const status = {
Expand Down Expand Up @@ -503,8 +508,29 @@ stork.status = (event, context, callback) => {
})
.then(() => callback())
.catch((err) => {
console.log(err);
callback(err);
const shaUri = `https://api.github.com/repos/${owner}/${repo}/commits/${sha}`;
const shaConfig = {
json: true,
headers: {
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: `token ${token}`
}
};

got.get(shaUri, shaConfig)
.then((res) => {
console.log(err);
callback(err);
})
.catch((shaErr) => {
if (shaErr.statusCode === 404 && shaErr.statusMessage === 'Not Found') {
console.log('Sha does not exist, ignore stork error');
return callback();
}
console.log(err);
callback(err);
});
});
});
};
Expand Down
263 changes: 263 additions & 0 deletions test/lambda.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,269 @@ test('[lambda] status: success', (assert) => {
});
});

test('[lambda] status: github 422 error, get sha returns 404 not found', (assert) => {
const environment = env(statusVars).mock();

sinon.stub(lambda, 'decrypt').callsFake(fakeDecrypt);

const getBuild = AWS.stub('CodeBuild', 'batchGetBuilds', function() {
this.request.promise.returns(Promise.resolve({
builds: [
{
logs: { deepLink: 'url for cloudwatch logs' },
sourceVersion: '12345shanotfound',
source: { location: 'https://github.com/mapbox/stork' }
}
]
}));
});

sinon.stub(got, 'get')
.onCall(0).callsFake(() => Promise.resolve())
.onCall(1).callsFake(() => Promise.reject({
statusCode: 404,
statusMessage: 'Not Found'
}));

sinon.stub(got, 'post')
.onCall(0).callsFake(() => Promise.resolve({
body: { token: 'v1.1f699f1069f60xxx' }
}))
.onCall(1).callsFake(() => Promise.reject({
statusCode: 422,
statusMessage: 'Unprocessable Entity'
}));

lambda.status(fakeStatusEvent, {}, (err) => {
const args = JSON.parse(JSON.stringify(got.post.args[0]));
const auth = args[1].headers.Authorization.replace('Bearer ', '');
const decoded = jwt.verify(auth, publicKey, { algorithms: ['RS256'] });

assert.ifError(err, 'does not error on missing GitSha');

assert.ok(
got.post.calledWith(
'https://api.github.com/repos/mapbox/stork/statuses/12345shanotfound',
{
json: true,
headers: {
'Content-type': 'application/json',
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: 'token v1.1f699f1069f60xxx'
},
body: JSON.stringify({
context: 'stork',
description: 'Your build succeeded',
state: 'success',
target_url: 'url for cloudwatch logs'
})
}
),
'sets PR status via github api request'
);

assert.equal(got.get.callCount, 2, '2 get requests to github api');
assert.ok(
got.get.calledWith(
'https://api.github.com/repos/mapbox/stork/commits/12345shanotfound',
{
json: true,
headers: {
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: 'token v1.1f699f1069f60xxx'
}
}
),
'call to check if sha exists'
);

environment.restore();
got.get.restore();
got.post.restore();
lambda.decrypt.restore();
AWS.CodeBuild.restore();
assert.end();
});
});

test('[lambda] status: github 422 error, get sha returns non 404 error', (assert) => {
const environment = env(statusVars).mock();

sinon.stub(lambda, 'decrypt').callsFake(fakeDecrypt);

const getBuild = AWS.stub('CodeBuild', 'batchGetBuilds', function() {
this.request.promise.returns(Promise.resolve({
builds: [
{
logs: { deepLink: 'url for cloudwatch logs' },
sourceVersion: '12345shanotfound',
source: { location: 'https://github.com/mapbox/stork' }
}
]
}));
});

sinon.stub(got, 'get')
.onCall(0).callsFake(() => Promise.resolve())
.onCall(1).callsFake(() => Promise.reject({
statusCode: 500,
statusMessage: 'Backend Error'
}));

sinon.stub(got, 'post')
.onCall(0).callsFake(() => Promise.resolve({
body: { token: 'v1.1f699f1069f60xxx' }
}))
.onCall(1).callsFake(() => Promise.reject({
statusCode: 422,
statusMessage: 'Unprocessable Entity'
}));

lambda.status(fakeStatusEvent, {}, (err) => {
const args = JSON.parse(JSON.stringify(got.post.args[0]));
const auth = args[1].headers.Authorization.replace('Bearer ', '');
const decoded = jwt.verify(auth, publicKey, { algorithms: ['RS256'] });

assert.equal(err.statusMessage, 'Unprocessable Entity', 'errors');

assert.ok(
got.post.calledWith(
'https://api.github.com/repos/mapbox/stork/statuses/12345shanotfound',
{
json: true,
headers: {
'Content-type': 'application/json',
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: 'token v1.1f699f1069f60xxx'
},
body: JSON.stringify({
context: 'stork',
description: 'Your build succeeded',
state: 'success',
target_url: 'url for cloudwatch logs'
})
}
),
'sets PR status via github api request'
);

assert.equal(got.get.callCount, 2, '2 get requests to github api');
assert.ok(
got.get.calledWith(
'https://api.github.com/repos/mapbox/stork/commits/12345shanotfound',
{
json: true,
headers: {
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: 'token v1.1f699f1069f60xxx'
}
}
),
'call to check if sha exists'
);

environment.restore();
got.get.restore();
got.post.restore();
lambda.decrypt.restore();
AWS.CodeBuild.restore();
assert.end();
});
});

test('[lambda] status: github 422 error, get sha returns 200', (assert) => {
const environment = env(statusVars).mock();

sinon.stub(lambda, 'decrypt').callsFake(fakeDecrypt);

const getBuild = AWS.stub('CodeBuild', 'batchGetBuilds', function() {
this.request.promise.returns(Promise.resolve({
builds: [
{
logs: { deepLink: 'url for cloudwatch logs' },
sourceVersion: '12345shaexists',
source: { location: 'https://github.com/mapbox/stork' }
}
]
}));
});

sinon.stub(got, 'get')
.onCall(0).callsFake(() => Promise.resolve())
.onCall(1).callsFake(() => Promise.resolve({
statusCode: 200,
statusMessage: 'OK',
body: {
sha: '12345shaexists'
}
}));

sinon.stub(got, 'post')
.onCall(0).callsFake(() => Promise.resolve({
body: { token: 'v1.1f699f1069f60xxx' }
}))
.onCall(1).callsFake(() => Promise.reject({
statusCode: 422,
statusMessage: 'Unprocessable Entity'
}));

lambda.status(fakeStatusEvent, {}, (err) => {
const args = JSON.parse(JSON.stringify(got.post.args[0]));
const auth = args[1].headers.Authorization.replace('Bearer ', '');
const decoded = jwt.verify(auth, publicKey, { algorithms: ['RS256'] });

assert.equal(err.statusMessage, 'Unprocessable Entity', 'errors');
assert.ok(
got.post.calledWith(
'https://api.github.com/repos/mapbox/stork/statuses/12345shaexists',
{
json: true,
headers: {
'Content-type': 'application/json',
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: 'token v1.1f699f1069f60xxx'
},
body: JSON.stringify({
context: 'stork',
description: 'Your build succeeded',
state: 'success',
target_url: 'url for cloudwatch logs'
})
}
),
'sets PR status via github api request'
);

assert.equal(got.get.callCount, 2, '2 get requests to github api');
assert.ok(
got.get.calledWith(
'https://api.github.com/repos/mapbox/stork/commits/12345shaexists',
{
json: true,
headers: {
'User-Agent': 'github.com/mapbox/stork',
Accept: 'application/vnd.github.machine-man-preview+json',
Authorization: 'token v1.1f699f1069f60xxx'
}
}
),
'authenticated as the github app'
);

environment.restore();
got.get.restore();
got.post.restore();
lambda.decrypt.restore();
AWS.CodeBuild.restore();
assert.end();
});
});

test('[lambda] forwarder: success', (assert) => {
const environment = env(forwaderVars).mock();

Expand Down

0 comments on commit 20c3136

Please sign in to comment.