Skip to content

Commit

Permalink
Remove PR submission (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
danerwilliams committed Aug 17, 2023
1 parent 13284a0 commit 65c0ce6
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 219 deletions.
13 changes: 0 additions & 13 deletions apps/cli/src/actions/submit/submit_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { TContext } from '../../lib/context';
import { TScopeSpec } from '../../lib/engine/scope_spec';
import { ExitFailedError, KilledError } from '../../lib/errors';
import { CommandFailedError } from '../../lib/git/runner';
import { cliAuthPrecondition } from '../../lib/preconditions';
import { getPRInfoForBranches } from './prepare_branches';
import { submitPullRequest } from './submit_prs';
import { validateBranchesToSubmit } from './validate_branches';

// eslint-disable-next-line max-lines-per-function
Expand Down Expand Up @@ -34,7 +32,6 @@ export async function submitAction(
);
}
const populateRemoteShasPromise = context.engine.populateRemoteShas();
const cliAuthToken = cliAuthPrecondition(context);
if (args.dryRun) {
context.splog.info(
chalk.yellow(
Expand Down Expand Up @@ -129,16 +126,6 @@ export async function submitAction(
}
throw err;
}

await submitPullRequest(
{
submissionInfo: [submissionInfo],
mergeWhenReady: args.mergeWhenReady,
trunkBranchName: context.engine.trunk,
cliAuthToken,
},
context
);
}

if (!context.interactive) {
Expand Down
137 changes: 0 additions & 137 deletions apps/cli/src/actions/submit/submit_prs.ts
Original file line number Diff line number Diff line change
@@ -1,143 +1,6 @@
import { API_ROUTES } from '@withgraphite/graphite-cli-routes';
import * as t from '@withgraphite/retype';
import chalk from 'chalk';
import { requestWithArgs } from '../../lib/api/request';
import { TContext } from '../../lib/context';
import { ExitFailedError, PreconditionsFailedError } from '../../lib/errors';
import { cuteString } from '../../lib/utils/cute_string';
import { Unpacked } from '../../lib/utils/ts_helpers';

export type TPRSubmissionInfo = t.UnwrapSchemaMap<
typeof API_ROUTES.submitPullRequests.params
>['prs'];

type TSubmittedPRRequest = Unpacked<TPRSubmissionInfo>;

type TSubmittedPRResponse = Unpacked<
t.UnwrapSchemaMap<typeof API_ROUTES.submitPullRequests.response>['prs']
>;

type TSubmittedPR = {
request: TSubmittedPRRequest;
response: TSubmittedPRResponse;
};

export async function submitPullRequest(
args: {
submissionInfo: TPRSubmissionInfo;
mergeWhenReady: boolean;
trunkBranchName: string;
cliAuthToken: string;
},
context: TContext
): Promise<void> {
const pr = (
await requestServerToSubmitPRs({
submissionInfo: args.submissionInfo,
mergeWhenReady: args.mergeWhenReady,
trunkBranchName: args.trunkBranchName,
context,
})
)[0];

if (pr.response.status === 'error') {
throw new ExitFailedError(
`Failed to submit PR for ${pr.response.head}: ${parseSubmitError(
pr.response.error
)}`
);
}

context.engine.upsertPrInfo(pr.response.head, {
number: pr.response.prNumber,
url: pr.response.prURL,
base: pr.request.base,
state: 'OPEN', // We know this is not closed or merged because submit succeeded
...(pr.request.action === 'create'
? {
title: pr.request.title,
body: pr.request.body,
reviewDecision: 'REVIEW_REQUIRED', // Because we just opened this PR
}
: {}),
...(pr.request.draft !== undefined ? { draft: pr.request.draft } : {}),
});
context.splog.info(
`${chalk.green(pr.response.head)}: ${pr.response.prURL} (${{
updated: chalk.yellow,
created: chalk.green,
}[pr.response.status](pr.response.status)})`
);
}

function parseSubmitError(error: string): string {
try {
return JSON.parse(error)?.response?.data?.message ?? error;
} catch {
return error;
}
}

const SUCCESS_RESPONSE_CODE = 200;
const UNAUTHORIZED_RESPONSE_CODE = 401;

// This endpoint is plural for legacy reasons.
// Leaving the function plural in case we want to revert.
async function requestServerToSubmitPRs({
submissionInfo,
mergeWhenReady,
trunkBranchName,
context,
}: {
submissionInfo: TPRSubmissionInfo;
mergeWhenReady: boolean;
trunkBranchName: string;
context: TContext;
}): Promise<TSubmittedPR[]> {
const response = await requestWithArgs(
context.userConfig,
API_ROUTES.submitPullRequests,
{
repoOwner: context.repoConfig.getRepoOwner(),
repoName: context.repoConfig.getRepoName(),
mergeWhenReady,
trunkBranchName,
prs: submissionInfo,
}
);

if (
response._response.status === SUCCESS_RESPONSE_CODE &&
response._response.body
) {
const requests: { [head: string]: TSubmittedPRRequest } = {};
submissionInfo.forEach((prRequest) => {
requests[prRequest.head] = prRequest;
});

return response.prs.map((prResponse) => {
return {
request: requests[prResponse.head],
response: prResponse,
};
});
} else if (response._response.status === UNAUTHORIZED_RESPONSE_CODE) {
throw new PreconditionsFailedError(
`Your Graphite auth token is invalid/expired.\n\nPlease obtain a new auth token by visiting ${context.userConfig.getAppServerUrl()}/activate`
);
} else {
const { headers } = response._response;
const debugHeaders = {
'x-graphite-request-id': headers.get('x-graphite-request-id'),
};
const formattedHeaders = Object.entries(debugHeaders)
.map(([key, value]) => ` ${key}: ${value || '<empty>'}`)
.join('\n');

throw new ExitFailedError(
`Unexpected server response (${
response._response.status
}).\n\nHeaders:\n${formattedHeaders}\n\nResponse: ${cuteString(response)}`
);
}
}
57 changes: 0 additions & 57 deletions apps/cli/src/background_tasks/post_survey.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/cli/src/lib/preconditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,6 @@ export function ensureSomeStagedChangesPrecondition(context: TContext): void {
throw new PreconditionsFailedError(`Cannot run without staged changes.`);
}

export function cliAuthPrecondition(context: TContext): string {
const token = context.userConfig.getAuthToken();
if (!token || token.length === 0) {
throw new PreconditionsFailedError(
`Please authenticate your Graphite CLI by visiting ${context.userConfig.getAppServerUrl()}/activate`
);
}
return token;
}

export function currentGitRepoPrecondition(): string {
const repoRootPath = runGitCommand({
args: [`rev-parse`, `--show-toplevel`],
Expand Down
2 changes: 0 additions & 2 deletions apps/cli/src/lib/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import chalk from 'chalk';
import { version } from '../../package.json';
import { init } from '../actions/init';
import { refreshPRInfoInBackground } from '../background_tasks/fetch_pr_info';
import { postSurveyResponsesInBackground } from '../background_tasks/post_survey';
import { postTelemetryInBackground } from '../background_tasks/post_traces';
import { fetchUpgradePromptInBackground } from '../background_tasks/upgrade_prompt';
import {
Expand Down Expand Up @@ -93,7 +92,6 @@ async function graphiteInternal(
},
async () => {
fetchUpgradePromptInBackground(contextLite);
postSurveyResponsesInBackground(contextLite);
if (!handlerMaybeWithCacheLock.repo) {
await handlerMaybeWithCacheLock.run(contextLite);
return;
Expand Down

0 comments on commit 65c0ce6

Please sign in to comment.