Skip to content

Commit

Permalink
add include-file-comments input
Browse files Browse the repository at this point in the history
  • Loading branch information
YajJackson committed Mar 20, 2024
1 parent 20ed19b commit 9707441
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
6 changes: 6 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ inputs:
description: "GitHub Token"
required: true

# Optional
include-file-comments:
description: "Include file comments in the output."
required: false
default: "true"

runs:
using: "node20"
main: "build/index.js"
30 changes: 20 additions & 10 deletions build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18737,7 +18737,7 @@ var require_core = __commonJS({
return inputs.map((input) => input.trim());
}
exports2.getMultilineInput = getMultilineInput;
function getBooleanInput(name, options) {
function getBooleanInput2(name, options) {
const trueValue = ["true", "True", "TRUE"];
const falseValue = ["false", "False", "FALSE"];
const val = getInput2(name, options);
Expand All @@ -18748,7 +18748,7 @@ var require_core = __commonJS({
throw new TypeError(`Input does not meet YAML 1.2 "Core Schema" specification: ${name}
Support boolean input list: \`true | True | TRUE | false | False | FALSE\``);
}
exports2.getBooleanInput = getBooleanInput;
exports2.getBooleanInput = getBooleanInput2;
function setOutput(name, value) {
const filePath = process.env["GITHUB_OUTPUT"] || "";
if (filePath) {
Expand Down Expand Up @@ -26962,14 +26962,14 @@ var require_commonjs = __commonJS({
* Find a value for which the supplied fn method returns a truthy value,
* similar to Array.find(). fn is called as fn(value, key, cache).
*/
find(fn, getOptions = {}) {
find(fn, getOptions2 = {}) {
for (const i of this.#indexes()) {
const v = this.#valList[i];
const value = this.#isBackgroundFetch(v) ? v.__staleWhileFetching : v;
if (value === void 0)
continue;
if (fn(value, this.#keyList[i], this)) {
return this.get(this.#keyList[i], getOptions);
return this.get(this.#keyList[i], getOptions2);
}
}
}
Expand Down Expand Up @@ -27489,8 +27489,8 @@ var require_commonjs = __commonJS({
*
* If the key is not found, get() will return `undefined`.
*/
get(k, getOptions = {}) {
const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status } = getOptions;
get(k, getOptions2 = {}) {
const { allowStale = this.allowStale, updateAgeOnGet = this.updateAgeOnGet, noDeleteOnStaleGet = this.noDeleteOnStaleGet, status } = getOptions2;
const index = this.#keyMap.get(k);
if (index !== void 0) {
const value = this.#valList[index];
Expand Down Expand Up @@ -31461,14 +31461,15 @@ async function run() {
runInfo,
pullRequestData
);
core.info("pythonFiles: " + JSON.stringify(pythonFiles));
if (pythonFiles.length === 0) {
core.info("No Python files have changed.");
return;
}
await installPyright();
const pyrightReport = await runPyright(pythonFiles);
await commentOnPR(runInfo, pyrightReport, pullRequestData);
if (runInfo.options.includeFileComments)
await addFileComments(runInfo, pyrightReport, pullRequestData);
await addSummaryComment(runInfo, pyrightReport, pullRequestData);
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);
}
Expand All @@ -31477,7 +31478,12 @@ var getRunInfo = () => {
const token = core.getInput("github-token", { required: true });
const octokit = new Octokit2({ auth: token });
const context2 = github.context;
return { token, octokit, context: context2 };
const options = getOptions();
return { token, octokit, context: context2, options };
};
var getOptions = () => {
const includeFileComments = core.getBooleanInput("include-file-comments") ?? true;
return { includeFileComments };
};
async function getChangedPythonFiles(runInfo, pullRequest) {
const { octokit, context: context2 } = runInfo;
Expand Down Expand Up @@ -31515,7 +31521,8 @@ async function runPyright(files) {
await (0, import_exec.exec)(pyrightCommand, [], options);
return parseReport(JSON.parse(output));
}
async function commentOnPR(runInfo, report, pullRequest) {
async function addFileComments(runInfo, report, pullRequest) {
core.info("Generating file comments.");
const { octokit, context: context2 } = runInfo;
const diagnostics = report.generalDiagnostics;
const { data: existingReviewComments } = await octokit.rest.pulls.listReviewComments({
Expand Down Expand Up @@ -31608,7 +31615,10 @@ async function commentOnPR(runInfo, report, pullRequest) {
comment_id: comment.id
});
}
}
async function addSummaryComment(runInfo, report, pullRequest) {
core.info("Generating summary.");
const { octokit, context: context2 } = runInfo;
let summary = `## Pyright Summary
**\u{1F4DD} Files Analyzed**: ${report.summary.filesAnalyzed}
`;
Expand Down
28 changes: 24 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ export async function run() {
runInfo,
pullRequestData,
);
core.info("pythonFiles: " + JSON.stringify(pythonFiles));
if (pythonFiles.length === 0) {
core.info("No Python files have changed.");
return;
}

await installPyright();
const pyrightReport = await runPyright(pythonFiles);
await commentOnPR(runInfo, pyrightReport, pullRequestData);

if (runInfo.options.includeFileComments)
await addFileComments(runInfo, pyrightReport, pullRequestData);
await addSummaryComment(runInfo, pyrightReport, pullRequestData);
} catch (error) {
core.setFailed(`Action failed with error: ${error}`);
}
Expand All @@ -38,7 +40,14 @@ const getRunInfo = () => {
const token = core.getInput("github-token", { required: true });
const octokit = new Octokit({ auth: token });
const context = github.context;
return { token, octokit, context };
const options = getOptions();
return { token, octokit, context, options };
};

const getOptions = () => {
const includeFileComments =
core.getBooleanInput("include-file-comments") ?? true;
return { includeFileComments };
};

async function getChangedPythonFiles(
Expand Down Expand Up @@ -93,11 +102,13 @@ async function runPyright(files: string[]): Promise<Report> {
return parseReport(JSON.parse(output));
}

async function commentOnPR(
async function addFileComments(
runInfo: ReturnType<typeof getRunInfo>,
report: Report,
pullRequest: Awaited<ReturnType<typeof getPullRequestData>>,
) {
core.info("Generating file comments.");

const { octokit, context } = runInfo;

const diagnostics = report.generalDiagnostics;
Expand Down Expand Up @@ -203,8 +214,17 @@ async function commentOnPR(
comment_id: comment.id,
});
}
}

async function addSummaryComment(
runInfo: ReturnType<typeof getRunInfo>,
report: Report,
pullRequest: Awaited<ReturnType<typeof getPullRequestData>>,
) {
core.info("Generating summary.");

const { octokit, context } = runInfo;

let summary =
`## Pyright Summary \n` +
`**📝 Files Analyzed**: ${report.summary.filesAnalyzed}\n`;
Expand Down
13 changes: 0 additions & 13 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,3 @@ export const parseSummaryCommentKey = (input: string) => {
const match = regex.exec(input);
return match ? match[1] : null;
};

// const exampleCommentBody = `### Pyright Issues
//
// - main.py:10:14 - ❌ error: Expression of type \`int\` cannot be assigned to declared type \`str\`
// \`int\` is incompatible with \`str\` (reportAssignmentType)
// - main.py:11:24 - ❌ error: Expression of type \`int\` cannot be assigned to declared type \`str\`
// \`int\` is incompatible with \`str\` (reportAssignmentType)
//
//
// ###### [diagnostic-key:89be8cb1cee8ef6a]`;
//
// const commentKey = parseCommentKey(exampleCommentBody);
// console.log({ commentKey });

0 comments on commit 9707441

Please sign in to comment.