Skip to content

Commit

Permalink
✨feature: add workflow summary (#48)
Browse files Browse the repository at this point in the history
* ✨feature: add workflow summary

* ✨feature: use local for testing

* 🤖 refactor: Apply dotnet-format changes

---------

Co-authored-by: github-actions <bot@users.noreply.github.com>
  • Loading branch information
maxisam and github-actions committed Oct 10, 2023
1 parent 980a832 commit 3350ebe
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-dotnet-format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
uses: actions/checkout@v4
- name: dotnet-format
id: dotnet-format
uses: maxisam/dotnet-format-plus@main
uses: ./
with:
authToken: ${{ secrets.GITHUB_TOKEN }}
workspace: ${{ github.workspace }}/__tests__/dotnet/Console
Expand All @@ -35,7 +35,7 @@ jobs:
uses: actions/checkout@v4
- name: dotnet-format
id: dotnet-format
uses: maxisam/dotnet-format-plus@main
uses: ./
with:
authToken: ${{ secrets.GITHUB_TOKEN }}
workspace: ${{ github.workspace }}/__tests__/dotnet/ConfigConsoleApp
Expand Down
2 changes: 1 addition & 1 deletion __tests__/dotnet/ConfigConsoleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
var test = "test";
string test2 = null;
string test2 = null;
console.log(test2);
49 changes: 35 additions & 14 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/dotnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,11 @@ export function getReportFiles(): string[] {
// check if file size is greater than 2 bytes to avoid empty report
return reportPaths.filter(p => fs.existsSync(p) && fs.statSync(p).size > 2);
}
function reportHeader(workspace: string): string {
return `## ✅ DOT NET FORMAT - ${workspace}`;
}

export function generateReport(reports: string[]): string {
export function generateReport(reports: string[], workspace: string): string {
let markdownReport = '';
for (const report of reports) {
// get file name from report path without extension
Expand All @@ -103,7 +106,7 @@ export function generateReport(reports: string[]): string {
if (!markdownReport) {
return '';
}
return `✅ Formatting succeeded\n\n ${markdownReport}`;
return `${reportHeader(workspace)}\n\n ${markdownReport}`;
}

export async function nugetRestore(nugetConfigPath: string, workspace: string): Promise<boolean> {
Expand Down
16 changes: 12 additions & 4 deletions src/duplicated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function duplicatedCheck(
const reportFiles = getReportFiles(cwd);
const markdownReport = reportFiles.find(file => file.endsWith('.md')) as string;
const jsonReport = reportFiles.find(file => file.endsWith('.json')) as string;
const message = await Comment(githubClient, markdownReport, clones);
const message = await postReport(githubClient, markdownReport, clones, workspace);
fs.writeFileSync(markdownReport, message);
await git.UploadReportToArtifacts([markdownReport, jsonReport], REPORT_ARTIFACT_NAME);
const isOverThreshold = checkThreshold(jsonReport, options.threshold || 0);
Expand Down Expand Up @@ -88,7 +88,11 @@ function showAnnotation(clones: IClone[], cwd: string, isError: boolean): void {
}
}

async function Comment(githubClient: InstanceType<typeof Octokit>, markdownReport: string, clones: IClone[]): Promise<string> {
function reportHeader(workspace: string): string {
return `## ❌ DUPLICATED CODE FOUND - ${workspace}`;
}

async function postReport(githubClient: InstanceType<typeof Octokit>, markdownReport: string, clones: IClone[], workspace: string): Promise<string> {
const report = fs.readFileSync(markdownReport, 'utf8');
const cwd = process.cwd();
let markdown = '<details>\n';
Expand All @@ -100,8 +104,12 @@ async function Comment(githubClient: InstanceType<typeof Octokit>, markdownRepor
markdown += '\n';
}
markdown += '</details>\n';
const message = `❌ DUPLICATED CODE FOUND \n\n${report}\n\n ${markdown}`;
await git.comment(githubClient, message);
let message = `${reportHeader(workspace)} \n\n${report}\n\n ${markdown}`;
await git.setSummary(message);
message += `\n\n[Workflow Runner](${git.getActionRunLink()})`;
if (context.eventName === 'pull_request') {
await git.comment(githubClient, message);
}
return message;
}

Expand Down
16 changes: 9 additions & 7 deletions src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export async function format(inputs: IInputs, githubClient: InstanceType<typeof
const reportFiles = dotnet.getReportFiles();
await git.UploadReportToArtifacts(reportFiles, REPORT_ARTIFACT_NAME);
const isDryRun = checkIsDryRun(configOptions);
if (context.eventName === 'pull_request') {
await postReportAsComment(reportFiles, githubClient);
}
const isReportRemoved = await Common.RemoveReportFiles();
const isReportPosted = await postReport(reportFiles, githubClient, inputs.workspace);
const isReportRemoved = isReportPosted && (await Common.RemoveReportFiles());
const isChanged = isReportRemoved && (await git.checkIsFileChanged());
setOutput(isDryRun, isChanged);
if (!isDryRun && isChanged && isReportRemoved && !inputs.skipCommit && context.eventName === 'pull_request') {
Expand All @@ -46,10 +44,14 @@ export async function format(inputs: IInputs, githubClient: InstanceType<typeof
return finalFormatResult;
}

async function postReportAsComment(reportFiles: string[], githubClient: InstanceType<typeof Octokit>): Promise<boolean> {
async function postReport(reportFiles: string[], githubClient: InstanceType<typeof Octokit>, workspace: string): Promise<boolean> {
if (reportFiles.length) {
const message = dotnet.generateReport(reportFiles);
return await git.comment(githubClient, message);
let message = dotnet.generateReport(reportFiles, workspace);
await git.setSummary(message);
message += `\n\n[Workflow Runner](${git.getActionRunLink()})`;
if (context.eventName === 'pull_request') {
return await git.comment(githubClient, message);
}
}
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,12 @@ export async function UploadReportToArtifacts(reports: string[], artifactName: s
core.info(`Artifact ${artifactName} uploaded successfully`);
}
}

// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
export async function setSummary(text: string): Promise<void> {
await core.summary.addRaw(text).write();
}

export function getActionRunLink(): string {
return `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
}

0 comments on commit 3350ebe

Please sign in to comment.