Hacky re-wire to /repositories to test bridge theory

This commit is contained in:
Travis Ralston 2021-12-06 15:43:44 -07:00
parent 9ab6c8f3f2
commit 256ac0ac9e
3 changed files with 54 additions and 17 deletions

View File

@ -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

View File

@ -53,9 +53,28 @@ export class HookshotGithubBridge extends HookshotBridge {
const res = await this.doProvisionRequest<HookshotGithubRepo[]>(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<HookshotGithubRepo[]> {
const bridge = await this.getDefaultBridge();
const results: HookshotGithubRepo[] = [];
let more = true;
let page = 1;
let perPage = 10;
do {
const res = await this.doProvisionRequest<HookshotGithubRepo[]>(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;
}

View File

@ -43,19 +43,22 @@ export class HookshotGithubBridgeConfigComponent extends BridgeComponent<Hooksho
this.tryLoadOrgs();
}
private tryOrgAuth() {
this.hookshot.getAuthUrls().then(urls => {
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<Hooksho
this.tryLoadOrgs();
}, 1000);
});
} else if (e.status === 400 && e.error.dim_errcode === "T2B_MISSING_AUTH") {
this.tryOrgAuth();
} else {
console.error(e);
this.translate.get('Error getting Github information').subscribe((res: string) => {