Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): update git latest commit functionality #205

Merged
merged 6 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/core/src/lib/implementation/persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { join } from 'path';
import { CoreConfig, Report } from '@code-pushup/models';
import {
formatBytes,
getLatestCommit,
reportToMd,
reportToStdout,
scoreReport,
Expand Down Expand Up @@ -47,7 +48,14 @@ export async function persistReport(

if (format.includes('md')) {
scoredReport = scoredReport || scoreReport(report);
results.push({ format: 'md', content: reportToMd(scoredReport) });
const commitData = await getLatestCommit();
if (!commitData) {
console.warn('no commit data available');
}
results.push({
format: 'md',
content: reportToMd(scoredReport, commitData),
});
}

if (!existsSync(outputDir)) {
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/lib/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { readFileSync } from 'fs';
import { join } from 'path';
import { uploadToPortal } from '@code-pushup/portal-client';
import { CoreConfig, reportSchema } from '@code-pushup/models';
import { latestHash } from '@code-pushup/utils';
import { getLatestCommit } from '@code-pushup/utils';
import { jsonToGql } from './implementation/json-to-gql';

export type UploadOptions = Pick<CoreConfig, 'upload' | 'persist'>;
Expand All @@ -24,11 +24,16 @@ export async function upload(
const report = reportSchema.parse(
JSON.parse(readFileSync(join(outputDir, 'report.json')).toString()),
);
const commitData = await getLatestCommit();

if (!commitData) {
throw new Error('no commit data available');
}

const data = {
organization,
project,
commit: await latestHash(),
commit: commitData.hash,
...jsonToGql(report),
};

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
executeProcess,
objectToCliArgs,
} from './lib/execute-process';
export { git, latestHash } from './lib/git';
export { git, getLatestCommit } from './lib/git';
export { importEsmModule } from './lib/load-file';
export { ProgressBar, getProgressBar } from './lib/progress';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Report was created by [Code PushUp](https://github.com/flowup/quality-metrics-cl

|Commit|Version|Duration|Plugins|Categories|Audits|
|:--|:--:|:--:|:--:|:--:|:--:|
|_Implement todos list_ ([3ac01d1](https://github.com/flowup/todos-app/commit/3ac01d192698e0a923bd410f79594371480a6e4c))|\`0.0.1\`|1.65 s|2|3|52|
|refactor(cli): fix exec target (41682a2)|\`0.0.1\`|1.65 s|2|3|52|

The following plugins were run:

Expand Down
21 changes: 16 additions & 5 deletions packages/utils/src/lib/git.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { expect } from 'vitest';
import { latestHash } from './git';
import { CommitData, getLatestCommit } from './git';

const gitCommitDateRegex =
/^(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}$/;

describe('git', () => {
it('should log current hash', async () => {
const hash = await latestHash();
expect(hash).toHaveLength(40);
expect(hash).toMatch(/^[0-9a-f]+$/);
it('should log latest commit', async () => {
const commit: CommitData | null = await getLatestCommit();
expect(commit).not.toBeNull();
expect(commit).toHaveProperty('hash');
expect(commit?.hash).toHaveLength(40);
expect(commit?.hash).toMatch(/^[0-9a-f]+$/);
expect(commit).toHaveProperty('message');
expect(commit?.message).not.toHaveLength(0);
expect(commit).toHaveProperty('author');
expect(commit?.author).not.toHaveLength(0);
expect(commit).toHaveProperty('date');
expect(commit?.date).toMatch(gitCommitDateRegex);
});
});
21 changes: 14 additions & 7 deletions packages/utils/src/lib/git.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import simpleGit from 'simple-git';

export type CommitData = {
hash: string;
message: string;
author: string;
date: string;
};

export const git = simpleGit();

export async function latestHash() {
// git log -1 --pretty=format:"%H" // logs hash e.g. 41682a2fec1d4ece81c696a26c08984baeb4bcf3
const log = await git.log({ maxCount: 1, format: { hash: '%H' } });
if (!log?.latest?.hash) {
throw new Error('no latest hash present in git history.');
}
return log?.latest?.hash;
export async function getLatestCommit() {
// git log -1 --pretty=format:"%H %s %an %ad" // logs hash, message, author, date
const log = await git.log({
maxCount: 1,
format: { hash: '%H', message: '%s', author: '%an', date: '%ad' },
});
return log?.latest;
}
11 changes: 10 additions & 1 deletion packages/utils/src/lib/report-to-md.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ describe('report-to-md', () => {
it('should contain all sections when using the fixture report', () => {
const date = new Date(2000, 0, 1, 0);
vi.setSystemTime(date);
const mdReport = reportToMd(scoreReport(report()));
const commit = {
hash: '41682a2fec1d4ece81c696a26c08984baeb4bcf3',
message: 'refactor(cli): fix exec target',
author: 'BioPhoton',
date: 'Sat Sep 10 12:00:00 2021 +0200',
};
const mdReport = reportToMd(scoreReport(report()), commit);
expect(mdReport).toContain(
`${commit.message} (${commit.hash.slice(0, 7)})`,
);
expect(mdReport).toMatchSnapshot();
});
});
21 changes: 14 additions & 7 deletions packages/utils/src/lib/report-to-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
Issue,
PluginReport,
} from '@code-pushup/models';
import { CommitData } from './git';
import {
NEW_LINE,
details,
Expand Down Expand Up @@ -36,7 +37,10 @@ import {
reportOverviewTableHeaders,
} from './utils';

export function reportToMd(report: ScoredReport): string {
export function reportToMd(
report: ScoredReport,
commitData: CommitData | null,
): string {
// header section
let md = reportToHeaderSection() + NEW_LINE;

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

// about section
md += reportToAboutSection(report) + NEW_LINE + NEW_LINE;
md += reportToAboutSection(report, commitData) + NEW_LINE + NEW_LINE;

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

function reportToAboutSection(report: ScoredReport): string {
function reportToAboutSection(
report: ScoredReport,
commitData: CommitData | null,
): string {
const date = new Date().toString();
const { duration, version, plugins, categories } = report;
// TODO: replace mock commitData with real data, ticket #192
const commitData =
'_Implement todos list_ ([3ac01d1](https://github.com/flowup/todos-app/commit/3ac01d192698e0a923bd410f79594371480a6e4c))';
const commitInfo = commitData
? `${commitData.message} (${commitData.hash.slice(0, 7)})`
: 'N/A';
const reportMetaTable: string[][] = [
reportMetaTableHeaders,
[
commitData,
commitInfo,
style(version || '', ['c']),
formatDuration(duration),
plugins.length.toString(),
Expand Down