Skip to content

Commit 58266ed

Browse files
authored
Shorten client:check and service:check output in CI (#1404)
* Add compact renderer behind flag * listr types * type fixes * type fixes * update CHANGELOG * Replace flag with isCi check * Make client:check compact in CI * Update CHANGELOG * Remove extra changes * Fix CI resetting
1 parent 8456f71 commit 58266ed

9 files changed

Lines changed: 187 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Upcoming
44

55
- `apollo`
6-
- <First `apollo` related entry goes here>
6+
- Shorten `client:check` and `service:check` output in CI [#1404](https://github.com/apollographql/apollo-tooling/pull/1404)
77
- `apollo-codegen-core`
88
- <First `apollo-codegen-core` related entry goes here>
99
- `apollo-codegen-flow`

package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"apollo-env": "file:packages/apollo-env",
4949
"apollo-graphql": "file:packages/apollo-graphql",
5050
"apollo-language-server": "file:packages/apollo-language-server",
51+
"env-ci": "^4.1.1",
5152
"vscode-apollo": "file:packages/vscode-apollo"
5253
},
5354
"devDependencies": {

packages/apollo/src/commands/client/check.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import { flags } from "@oclif/command";
22
import { print } from "graphql";
33
import { gitInfo } from "../../git";
44
import { ClientCommand } from "../../Command";
5+
import { CompactRenderer } from "../../utils";
56
import URI from "vscode-uri";
67
import { relative } from "path";
78
import { graphqlTypes } from "apollo-language-server";
89
import chalk from "chalk";
10+
import envCi from "env-ci";
911

1012
const { ValidationErrorType } = graphqlTypes;
1113
type ValidationResult = graphqlTypes.ValidateOperations_service_validateOperations_validationResults;
@@ -28,44 +30,51 @@ export default class ClientCheck extends ClientCommand {
2830
};
2931

3032
async run() {
33+
const { isCi } = envCi();
34+
3135
const { validationResults, operations } = await this.runTasks<{
3236
operations: Operation[];
3337
validationResults: ValidationResult[];
34-
}>(({ project, config }) => [
35-
{
36-
title: "Checking client compatibility with service",
37-
task: async ctx => {
38-
if (!config.name) {
39-
throw new Error(
40-
"No service found to link to Engine. Engine is required for this command."
41-
);
38+
}>(
39+
({ project, config }) => [
40+
{
41+
title: "Checking client compatibility with service",
42+
task: async ctx => {
43+
if (!config.name) {
44+
throw new Error(
45+
"No service found to link to Engine. Engine is required for this command."
46+
);
47+
}
48+
ctx.gitContext = await gitInfo(this.log);
49+
50+
ctx.operations = Object.entries(
51+
this.project.mergedOperationsAndFragmentsForService
52+
).map(([name, doc]) => ({
53+
body: print(doc),
54+
name,
55+
relativePath: relative(
56+
config.configURI ? config.configURI.fsPath : "",
57+
URI.parse(doc.definitions[0].loc!.source.name).fsPath
58+
),
59+
locationOffset: doc.definitions[0].loc!.source.locationOffset
60+
}));
61+
62+
ctx.validationResults = await project.engine.validateOperations({
63+
id: config.name,
64+
tag: config.tag,
65+
operations: ctx.operations.map(({ body, name }) => ({
66+
body,
67+
name
68+
})),
69+
gitContext: ctx.gitContext
70+
});
4271
}
43-
ctx.gitContext = await gitInfo(this.log);
44-
45-
ctx.operations = Object.entries(
46-
this.project.mergedOperationsAndFragmentsForService
47-
).map(([name, doc]) => ({
48-
body: print(doc),
49-
name,
50-
relativePath: relative(
51-
config.configURI ? config.configURI.fsPath : "",
52-
URI.parse(doc.definitions[0].loc!.source.name).fsPath
53-
),
54-
locationOffset: doc.definitions[0].loc!.source.locationOffset
55-
}));
56-
57-
ctx.validationResults = await project.engine.validateOperations({
58-
id: config.name,
59-
tag: config.tag,
60-
operations: ctx.operations.map(({ body, name }) => ({
61-
body,
62-
name
63-
})),
64-
gitContext: ctx.gitContext
65-
});
6672
}
67-
}
68-
]);
73+
],
74+
() => ({
75+
renderer: isCi ? CompactRenderer : "default"
76+
})
77+
);
6978

7079
// Group the validation results by operation name
7180
const messagesByOperationName = this.getMessagesByOperationName(

packages/apollo/src/commands/service/__tests__/__snapshots__/check.test.ts.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ exports[`service:check integration federated should report composition errors co
8383
"
8484
`;
8585

86+
exports[`service:check integration federated should report composition errors correctly compacts output in CI 1`] = `
87+
"Loading Apollo Project
88+
Found 3 graph composition errors for service accounts on graph engine
89+
90+
Service Field Message
91+
──────── ─────── ────────────────────────────────────────────────────────────────────────────────────────
92+
reviews User.id marked @external but it does not have a matching field on on the base service (accounts)
93+
reviews User A @key selects id, but User.id could not be found
94+
accounts User A @key selects id, but User.id could not be found
95+
"
96+
`;
97+
8698
exports[`service:check integration federated should report composition errors correctly vanilla 1`] = `
8799
"Loading Apollo Project [started]
88100
Loading Apollo Project [completed]
@@ -133,6 +145,19 @@ exports[`service:check integration federated should report composition success c
133145
"
134146
`;
135147

148+
exports[`service:check integration federated should report composition success correctly compacts output in CI 1`] = `
149+
"Loading Apollo Project
150+
Found 0 graph composition errors for service accounts on graph engine
151+
Validated composed schema against tag master on graph engine
152+
Compared 1 schema change against 0 operations over the last 548 days
153+
Found 0 breaking changes and 1 compatible change
154+
155+
PASS ARG_CHANGED_TYPE \`Query.launches\` argument \`after\` has changed type from \`String\` to \`String!\`
156+
157+
View full details at: https://engine-staging.apollographql.com/service/justin-fullstack-tutorial/check/3acd7765-61b2-4f1a-9227-8b288e42bfdc
158+
"
159+
`;
160+
136161
exports[`service:check integration federated should report composition success correctly vanilla 1`] = `
137162
"Loading Apollo Project [started]
138163
Loading Apollo Project [completed]

packages/apollo/src/commands/service/__tests__/check.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ let mockedConsoleLogOriginal: Console["log"] | null = null;
5050
*/
5151
let mockedConsoleLogValues: string[] | null = null;
5252

53+
// Get original CI environment variables
54+
const { CI, CIRCLECI, GITHUB_ACTION, BUILD_BUILDURI } = process.env;
55+
5356
// TODO: the following two functions are identical to the ones found in list.test.ts
5457
// we are choosing to duplicate them for now, because with a shared helper function,
5558
// jest overwrites console log output as the tests are run in parallel
@@ -369,6 +372,11 @@ describe("service:check", () => {
369372

370373
nock.disableNetConnect();
371374

375+
delete process.env.CI;
376+
delete process.env.CIRCLECI;
377+
delete process.env.GITHUB_ACTION;
378+
delete process.env.BUILD_BUILDURI;
379+
372380
// Set the jest timeout to be longer than the default 5000ms to compensate for slow CI.
373381
jest.setTimeout(25000);
374382
});
@@ -382,6 +390,11 @@ describe("service:check", () => {
382390
// Clean up all network mocks and restore original functionality
383391
nock.cleanAll();
384392
nock.enableNetConnect();
393+
394+
process.env.CI = CI;
395+
process.env.CIRCLECI = CIRCLECI;
396+
process.env.GITHUB_ACTION = GITHUB_ACTION;
397+
process.env.BUILD_BUILDURI = BUILD_BUILDURI;
385398
});
386399

387400
// These are integration tests and not e2e tests because these don't actually hit the remote server.
@@ -406,6 +419,26 @@ describe("service:check", () => {
406419
expect(uncaptureApplicationOutput()).toMatchSnapshot();
407420
});
408421

422+
it("compacts output in CI", async () => {
423+
captureApplicationOutput();
424+
mockCompositionFailure();
425+
426+
expect.assertions(2);
427+
428+
process.env.CI = "true";
429+
430+
await expect(
431+
ServiceCheck.run([
432+
...cliKeyParameter,
433+
"--serviceName=accounts",
434+
`--endpoint=${localURL}/graphql`
435+
])
436+
).rejects.toThrow();
437+
438+
// Inline snapshots don't work here due to https://github.com/facebook/jest/issues/6744.
439+
expect(uncaptureApplicationOutput()).toMatchSnapshot();
440+
});
441+
409442
it("--markdown", async () => {
410443
captureApplicationOutput();
411444
mockCompositionFailure();
@@ -466,6 +499,26 @@ describe("service:check", () => {
466499
expect(uncaptureApplicationOutput()).toMatchSnapshot();
467500
});
468501

502+
it("compacts output in CI", async () => {
503+
captureApplicationOutput();
504+
mockCompositionSuccess();
505+
506+
expect.assertions(2);
507+
508+
process.env.CI = "true";
509+
510+
await expect(
511+
ServiceCheck.run([
512+
...cliKeyParameter,
513+
"--serviceName=accounts",
514+
`--endpoint=${localURL}/graphql`
515+
])
516+
).resolves.not.toThrow();
517+
518+
// Inline snapshots don't work here due to https://github.com/facebook/jest/issues/6744.
519+
expect(uncaptureApplicationOutput()).toMatchSnapshot();
520+
});
521+
469522
it("--markdown", async () => {
470523
captureApplicationOutput();
471524
mockCompositionSuccess();

packages/apollo/src/commands/service/check.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import { flags } from "@oclif/command";
22
import { table } from "heroku-cli-util";
33
import { introspectionFromSchema, printSchema, GraphQLSchema } from "graphql";
44
import chalk from "chalk";
5+
import envCi from "env-ci";
56
import { gitInfo } from "../../git";
67
import { ProjectCommand } from "../../Command";
7-
import { validateHistoricParams, pluralize } from "../../utils";
8+
import {
9+
validateHistoricParams,
10+
pluralize,
11+
CompactRenderer
12+
} from "../../utils";
813
import {
914
CheckSchema_service_checkSchema,
1015
CheckSchema_service_checkSchema_diffToPrevious_changes as Change,
@@ -303,6 +308,8 @@ export default class ServiceCheck extends ProjectCommand {
303308
const federatedServiceCompositionUnsuccessfulErrorMessage =
304309
"Federated service composition was unsuccessful. Please see the reasons below.";
305310

311+
const { isCi } = envCi();
312+
306313
let schema: GraphQLSchema | undefined;
307314
try {
308315
await this.runTasks<TasksOutput>(
@@ -568,8 +575,11 @@ export default class ServiceCheck extends ProjectCommand {
568575
// the `this.log` output to `stdout`.
569576
//
570577
// @see https://github.com/SamVerschueren/listr#renderer
571-
renderer:
572-
context.flags.markdown || context.flags.json ? "silent" : "default"
578+
renderer: isCi
579+
? CompactRenderer
580+
: context.flags.markdown || context.flags.json
581+
? "silent"
582+
: "default"
573583
})
574584
);
575585
} catch (error) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
ListrRenderer,
3+
ListrTaskObject,
4+
ListrOptions,
5+
ListrError
6+
} from "listr";
7+
8+
export class CompactRenderer<Ctx> implements ListrRenderer {
9+
_tasks: ReadonlyArray<ListrTaskObject<Ctx>>;
10+
11+
constructor(
12+
tasks: ReadonlyArray<ListrTaskObject<Ctx>>,
13+
options: ListrOptions<Ctx>
14+
) {
15+
this._tasks = tasks;
16+
}
17+
18+
static get nonTTY() {
19+
return true;
20+
}
21+
22+
render() {
23+
this._tasks.forEach(task => {
24+
task.subscribe(event => {
25+
if (
26+
event.type === "STATE" &&
27+
(task.state === "completed" || task.state === "failed")
28+
) {
29+
console.log(task.title);
30+
}
31+
});
32+
});
33+
}
34+
35+
end(err: ListrError<Ctx>) {}
36+
}

packages/apollo/src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from "./validateHistoricParams";
22

33
export * from "./pluralize";
4+
5+
export * from "./CompactRenderer";

0 commit comments

Comments
 (0)