Skip to content

Commit

Permalink
Move tracking config checks into classes
Browse files Browse the repository at this point in the history
- move parsing logic into separate classes to keep the code in the executing for-loop small

https://phabricator.wikimedia.org/T380052
  • Loading branch information
moiikana committed Nov 21, 2024
1 parent 87635bc commit a30d26e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 43 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"lint:test-imports": "node test/find_mismatched_imports.mjs",
"ci": "npm-run-all lint test check-content-version",
"check-content-version": "ts-node tools/check-content-version.ts",
"check-tracking-conf": "ts-node tools/check-tracking-number-and-date.mjs",
"coverage": "vitest run --coverage",
"check-tracking-conf": "ts-node tools/CampaignConfigurationCheck/check-tracking-number-and-date.mjs",
"coverage": "vitest run --coverage",
"update-content": "npm update fundraising-frontend-content"
},
"keywords": [],
Expand Down
15 changes: 15 additions & 0 deletions tools/CampaignConfigurationCheck/CampaignName.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import chalk from 'chalk';

export default class CampaignName {
/**
* @param {string} campaignName
*/
constructor( campaignName ) {
const testNumberRegexResult = campaignName.match( /_(\d{2})/ );
if ( testNumberRegexResult === null ) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Cannot parse test number ( "_dd" format) for ${ campaignName } ` );
process.exit( 1 );
}
this.testNumber = testNumberRegexResult[ 1 ];
}
}
22 changes: 22 additions & 0 deletions tools/CampaignConfigurationCheck/CampaignTrackingCode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import chalk from 'chalk';

export default class CampaignTrackingCode {
/**
* @param {string} campaignTrackingCode
*/
constructor( campaignTrackingCode ) {
const textNumberRegexResult = campaignTrackingCode.match( /^\D*(\d{2})([-_])/ );
if ( textNumberRegexResult === null ) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Wrong test number format for ${ campaignTrackingCode } ` );
process.exit( 1 );
}
this.testNumber = textNumberRegexResult[ 1 ];

const startDateRegexResult = campaignTrackingCode.match( /-(\d{6})$/ );
if ( startDateRegexResult === null ) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Wrong start date format (should be 6-digit) for ${ campaignTrackingCode } ` );
process.exit( 1 );
}
this.startDate = startDateRegexResult[ 1 ];
}
}
30 changes: 30 additions & 0 deletions tools/CampaignConfigurationCheck/KeywordTrackingCode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import chalk from 'chalk';

export default class KeywordTrackingCode {
/**
* @param {string} trackingCode
*/
constructor( trackingCode ) {
const testNumberRegexResult = trackingCode.match( /^\D*(\d{2})([-_])/ );
if ( testNumberRegexResult === null ) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Cannot parse test number ( = "first pair of digits" ) for ${ trackingCode } ` );
process.exit( 1 );
}
this.testNumber = testNumberRegexResult[ 1 ];

const startDateRegexResult = trackingCode.match( /-(\d{6})-/ );
if ( startDateRegexResult === null ) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Cannot parse 6-digit start date for ${ trackingCode } ` );
process.exit( 1 );
}
this.startDate = startDateRegexResult[ 1 ];
}

/**
* @param {CampaignTrackingCode|KeywordTrackingCode} trackingCode
* @return {boolean}
*/
matchesDate( trackingCode ) {
return this.startDate === trackingCode.startDate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { parse as parseTOML } from 'toml';
import { readFileSync } from 'fs';
import { campaignInfoToCampaignConfig } from '../../webpack/convert_info_to_type.js';
import chalk from 'chalk';
import CampaignName from './CampaignName.mjs';
import KeywordTrackingCode from './KeywordTrackingCode.mjs';
import CampaignTrackingCode from './CampaignTrackingCode.mjs';

const rawConfigForAllCampaigns = parseTOML( readFileSync( 'campaign_info.toml', 'utf-8' ) );
const configObject = campaignInfoToCampaignConfig( rawConfigForAllCampaigns );

for ( const channelConfig of Object.values( configObject ) ) {

const campaignName = new CampaignName( channelConfig.campaign );
const campaignTracking = new CampaignTrackingCode( channelConfig.tracking );
const ctrlTracking = new KeywordTrackingCode( channelConfig.banners.ctrl.tracking );
const varTracking = new KeywordTrackingCode( channelConfig.banners.var.tracking );

if ( campaignName.testNumber !== varTracking.testNumber ||
campaignName.testNumber !== ctrlTracking.testNumber ||
campaignName.testNumber !== campaignTracking.testNumber
) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Faulty test number for ${ channelConfig.campaign } ` );
console.log( campaignName.testNumber );
console.log( campaignTracking.testNumber );
console.log( ctrlTracking.testNumber );
console.log( varTracking.testNumber );
process.exit( 1 );
}
if ( !ctrlTracking.matchesDate( varTracking ) || !ctrlTracking.matchesDate( campaignTracking ) ) {
console.warn( `${ chalk.red( 'CAMPAIGN CONFIGURATION ERROR:' ) } Faulty start date for ${ channelConfig.campaign } ` );
console.log( campaignTracking.startDate );
console.log( ctrlTracking.startDate );
console.log( varTracking.startDate );
process.exit( 1 );
}

}
41 changes: 0 additions & 41 deletions tools/check-tracking-number-and-date.mjs

This file was deleted.

0 comments on commit a30d26e

Please sign in to comment.