Last active
May 10, 2017 03:15
-
-
Save cknowles/c1050b425ce3ac4c3f7ecb5c9fb0b00c to your computer and use it in GitHub Desktop.
Quick AWS Lambda hack to redirect from a Codeship project UUID to integer based ID (prior to then adding more information to build env)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AWS = require('aws-sdk'); | |
const https = require('https'); | |
const config = { | |
}; | |
const projectLookupCache = { | |
}; | |
// use KMS to decrypt API key in apiKey environment variable | |
const decryptParams = { | |
CiphertextBlob: new Buffer(process.env.apiKey, 'base64'), | |
}; | |
const kms = new AWS.KMS({ apiVersion: '2014-11-01' }); | |
kms.decrypt(decryptParams, (error, data) => { | |
if (error) { | |
config.apiKeyInitError = error; | |
console.log(error); | |
} else { | |
config.apiKey = data.Plaintext.toString('ascii'); | |
} | |
}); | |
// entry point | |
exports.handler = (event, context, callback) => { | |
// console.log(event); | |
function redirect(uuid, buildId, projectId, cached, callback) { | |
let redirect = `https://app.codeship.com/projects/${projectId}/`; | |
if (buildId) { | |
redirect += `builds/${buildId}`; | |
} | |
callback(null, { | |
statusCode: 301, | |
headers: { Location: redirect }, | |
body: JSON.stringify({ id: projectId, uuid: uuid, build_id: buildId, redirect: redirect, cached: cached }) | |
}); | |
}; | |
function getProjectID() { | |
if (!config.apiKey) { | |
if (config.apiKeyInitError) { | |
console.log('Error in decrypt the API key. Not retrying.'); | |
return callback(new Error(config.apiKeyInitError)); | |
} | |
console.log('Cannot get project ID since API key has not been initialized yet. Trying again in 100 ms.'); | |
setTimeout(() => getProjectID(), 100); | |
return; | |
} | |
try { | |
const uuid = event.queryStringParameters.uuid; | |
const buildId = event.queryStringParameters.build_id; | |
if (projectLookupCache[uuid]) { | |
console.log(`Found project ID ${projectLookupCache[uuid]} for UUID ${uuid} in info already looked up`); | |
redirect(uuid, buildId, projectLookupCache[uuid], true, callback); | |
} else { | |
// query codeship | |
console.log(`Going to query codeship for the project ID for UUID ${uuid}`); | |
const buildId = event.queryStringParameters.build_id; | |
const url = `https://app.codeship.com/api/v1/projects.json?api_key=${config.apiKey}`; | |
const req = https.get(url, (res) => { | |
console.log('Codeship response statusCode:', res.statusCode); | |
let body = ''; | |
res.on('data', (data) => { | |
// console.log(data.toString()); | |
body += data.toString(); | |
}); | |
res.on('end', () => { | |
console.log('No more data in codeship response'); | |
console.log('Codeship data', body); | |
const result = JSON.parse(body); | |
if (result.projects) { | |
const found = result.projects.filter((project) => { | |
return project.uuid === uuid; | |
}); | |
console.log('Found project ', found); | |
if (found && found[0]) { | |
// setup the cache | |
projectLookupCache[uuid] = found[0].id; | |
redirect(uuid, buildId, found[0].id, false, callback); | |
} else { | |
callback(null, { | |
statusCode: 404, | |
body: JSON.stringify({ message: `Project with UUID ${uuid} not found` }) | |
}); | |
} | |
} else { | |
console.log('`projects` not found in response'); | |
callback(new Error('`projects` not found in response')); | |
} | |
}); | |
}); | |
req.on('error', (err) => { | |
console.log('problem with request:', err.toString()); | |
callback(new Error(err)); | |
}); | |
req.end(); | |
} | |
} catch (ex) { | |
console.log(ex.message); | |
callback(new Error(ex.message)); | |
} | |
} | |
getProjectID(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment