Monorepo Utils: Add Code Freeze date function (#37589)

This commit is contained in:
Paul Sealock 2023-04-13 09:28:53 +12:00 committed by GitHub
parent 1187df9d82
commit 78ff0195b9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 337 additions and 348 deletions

View File

@ -29,32 +29,19 @@ jobs:
outputs:
freeze: ${{ steps.check-freeze.outputs.freeze }}
steps:
- name: 'Install PHP'
uses: shivammathur/setup-php@8e2ac35f639d3e794c1da1f28999385ab6fdf0fc
- name: Checkout code
uses: actions/checkout@v3
with:
php-version: '7.4'
fetch-depth: 0
- name: Install prerequisites
run: |
npm install -g pnpm@7
pnpm install --filter monorepo-utils
- name: 'Check whether today is the code freeze day'
id: check-freeze
shell: php {0}
run: |
<?php
$now = time();
if ( getenv( 'TIME_OVERRIDE' ) ) {
$now = strtotime( getenv( 'TIME_OVERRIDE' ) );
}
// Code freeze comes 22 days prior to release day.
$release_time = strtotime( '+22 days', $now );
$release_day_of_week = date( 'l', $release_time );
$release_day_of_month = (int) date( 'j', $release_time );
// If 22 days from now isn't the second Tuesday, then it's not code freeze day.
if ( 'Tuesday' !== $release_day_of_week || $release_day_of_month < 8 || $release_day_of_month > 14 ) {
file_put_contents( getenv( 'GITHUB_OUTPUT' ), "freeze=1\n", FILE_APPEND );
} else {
file_put_contents( getenv( 'GITHUB_OUTPUT' ), "freeze=0\n", FILE_APPEND );
}
run: pnpm utils code-freeze verify-day -g -o $TIME_OVERRIDE
maybe-create-next-milestone-and-release-branch:
name: 'Maybe create next milestone and release branch'
@ -63,12 +50,16 @@ jobs:
contents: write
issues: write
needs: verify-code-freeze
if: needs.verify-code-freeze.outputs.freeze == 0
if: needs.verify-code-freeze.outputs.freeze == 'true'
outputs:
branch: ${{ steps.freeze.outputs.branch }}
release_version: ${{ steps.freeze.outputs.release_version }}
next_version: ${{ steps.freeze.outputs.next_version }}
steps:
- name: 'Install PHP'
uses: shivammathur/setup-php@8e2ac35f639d3e794c1da1f28999385ab6fdf0fc
with:
php-version: '7.4'
- name: Checkout code
uses: actions/checkout@v3
with:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1 @@
dist/

View File

@ -6,10 +6,12 @@
"homepage": "https://github.com/woocommerce/woocommerce",
"license": "GPLv2",
"repository": "woocommerce/woocommerce",
"main": "dist/index.js",
"dependencies": {
"@actions/core": "^1.10.0",
"@commander-js/extra-typings": "^0.1.0",
"chalk": "^4.1.2",
"commander": "^9.4.0",
"@commander-js/extra-typings": "^0.1.0",
"dotenv": "^10.0.0"
},
"devDependencies": {

View File

@ -2,11 +2,18 @@
* External dependencies
*/
import { Command } from '@commander-js/extra-typings';
import chalk from 'chalk';
import { setOutput } from '@actions/core';
/**
* Internal dependencies
*/
import { verifyDay } from '../utils/index';
import {
isTodayCodeFreezeDay,
DAYS_BETWEEN_CODE_FREEZE_AND_RELEASE,
getToday,
getFutureDate,
} from '../utils/index';
export const verifyDayCommand = new Command( 'verify-day' )
.description( 'Verify if today is the code freeze day' )
@ -14,8 +21,40 @@ export const verifyDayCommand = new Command( 'verify-day' )
'-o, --override <override>',
"Time Override: The time to use in checking whether the action should run (default: 'now')."
)
.action( () => {
console.log( verifyDay() );
.option(
'-g --github',
'CLI command is used in the Github Actions context.'
)
.action( ( { override, github } ) => {
const today = getToday( override );
const futureDate = getFutureDate( today );
console.log(
chalk.yellow( "Today's timestamp UTC is: " + today.toUTCString() )
);
console.log(
chalk.yellow(
`Checking to see if ${ DAYS_BETWEEN_CODE_FREEZE_AND_RELEASE } days from today is the second Tuesday of the month.`
)
);
const isCodeFreezeDay = isTodayCodeFreezeDay( override );
console.log(
chalk.green(
`${ futureDate.toUTCString() } ${
isCodeFreezeDay ? 'is' : 'is not'
} release day.`
)
);
console.log(
chalk.green(
`Today is ${
isCodeFreezeDay ? 'indeed' : 'not'
} code freeze day.`
)
);
if ( github ) {
setOutput( 'freeze', isCodeFreezeDay.toString() );
}
process.exit( 0 );
} );

View File

@ -1,10 +1,36 @@
/**
* Internal dependencies
*/
import { verifyDay } from '../index';
import { isTodayCodeFreezeDay } from '../index';
describe( 'verifyDay', () => {
it( 'should return a string', () => {
expect( verifyDay() ).toBe( 'Today is a good day to code freeze!' );
describe( 'isTodayCodeFreezeDay', () => {
it( 'should return false when given a day not 22 days before release', () => {
const JUNE_5_2023 = '2023-06-05T00:00:00.000Z';
const JUNE_12_2023 = '2023-06-12T00:00:00.000Z';
const JUNE_26_2023 = '2023-06-26T00:00:00.000Z';
const AUG_10_2023 = '2023-08-10T00:00:00.000Z';
const AUG_17_2023 = '2023-08-17T00:00:00.000Z';
const AUG_24_2023 = '2023-08-24T00:00:00.000Z';
expect( isTodayCodeFreezeDay( JUNE_5_2023 ) ).toBeFalsy();
expect( isTodayCodeFreezeDay( JUNE_12_2023 ) ).toBeFalsy();
expect( isTodayCodeFreezeDay( JUNE_26_2023 ) ).toBeFalsy();
expect( isTodayCodeFreezeDay( AUG_10_2023 ) ).toBeFalsy();
expect( isTodayCodeFreezeDay( AUG_17_2023 ) ).toBeFalsy();
expect( isTodayCodeFreezeDay( AUG_24_2023 ) ).toBeFalsy();
} );
it( 'should return true when given a day 22 days before release', () => {
const JUNE_19_2023 = '2023-06-19T00:00:00.000Z';
const JULY_17_2023 = '2023-07-17T00:00:00.000Z';
const AUGUST_21_2023 = '2023-08-21T00:00:00.000Z';
expect( isTodayCodeFreezeDay( JUNE_19_2023 ) ).toBeTruthy();
expect( isTodayCodeFreezeDay( JULY_17_2023 ) ).toBeTruthy();
expect( isTodayCodeFreezeDay( AUGUST_21_2023 ) ).toBeTruthy();
} );
it( 'should error out when passed an invalid date', () => {
expect( () => isTodayCodeFreezeDay( 'invalid date' ) ).toThrow();
} );
} );

View File

@ -1,3 +1,46 @@
export const verifyDay = () => {
return 'Today is a good day to code freeze!';
const MILLIS_IN_A_DAY = 24 * 60 * 60 * 1000;
export const DAYS_BETWEEN_CODE_FREEZE_AND_RELEASE = 22;
/**
* Get a Date object of now or the override time when specified.
*
* @param {string} now The time to use in checking if today is the day of the code freeze. Default to now.
* @return {Date} The Date object of now or the override time when specified.
*/
export const getToday = ( now = 'now' ): Date => {
const today = now === 'now' ? new Date() : new Date( now );
if ( isNaN( today.getTime() ) ) {
throw new Error(
'Invalid date: Check the override parameter (-o, --override) is a correct Date string'
);
}
return today;
};
/**
* Get a future date from today to see if its the release day.
*
* @param {string} today The time to use in checking if today is the day of the code freeze. Default to now.
* @return {Date} The Date object of the future date.
*/
export const getFutureDate = ( today: Date ) => {
return new Date(
today.getTime() + DAYS_BETWEEN_CODE_FREEZE_AND_RELEASE * MILLIS_IN_A_DAY
);
};
/**
* Determines if today is the day of the code freeze.
*
* @param {string} now The time to use in checking if today is the day of the code freeze. Default to now.
* @return {boolean} true if today is the day of the code freeze.
*/
export const isTodayCodeFreezeDay = ( now: string ) => {
const today = getToday( now );
const futureDate = getFutureDate( today );
const month = futureDate.getUTCMonth();
const year = futureDate.getUTCFullYear();
const firstDayOfMonth = new Date( Date.UTC( year, month, 1 ) );
const dayOfWeek = firstDayOfMonth.getUTCDay();
const secondTuesday = dayOfWeek <= 2 ? 10 - dayOfWeek : 17 - dayOfWeek;
return futureDate.getUTCDate() === secondTuesday;
};