Skip to content

Commit

Permalink
fix: Simplify github config (#95)
Browse files Browse the repository at this point in the history
* Simplify github config
* Fix hoisting
  • Loading branch information
diervo authored Mar 18, 2018
1 parent 5cb6c76 commit 9a502c6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/best-frontend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = function (options = {}) {
// -- Routes ---------------------------

// API for benchmarks, stats & artifacts
parent.use('/api/v1', ApiV1.addRoutes(express.Router(), storeInstance));
parent.use('/api/v1', ApiV1.addRoutes(express.Router(), storeInstance, config));

// Static assets
parent.use('/assets', express.static(path.resolve(__dirname, 'public/assets')));
Expand Down
1 change: 1 addition & 0 deletions packages/best-frontend/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const config = {
// projectsMapping
// GIT_APP_ID
// GIT_ORG
// GIT_REPO
}
};

Expand Down
42 changes: 24 additions & 18 deletions packages/best-frontend/server/api_v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,52 @@ const onlyStatus200 = (req, res) => res.statusCode === 0;
const cacheSuccesses = cache('2 minutes', onlyStatus200);

// -- Globals configs ---------------------

const GIT_ORG = process.env.GIT_ORG;
const GIT_PROJECTS = process.env.GIT_PROJECTS || '{}';
const GIT_REPO = process.env.GIT_REPO;
let GIT_ORG_API;
let PROJECTS = {};

const memoizeConfig = { promise: true };
let memoizedGetBenchPerCommit;
let gitRepository;

// -- Internal APIs ------------------------

async function getOrganizationInstallation(org) {
async function getOrganizationInstallation(repo) {
const org = repo.split('/')[0];
const APP = gitIntegration.createGithubApp();
const gitAppAuth = await APP.authAsApp();
const installations = await gitAppAuth.apps.getInstallations({});
const repoInstallation = installations.data.find((i) => i.account.login === org);
const installationId = repoInstallation.id;
const owner = repoInstallation.account.login;
const gitOrgApi = await APP.authAsInstallation(installationId);
const repos = await gitOrgApi.apps.getInstallationRepositories();
const activeRepo = repos.data.repositories.find((r) => r.full_name === repo);

if (!activeRepo) {
throw new Error(`Couldn't match git repository '${repo}'`);
}

return gitOrgApi;
}

function initialize(store) {
if (GIT_ORG) {
PROJECTS = JSON.parse(GIT_PROJECTS[0] === '\'' ? GIT_PROJECTS.slice(1, -1) : GIT_PROJECTS);
getOrganizationInstallation(GIT_ORG).then((gitAPI) => {
function initialize(store, { githubConfig = {} }) {
gitRepository = githubConfig.repo || GIT_REPO;
if (gitRepository) {
getOrganizationInstallation(gitRepository).then((gitAPI) => {
GIT_ORG_API = gitAPI;
}, () => {
}, (err) => {
console.log(err);
console.log(`Unable to initialize Github API`);
});
}

// Memoize calls when routers gets invoked
memoizedGetBenchPerCommit = memoize(async (project, commit) =>
Object.freeze(store.getAllBenchmarkStatsPerCommit(project, commit), memoizeConfig)
);
}

function addRoutes(router, store) {
initialize(store);
function addRoutes(router, store, config) {
initialize(store, config);
router.get('/cache/index', (req, res) => res.json(apicache.getIndex()));
router.get('/cache/clear', (req, res) => res.json(apicache.clear()));

Expand Down Expand Up @@ -118,10 +125,10 @@ async function getLatestsCommits(gitRepo, size, retried = false) {
console.log('[GIT] getCommits() >> FETCH');
const { data } = await GIT_ORG_API.repos.getCommits({ owner, repo, per_page: size });
return data;
} catch(err) {
} catch (err) {
console.log('[GIT] getCommits() >> RETRY');
if (err.code === 401 && !retried) {
GIT_ORG_API = await getOrganizationInstallation(GIT_ORG);
GIT_ORG_API = await getOrganizationInstallation(gitRepository);
return getLatestsCommits(gitRepo, size, true);
}
console.log('[GIT] getCommits() >> ERROR');
Expand All @@ -130,11 +137,10 @@ async function getLatestsCommits(gitRepo, size, retried = false) {
}

async function getLastCommitStats(store, projectName, branch, size = 30) {
const gitRepo = PROJECTS[projectName];
let gitLastCommits = [];

if (GIT_ORG_API && gitRepo) {
const gitCommits = await getLatestsCommits(gitRepo, size);
if (GIT_ORG_API) {
const gitCommits = await getLatestsCommits(GIT_REPO, size);
gitLastCommits = gitCommits.map(c => c.sha.slice(0, 7));
}

Expand Down

0 comments on commit 9a502c6

Please sign in to comment.