Skip to content

Commit

Permalink
feat(apps/user-access): use apps API to query user repository access
Browse files Browse the repository at this point in the history
Closes #80
  • Loading branch information
nikku committed Nov 16, 2019
1 parent 78cd85a commit 579743a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
1 change: 0 additions & 1 deletion packages/app/lib/apps/auth-routes/AuthRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ function AuthRoutes(logger, router, securityContext) {
const params = new URLSearchParams();
params.append('client_id', process.env.GITHUB_CLIENT_ID);
params.append('state', state);
params.append('scope', 'public_repo,repo');
params.append('redirect_uri', appUrl('/wuffle/login/callback'));

return res.redirect(`https://github.com/login/oauth/authorize?${params.toString()}`);
Expand Down
64 changes: 46 additions & 18 deletions packages/app/lib/apps/user-access/UserAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const {
Cache
} = require('../../util');

// 10 minutes
// 30 minutes
const TTL = 1000 * 60 * 10;


Expand Down Expand Up @@ -35,6 +35,36 @@ function UserAccess(logger, githubClient, events) {
return repository;
}

async function fetchUserRepositories(token) {

const github = await githubClient.getUserScoped(token);

const installations = await github.paginate(
github.apps.listInstallationsForAuthenticatedUser.endpoint.merge({}),
res => res.data
);

const repositoriesByInstallation = await Promise.all(installations.map(
installation => github.paginate(
github.apps.listInstallationReposForAuthenticatedUser.endpoint.merge({
installation_id: installation.id
}),
res => res.data
)
));

return [].concat(...repositoriesByInstallation);
}

function getUserVisibleRepositoryNames(token) {

return cache.get(`user-repositories:${token}`, () => {
return fetchUserRepositories(token);
}).then(repositories => repositories.map(repo => {
return repo.full_name;
}));
}

/**
* Show publicly accessible issues only.
*/
Expand Down Expand Up @@ -69,26 +99,24 @@ function UserAccess(logger, githubClient, events) {

function createReadFilter(token) {

log.info({ token }, 'creating read filter');
const t = Date.now();

return githubClient.getUserScoped(token)
.then(github => {
return github.paginate(
github.repos.list.endpoint.merge({
visibility: 'private'
}),
res => res.data
);
}).then(repositories => {
const repositoryNames = repositories.map(fullName);
log.debug({ token }, 'creating read filter');

log.debug({
token,
repositories: repositoryNames
}, 'creating member filter');
return getUserVisibleRepositoryNames(token).then(repositoryNames => {

log.debug({
token,
repositories: repositoryNames
}, 'creating member filter');

console.log(repositoryNames);

return createMemberFilter(repositoryNames);
}).finally(() => {
log.info({ token, t: Date.now() - t }, 'created read filter');
});

return createMemberFilter(repositoryNames);
});
}

function getReadFilter(token) {
Expand Down

0 comments on commit 579743a

Please sign in to comment.