Skip to content

Commit

Permalink
🚸 Add tool name to the summary, fix reactions
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin committed Jun 17, 2023
1 parent 810020a commit 9ee2bde
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ jobs:
with:
args: --print-problems,--log-level,debug
pr-mode: false
use-annotations: false
cache-only-default-branch: true

# Not possible at the moment for GitHub-hosted Windows agents: https://github.com/JetBrains/qodana-action/pull/31#issue-812728409
# test-windows:
Expand Down
50 changes: 19 additions & 31 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63941,7 +63941,7 @@ var require_utils8 = __commonJS({
return mod && mod.__esModule ? mod : { "default": mod };
};
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.putReaction = exports2.updateComment = exports2.createComment = exports2.findCommentByTag = exports2.getFirstCommentId = exports2.getWorkflowRunUrl = exports2.getProblemPlural = exports2.isNeedToUploadCache = exports2.isPRMode = exports2.isServer = exports2.restoreCaches = exports2.uploadCaches = exports2.uploadReport = exports2.prepareAgent = exports2.qodana = exports2.getInputs = exports2.ANALYSIS_STARTED_REACTION = exports2.ANALYSIS_FINISHED_REACTION = void 0;
exports2.putReaction = exports2.updateComment = exports2.createComment = exports2.findCommentByTag = exports2.getWorkflowRunUrl = exports2.getProblemPlural = exports2.isNeedToUploadCache = exports2.isPRMode = exports2.isServer = exports2.restoreCaches = exports2.uploadCaches = exports2.uploadReport = exports2.prepareAgent = exports2.qodana = exports2.getInputs = exports2.ANALYSIS_STARTED_REACTION = exports2.ANALYSIS_FINISHED_REACTION = void 0;
var artifact = __importStar2(require_artifact_client2());
var cache = __importStar2(require_cache());
var core2 = __importStar2(require_core());
Expand Down Expand Up @@ -64129,20 +64129,6 @@ var require_utils8 = __commonJS({
}
__name(getWorkflowRunUrl, "getWorkflowRunUrl");
exports2.getWorkflowRunUrl = getWorkflowRunUrl;
function getFirstCommentId() {
return __awaiter2(this, void 0, void 0, function* () {
const client = github.getOctokit(getInputs().githubToken);
try {
const { data: comments } = yield client.rest.issues.listComments(Object.assign(Object.assign({}, github.context.repo), { issue_number: github.context.issue.number }));
return comments[0].id;
} catch (error) {
core2.debug(`Failed to get first comment id \u2013 ${error.message}`);
return -1;
}
});
}
__name(getFirstCommentId, "getFirstCommentId");
exports2.getFirstCommentId = getFirstCommentId;
function findCommentByTag(tag) {
return __awaiter2(this, void 0, void 0, function* () {
const client = github.getOctokit(getInputs().githubToken);
Expand Down Expand Up @@ -64195,24 +64181,26 @@ var require_utils8 = __commonJS({
}
__name(updateComment, "updateComment");
exports2.updateComment = updateComment;
function putReaction(comment_id, newReaction, oldReaction) {
function putReaction(newReaction, oldReaction) {
var _a;
return __awaiter2(this, void 0, void 0, function* () {
const client = github.getOctokit(getInputs().githubToken);
const issue_number = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number;
if (oldReaction !== "") {
try {
const { data: reactions } = yield client.rest.reactions.listForPullRequestReviewComment(Object.assign(Object.assign({}, github.context.repo), { comment_id }));
const { data: reactions } = yield client.rest.reactions.listForIssue(Object.assign(Object.assign({}, github.context.repo), { issue_number }));
const previousReaction = reactions.find((r) => r.content === oldReaction);
if (previousReaction) {
yield client.rest.reactions.deleteForPullRequestComment(Object.assign(Object.assign({}, github.context.repo), { comment_id, reaction_id: previousReaction.id }));
yield client.rest.reactions.deleteForIssue(Object.assign(Object.assign({}, github.context.repo), { issue_number, reaction_id: previousReaction.id }));
}
} catch (error) {
core2.debug(`Failed to delete previous reaction \u2013 ${error.message}`);
core2.warning(`Failed to delete the initial reaction \u2013 ${error.message}`);
}
}
try {
yield client.rest.reactions.createForPullRequestReviewComment(Object.assign(Object.assign({}, github.context.repo), { comment_id, content: newReaction }));
yield client.rest.reactions.createForIssue(Object.assign(Object.assign({}, github.context.repo), { issue_number, content: newReaction }));
} catch (error) {
core2.debug(`Failed to put reaction \u2013 ${error.message}`);
core2.warning(`Failed to set reaction \u2013 ${error.message}`);
}
});
}
Expand Down Expand Up @@ -64441,7 +64429,7 @@ var require_annotations = __commonJS({
yield publishGitHubCheck(failedByThreshold, name, token, problems);
}
} catch (error) {
core2.warning(`Failed to publish annotations \u2013 ${error.message}`);
core2.debug(`Failed to publish annotations \u2013 ${error.message}`);
}
});
}
Expand Down Expand Up @@ -64562,7 +64550,7 @@ ${body}
return Array.from(problems.entries()).sort((a, b) => b[1] - a[1]).map(([title, count]) => `| ${title} | ${level} | ${count} |`).join("\n");
}
__name(getRowsByLevel, "getRowsByLevel");
function getSummary(annotations, licensesInfo, reportUrl) {
function getSummary(toolName, annotations, licensesInfo, reportUrl) {
const contactBlock = wrapToToggleBlock("Contact Qodana team", SUMMARY_MISC);
let licensesBlock = "";
if (licensesInfo !== "") {
Expand All @@ -64574,7 +64562,7 @@ ${body}
}
if (annotations.length === 0) {
return [
`# ${QODANA_CHECK_NAME}`,
`# ${toolName}`,
"",
"**It seems all right \u{1F44C}**",
"",
Expand All @@ -64586,7 +64574,7 @@ ${body}
].join("\n");
}
return [
`# ${QODANA_CHECK_NAME}`,
`# ${toolName}`,
"",
`**${annotations.length} ${(0, utils_12.getProblemPlural)(annotations.length)}** found`,
"",
Expand Down Expand Up @@ -64630,10 +64618,10 @@ ${body}
}
const annotations = (_a = problems.annotations) !== null && _a !== void 0 ? _a : [];
const toolName = (_b = problems.title.split("found by ")[1]) !== null && _b !== void 0 ? _b : QODANA_CHECK_NAME;
problems.summary = getSummary(annotations, licensesInfo, reportUrl);
problems.summary = getSummary(toolName, annotations, licensesInfo, reportUrl);
yield Promise.all([
(0, utils_12.putReaction)(yield (0, utils_12.getFirstCommentId)(), utils_12.ANALYSIS_FINISHED_REACTION, utils_12.ANALYSIS_STARTED_REACTION),
postResultsToPRComments(problems.summary, postComment),
(0, utils_12.putReaction)(utils_12.ANALYSIS_FINISHED_REACTION, utils_12.ANALYSIS_STARTED_REACTION),
postResultsToPRComments(toolName, problems.summary, postComment),
core2.summary.addRaw(problems.summary).write(),
(0, annotations_1.publishAnnotations)(toolName, problems, failedByThreshold, (0, utils_12.getInputs)().githubToken, useAnnotations)
]);
Expand All @@ -64644,14 +64632,14 @@ ${body}
}
__name(publishOutput, "publishOutput");
exports2.publishOutput = publishOutput;
function postResultsToPRComments(content, postComment) {
function postResultsToPRComments(toolName, content, postComment) {
var _a;
return __awaiter2(this, void 0, void 0, function* () {
const pr = (_a = github.context.payload.pull_request) !== null && _a !== void 0 ? _a : "";
if (!postComment || !pr) {
return;
}
const comment_tag_pattern = `<!-- JetBrains/qodana-action@v${qodana_12.VERSION} -->`;
const comment_tag_pattern = `<!-- JetBrains/qodana-action@v${qodana_12.VERSION} : ${toolName} -->`;
const body = comment_tag_pattern ? `${content}
${comment_tag_pattern}` : content;
const comment_id = yield (0, utils_12.findCommentByTag)(comment_tag_pattern);
Expand Down Expand Up @@ -64748,7 +64736,7 @@ function main() {
yield io.mkdirP(inputs.resultsDir);
yield io.mkdirP(inputs.cacheDir);
yield Promise.all([
(0, utils_1.putReaction)(yield (0, utils_1.getFirstCommentId)(), utils_1.ANALYSIS_STARTED_REACTION, utils_1.ANALYSIS_FINISHED_REACTION),
(0, utils_1.putReaction)(utils_1.ANALYSIS_STARTED_REACTION, utils_1.ANALYSIS_FINISHED_REACTION),
(0, utils_1.prepareAgent)(inputs.args),
(0, utils_1.restoreCaches)(inputs.cacheDir, inputs.primaryCacheKey, inputs.additionalCacheKey, inputs.useCaches)
]);
Expand Down
2 changes: 1 addition & 1 deletion scan/src/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,6 @@ export async function publishAnnotations(
await publishGitHubCheck(failedByThreshold, name, token, problems)
}
} catch (error) {
core.warning(`Failed to publish annotations – ${(error as Error).message}`)
core.debug(`Failed to publish annotations – ${(error as Error).message}`)
}
}
7 changes: 1 addition & 6 deletions scan/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import {
ANALYSIS_FINISHED_REACTION,
ANALYSIS_STARTED_REACTION,
getFirstCommentId,
getInputs,
isNeedToUploadCache,
prepareAgent,
Expand Down Expand Up @@ -46,11 +45,7 @@ async function main(): Promise<void> {
await io.mkdirP(inputs.resultsDir)
await io.mkdirP(inputs.cacheDir)
await Promise.all([
putReaction(
await getFirstCommentId(),
ANALYSIS_STARTED_REACTION,
ANALYSIS_FINISHED_REACTION
),
putReaction(ANALYSIS_STARTED_REACTION, ANALYSIS_FINISHED_REACTION),
prepareAgent(inputs.args),
restoreCaches(
inputs.cacheDir,
Expand Down
26 changes: 15 additions & 11 deletions scan/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
ANALYSIS_STARTED_REACTION,
createComment,
findCommentByTag,
getFirstCommentId,
getInputs,
getProblemPlural,
isPRMode,
Expand Down Expand Up @@ -87,11 +86,13 @@ function getRowsByLevel(annotations: Annotation[], level: string): string {

/**
* Generates action summary string of annotations.
* @param toolName The name of the tool to generate the summary from.
* @param annotations The annotations to generate the summary from.
* @param licensesInfo The licenses a Markdown text to generate the summary from.
* @param reportUrl The URL to the Qodana report.
*/
function getSummary(
toolName: string,
annotations: Annotation[],
licensesInfo: string,
reportUrl: string
Expand All @@ -107,7 +108,7 @@ function getSummary(
}
if (annotations.length === 0) {
return [
`# ${QODANA_CHECK_NAME}`,
`# ${toolName}`,
'',
'**It seems all right 👌**',
'',
Expand All @@ -120,7 +121,7 @@ function getSummary(
}

return [
`# ${QODANA_CHECK_NAME}`,
`# ${toolName}`,
'',
`**${annotations.length} ${getProblemPlural(annotations.length)}** found`,
'',
Expand Down Expand Up @@ -193,15 +194,16 @@ export async function publishOutput(
}
const annotations: Annotation[] = problems.annotations ?? []
const toolName = problems.title.split('found by ')[1] ?? QODANA_CHECK_NAME
problems.summary = getSummary(annotations, licensesInfo, reportUrl)
problems.summary = getSummary(
toolName,
annotations,
licensesInfo,
reportUrl
)

await Promise.all([
putReaction(
await getFirstCommentId(),
ANALYSIS_FINISHED_REACTION,
ANALYSIS_STARTED_REACTION
),
postResultsToPRComments(problems.summary, postComment),
putReaction(ANALYSIS_FINISHED_REACTION, ANALYSIS_STARTED_REACTION),
postResultsToPRComments(toolName, problems.summary, postComment),
core.summary.addRaw(problems.summary).write(),
publishAnnotations(
toolName,
Expand All @@ -222,18 +224,20 @@ export async function publishOutput(

/**
* Post a new comment to the pull request.
* @param toolName The name of the tool to mention in comment.
* @param content The comment to post.
* @param postComment Whether to post a comment or not.
*/
async function postResultsToPRComments(
toolName: string,
content: string,
postComment: boolean
): Promise<void> {
const pr = github.context.payload.pull_request ?? ''
if (!postComment || !pr) {
return
}
const comment_tag_pattern = `<!-- JetBrains/qodana-action@v${VERSION} -->`
const comment_tag_pattern = `<!-- JetBrains/qodana-action@v${VERSION} : ${toolName} -->`
const body = comment_tag_pattern
? `${content}\n${comment_tag_pattern}`
: content
Expand Down
46 changes: 12 additions & 34 deletions scan/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,26 +295,6 @@ export function getWorkflowRunUrl(): string {
return `${serverUrl}/${repo.owner}/${repo.repo}/actions/runs/${runId}`
}

/**
* Fetches the ID of the first comment in the repository issue using the GitHub Octokit REST API.
*
* @returns A Promise resolving to the ID of the first comment or -1 if the operation failed.
*/
export async function getFirstCommentId(): Promise<number> {
const client = github.getOctokit(getInputs().githubToken)

try {
const {data: comments} = await client.rest.issues.listComments({
...github.context.repo,
issue_number: github.context.issue.number
})
return comments[0].id
} catch (error) {
core.debug(`Failed to get first comment id – ${(error as Error).message}`)
return -1
}
}

/**
* Asynchronously finds a comment on the GitHub issue and returns its ID based on the provided tag. If the
* comment is not found, returns -1. Utilizes GitHub's Octokit REST API client.
Expand Down Expand Up @@ -389,47 +369,45 @@ export async function updateComment(
* Updates the reaction of a pull request review comment to the given 'newReaction'.
* Removes the previous reaction if 'oldReaction' is non-empty.
*
* @param comment_id The ID of the pull request review comment.
* @param newReaction The new reaction to be added.
* @param oldReaction The old reaction to be removed (if non-empty).
* @returns A Promise resolving to void.
*/
export async function putReaction(
comment_id: number,
newReaction: Reaction,
oldReaction: string
): Promise<void> {
const client = github.getOctokit(getInputs().githubToken)

const issue_number = github.context.payload.pull_request?.number as number
if (oldReaction !== '') {
try {
const {data: reactions} =
await client.rest.reactions.listForPullRequestReviewComment({
...github.context.repo,
comment_id
})
const {data: reactions} = await client.rest.reactions.listForIssue({
...github.context.repo,
issue_number
})
const previousReaction = reactions.find(r => r.content === oldReaction)
if (previousReaction) {
await client.rest.reactions.deleteForPullRequestComment({
await client.rest.reactions.deleteForIssue({
...github.context.repo,
comment_id,
issue_number,
reaction_id: previousReaction.id
})
}
} catch (error) {
core.debug(
`Failed to delete previous reaction – ${(error as Error).message}`
core.warning(
`Failed to delete the initial reaction – ${(error as Error).message}`
)
}
}

try {
await client.rest.reactions.createForPullRequestReviewComment({
await client.rest.reactions.createForIssue({
...github.context.repo,
comment_id,
issue_number,
content: newReaction
})
} catch (error) {
core.debug(`Failed to put reaction – ${(error as Error).message}`)
core.warning(`Failed to set reaction – ${(error as Error).message}`)
}
}

0 comments on commit 9ee2bde

Please sign in to comment.