Skip to content

Commit

Permalink
Handle ref and sha errors in pull_request_handler
Browse files Browse the repository at this point in the history
[changelog:changed]
  • Loading branch information
cdupuis committed Jun 7, 2021
1 parent f4d3432 commit 5a229db
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 21 deletions.
3 changes: 3 additions & 0 deletions lib/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export { Check, CreateCheck, createCheck, UpdateCheck } from "./check";
export { uploadCodeScanningResults } from "./code_scanning";
export {
api,
BlobMode,
ContentEditor,
convergeLabel,
editContent,
EditContentError,
EditContentErrorCode,
formatCommitMarkers,
formatFooter,
formatMarkers,
Expand Down
28 changes: 21 additions & 7 deletions lib/github/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export async function editContent<D>(
!parameters.base
) {
throw new EditContentError(
"INVALID_PARAMETERS",
EditContentErrorCode.InvalidParameters,
"Required parameter missing",
);
}
Expand All @@ -242,13 +242,13 @@ export async function editContent<D>(
).data;
} catch (e) {
throw new EditContentError(
"INVALID_REF",
EditContentErrorCode.InvalidRef,
`Failed to read ref '${parameters.base}'`,
);
}
if (parameters.sha !== ref.object.sha) {
throw new EditContentError(
"INVALID_SHA",
EditContentErrorCode.InvalidSha,
`Ref '${parameters.base}' points to different commit '${ref.object.sha}'`,
);
}
Expand Down Expand Up @@ -283,7 +283,7 @@ export async function editContent<D>(
validatePath(path);
if (content === undefined) {
throw new EditContentError(
"INVALID_CONTENT",
EditContentErrorCode.InvalidContent,
"Content required",
);
}
Expand Down Expand Up @@ -391,18 +391,32 @@ export async function editContent<D>(
};
}

export enum EditContentErrorCode {
InvalidPath = "INVALID_PATH",
InvalidContent = "INVALID_CONTENT",
InvalidRef = "INVALID_REF",
InvalidSha = "INVALID_SHA",
InvalidParameters = "INVALID_PARAMETERS",
}

export class EditContentError extends Error {
constructor(public readonly code: string, public readonly message: string) {
constructor(
public readonly code: EditContentErrorCode,
public readonly message: string,
) {
super(message);
}
}

export function validatePath(name: string): void {
if (!name) {
throw new EditContentError("INVALID_PATH", `Path required`);
throw new EditContentError(
EditContentErrorCode.InvalidPath,
`Path required`,
);
} else if (!/^(?![/])[a-zA-Z0-9+_#/.-]+$/.test(name)) {
throw new EditContentError(
"INVALID_PATH",
EditContentErrorCode.InvalidPath,
`Invalid path '${name}' provided`,
);
}
Expand Down
5 changes: 4 additions & 1 deletion lib/policy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ export {
toConclusion,
toSeverity,
} from "./policy";
export { pullRequestHandler } from "./pull_request_handler";
export {
pullRequestHandler,
PullRequestHandlerResponse,
} from "./pull_request_handler";
40 changes: 27 additions & 13 deletions lib/policy/pull_request_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
api,
ContentEditor,
editContent,
EditContentError,
EditContentErrorCode,
formatFooter,
formatMarkers,
} from "../github/operation";
Expand All @@ -27,9 +29,9 @@ import { chain, createRef, CreateRepositoryId } from "../handler/util";
import { AuthenticatedRepositoryId } from "../repository/id";
import * as status from "../status";
import { hash } from "../util";
import map = require("lodash.map");

import uniq = require("lodash.uniq");
import map = require("lodash.map");

export interface PullRequestHandlerResponse<S, C, D = string> {
commit: {
Expand Down Expand Up @@ -92,17 +94,29 @@ export function pullRequestHandler<S, C, D = string>(parameters: {

const gh = api(ctx.chain.id);

const editResult = await editContent<D>(
{
credential: ctx.chain.id.credential,
owner: ctx.chain.id.owner,
repo: ctx.chain.id.repo,
sha: ctx.chain.id.sha,
base: ctx.chain.id.branch,
force: true,
},
...result.commit.editors,
);
let editResult;
try {
editResult = await editContent<D>(
{
credential: ctx.chain.id.credential,
owner: ctx.chain.id.owner,
repo: ctx.chain.id.repo,
sha: ctx.chain.id.sha,
base: ctx.chain.id.branch,
force: true,
},
...result.commit.editors,
);
} catch (e) {
if (e instanceof EditContentError) {
if (e.code === EditContentErrorCode.InvalidSha) {
return status.success(`Branch moved on`).hidden();
} else if (e.code === EditContentErrorCode.InvalidRef) {
return status.success(`Ref not found`).hidden();
}
}
throw e;
}

if (editResult.sha !== ctx.chain.id.sha) {
const pullRequest = await result.pullRequest(
Expand Down Expand Up @@ -199,7 +213,7 @@ ${formatMarkers(ctx, `atomist-diff:${diffHash}`)}
}](${pr.html_url})`,
);
} else {
return status.success(`No changes to push`);
return status.success(`No changes to push`).hidden();
}
},
) as EventHandler<S, C>;
Expand Down

0 comments on commit 5a229db

Please sign in to comment.