Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix status checks rendering #4542

Merged
merged 4 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 119 additions & 32 deletions src/github/githubRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ import {
PullRequestsResponse,
ViewerPermissionResponse,
} from './graphql';
import { CheckState, IAccount, IMilestone, Issue, PullRequest, PullRequestChecks, RepoAccessAndMergeMethods } from './interface';
import {
CheckState,
IAccount,
IMilestone,
Issue,
PullRequest,
PullRequestChecks,
PullRequestReviewRequirement,
RepoAccessAndMergeMethods,
} from './interface';
import { IssueModel } from './issueModel';
import { LoggingOctokit } from './loggingOctokit';
import { PullRequestModel } from './pullRequestModel';
Expand Down Expand Up @@ -1060,10 +1069,10 @@ export class GitHubRepository implements vscode.Disposable {
* This method should go in PullRequestModel, but because of the status checks bug we want to track `_useFallbackChecks` at a repo level.
*/
private _useFallbackChecks: boolean = false;
async getStatusChecks(number: number): Promise<PullRequestChecks | undefined> {
async getStatusChecks(number: number): Promise<[PullRequestChecks | null, PullRequestReviewRequirement | null]> {
const { query, remote, schema } = await this.ensure();
const captureUseFallbackChecks = this._useFallbackChecks;
let result;
let result: ApolloQueryResult<GetChecksResponse>;
try {
result = await query<GetChecksResponse>({
query: captureUseFallbackChecks ? schema.GetChecksWithoutSuite : schema.GetChecks,
Expand All @@ -1088,38 +1097,116 @@ export class GitHubRepository implements vscode.Disposable {
// We always fetch the status checks for only the last commit, so there should only be one node present
const statusCheckRollup = result.data.repository.pullRequest.commits.nodes[0].commit.statusCheckRollup;

if (!statusCheckRollup) {
return undefined;
}
const checks: PullRequestChecks = !statusCheckRollup
? {
state: CheckState.Success,
statuses: []
}
: {
state: this.mapStateAsCheckState(statusCheckRollup.state),
statuses: statusCheckRollup.contexts.nodes.map(context => {
if (isCheckRun(context)) {
return {
id: context.id,
url: context.checkSuite?.app?.url,
avatarUrl:
context.checkSuite?.app?.logoUrl &&
getAvatarWithEnterpriseFallback(
context.checkSuite.app.logoUrl,
undefined,
this.remote.authProviderId,
),
state: this.mapStateAsCheckState(context.conclusion),
description: context.title,
context: context.name,
targetUrl: context.detailsUrl,
isRequired: context.isRequired,
};
} else {
return {
id: context.id,
url: context.targetUrl ?? undefined,
avatarUrl: context.avatarUrl
? getAvatarWithEnterpriseFallback(context.avatarUrl, undefined, this.remote.authProviderId)
: undefined,
state: this.mapStateAsCheckState(context.state),
description: context.description,
context: context.context,
targetUrl: context.targetUrl,
isRequired: context.isRequired,
};
}
}),
};

const checks: PullRequestChecks = {
state: statusCheckRollup.state.toLowerCase(),
statuses: statusCheckRollup.contexts.nodes.map(context => {
if (isCheckRun(context)) {
return {
id: context.id,
url: context.checkSuite?.app?.url,
avatar_url: context.checkSuite?.app?.logoUrl,
state: context.conclusion?.toLowerCase() || CheckState.Pending,
description: context.title,
context: context.name,
target_url: context.detailsUrl,
};
} else {
return {
id: context.id,
url: context.targetUrl,
avatar_url: context.avatarUrl,
state: context.state?.toLowerCase(),
description: context.description,
context: context.context,
target_url: context.targetUrl,
};
let reviewRequirement: PullRequestReviewRequirement | null = null;
const rule = result.data.repository.pullRequest.baseRef.refUpdateRule;
if (rule) {
const prUrl = result.data.repository.pullRequest.url;

for (const context of rule.requiredStatusCheckContexts || []) {
if (!checks.statuses.some(status => status.context === context)) {
checks.state = CheckState.Pending;
checks.statuses.push({
id: '',
url: undefined,
avatarUrl: undefined,
state: CheckState.Pending,
description: vscode.l10n.t('Waiting for status to be reported'),
context: context,
targetUrl: prUrl,
isRequired: true
});
}
}),
};
}

const requiredApprovingReviews = rule.requiredApprovingReviewCount ?? 0;
const approvingReviews = result.data.repository.pullRequest.latestReviews.nodes.filter(
review => review.authorCanPushToRepository && review.state === 'APPROVED',
);
const requestedChanges = result.data.repository.pullRequest.reviewsRequestingChanges.nodes.filter(
review => review.authorCanPushToRepository
);
let state: CheckState = CheckState.Success;
if (approvingReviews.length < requiredApprovingReviews) {
state = CheckState.Failure;

if (requestedChanges.length) {
state = CheckState.Pending;
}
}
if (requiredApprovingReviews > 0) {
reviewRequirement = {
count: requiredApprovingReviews,
approvals: approvingReviews.map(review => review.author.login),
requestedChanges: requestedChanges.map(review => review.author.login),
state: state
};
}
}

return [checks.statuses.length ? checks : null, reviewRequirement];
}

mapStateAsCheckState(state: string | null | undefined): CheckState {
switch (state) {
case 'EXPECTED':
case 'PENDING':
case 'ACTION_REQUIRED':
case 'STALE':
return CheckState.Pending;
case 'ERROR':
case 'FAILURE':
case 'TIMED_OUT':
case 'STARTUP_FAILURE':
return CheckState.Failure;
case 'SUCCESS':
return CheckState.Success;
case 'NEUTRAL':
case 'SKIPPED':
return CheckState.Neutral;
}

return checks;
return CheckState.Unknown;
}
}
67 changes: 42 additions & 25 deletions src/github/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,48 +639,77 @@ export interface StartReviewResponse {
}

export interface StatusContext {
__typename: string;
id: string;
state?: 'ERROR' | 'EXPECTED' | 'FAILURE' | 'PENDING' | 'SUCCESS';
description?: string;
state: 'ERROR' | 'EXPECTED' | 'FAILURE' | 'PENDING' | 'SUCCESS';
description: string | null;
context: string;
targetUrl?: string;
avatarUrl?: string;
targetUrl: string | null;
avatarUrl: string | null;
isRequired: boolean;
}

export interface CheckRun {
__typename: string;
id: string;
conclusion?:
conclusion:
| 'ACTION_REQUIRED'
| 'CANCELLED'
| 'FAILURE'
| 'NEUTRAL'
| 'SKIPPED'
| 'STALE'
| 'SUCCESS'
| 'TIMED_OUT';
| 'TIMED_OUT'
| null;
name: string;
title?: string;
detailsUrl?: string;
title: string | null;
detailsUrl: string | null;
checkSuite?: {
app?: {
app: {
logoUrl: string;
url: string;
};
} | null;
};
isRequired: boolean;
}

export function isCheckRun(x: CheckRun | StatusContext): x is CheckRun {
return (x as any).__typename === 'CheckRun';
return x.__typename === 'CheckRun';
}

export interface ChecksReviewNode {
authorAssociation: 'MEMBER' | 'OWNER' | 'MANNEQUIN' | 'COLLABORATOR' | 'CONTRIBUTOR' | 'FIRST_TIME_CONTRIBUTOR' | 'FIRST_TIMER' | 'NONE';
authorCanPushToRepository: boolean
state: 'PENDING' | 'COMMENTED' | 'APPROVED' | 'CHANGES_REQUESTED' | 'DISMISSED';
author: {
login: string;
}
}

export interface GetChecksResponse {
repository: {
pullRequest: {
url: string;
latestReviews: {
nodes: ChecksReviewNode[];
};
reviewsRequestingChanges: {
nodes: ChecksReviewNode[];
};
baseRef: {
refUpdateRule: {
requiredApprovingReviewCount: number | null;
requiredStatusCheckContexts: string[] | null;
requiresCodeOwnerReviews: boolean;
viewerCanPush: boolean;
} | null;
};
commits: {
nodes: {
commit: {
statusCheckRollup?: {
state: string;
state: 'EXPECTED' | 'ERROR' | 'FAILURE' | 'PENDING' | 'SUCCESS';
contexts: {
nodes: (StatusContext | CheckRun)[];
};
Expand All @@ -692,18 +721,6 @@ export interface GetChecksResponse {
};
}

export interface LatestReviewsResponse {
repository: {
pullRequest: {
latestReviews: {
nodes: {
state: 'COMMENTED' | 'APPROVED' | 'CHANGES_REQUESTED' | 'PENDING';
}[]
}
}
}
}

export interface ResolveReviewThreadResponse {
resolveReviewThread: {
thread: ReviewThread;
Expand Down Expand Up @@ -731,4 +748,4 @@ export interface PullRequestFilesResponse {
}
}
}
}
}
29 changes: 20 additions & 9 deletions src/github/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum PullRequestMergeability {
NotMergeable,
Conflict,
Unknown,
Behind,
}

export interface ReviewState {
Expand Down Expand Up @@ -170,15 +171,25 @@ export enum CheckState {
Unknown = 'unknown'
}

export interface PullRequestCheckStatus {
id: string;
url: string | undefined;
avatarUrl: string | undefined;
state: CheckState;
description: string | null;
targetUrl: string | null;
context: string;
isRequired: boolean;
}

export interface PullRequestChecks {
state: CheckState;
statuses: {
id: string;
url?: string;
avatar_url?: string;
state?: CheckState;
description?: string;
target_url?: string;
context: string;
}[];
statuses: PullRequestCheckStatus[];
}

export interface PullRequestReviewRequirement {
count: number;
state: CheckState;
approvals: string[];
requestedChanges: string[];
}
Loading