Skip to content
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
26 changes: 19 additions & 7 deletions src/github/pullRequestModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2315,7 +2315,7 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
try {
const { mutate, schema } = await this.githubRepository.ensure();

const { data } = await mutate<MarkPullRequestReadyForReviewResponse>({
const { data, errors } = await mutate<MarkPullRequestReadyForReviewResponse>({
mutation: schema.ReadyForReview,
variables: {
input: {
Expand All @@ -2324,16 +2324,22 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
},
});

const pullRequest = data?.markPullRequestReadyForReview?.pullRequest;
if (!pullRequest) {
throw new Error(errors?.map(error => error.message).join(', ') || vscode.l10n.t('GitHub did not return the updated pull request. Please try again.'));
}

/* __GDPR__
"pr.readyForReview.success" : {}
*/
this._telemetry.sendTelemetryEvent('pr.readyForReview.success');

const result: ReadyForReview = {
isDraft: data!.markPullRequestReadyForReview.pullRequest.isDraft,
mergeable: parseMergeability(data!.markPullRequestReadyForReview.pullRequest.mergeable, data!.markPullRequestReadyForReview.pullRequest.mergeStateStatus),
allowAutoMerge: data!.markPullRequestReadyForReview.pullRequest.viewerCanEnableAutoMerge || data!.markPullRequestReadyForReview.pullRequest.viewerCanDisableAutoMerge
isDraft: pullRequest.isDraft,
mergeable: parseMergeability(pullRequest.mergeable, pullRequest.mergeStateStatus),
allowAutoMerge: pullRequest.viewerCanEnableAutoMerge || pullRequest.viewerCanDisableAutoMerge
};
this.isDraft = result.isDraft;
this.item.isDraft = result.isDraft;
this.item.mergeable = result.mergeable;
this.item.allowAutoMerge = result.allowAutoMerge;
Expand All @@ -2355,7 +2361,7 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
try {
const { mutate, schema } = await this.githubRepository.ensure();

const { data } = await mutate<ConvertPullRequestToDraftResponse>({
const { data, errors } = await mutate<ConvertPullRequestToDraftResponse>({
mutation: schema.ConvertToDraft,
variables: {
input: {
Expand All @@ -2364,15 +2370,21 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
},
});

const pullRequest = data?.convertPullRequestToDraft?.pullRequest;
if (!pullRequest) {
throw new Error(errors?.map(error => error.message).join(', ') || vscode.l10n.t('GitHub did not return the updated pull request. Please try again.'));
}

/* __GDPR__
"pr.convertToDraft.success" : {}
*/
this._telemetry.sendTelemetryEvent('pr.convertToDraft.success');

const result: ConvertToDraft = {
isDraft: data!.convertPullRequestToDraft.pullRequest.isDraft,
mergeable: parseMergeability(data!.convertPullRequestToDraft.pullRequest.mergeable, data!.convertPullRequestToDraft.pullRequest.mergeStateStatus),
isDraft: pullRequest.isDraft,
mergeable: parseMergeability(pullRequest.mergeable, pullRequest.mergeStateStatus),
};
this.isDraft = result.isDraft;
this.item.isDraft = result.isDraft;
this.item.mergeable = result.mergeable;
this._onDidChange.fire({ draft: true });
Expand Down
95 changes: 94 additions & 1 deletion src/test/github/pullRequestModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GitChangeType, SlimFileChange } from '../../common/file';
import { CredentialStore } from '../../github/credentials';
import { FolderRepositoryManager } from '../../github/folderRepositoryManager';
import { PullRequestModel } from '../../github/pullRequestModel';
import { GithubItemStateEnum, isStackMergeable, isStackUpdatable, MergeQueueState, PullRequestMergeability, PullRequestStack } from '../../github/interface';
import { GithubItemStateEnum, isStackMergeable, isStackUpdatable, MergeQueueState, PullRequestStack, PullRequestMergeability } from '../../github/interface';
import { Protocol } from '../../common/protocol';
import { GitHubRemote, Remote } from '../../common/remote';
import { convertRESTPullRequestToRawPullRequest } from '../../github/utils';
Expand All @@ -28,6 +28,7 @@ import { mergeQuerySchemaWithShared } from '../../github/common';
import { GitHubRepository } from '../../github/githubRepository';
import { LoggingApolloClient, LoggingOctokit } from '../../github/loggingOctokit';
import Logger from '../../common/logger';
import { GraphQLError } from 'graphql';
const queries = mergeQuerySchemaWithShared(require('../../github/queries.gql'), require('../../github/queriesShared.gql')) as any;

const telemetry = new MockTelemetry();
Expand Down Expand Up @@ -963,6 +964,98 @@ describe('PullRequestModel', function () {
});
});

for (const [method, mutationName, responseField, eventName, isDraft] of [
['convertToDraft', 'ConvertToDraft', 'convertPullRequestToDraft', 'pr.convertToDraft', true],
['setReadyForReview', 'ReadyForReview', 'markPullRequestReadyForReview', 'pr.readyForReview', false],
] as const) {
describe(method, function () {
let model: PullRequestModel;

beforeEach(function () {
const pr = new PullRequestBuilder().draft!(!isDraft).build();
model = new PullRequestModel(credentials, telemetry, repo, remote, convertRESTPullRequestToRawPullRequest(pr, repo));
});

afterEach(function () {
model.dispose();
});

it('updates state and notifies listeners after a successful mutation', async function () {
const mutate = sinon.stub(repo, 'mutate').resolves({
data: {
[responseField]: {
pullRequest: {
isDraft,
mergeable: 'CONFLICTING',
mergeStateStatus: 'DIRTY',
viewerCanEnableAutoMerge: true,
viewerCanDisableAutoMerge: false,
},
},
},
});
const onDidChange = sinon.spy();
context.subscriptions.push(model.onDidChange(onDidChange));
const success = sinon.spy(telemetry, 'sendTelemetryEvent');
const failure = sinon.spy(telemetry, 'sendTelemetryErrorEvent');

const result = await model[method]();

assert.deepStrictEqual(mutate.firstCall.args[0], {
mutation: repo.schema[mutationName],
variables: { input: { pullRequestId: model.graphNodeId } },
});
assert.strictEqual(result.isDraft, isDraft);
assert.strictEqual(model.isDraft, isDraft);
assert.strictEqual(result.mergeable, PullRequestMergeability.Conflict);
assert.strictEqual(model.item.mergeable, result.mergeable);
if (method === 'setReadyForReview') {
assert.strictEqual(model.allowAutoMerge, true);
}
assert(onDidChange.calledOnceWithExactly({ draft: true }));
assert(success.calledOnceWithExactly(`${eventName}.success`));
assert(failure.notCalled);
});

for (const [name, response, message] of [
['missing data', {}, 'GitHub did not return the updated pull request. Please try again.'],
['null data', { data: null }, 'GitHub did not return the updated pull request. Please try again.'],
['missing payload', { data: {} }, 'GitHub did not return the updated pull request. Please try again.'],
['null payload', { data: { [responseField]: null } }, 'GitHub did not return the updated pull request. Please try again.'],
['null pull request', { data: { [responseField]: { pullRequest: null } } }, 'GitHub did not return the updated pull request. Please try again.'],
['GraphQL error', {
data: { [responseField]: null },
errors: [new GraphQLError('Resource not accessible by integration')],
}, 'Resource not accessible by integration'],
] as const) {
it(`rejects ${name} without changing state or reporting success`, async function () {
sinon.stub(repo, 'mutate').resolves(response);
const mergeable = model.item.mergeable;
const onDidChange = sinon.spy();
context.subscriptions.push(model.onDidChange(onDidChange));
const success = sinon.spy(telemetry, 'sendTelemetryEvent');
const failure = sinon.spy(telemetry, 'sendTelemetryErrorEvent');

await assert.rejects(model[method](), { message });

assert.strictEqual(model.isDraft, !isDraft);
assert.strictEqual(model.item.mergeable, mergeable);
assert(onDidChange.notCalled);
assert(success.notCalled);
assert(failure.calledOnceWithExactly(`${eventName}.failure`));
});
}

it('preserves a rejected mutation error', async function () {
const error = new Error('Network request failed');
sinon.stub(repo, 'mutate').rejects(error);

await assert.rejects(model[method](), candidate => candidate === error);
assert.strictEqual(model.isDraft, !isDraft);
});
});
}

describe('openReadonlyChanges', function () {
const baseCommit = '1111111111111111111111111111111111111111';
const mergeBase = '2222222222222222222222222222222222222222';
Expand Down
Loading