-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into chore/ramp-codeowners
- Loading branch information
Showing
322 changed files
with
109,218 additions
and
36,570 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export MM_FOX_CODE="EXAMPLE_FOX_CODE" | ||
export MM_BRANCH_KEY_TEST= | ||
export MM_BRANCH_KEY_LIVE= | ||
export MM_MIXPANEL_TOKEN= |
39 changes: 39 additions & 0 deletions
39
.github/scripts/fitness-functions/common/constants.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { EXCLUDE_REGEX } from './constants'; | ||
|
||
describe('Regular Expressions used in Fitness Functions', (): void => { | ||
describe(`EXCLUDE_REGEX "${EXCLUDE_REGEX}"`, (): void => { | ||
const PATHS_IT_SHOULD_MATCH = [ | ||
'.github/file.js', | ||
'.github/file.ts', | ||
'.github/path/file.js', | ||
'.github/much/longer/path/file.js', | ||
]; | ||
|
||
const PATHS_IT_SHOULD_NOT_MATCH = [ | ||
'file.js', | ||
'file.ts', | ||
'app/file.ts', | ||
'app/.github/file.ts', | ||
]; | ||
|
||
describe('included paths', (): void => { | ||
PATHS_IT_SHOULD_MATCH.forEach((path: string): void => { | ||
it(`should match "${path}"`, (): void => { | ||
const result = EXCLUDE_REGEX.test(path); | ||
|
||
expect(result).toStrictEqual(true); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('excluded paths', (): void => { | ||
PATHS_IT_SHOULD_NOT_MATCH.forEach((path: string): void => { | ||
it(`should not match "${path}"`, (): void => { | ||
const result = EXCLUDE_REGEX.test(path); | ||
|
||
expect(result).toStrictEqual(false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Exclude checking for files in .github directory | ||
const EXCLUDE_REGEX = /^.github/; | ||
|
||
enum AUTOMATION_TYPE { | ||
CI = 'ci', | ||
PRE_COMMIT_HOOK = 'pre-commit-hook', | ||
PRE_PUSH_HOOK = 'pre-push-hook', | ||
} | ||
|
||
export { EXCLUDE_REGEX, AUTOMATION_TYPE }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { execSync } from 'child_process'; | ||
import fs from 'fs'; | ||
import { AUTOMATION_TYPE } from './constants'; | ||
|
||
function getDiffByAutomationType( | ||
automationType: AUTOMATION_TYPE, | ||
): string | undefined { | ||
if (!Object.values(AUTOMATION_TYPE).includes(automationType)) { | ||
console.error('Invalid automation type.'); | ||
process.exit(1); | ||
} | ||
|
||
let diff; | ||
if (automationType === AUTOMATION_TYPE.CI) { | ||
const optionalArguments = process.argv.slice(3); | ||
if (optionalArguments.length !== 1) { | ||
console.error('Invalid number of arguments.'); | ||
process.exit(1); | ||
} | ||
|
||
diff = getCIDiff(optionalArguments[0]); | ||
} else if (automationType === AUTOMATION_TYPE.PRE_COMMIT_HOOK) { | ||
diff = getPreCommitHookDiff(); | ||
} else if (automationType === AUTOMATION_TYPE.PRE_PUSH_HOOK) { | ||
diff = getPrePushHookDiff(); | ||
} | ||
|
||
return diff; | ||
} | ||
|
||
function getCIDiff(path: string): string { | ||
return fs.readFileSync(path, { | ||
encoding: 'utf8', | ||
flag: 'r', | ||
}); | ||
} | ||
|
||
function getPreCommitHookDiff(): string { | ||
return execSync(`git diff --cached HEAD`).toString().trim(); | ||
} | ||
|
||
function getPrePushHookDiff(): string { | ||
const currentBranch = execSync(`git rev-parse --abbrev-ref HEAD`) | ||
.toString() | ||
.trim(); | ||
|
||
return execSync( | ||
`git diff ${currentBranch} origin/${currentBranch} -- . ':(exclude)development/fitness-functions/'`, | ||
) | ||
.toString() | ||
.trim(); | ||
} | ||
|
||
export { getDiffByAutomationType }; |
153 changes: 153 additions & 0 deletions
153
.github/scripts/fitness-functions/common/shared.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import { | ||
filterDiffLineAdditions, | ||
hasNumberOfCodeBlocksIncreased, | ||
filterDiffByFilePath, | ||
filterDiffFileCreations, | ||
} from './shared'; | ||
import { generateCreateFileDiff, generateModifyFilesDiff } from './test-data'; | ||
|
||
describe('filterDiffLineAdditions()', (): void => { | ||
it('should return code additions in the diff', (): void => { | ||
const testFilePath = 'new-file.js'; | ||
const testAddition = 'foo'; | ||
const testFileDiff = generateCreateFileDiff(testFilePath, testAddition); | ||
|
||
const actualResult = filterDiffLineAdditions(testFileDiff); | ||
const expectedResult = ` +${testAddition}`; | ||
|
||
expect(actualResult).toStrictEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('filterDiffFileCreations()', (): void => { | ||
it('should return code additions in the diff', (): void => { | ||
const testFileDiff = [ | ||
generateModifyFilesDiff('new-file.ts', 'foo', 'bar'), | ||
generateCreateFileDiff('old-file.js', 'ping'), | ||
generateModifyFilesDiff('old-file.jsx', 'yin', 'yang'), | ||
].join(''); | ||
|
||
const actualResult = filterDiffFileCreations(testFileDiff); | ||
|
||
expect(actualResult).toMatchInlineSnapshot(` | ||
"diff --git a/old-file.js b/old-file.js | ||
new file mode 100644 | ||
index 000000000..30d74d258 | ||
--- /dev/null | ||
+++ b/old-file.js | ||
@@ -0,0 +1 @@ | ||
+ping" | ||
`); | ||
}); | ||
}); | ||
|
||
describe('hasNumberOfCodeBlocksIncreased()', (): void => { | ||
it('should show which code blocks have increased', (): void => { | ||
const testDiffFragment = ` | ||
+foo | ||
+bar | ||
+baz`; | ||
const testCodeBlocks = ['code block 1', 'foo', 'baz']; | ||
|
||
const actualResult = hasNumberOfCodeBlocksIncreased( | ||
testDiffFragment, | ||
testCodeBlocks, | ||
); | ||
const expectedResult = { 'code block 1': false, foo: true, baz: true }; | ||
|
||
expect(actualResult).toStrictEqual(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('filterDiffByFilePath()', (): void => { | ||
const testFileDiff = [ | ||
generateModifyFilesDiff('new-file.ts', 'foo', 'bar'), | ||
generateModifyFilesDiff('old-file.js', 'ping', 'pong'), | ||
generateModifyFilesDiff('old-file.jsx', 'yin', 'yang'), | ||
].join(''); | ||
|
||
it('should return the right diff for a generic matcher', (): void => { | ||
const actualResult = filterDiffByFilePath( | ||
testFileDiff, | ||
/^(.*\/)?.*\.(jsx)$/, // Exclude jsx files | ||
); | ||
|
||
expect(actualResult).toMatchInlineSnapshot(` | ||
"diff --git a/new-file.ts b/new-file.ts | ||
index 57d5de75c..808d8ba37 100644 | ||
--- a/new-file.ts | ||
+++ b/new-file.ts | ||
@@ -1,3 +1,8 @@ | ||
+foo | ||
@@ -34,33 +39,4 @@ | ||
-bar | ||
diff --git a/old-file.js b/old-file.js | ||
index 57d5de75c..808d8ba37 100644 | ||
--- a/old-file.js | ||
+++ b/old-file.js | ||
@@ -1,3 +1,8 @@ | ||
+ping | ||
@@ -34,33 +39,4 @@ | ||
-pong" | ||
`); | ||
}); | ||
|
||
it('should return the right diff for a specific file in any dir matcher', (): void => { | ||
const actualResult = filterDiffByFilePath(testFileDiff, /.*old-file\.js$/); // Exclude old-file.js | ||
|
||
expect(actualResult).toMatchInlineSnapshot(` | ||
"diff --git a/new-file.ts b/new-file.ts | ||
index 57d5de75c..808d8ba37 100644 | ||
--- a/new-file.ts | ||
+++ b/new-file.ts | ||
@@ -1,3 +1,8 @@ | ||
+foo | ||
@@ -34,33 +39,4 @@ | ||
-bar | ||
diff --git a/old-file.jsx b/old-file.jsx | ||
index 57d5de75c..808d8ba37 100644 | ||
--- a/old-file.jsx | ||
+++ b/old-file.jsx | ||
@@ -1,3 +1,8 @@ | ||
+yin | ||
@@ -34,33 +39,4 @@ | ||
-yang" | ||
`); | ||
}); | ||
|
||
it('should return the right diff for a multiple file extension (OR) matcher', (): void => { | ||
const actualResult = filterDiffByFilePath( | ||
testFileDiff, | ||
/^(\.\/)*old-file\.(js|ts|jsx)$/, // Exclude files named old-file that have js, ts, or jsx extensions | ||
); | ||
|
||
expect(actualResult).toMatchInlineSnapshot(` | ||
"diff --git a/new-file.ts b/new-file.ts | ||
index 57d5de75c..808d8ba37 100644 | ||
--- a/new-file.ts | ||
+++ b/new-file.ts | ||
@@ -1,3 +1,8 @@ | ||
+foo | ||
@@ -34,33 +39,4 @@ | ||
-bar" | ||
`); | ||
}); | ||
|
||
it('should return the right diff for a file name negation matcher', (): void => { | ||
const actualResult = filterDiffByFilePath( | ||
testFileDiff, | ||
/^(?!.*old-file\.js$).*\.[a-zA-Z]+$/, // Exclude files that do not end with old-file.js but include all other file extensions | ||
); | ||
|
||
expect(actualResult).toMatchInlineSnapshot(` | ||
"diff --git a/old-file.js b/old-file.js | ||
index 57d5de75c..808d8ba37 100644 | ||
--- a/old-file.js | ||
+++ b/old-file.js | ||
@@ -1,3 +1,8 @@ | ||
+ping | ||
@@ -34,33 +39,4 @@ | ||
-pong" | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
function filterDiffByFilePath(diff: string, regex: RegExp): string { | ||
// split by `diff --git` and remove the first element which is empty | ||
const diffBlocks = diff.split(`diff --git`).slice(1); | ||
|
||
const filteredDiff = diffBlocks | ||
.map((block) => block.trim()) | ||
.filter((block) => { | ||
let shouldCheckBlock = false; | ||
|
||
block | ||
// get the first line of the block which has the paths | ||
.split('\n')[0] | ||
.trim() | ||
// split the two paths | ||
.split(' ') | ||
// remove `a/` and `b/` from the paths | ||
.map((path) => path.substring(2)) | ||
// if at least one of the two paths matches the regex, filter the | ||
// corresponding diff block in | ||
.forEach((path) => { | ||
if (!regex.test(path)) { | ||
// Not excluded, include in check | ||
shouldCheckBlock = true; | ||
} | ||
}); | ||
|
||
return shouldCheckBlock; | ||
}) | ||
// prepend `git --diff` to each block | ||
.map((block) => `diff --git ${block}`) | ||
.join('\n'); | ||
|
||
return filteredDiff; | ||
} | ||
|
||
// This function returns all lines that are additions to files that are being | ||
// modified but that previously already existed. Example: | ||
// diff --git a/test.js b/test.js | ||
// index 0000000000..872e0d8293 | ||
// --- /dev/null | ||
// +++ b/test.js | ||
// @@ -0,0 +1 @@ | ||
// +new line change to a previously existing file | ||
function filterDiffLineAdditions(diff: string): string { | ||
const diffLines = diff.split('\n'); | ||
|
||
const diffAdditionLines = diffLines.filter((line) => { | ||
let trimmedLine = line.trim(); | ||
const isAdditionLine = | ||
trimmedLine.startsWith('+') && !trimmedLine.startsWith('+++'); | ||
|
||
return isAdditionLine; | ||
}); | ||
|
||
return diffAdditionLines.join('/n'); | ||
} | ||
|
||
// This function returns all lines that are additions to new files that are being | ||
// created. Example: | ||
// diff --git a/test.js b/test.js | ||
// new file mode 100644 | ||
// index 0000000000..872e0d8293 | ||
// --- /dev/null | ||
// +++ b/test.js | ||
// @@ -0,0 +1 @@ | ||
// +new line change as the new file is created | ||
function filterDiffFileCreations(diff: string): string { | ||
// split by `diff --git` and remove the first element which is empty | ||
const diffBlocks = diff.split(`diff --git`).slice(1); | ||
|
||
const filteredDiff = diffBlocks | ||
.map((block) => block.trim()) | ||
.filter((block) => { | ||
const isFileCreationLine = | ||
block | ||
// get the second line of the block which has the file mode | ||
.split('\n')[1] | ||
.trim() | ||
.substring(0, 13) === 'new file mode'; | ||
|
||
return isFileCreationLine; | ||
}) | ||
// prepend `git --diff` to each block | ||
.map((block) => `diff --git ${block}`) | ||
.join('\n'); | ||
|
||
return filteredDiff; | ||
} | ||
|
||
function hasNumberOfCodeBlocksIncreased( | ||
diffFragment: string, | ||
codeBlocks: string[], | ||
): { [codeBlock: string]: boolean } { | ||
const diffLines = diffFragment.split('\n'); | ||
|
||
const codeBlockFound: { [codeBlock: string]: boolean } = {}; | ||
|
||
for (const codeBlock of codeBlocks) { | ||
codeBlockFound[codeBlock] = false; | ||
|
||
for (const diffLine of diffLines) { | ||
if (diffLine.includes(codeBlock)) { | ||
codeBlockFound[codeBlock] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return codeBlockFound; | ||
} | ||
|
||
export { | ||
filterDiffByFilePath, | ||
filterDiffFileCreations, | ||
filterDiffLineAdditions, | ||
hasNumberOfCodeBlocksIncreased, | ||
}; |
Oops, something went wrong.