Skip to content

Commit

Permalink
fix: chunk annotations (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpoehnelt committed Oct 14, 2020
1 parent 79c28da commit 0c34be0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/solidarity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
* limitations under the License.
*/

import * as nock from "nock";

import { Conclusion, OutputTitle, Solidarity, logger } from "./solidarity";

import { DEFAULT_CONFIGURATION } from "./config";
import fs from "fs";
import { parse } from "./parse";
import { version } from "../package.json";

nock.disableNetConnect();

const payload = JSON.parse(fs.readFileSync("./fixtures/payload.json", "utf8"));
const failingDIff = parse(
fs.readFileSync("./fixtures/pull.failing.diff", "utf8")
Expand Down Expand Up @@ -110,3 +114,38 @@ test("solidarity should generate correct summary", async () => {
s.config = DEFAULT_CONFIGURATION;
expect(s.summary("foo", "1.0.0")).toMatchSnapshot();
});

test("update should chunk annotations", async () => {
const s = new Solidarity({ name: "foo", id: "bar", payload: payload } as any);
s.config = DEFAULT_CONFIGURATION;
const status = "completed";
const conclusion = Conclusion.FAILURE;

const update = jest.fn();
s["context"].github = {
checks: {
update: update as any,
} as any,
} as any;

const output = {
title: "title",
summary: "summary",
annotations: Array.from({ length: 101 }, () => {
return {
annotation_level: "warning" as const,
path: "path",
start_line: 0,
end_line: 1,
message: "message",
};
}),
};

await s.update(status, conclusion, output);

expect(update).toBeCalledTimes(3);
expect(update.mock.calls[0][0].output.annotations.length).toBe(50);
expect(update.mock.calls[1][0].output.annotations.length).toBe(50);
expect(update.mock.calls[2][0].output.annotations.length).toBe(1);
});
34 changes: 28 additions & 6 deletions src/solidarity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,33 @@ export class Solidarity {
async update(
status: "queued" | "in_progress" | "completed",
conclusion?: Conclusion,
output?: ChecksUpdateParamsOutput,
details_url?: string
output?: ChecksUpdateParamsOutput
): Promise<void> {
try {
await this.context.github.checks.update({
const request = {
...this.checkOptions,
check_run_id: this.checkId as number,
status,
conclusion,
...(status === "completed" && {
completed_at: new Date().toISOString(),
...(output && { output }),
}),
...(output && { output }),
details_url,
});
};

// There is a limit of 50 annotations per request
if (output && output.annotations && output.annotations.length > 50) {
await Promise.all(
chunk(output.annotations, 50).map((annotations) => {
return this.context.github.checks.update({
...request,
output: { ...output, annotations },
});
})
);
} else {
await this.context.github.checks.update(request);
}
} catch (e) {
this.logger.error({ err: e }, "Failed to update check");
}
Expand Down Expand Up @@ -294,3 +306,13 @@ export class Solidarity {
});
}
}

const chunk = <T>(arr: Array<T>, chunkSize: number): Array<Array<T>> => {
return arr.reduce(
(previous: any, _: any, index: number, array: Array<T>) =>
!(index % chunkSize)
? previous.concat([array.slice(index, index + chunkSize)])
: previous,
[]
);
};

0 comments on commit 0c34be0

Please sign in to comment.