Skip to content

Commit

Permalink
Add Summary table under summary section (#33)
Browse files Browse the repository at this point in the history
* Added Summary plugin

* Testing temp folder generation

* Testing temp folder generation

* Testing temp folder generation

* Testing temp folder generation

* Testing temp folder generation

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Testing JSON parse

* Refactored the JSON strategy

* Refactored the JSON strategy

* Changed temp foldr for summary json

* Added unicode for status

* Handdled failure scenarios

* Refactored mathods for good readability

* Refactored mathods for good readability

* Refactored mathods for good readability

* Refactored mathods for good readability

* Added tests

* Added tests

* Added tests

* Added tests

* Added tests

* Added tests

* Added tests

* Added tests

* Added tests for json read

* Added tests for json read

* Added tests for json read

* Updated tessts

* Updated tessts

* Changed the tabe heading

* Updated as per review comments

* Updated as per review comments

* Updated as per review comments

* Updated as per review comments

* Handdled multiple try catch issue

* Handdled multiple try catch issue

* Handdled multiple try catch issue

* Updated build plugin error message

* Changed indenattion

* Handdled errors close to the functions

* Handdled errors close to the functions

* Handdled errors close to the functions

* Handdled errors close to the functions

* Updating as per review comments

* Updating as per review comments

* Updating as per review comments
  • Loading branch information
nbhoski committed Jul 2, 2024
1 parent 21cf183 commit b8ca34d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 8 deletions.
28 changes: 28 additions & 0 deletions plugins/+ciplugins/+github/BuildSummaryPlugin.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
classdef BuildSummaryPlugin < matlab.buildtool.plugins.BuildRunnerPlugin

% Copyright 2024 The MathWorks, Inc.

methods (Access=protected)

function runTaskGraph(plugin, pluginData)
runTaskGraph@matlab.buildtool.plugins.BuildRunnerPlugin(plugin, pluginData);
[fID, msg] = fopen(fullfile(getenv("RUNNER_TEMP") ,"buildSummary" + getenv("GITHUB_RUN_ID") + ".json"), "w");

if fID == -1
warning("ciplugins:github:BuildSummaryPlugin:UnableToOpenFile","Unable to open a file required to create the MATLAB build summary table: %s", msg);
else
closeFile = onCleanup(@()fclose(fID));
taskDetails = struct();
for idx = 1:numel(pluginData.TaskResults)
taskDetails(idx).name = pluginData.TaskResults(idx).Name;
taskDetails(idx).description = pluginData.TaskGraph.Tasks(idx).Description;
taskDetails(idx).failed = pluginData.TaskResults(idx).Failed;
taskDetails(idx).skipped = pluginData.TaskResults(idx).Skipped;
taskDetails(idx).duration = string(pluginData.TaskResults(idx).Duration);
end
s = jsonencode(taskDetails);
fprintf(fID, "%s",s);
end
end
end
end
1 change: 1 addition & 0 deletions plugins/+ciplugins/+github/getDefaultPlugins.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
plugins = [ ...
matlab.buildtool.internal.getFactoryDefaultPlugins(pluginProviderData) ...
ciplugins.github.GitHubLogPlugin() ...
ciplugins.github.BuildSummaryPlugin() ...
];
end
63 changes: 63 additions & 0 deletions src/buildSummary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 The MathWorks, Inc.
import * as core from "@actions/core";
import { join } from 'path';
import { readFileSync } from 'fs';

export interface Task {
name: string;
description: string;
failed: boolean;
skipped: boolean;
duration: string;
}


export function getBuildSummaryTable(tasks: Task[]): string[][] {
const header: string[] = ['MATLAB Build Task', 'Status', 'Description', 'Duration (HH:MM:SS)'];
let taskSummaryTableRows: string[][] = [header];

tasks.forEach((task, index) => {
let taskDetails: string[] = [];
taskDetails.push(task.name);
if (task.failed) {
taskDetails.push('🔴 FAILED');
} else if (task.skipped) {
taskDetails.push('🔵 SKIPPED');
} else {
taskDetails.push('🟢 SUCCESS');
}
taskDetails.push(task.description);
taskDetails.push(task.duration);

taskSummaryTableRows.push(taskDetails);
});

return taskSummaryTableRows;
}

export function writeSummary(taskSummaryTableRows: string[][]) {
try {
core.summary
.addTable(taskSummaryTableRows)
.write();
} catch (e) {
console.error('An error occurred while adding the build results table to the summary:', e);
}
}

export function processAndDisplayBuildSummary() {
const runId = process.env.GITHUB_RUN_ID || '';
const runnerTemp = process.env.RUNNER_TEMP || '';

let taskSummaryTableRows;
try {
const filePath: string = join(runnerTemp, `buildSummary${runId}.json`);
const data = JSON.parse(readFileSync(filePath, { encoding: 'utf8' }));
taskSummaryTableRows = getBuildSummaryTable(data);
} catch (e) {
console.error('An error occurred while reading the build summary file:', e);
return;
}
writeSummary(taskSummaryTableRows);
}

41 changes: 41 additions & 0 deletions src/buildSummary.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 The MathWorks, Inc.

import * as buildSummary from './buildSummary';
import * as core from '@actions/core';


jest.mock('@actions/core', () => ({
summary: {
addTable: jest.fn().mockReturnThis(),
write: jest.fn().mockReturnThis(),
},
}));

describe('summaryGeneration', () => {
it('generates a summary table correctly', () => {
const mockTasks: buildSummary.Task[] = [
{ name: 'Test Task', description: 'A test task', failed: true, skipped: false, duration: '00:00:10' }
];

const expectedTable = [
['MATLAB Build Task', 'Status', 'Description', 'Duration (HH:MM:SS)'],
['Test Task', '🔴 FAILED', 'A test task', '00:00:10'],
];

const table = buildSummary.getBuildSummaryTable(mockTasks);

expect(table).toEqual(expectedTable);
});

it('writes the summary correctly', () => {
const mockTableRows = [
['MATLAB Build Task', 'Status', 'Description', 'Duration (HH:MM:SS)'],
['Test Task', '🔴 FAILED', 'A test task', '00:00:10'],
];

buildSummary.writeSummary(mockTableRows);

expect(core.summary.addTable).toHaveBeenCalledTimes(1);
expect(core.summary.addTable).toHaveBeenCalledWith(mockTableRows);
});
});
23 changes: 15 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import * as core from "@actions/core";
import * as exec from "@actions/exec";
import { matlab } from "run-matlab-command-action";
import * as buildtool from "./buildtool";
import * as buildSummary from "./buildSummary";

/**
* Gather action inputs and then run action.
*/
async function run() {

const platform = process.platform;
const architecture = process.arch;
const workspaceDir = process.cwd();
Expand All @@ -22,20 +24,25 @@ async function run() {
const startupOptions = core.getInput("startup-options").split(" ");

const helperScript = await matlab.generateScript(workspaceDir, command);
const execOptions = { env: {
...process.env,
"MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE":"ciplugins.github.getDefaultPlugins",
}};
const execOptions = {
env: {
...process.env,
"MW_MATLAB_BUILDTOOL_DEFAULT_PLUGINS_FCN_OVERRIDE": "ciplugins.github.getDefaultPlugins",
}
};

await matlab.runCommand(
helperScript,
platform,
architecture,
(cmd,args)=>exec.exec(cmd,args,execOptions),
(cmd, args) => exec.exec(cmd, args, execOptions),
startupOptions
);
).finally(() => {
buildSummary.processAndDisplayBuildSummary();
});

}

run().catch((e) => {
run().catch(e => {
core.setFailed(e);
});
});

0 comments on commit b8ca34d

Please sign in to comment.