Use graphql in GitHubClient and misc fixes (#8)

* Use graphql in GitHubClient

* Replace usage of Array.find with _.find
This commit is contained in:
Tanuj Mittal
2020-06-05 12:22:41 -07:00
committed by GitHub
parent e9d3160b57
commit aa8236666e
24 changed files with 761 additions and 657 deletions

View File

@@ -1,19 +1,20 @@
import _ from "underscore";
import { Areas, HttpStatusCodes } from "../../Common/Constants";
import { Logger } from "../../Common/Logger";
import * as ViewModels from "../../Contracts/ViewModels";
import { GitHubClient, IGitHubRepo } from "../../GitHub/GitHubClient";
import { GitHubClient, IGitHubPageInfo, IGitHubRepo } from "../../GitHub/GitHubClient";
import { IPinnedRepo, JunoClient } from "../../Juno/JunoClient";
import { Action, ActionModifiers } from "../../Shared/Telemetry/TelemetryConstants";
import TelemetryProcessor from "../../Shared/Telemetry/TelemetryProcessor";
import { GitHubUtils } from "../../Utils/GitHubUtils";
import { JunoUtils } from "../../Utils/JunoUtils";
import { NotificationConsoleUtils } from "../../Utils/NotificationConsoleUtils";
import { AuthorizeAccessComponent } from "../Controls/GitHub/AuthorizeAccessComponent";
import { GitHubReposComponentProps, RepoListItem, GitHubReposComponent } from "../Controls/GitHub/GitHubReposComponent";
import { GitHubReposComponent, GitHubReposComponentProps, RepoListItem } from "../Controls/GitHub/GitHubReposComponent";
import { GitHubReposComponentAdapter } from "../Controls/GitHub/GitHubReposComponentAdapter";
import { BranchesProps, PinnedReposProps, UnpinnedReposProps } from "../Controls/GitHub/ReposListComponent";
import { ConsoleDataType } from "../Menus/NotificationConsole/NotificationConsoleComponent";
import { ContextualPaneBase } from "./ContextualPaneBase";
import { JunoUtils } from "../../Utils/JunoUtils";
export class GitHubReposPane extends ContextualPaneBase {
private static readonly PageSize = 30;
@@ -29,6 +30,7 @@ export class GitHubReposPane extends ContextualPaneBase {
private gitHubReposAdapter: GitHubReposComponentAdapter;
private allGitHubRepos: IGitHubRepo[];
private allGitHubReposLastPageInfo?: IGitHubPageInfo;
private pinnedReposUpdated: boolean;
constructor(options: ViewModels.GitHubReposPaneOptions) {
@@ -73,6 +75,7 @@ export class GitHubReposPane extends ContextualPaneBase {
this.gitHubReposAdapter = new GitHubReposComponentAdapter(this.gitHubReposProps);
this.allGitHubRepos = [];
this.allGitHubReposLastPageInfo = undefined;
this.pinnedReposUpdated = false;
}
@@ -115,6 +118,7 @@ export class GitHubReposPane extends ContextualPaneBase {
// Reset cached repos
this.allGitHubRepos = [];
this.allGitHubReposLastPageInfo = undefined;
// Reset flags
this.pinnedReposUpdated = false;
@@ -164,29 +168,28 @@ export class GitHubReposPane extends ContextualPaneBase {
const unpinnedGitHubRepos = this.allGitHubRepos.filter(
gitHubRepo =>
this.pinnedReposProps.repos.findIndex(
pinnedRepo => pinnedRepo.key === GitHubUtils.toRepoFullName(gitHubRepo.owner.login, gitHubRepo.name)
pinnedRepo => pinnedRepo.key === GitHubUtils.toRepoFullName(gitHubRepo.owner, gitHubRepo.name)
) === -1
);
return unpinnedGitHubRepos.map(gitHubRepo => ({
key: GitHubUtils.toRepoFullName(gitHubRepo.owner.login, gitHubRepo.name),
key: GitHubUtils.toRepoFullName(gitHubRepo.owner, gitHubRepo.name),
repo: gitHubRepo,
branches: []
}));
}
private async loadMoreBranches(repo: IGitHubRepo): Promise<void> {
const branchesProps = this.branchesProps[GitHubUtils.toRepoFullName(repo.owner.login, repo.name)];
const branchesProps = this.branchesProps[GitHubUtils.toRepoFullName(repo.owner, repo.name)];
branchesProps.hasMore = true;
branchesProps.isLoading = true;
this.triggerRender();
const nextPage = Math.floor(branchesProps.branches.length / GitHubReposPane.PageSize) + 1;
try {
const response = await this.gitHubClient.getBranchesAsync(
repo.owner.login,
repo.owner,
repo.name,
nextPage,
GitHubReposPane.PageSize
GitHubReposPane.PageSize,
branchesProps.lastPageInfo?.endCursor
);
if (response.status !== HttpStatusCodes.OK) {
throw new Error(`Received HTTP ${response.status} when fetching branches`);
@@ -194,6 +197,7 @@ export class GitHubReposPane extends ContextualPaneBase {
if (response.data) {
branchesProps.branches = branchesProps.branches.concat(response.data);
branchesProps.lastPageInfo = response.pageInfo;
}
} catch (error) {
const message = `Failed to fetch branches: ${error}`;
@@ -202,7 +206,7 @@ export class GitHubReposPane extends ContextualPaneBase {
}
branchesProps.isLoading = false;
branchesProps.hasMore = branchesProps.branches.length === GitHubReposPane.PageSize * nextPage;
branchesProps.hasMore = branchesProps.lastPageInfo?.hasNextPage;
this.triggerRender();
}
@@ -211,15 +215,18 @@ export class GitHubReposPane extends ContextualPaneBase {
this.unpinnedReposProps.hasMore = true;
this.triggerRender();
const nextPage = Math.floor(this.allGitHubRepos.length / GitHubReposPane.PageSize) + 1;
try {
const response = await this.gitHubClient.getReposAsync(nextPage, GitHubReposPane.PageSize);
const response = await this.gitHubClient.getReposAsync(
GitHubReposPane.PageSize,
this.allGitHubReposLastPageInfo?.endCursor
);
if (response.status !== HttpStatusCodes.OK) {
throw new Error(`Received HTTP ${response.status} when fetching unpinned repos`);
}
if (response.data) {
this.allGitHubRepos = this.allGitHubRepos.concat(response.data);
this.allGitHubReposLastPageInfo = response.pageInfo;
this.unpinnedReposProps.repos = this.calculateUnpinnedRepos();
}
} catch (error) {
@@ -229,7 +236,7 @@ export class GitHubReposPane extends ContextualPaneBase {
}
this.unpinnedReposProps.isLoading = false;
this.unpinnedReposProps.hasMore = this.allGitHubRepos.length === GitHubReposPane.PageSize * nextPage;
this.unpinnedReposProps.hasMore = this.allGitHubReposLastPageInfo?.hasNextPage;
this.triggerRender();
}
@@ -253,7 +260,7 @@ export class GitHubReposPane extends ContextualPaneBase {
this.pinnedReposUpdated = true;
const initialReposLength = this.pinnedReposProps.repos.length;
const existingRepo = this.pinnedReposProps.repos.find(repo => repo.key === item.key);
const existingRepo = _.find(this.pinnedReposProps.repos, repo => repo.key === item.key);
if (existingRepo) {
existingRepo.branches = item.branches;
} else {
@@ -318,6 +325,7 @@ export class GitHubReposPane extends ContextualPaneBase {
if (!this.branchesProps[item.key]) {
this.branchesProps[item.key] = {
branches: [],
lastPageInfo: undefined,
hasMore: true,
isLoading: true,
loadMore: (): Promise<void> => this.loadMoreBranches(item.repo)
@@ -329,6 +337,7 @@ export class GitHubReposPane extends ContextualPaneBase {
private async refreshUnpinnedRepoListItems(): Promise<void> {
this.allGitHubRepos = [];
this.allGitHubReposLastPageInfo = undefined;
this.unpinnedReposProps.repos = [];
this.loadMoreUnpinnedRepos();
}