Skip to content

Commit 7ec4582

Browse files
feat(utils): update git latest commit functionality (#205)
Expanded the git commit data fetching, substituted the method `latestHash()` with `getLatestCommit()`. Closes #192
1 parent 213a4b8 commit 7ec4582

File tree

8 files changed

+72
-25
lines changed

8 files changed

+72
-25
lines changed

packages/core/src/lib/implementation/persist.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { join } from 'path';
55
import { CoreConfig, Report } from '@code-pushup/models';
66
import {
77
formatBytes,
8+
getLatestCommit,
89
reportToMd,
910
reportToStdout,
1011
scoreReport,
@@ -47,7 +48,14 @@ export async function persistReport(
4748

4849
if (format.includes('md')) {
4950
scoredReport = scoredReport || scoreReport(report);
50-
results.push({ format: 'md', content: reportToMd(scoredReport) });
51+
const commitData = await getLatestCommit();
52+
if (!commitData) {
53+
console.warn('no commit data available');
54+
}
55+
results.push({
56+
format: 'md',
57+
content: reportToMd(scoredReport, commitData),
58+
});
5159
}
5260

5361
if (!existsSync(outputDir)) {

packages/core/src/lib/upload.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
22
import { join } from 'path';
33
import { uploadToPortal } from '@code-pushup/portal-client';
44
import { CoreConfig, reportSchema } from '@code-pushup/models';
5-
import { latestHash } from '@code-pushup/utils';
5+
import { getLatestCommit } from '@code-pushup/utils';
66
import { jsonToGql } from './implementation/json-to-gql';
77

88
export type UploadOptions = Pick<CoreConfig, 'upload' | 'persist'>;
@@ -24,11 +24,16 @@ export async function upload(
2424
const report = reportSchema.parse(
2525
JSON.parse(readFileSync(join(outputDir, 'report.json')).toString()),
2626
);
27+
const commitData = await getLatestCommit();
28+
29+
if (!commitData) {
30+
throw new Error('no commit data available');
31+
}
2732

2833
const data = {
2934
organization,
3035
project,
31-
commit: await latestHash(),
36+
commit: commitData.hash,
3237
...jsonToGql(report),
3338
};
3439

packages/utils/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export {
77
executeProcess,
88
objectToCliArgs,
99
} from './lib/execute-process';
10-
export { git, latestHash } from './lib/git';
10+
export { git, getLatestCommit } from './lib/git';
1111
export { importEsmModule } from './lib/load-file';
1212
export { ProgressBar, getProgressBar } from './lib/progress';
1313
export {

packages/utils/src/lib/__snapshots__/report-to-md.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cl
427427
428428
|Commit|Version|Duration|Plugins|Categories|Audits|
429429
|:--|:--:|:--:|:--:|:--:|:--:|
430-
|_Implement todos list_ ([3ac01d1](https://github.com/flowup/todos-app/commit/3ac01d192698e0a923bd410f79594371480a6e4c))|\`0.0.1\`|1.65 s|2|3|52|
430+
|refactor(cli): fix exec target (41682a2)|\`0.0.1\`|1.65 s|2|3|52|
431431
432432
The following plugins were run:
433433

packages/utils/src/lib/git.spec.ts

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { expect } from 'vitest';
2-
import { latestHash } from './git';
2+
import { CommitData, getLatestCommit } from './git';
3+
4+
const gitCommitDateRegex =
5+
/^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{2}:\d{2}:\d{2} \d{4} [+|-]\d{4}$/;
36

47
describe('git', () => {
5-
it('should log current hash', async () => {
6-
const hash = await latestHash();
7-
expect(hash).toHaveLength(40);
8-
expect(hash).toMatch(/^[0-9a-f]+$/);
8+
it('should log latest commit', async () => {
9+
const commit: CommitData | null = await getLatestCommit();
10+
expect(commit).not.toBeNull();
11+
expect(commit).toHaveProperty('hash');
12+
expect(commit?.hash).toHaveLength(40);
13+
expect(commit?.hash).toMatch(/^[0-9a-f]+$/);
14+
expect(commit).toHaveProperty('message');
15+
expect(commit?.message).not.toHaveLength(0);
16+
expect(commit).toHaveProperty('author');
17+
expect(commit?.author).not.toHaveLength(0);
18+
expect(commit).toHaveProperty('date');
19+
expect(commit?.date).toMatch(gitCommitDateRegex);
920
});
1021
});

packages/utils/src/lib/git.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import simpleGit from 'simple-git';
22

3+
export type CommitData = {
4+
hash: string;
5+
message: string;
6+
author: string;
7+
date: string;
8+
};
9+
310
export const git = simpleGit();
411

5-
export async function latestHash() {
6-
// git log -1 --pretty=format:"%H" // logs hash e.g. 41682a2fec1d4ece81c696a26c08984baeb4bcf3
7-
const log = await git.log({ maxCount: 1, format: { hash: '%H' } });
8-
if (!log?.latest?.hash) {
9-
throw new Error('no latest hash present in git history.');
10-
}
11-
return log?.latest?.hash;
12+
export async function getLatestCommit() {
13+
// git log -1 --pretty=format:"%H %s %an %ad" // logs hash, message, author, date
14+
const log = await git.log({
15+
maxCount: 1,
16+
format: { hash: '%H', message: '%s', author: '%an', date: '%ad' },
17+
});
18+
return log?.latest;
1219
}

packages/utils/src/lib/report-to-md.spec.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ describe('report-to-md', () => {
1515
it('should contain all sections when using the fixture report', () => {
1616
const date = new Date(2000, 0, 1, 0);
1717
vi.setSystemTime(date);
18-
const mdReport = reportToMd(scoreReport(report()));
18+
const commit = {
19+
hash: '41682a2fec1d4ece81c696a26c08984baeb4bcf3',
20+
message: 'refactor(cli): fix exec target',
21+
author: 'BioPhoton',
22+
date: 'Sat Sep 10 12:00:00 2021 +0200',
23+
};
24+
const mdReport = reportToMd(scoreReport(report()), commit);
25+
expect(mdReport).toContain(
26+
`${commit.message} (${commit.hash.slice(0, 7)})`,
27+
);
1928
expect(mdReport).toMatchSnapshot();
2029
});
2130
});

packages/utils/src/lib/report-to-md.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Issue,
55
PluginReport,
66
} from '@code-pushup/models';
7+
import { CommitData } from './git';
78
import {
89
NEW_LINE,
910
details,
@@ -36,7 +37,10 @@ import {
3637
reportOverviewTableHeaders,
3738
} from './utils';
3839

39-
export function reportToMd(report: ScoredReport): string {
40+
export function reportToMd(
41+
report: ScoredReport,
42+
commitData: CommitData | null,
43+
): string {
4044
// header section
4145
let md = reportToHeaderSection() + NEW_LINE;
4246

@@ -50,7 +54,7 @@ export function reportToMd(report: ScoredReport): string {
5054
md += reportToAuditsSection(report) + NEW_LINE + NEW_LINE;
5155

5256
// about section
53-
md += reportToAboutSection(report) + NEW_LINE + NEW_LINE;
57+
md += reportToAboutSection(report, commitData) + NEW_LINE + NEW_LINE;
5458

5559
// footer section
5660
md += `${FOOTER_PREFIX} ${link(README_LINK, 'Code PushUp')}`;
@@ -248,16 +252,19 @@ function reportToAuditsSection(report: ScoredReport): string {
248252
return h2('🛡️ Audits') + NEW_LINE + NEW_LINE + auditsData;
249253
}
250254

251-
function reportToAboutSection(report: ScoredReport): string {
255+
function reportToAboutSection(
256+
report: ScoredReport,
257+
commitData: CommitData | null,
258+
): string {
252259
const date = new Date().toString();
253260
const { duration, version, plugins, categories } = report;
254-
// TODO: replace mock commitData with real data, ticket #192
255-
const commitData =
256-
'_Implement todos list_ ([3ac01d1](https://github.com/flowup/todos-app/commit/3ac01d192698e0a923bd410f79594371480a6e4c))';
261+
const commitInfo = commitData
262+
? `${commitData.message} (${commitData.hash.slice(0, 7)})`
263+
: 'N/A';
257264
const reportMetaTable: string[][] = [
258265
reportMetaTableHeaders,
259266
[
260-
commitData,
267+
commitInfo,
261268
style(version || '', ['c']),
262269
formatDuration(duration),
263270
plugins.length.toString(),

0 commit comments

Comments
 (0)