diff --git a/src/api/dimension/DimensionHookshotGithubService.ts b/src/api/dimension/DimensionHookshotGithubService.ts index eddf94c..6f392cf 100644 --- a/src/api/dimension/DimensionHookshotGithubService.ts +++ b/src/api/dimension/DimensionHookshotGithubService.ts @@ -46,12 +46,19 @@ export class DimensionHookshotGithubService { public async getOrgs(): Promise<{ orgs: HookshotGithubOrg[] }> { const userId = this.context.request.user.userId; - const hookshot = new HookshotGithubBridge(userId); - const userInfo = await hookshot.getLoggedInUserInfo(); - if (!userInfo.loggedIn) { - throw new ApiError(403, "Not logged in", "T2B_NOT_LOGGED_IN"); + try { + const hookshot = new HookshotGithubBridge(userId); + const userInfo = await hookshot.getLoggedInUserInfo(); + if (!userInfo.loggedIn) { + throw new ApiError(403, "Not logged in", "T2B_NOT_LOGGED_IN"); + } + const repos = await hookshot.getInstalledRepos(); + const orgs = Array.from(new Set(repos.map(r => r.owner))).map(o => ({ name: o, avatarUrl: null })); + return { orgs: orgs }; // was from userInfo + } catch (e) { + LogService.error("DimensionHookshotGithubService", e); + throw new ApiError(400, "Error getting org information", "T2B_MISSING_AUTH"); } - return {orgs: userInfo.organisations}; } @GET @@ -60,9 +67,15 @@ export class DimensionHookshotGithubService { public async getRepos(@PathParam("orgId") orgId: string): Promise<{ repos: HookshotGithubRepo[] }> { const userId = this.context.request.user.userId; - const hookshot = new HookshotGithubBridge(userId); - const repos = await hookshot.getRepos(orgId); - return {repos}; + try { + const hookshot = new HookshotGithubBridge(userId); + // const repos = await hookshot.getRepos(orgId); + const repos = await hookshot.getInstalledRepos(); + return {repos: repos.filter(r => r.owner === orgId)}; + } catch (e) { + LogService.error("DimensionHookshotGithubService", e); + throw new ApiError(400, "Error getting repo information", "T2B_MISSING_AUTH"); + } } @POST diff --git a/src/bridges/HookshotGithubBridge.ts b/src/bridges/HookshotGithubBridge.ts index 2c159e9..2b7deb1 100644 --- a/src/bridges/HookshotGithubBridge.ts +++ b/src/bridges/HookshotGithubBridge.ts @@ -53,9 +53,28 @@ export class HookshotGithubBridge extends HookshotBridge { const res = await this.doProvisionRequest(bridge, "GET", `/v1/github/orgs/${orgId}/repositories`, { page, perPage, - }); + }).then(r => r['repositories']); results.push(...res); if (res.length < perPage) more = false; + page++; + } while(more); + return results; + } + + public async getInstalledRepos(): Promise { + const bridge = await this.getDefaultBridge(); + const results: HookshotGithubRepo[] = []; + let more = true; + let page = 1; + let perPage = 10; + do { + const res = await this.doProvisionRequest(bridge, "GET", `/v1/github/repositories`, { + page, + perPage, + }).then(r => r['repositories']); + results.push(...res); + if (res.length < perPage) more = false; + page++; } while(more); return results; } diff --git a/web/app/configs/bridge/hookshot-github/hookshot-github.bridge.component.ts b/web/app/configs/bridge/hookshot-github/hookshot-github.bridge.component.ts index 33aa7bb..0f884f9 100644 --- a/web/app/configs/bridge/hookshot-github/hookshot-github.bridge.component.ts +++ b/web/app/configs/bridge/hookshot-github/hookshot-github.bridge.component.ts @@ -43,19 +43,22 @@ export class HookshotGithubBridgeConfigComponent extends BridgeComponent { + this.orgAuthUrl = this.sanitizer.bypassSecurityTrustResourceUrl(urls.orgUrl); + this.loadingConnections = false; + this.timerId = setTimeout(() => { + this.tryLoadOrgs(); + }, 1000); + }); + } + private tryLoadOrgs() { this.hookshot.getOrgs().then(r => { this.authUrl = null; if (r.length <= 0) { - this.hookshot.getAuthUrls().then(urls => { - console.log(urls); - this.orgAuthUrl = this.sanitizer.bypassSecurityTrustResourceUrl(urls.orgUrl); - this.loadingConnections = false; - this.timerId = setTimeout(() => { - this.tryLoadOrgs(); - }, 1000); - }); + this.tryOrgAuth(); return; } @@ -76,6 +79,8 @@ export class HookshotGithubBridgeConfigComponent extends BridgeComponent {