-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@knapsack-pro/core": minor | ||
--- | ||
|
||
Send authors to the API |
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
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,10 +1,15 @@ | ||
import { KnapsackProEnvConfig } from '../src/config/knapsack-pro-env.config'; | ||
import { | ||
KnapsackProEnvConfig, | ||
buildAuthor, | ||
commitAuthors, | ||
} from '../src/config/knapsack-pro-env.config'; | ||
import { KnapsackProLogger } from '../src/knapsack-pro-logger'; | ||
import * as Urls from '../src/urls'; | ||
|
||
describe('KnapsackProEnvConfig', () => { | ||
// Since we use CircleCI for testing, we prevent it from interfering with the tests. | ||
delete process.env.CIRCLECI; | ||
delete process.env.CI; | ||
const ENV = { ...process.env }; | ||
|
||
afterEach(() => { | ||
|
@@ -296,4 +301,85 @@ describe('KnapsackProEnvConfig', () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe('.maskedUserSeat', () => { | ||
describe('when KNAPSACK_PRO_USER_SEAT is set', () => { | ||
it('it returns the masked KNAPSACK_PRO_USER_SEAT', () => { | ||
process.env.KNAPSACK_PRO_USER_SEAT = 'riccardo'; | ||
|
||
expect(KnapsackProEnvConfig.maskedUserSeat).toEqual('ri******'); | ||
}); | ||
}); | ||
|
||
describe('when CI is CircleCI', () => { | ||
it('it returns the masked CIRCLE_USERNAME', () => { | ||
process.env.CIRCLECI = 'whatever'; | ||
process.env.CIRCLE_USERNAME = 'riccardo'; | ||
|
||
expect(KnapsackProEnvConfig.maskedUserSeat).toEqual('ri******'); | ||
}); | ||
}); | ||
|
||
describe('when both KNAPSACK_PRO_USER_SEAT is set and CI is CircleCI', () => { | ||
it('it returns the masked KNAPSACK_PRO_USER_SEAT', () => { | ||
process.env.KNAPSACK_PRO_USER_SEAT = 'jane'; | ||
process.env.CIRCLECI = 'whatever'; | ||
process.env.CIRCLE_USERNAME = 'riccardo'; | ||
|
||
expect(KnapsackProEnvConfig.maskedUserSeat).toEqual('ja**'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('buildAuthor', () => { | ||
it('returns the masked build author', () => { | ||
const actual = buildAuthor(() => | ||
Buffer.from('John Doe <[email protected]>\n'), | ||
); | ||
|
||
expect(actual).toEqual('Jo** Do* <jo**.do*@ex*****.co*>'); | ||
}); | ||
|
||
describe('when the command raises an exception', () => { | ||
it('returns the no-git author', () => { | ||
const actual = buildAuthor(() => { | ||
throw new Error(); | ||
}); | ||
|
||
expect(actual).toEqual('no git <[email protected]>'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('commitAuthors', () => { | ||
it('returns the masked commit authors', () => { | ||
const actual = commitAuthors(() => | ||
Buffer.from( | ||
[ | ||
' 5\t3v0k4 <[email protected]>\n', | ||
' 10\tArtur Nowak <[email protected]>\n', | ||
' 2\tRiccardo <[email protected]>\n', | ||
' 3 \tshadre <[email protected]>\n', | ||
].join(''), | ||
), | ||
); | ||
|
||
expect(actual).toEqual([ | ||
{ commits: 5, author: '3v0*4 <ri******@ex*****.co*>' }, | ||
{ commits: 10, author: 'Ar*** No*** <ar***@ex*****.co*>' }, | ||
{ commits: 2, author: 'Ri****** <ri******@ex*****.co*>' }, | ||
{ commits: 3, author: 'sh**** <sh***@ex*****.co*>' }, | ||
]); | ||
}); | ||
|
||
describe('when the command raises an exception', () => { | ||
it('returns []', () => { | ||
const actual = commitAuthors(() => { | ||
throw new Error(); | ||
}); | ||
|
||
expect(actual).toEqual([]); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
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,9 +1,9 @@ | ||
import childProcess = require('child_process'); | ||
import { CIEnvConfig } from '.'; | ||
import { CIEnvConfig, isCI } from '.'; | ||
import { KnapsackProLogger } from '../knapsack-pro-logger'; | ||
import * as Urls from '../urls'; | ||
|
||
const { spawnSync } = childProcess; | ||
const { spawnSync, execSync } = childProcess; | ||
|
||
function logLevel(): string { | ||
if (process.env.KNAPSACK_PRO_LOG_LEVEL) { | ||
|
@@ -15,6 +15,12 @@ function logLevel(): string { | |
|
||
const knapsackProLogger = new KnapsackProLogger(logLevel()); | ||
|
||
const mask = (string: string): string => { | ||
const regexp = /(?<=\w{2})[a-zA-Z]/g; | ||
const maskingChar = '*'; | ||
return string.replace(regexp, maskingChar); | ||
}; | ||
|
||
export class KnapsackProEnvConfig { | ||
private static $fixedQueueSplit: boolean | undefined; | ||
|
||
|
@@ -211,18 +217,63 @@ export class KnapsackProEnvConfig { | |
} | ||
|
||
public static get maskedUserSeat(): string | void { | ||
const regexp = /(?<=\w{2})[a-zA-Z]/g; | ||
const maskingChar = '*'; | ||
|
||
if (process.env.KNAPSACK_PRO_USER_SEAT) { | ||
return process.env.KNAPSACK_PRO_USER_SEAT.replace(regexp, maskingChar); | ||
return mask(process.env.KNAPSACK_PRO_USER_SEAT); | ||
} | ||
|
||
const { userSeat } = CIEnvConfig; | ||
if (userSeat) { | ||
userSeat.replace(regexp, maskingChar); | ||
return mask(userSeat); | ||
} | ||
|
||
return undefined; | ||
} | ||
} | ||
|
||
const $buildAuthor = (command: () => Buffer): string => { | ||
try { | ||
const author = command().toString().trim(); | ||
return mask(author); | ||
} catch (error) { | ||
return 'no git <[email protected]>'; | ||
} | ||
}; | ||
|
||
const gitBuildAuthor = () => | ||
execSync('git log --format="%aN <%aE>" -1 2>/dev/null'); | ||
|
||
export const buildAuthor = (command = gitBuildAuthor): string => | ||
$buildAuthor(command); | ||
|
||
const $commitAuthors = ( | ||
command: () => Buffer, | ||
): { commits: number; author: string }[] => { | ||
try { | ||
return command() | ||
.toString() | ||
.split('\n') | ||
.filter((line) => line !== '') | ||
.map((line) => line.trim()) | ||
.map((line) => line.split('\t')) | ||
.map(([commits, author]) => ({ | ||
commits: parseInt(commits, 10), | ||
author: mask(author), | ||
})); | ||
} catch (error) { | ||
return []; | ||
} | ||
}; | ||
|
||
const gitCommitAuthors = () => { | ||
if (isCI) { | ||
execSync(`git fetch --shallow-since "one month ago" --quiet 2>/dev/null`); | ||
} | ||
|
||
return execSync( | ||
`git log --since "one month ago" 2>/dev/null | git shortlog --summary --email 2>/dev/null`, | ||
); | ||
}; | ||
|
||
export const commitAuthors = ( | ||
command = gitCommitAuthors, | ||
): { commits: number; author: string }[] => $commitAuthors(command); |
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