Skip to content

Commit 156a2cf

Browse files
committed
Add more tests, github workflow
1 parent 3f62f0c commit 156a2cf

File tree

8 files changed

+312
-10
lines changed

8 files changed

+312
-10
lines changed

.github/workflows/update-cta.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Update greeting CTA
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- config/cta.conf.js
8+
workflow_dispatch:
9+
inputs:
10+
dry-run:
11+
description: Run the script without updating the CTA
12+
type: boolean
13+
required: false
14+
default: false
15+
environment:
16+
description: The environment to run the script in - must have the DOWNLOAD_CENTER_AWS_KEY and DOWNLOAD_CENTER_AWS_SECRET secrets configured
17+
type: environment
18+
required: true
19+
default: CTA-Production
20+
21+
jobs:
22+
dry-run:
23+
name: Update greeting CTA
24+
runs-on: ubuntu-latest
25+
environment: ${{ github.event.inputs.environment || 'CTA-Production'}}
26+
env:
27+
npm_config_loglevel: verbose
28+
npm_config_foreground_scripts: "true"
29+
PUPPETEER_SKIP_DOWNLOAD: "true"
30+
DOWNLOAD_CENTER_AWS_KEY: ${{ secrets.DOWNLOAD_CENTER_AWS_KEY }}
31+
DOWNLOAD_CENTER_AWS_SECRET: ${{ secrets.DOWNLOAD_CENTER_AWS_SECRET }}
32+
steps:
33+
- uses: actions/checkout@v4
34+
- uses: actions/setup-node@v4
35+
with:
36+
node-version: ^20.x
37+
cache: "npm"
38+
39+
- name: Install Dependencies and Compile
40+
run: |
41+
npm ci
42+
npm run compile
43+
44+
- name: Update greeting CTA
45+
run: |
46+
npm run update-cta ${{ github.event.inputs.dry-run && '-- --dry-run' || '' }}

config/cta.conf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ module.exports = {
77
// Define the ctas per version here. '*' is the default cta which will be shown if there's no specific cta
88
// for the current version.
99
// '*': {
10-
// runs: [
10+
// chunks: [
1111
// { text: 'Example', style: 'bold' },
1212
// ]
1313
// },
1414
// '1.2.3': {
15-
// runs: [
15+
// chunks: [
1616
// { text: 'Example', style: 'mongosh:uri' },
1717
// ]
1818
// }
1919
},
2020
isDryRun: false,
21-
}
21+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"compile-all": "npm run compile-compass && npm run compile-exec",
4040
"evergreen-release": "cd packages/build && npm run evergreen-release --",
4141
"release": "cd packages/build && npm run release --",
42+
"update-cta": "cd packages/build && npm run update-cta --",
4243
"report-missing-help": "npm run report-missing-help --workspace @mongosh/shell-api",
4344
"report-supported-api": "npm run report-supported-api --workspace @mongosh/shell-api",
4445
"post-process-nyc": "ts-node scripts/nyc/post-process-nyc-output.ts",

packages/build/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"evergreen-release": "ts-node -r ../../scripts/import-expansions.js src/index.ts",
2727
"release": "ts-node src/index.ts trigger-release",
2828
"prettier": "prettier",
29-
"reformat": "npm run prettier -- --write . && npm run eslint --fix"
29+
"reformat": "npm run prettier -- --write . && npm run eslint --fix",
30+
"update-cta": "ts-node src/index.ts update-cta"
3031
},
3132
"license": "Apache-2.0",
3233
"publishConfig": {

packages/build/src/download-center/config.spec.ts

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import {
1010
getUpdatedDownloadCenterConfig,
1111
createAndPublishDownloadCenterConfig,
1212
createJsonFeedEntry,
13+
updateJsonFeedCTA,
14+
UpdateCTAConfig,
15+
JsonFeed,
1316
} from './config';
1417
import { promises as fs } from 'fs';
1518
import path from 'path';
@@ -529,4 +532,225 @@ describe('DownloadCenter config', function () {
529532
expect(serverTargets).to.include(target);
530533
});
531534
});
535+
536+
describe('updateJsonFeedCTA', function () {
537+
let dlCenter: any;
538+
let uploadConfig: sinon.SinonStub;
539+
let downloadConfig: sinon.SinonStub;
540+
let uploadAsset: sinon.SinonStub;
541+
let downloadAsset: sinon.SinonStub;
542+
543+
const existingUploadedJsonFeed = require(path.resolve(
544+
__dirname,
545+
'..',
546+
'..',
547+
'test',
548+
'fixtures',
549+
'cta-versions.json'
550+
)) as JsonFeed;
551+
552+
const getConfig = (ctas: UpdateCTAConfig['ctas']): UpdateCTAConfig => {
553+
return {
554+
ctas,
555+
isDryRun: false,
556+
awsAccessKeyId: 'accessKey',
557+
awsSecretAccessKey: 'secretKey',
558+
};
559+
};
560+
561+
const getUploadedJsonFeed = (): JsonFeed => {
562+
return JSON.parse(uploadAsset.lastCall.args[1]) as JsonFeed;
563+
};
564+
565+
beforeEach(function () {
566+
uploadConfig = sinon.stub();
567+
downloadConfig = sinon.stub();
568+
uploadAsset = sinon.stub();
569+
downloadAsset = sinon.stub();
570+
dlCenter = sinon.stub();
571+
572+
downloadAsset.returns(JSON.stringify(existingUploadedJsonFeed));
573+
574+
dlCenter.returns({
575+
downloadConfig,
576+
uploadConfig,
577+
uploadAsset,
578+
downloadAsset,
579+
});
580+
});
581+
582+
for (let dryRun of [false, true]) {
583+
it(`when dryRun is ${dryRun}, does ${
584+
dryRun ? 'not ' : ''
585+
}upload the updated json feed`, async function () {
586+
const config = getConfig({
587+
'1.10.3': {
588+
chunks: [{ text: 'Foo' }],
589+
},
590+
'*': {
591+
chunks: [{ text: 'Bar' }],
592+
},
593+
});
594+
595+
config.isDryRun = dryRun;
596+
597+
await updateJsonFeedCTA(config, dlCenter);
598+
if (dryRun) {
599+
expect(uploadAsset).to.not.have.been.called;
600+
} else {
601+
expect(uploadAsset).to.have.been.called;
602+
603+
const updatedJsonFeed = getUploadedJsonFeed();
604+
expect(updatedJsonFeed.cta?.chunks).to.deep.equal([{ text: 'Bar' }]);
605+
expect(
606+
updatedJsonFeed.versions.filter((v) => v.version === '1.10.3')[0]
607+
.cta?.chunks
608+
).to.deep.equal([{ text: 'Foo' }]);
609+
expect(
610+
updatedJsonFeed.versions.filter((v) => v.version === '1.10.4')[0]
611+
.cta
612+
).to.be.undefined;
613+
}
614+
});
615+
}
616+
617+
it('cannot add new versions', async function () {
618+
expect(
619+
existingUploadedJsonFeed.versions.filter((v) => v.version === '1.10.5')
620+
).to.have.lengthOf(0);
621+
622+
const config = getConfig({
623+
'1.10.5': {
624+
chunks: [{ text: 'Foo' }],
625+
},
626+
});
627+
628+
await updateJsonFeedCTA(config, dlCenter);
629+
630+
const updatedJsonFeed = getUploadedJsonFeed();
631+
632+
expect(
633+
updatedJsonFeed.versions.filter((v) => v.version === '1.10.5')
634+
).to.have.lengthOf(0);
635+
});
636+
637+
it('can remove global cta', async function () {
638+
// Preserve existing CTAs, but omit the global one
639+
const ctas = (existingUploadedJsonFeed.versions as any[]).reduce(
640+
(acc, current) => {
641+
acc[current.version] = current.cta;
642+
return acc;
643+
},
644+
{}
645+
);
646+
const config = getConfig(ctas);
647+
648+
expect(config.ctas['*']).to.be.undefined;
649+
await updateJsonFeedCTA(config, dlCenter);
650+
651+
const updatedJsonFeed = getUploadedJsonFeed();
652+
653+
expect(updatedJsonFeed.cta).to.be.undefined;
654+
});
655+
656+
it('can remove version specific cta', async function () {
657+
expect(
658+
existingUploadedJsonFeed.versions.map((v) => v.cta).filter((cta) => cta)
659+
).to.have.length.greaterThan(0);
660+
661+
const config = getConfig({
662+
'*': existingUploadedJsonFeed.cta!,
663+
});
664+
665+
await updateJsonFeedCTA(config, dlCenter);
666+
667+
const updatedJsonFeed = getUploadedJsonFeed();
668+
expect(updatedJsonFeed.cta).to.not.be.undefined;
669+
expect(
670+
updatedJsonFeed.versions.map((v) => v.cta).filter((cta) => cta)
671+
).to.have.lengthOf(0);
672+
});
673+
674+
it('can update global cta', async function () {
675+
const config = getConfig({
676+
'*': {
677+
chunks: [{ text: "It's a beautiful day", style: 'imagePositive' }],
678+
},
679+
});
680+
681+
await updateJsonFeedCTA(config, dlCenter);
682+
683+
const updatedJsonFeed = getUploadedJsonFeed();
684+
685+
expect(updatedJsonFeed.cta).to.deep.equal({
686+
chunks: [{ text: "It's a beautiful day", style: 'imagePositive' }],
687+
});
688+
});
689+
690+
it('can update version-specific cta', async function () {
691+
const config = getConfig({
692+
'1.10.3': {
693+
chunks: [{ text: "It's a beautiful day", style: 'imagePositive' }],
694+
},
695+
});
696+
697+
await updateJsonFeedCTA(config, dlCenter);
698+
699+
const updatedJsonFeed = getUploadedJsonFeed();
700+
701+
expect(
702+
updatedJsonFeed.versions.filter((v) => v.version === '1.10.3')[0].cta
703+
).to.deep.equal({
704+
chunks: [{ text: "It's a beautiful day", style: 'imagePositive' }],
705+
});
706+
});
707+
708+
it('can add global cta', async function () {
709+
// Remove the existing cta
710+
existingUploadedJsonFeed.cta = undefined;
711+
712+
const config = getConfig({
713+
'*': {
714+
chunks: [
715+
{ text: 'Go outside and enjoy the sun', style: 'imagePositive' },
716+
],
717+
},
718+
});
719+
720+
await updateJsonFeedCTA(config, dlCenter);
721+
722+
const updatedJsonFeed = getUploadedJsonFeed();
723+
724+
expect(updatedJsonFeed.cta).to.deep.equal({
725+
chunks: [
726+
{ text: 'Go outside and enjoy the sun', style: 'imagePositive' },
727+
],
728+
});
729+
});
730+
731+
it('can add version-specific cta', async function () {
732+
// Remove the existing cta
733+
existingUploadedJsonFeed.cta = undefined;
734+
735+
const config = getConfig({
736+
'1.10.4': {
737+
chunks: [
738+
{ text: 'Go outside and enjoy the sun', style: 'imagePositive' },
739+
],
740+
},
741+
});
742+
743+
await updateJsonFeedCTA(config, dlCenter);
744+
745+
const updatedJsonFeed = getUploadedJsonFeed();
746+
747+
expect(
748+
updatedJsonFeed.versions.filter((v) => v.version === '1.10.4')[0].cta
749+
).to.deep.equal({
750+
chunks: [
751+
{ text: 'Go outside and enjoy the sun', style: 'imagePositive' },
752+
],
753+
});
754+
});
755+
});
532756
});

packages/build/src/download-center/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,20 @@ export function createVersionConfig(
241241
interface GreetingCTADetails {
242242
chunks: {
243243
text: string;
244-
style: string; // TODO: this is actually clr.ts/StyleDefinition
244+
style?: string; // TODO: this is actually clr.ts/StyleDefinition
245245
}[];
246246
}
247247

248248
export interface UpdateCTAConfig {
249249
ctas: {
250-
[version: string]: GreetingCTADetails;
250+
[version: string | '*']: GreetingCTADetails;
251251
};
252252
awsAccessKeyId: string;
253253
awsSecretAccessKey: string;
254254
isDryRun: boolean;
255255
}
256256

257-
interface JsonFeed {
257+
export interface JsonFeed {
258258
versions: JsonFeedVersionEntry[];
259259
cta?: GreetingCTADetails;
260260
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"versions": [
3+
{
4+
"version": "1.10.3",
5+
"cta": {
6+
"chunks": [
7+
{
8+
"text": "Critical update available: 1.10.4 ",
9+
"style": "bold"
10+
},
11+
{
12+
"text": "https://www.mongodb.com/try/download/shell",
13+
"style": "mongosh:uri"
14+
}
15+
]
16+
}
17+
},
18+
{
19+
"version": "1.10.4"
20+
}
21+
],
22+
"cta": {
23+
"chunks": [
24+
{
25+
"text": "Vote for your favorite feature in mongosh "
26+
},
27+
{
28+
"text": "https://mongodb.com/surveys/shell/2024-Q4",
29+
"style": "mongosh:uri"
30+
}
31+
]
32+
}
33+
}

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,9 +586,6 @@ class MongoshNodeRepl implements EvaluationListener {
586586
mongodVersion: string,
587587
greeting?: GreetingDetails
588588
): Promise<void> {
589-
this.output.write('sadfasdfasdfassafsa');
590-
this.output.write(JSON.stringify({ mongodVersion, greeting }) + '\n');
591-
592589
if (this.shellCliOptions.quiet) {
593590
return;
594591
}

0 commit comments

Comments
 (0)