Skip to content

Commit

Permalink
🐛 Fix git state with pr-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tiulpin committed Jul 18, 2023
1 parent 243353f commit 2bd1b0c
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 153 deletions.
6 changes: 4 additions & 2 deletions common/qodana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ export function getQodanaScanArgs(
}
return cliArgs
}

const PUSH_FIXES_TYPES = ['none', 'branch', 'pull-request']
export const NONE = 'none'
export const BRANCH = 'branch'
export const PULL_REQUEST = 'pull-request'
const PUSH_FIXES_TYPES = [NONE, BRANCH, PULL_REQUEST]
export type PushFixesType = (typeof PUSH_FIXES_TYPES)[number]
/**
* The context of the current run – described in action.yaml.
Expand Down
152 changes: 81 additions & 71 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2796,9 +2796,12 @@ var init_cli = __esm({
// ../common/qodana.ts
var qodana_exports = {};
__export(qodana_exports, {
BRANCH: () => BRANCH,
COVERAGE_THRESHOLD: () => COVERAGE_THRESHOLD,
EXECUTABLE: () => EXECUTABLE,
FAIL_THRESHOLD_OUTPUT: () => FAIL_THRESHOLD_OUTPUT,
NONE: () => NONE,
PULL_REQUEST: () => PULL_REQUEST,
QODANA_LICENSES_JSON: () => QODANA_LICENSES_JSON,
QODANA_LICENSES_MD: () => QODANA_LICENSES_MD,
QODANA_REPORT_URL_NAME: () => QODANA_REPORT_URL_NAME,
Expand Down Expand Up @@ -2926,7 +2929,7 @@ function sha256sum(file) {
function getQodanaSha256MismatchMessage(expected, actual) {
return `Downloaded Qodana CLI binary is corrupted. Expected SHA-256 checksum: ${expected}, actual checksum: ${actual}`;
}
var import_crypto4, import_fs, SUPPORTED_PLATFORMS, SUPPORTED_ARCHS, FAIL_THRESHOLD_OUTPUT, QODANA_SARIF_NAME, QODANA_SHORT_SARIF_NAME, QODANA_REPORT_URL_NAME, QODANA_LICENSES_MD, QODANA_LICENSES_JSON, EXECUTABLE, VERSION, COVERAGE_THRESHOLD, QodanaExitCode;
var import_crypto4, import_fs, SUPPORTED_PLATFORMS, SUPPORTED_ARCHS, FAIL_THRESHOLD_OUTPUT, QODANA_SARIF_NAME, QODANA_SHORT_SARIF_NAME, QODANA_REPORT_URL_NAME, QODANA_LICENSES_MD, QODANA_LICENSES_JSON, EXECUTABLE, VERSION, COVERAGE_THRESHOLD, QodanaExitCode, NONE, BRANCH, PULL_REQUEST;
var init_qodana = __esm({
"../common/qodana.ts"() {
"use strict";
Expand Down Expand Up @@ -2957,6 +2960,9 @@ var init_qodana = __esm({
__name(extractArg, "extractArg");
__name(getQodanaPullArgs, "getQodanaPullArgs");
__name(getQodanaScanArgs, "getQodanaScanArgs");
NONE = "none";
BRANCH = "branch";
PULL_REQUEST = "pull-request";
__name(getCoverageFromSarif, "getCoverageFromSarif");
__name(sha256sum, "sha256sum");
__name(getQodanaSha256MismatchMessage, "getQodanaSha256MismatchMessage");
Expand Down Expand Up @@ -68654,12 +68660,14 @@ var require_output = __commonJS({
});
};
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.getProblemPlural = exports2.getSummary = exports2.publishOutput = exports2.getCoverageStats = void 0;
exports2.prFixesBody = exports2.getProblemPlural = exports2.getSummary = exports2.publishOutput = exports2.getCoverageStats = exports2.COMMIT_EMAIL = exports2.COMMIT_USER = void 0;
var core2 = __importStar3(require_core());
var fs2 = __importStar3(require("fs"));
var qodana_12 = (init_qodana(), __toCommonJS(qodana_exports));
var utils_12 = require_utils8();
var annotations_1 = require_annotations();
exports2.COMMIT_USER = "qodana-bot";
exports2.COMMIT_EMAIL = "qodana-support@jetbrains.com";
var QODANA_CHECK_NAME = "Qodana";
var UNKNOWN_RULE_ID = "Unknown";
var SUMMARY_TABLE_HEADER = "| Inspection name | Severity | Problems |";
Expand Down Expand Up @@ -68835,6 +68843,21 @@ ${body}
}
__name(getProblemPlural, "getProblemPlural");
exports2.getProblemPlural = getProblemPlural;
function prFixesBody(jobUrl) {
return ` \u{1F590} Hey there!

This pull request has been auto-generated by the [Qodana Scan workflow](${jobUrl}) configured in your repository.
It has performed code analysis and applied some suggested fixes to improve your code quality \u{1F9F9}\u2728

> **Warning**
> It's crucial to review these changes to ensure everything shipshape manually. Please take a moment to examine the changes here. Remember to run your integration tests against this PR to validate the fixes and ensure everything's functioning as expected.

_\u{1F4BB}\u{1F50D} Happy reviewing and testing!
Best,
[Qodana Scan \u{1F916}](https://github.com/marketplace/actions/qodana-scan)_`;
}
__name(prFixesBody, "prFixesBody");
exports2.prFixesBody = prFixesBody;
}
});

Expand Down Expand Up @@ -69135,6 +69158,7 @@ var require_utils8 = __commonJS({
var path_1 = __importDefault2(require("path"));
var fs2 = __importStar3(require("fs"));
var os = __importStar3(require("os"));
var output_12 = require_output();
exports2.ANALYSIS_FINISHED_REACTION = "+1";
exports2.ANALYSIS_STARTED_REACTION = "eyes";
function getInputs() {
Expand Down Expand Up @@ -69176,78 +69200,35 @@ var require_utils8 = __commonJS({
}
__name(qodana, "qodana");
exports2.qodana = qodana;
function prFixesBody(jobUrl) {
return ` \u{1F590} Hey there!

This pull request has been auto-generated by the [Qodana Scan workflow](${jobUrl}) configured in your repository.
It has performed code analysis and applied some suggested fixes to improve your code quality \u{1F9F9}\u2728

> **Warning**
> It's crucial to review these changes to ensure everything shipshape manually. Please take a moment to examine the changes here. Remember to run your integration tests against this PR to validate the fixes and ensure everything's functioning as expected.

_\u{1F4BB}\u{1F50D} Happy reviewing and testing!
Best,
[Qodana Scan \u{1F916}](https://github.com/marketplace/actions/qodana-scan)_`;
}
__name(prFixesBody, "prFixesBody");
function getJobUrl() {
const repo = github.context.repo;
const runId = github.context.runId;
const serverUrl = process.env["GITHUB_SERVER_URL"];
return `${serverUrl}/${repo.owner}/${repo.repo}/actions/runs/${runId}`;
}
__name(getJobUrl, "getJobUrl");
function pushQuickFixes() {
function pushQuickFixes(mode, commitMessage) {
var _a;
return __awaiter3(this, void 0, void 0, function* () {
const title = getInputs().commitMessage;
if (getInputs().pushFixes === "none") {
if (mode === qodana_12.NONE) {
return;
}
let currentBranch = github.context.ref;
if (((_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.head.ref) !== void 0) {
currentBranch = github.context.payload.pull_request.head.ref;
}
const newBranchName = `qodana/quick-fixes-${github.context.runId}`;
yield exec.getExecOutput("git", ["config", "user.name", "qodana-bot"]);
yield exec.getExecOutput("git", [
"config",
"user.email",
"qodana-support@jetbrains.com"
]);
yield exec.getExecOutput("git", ["add", "."]);
yield exec.getExecOutput("git", ["commit", "-m", title]);
if (getInputs().prMode) {
yield exec.getExecOutput("git", [
"pull",
"--rebase",
"origin",
currentBranch
]);
}
if (getInputs().pushFixes === "branch") {
yield exec.getExecOutput("git", ["push", "origin", github.context.ref]);
} else if (getInputs().pushFixes === "pull-request") {
const prBodyFile = path_1.default.join(os.tmpdir(), "pr-body.txt");
fs2.writeFileSync(prBodyFile, prFixesBody(getJobUrl()));
yield exec.getExecOutput("git", ["checkout", "-b", newBranchName]);
yield exec.getExecOutput("git", ["push", "origin", newBranchName]);
yield exec.getExecOutput("gh", [
"pr",
"create",
"--repo",
`${github.context.repo.owner}/${github.context.repo.repo}`,
"--title",
title,
"--body-file",
prBodyFile,
"--base",
currentBranch,
"--head",
newBranchName
], {
env: Object.assign(Object.assign({}, process.env), { GH_TOKEN: getInputs().githubToken })
});
const c = github.context;
let currentBranch = c.ref;
if (((_a = c.payload.pull_request) === null || _a === void 0 ? void 0 : _a.head.ref) !== void 0) {
currentBranch = c.payload.pull_request.head.ref;
}
yield git(["config", "user.name", output_12.COMMIT_USER]);
yield git(["config", "user.email", output_12.COMMIT_EMAIL]);
yield git(["add", "."]);
if (isPRMode()) {
yield git(["stash"]);
yield git(["fetch", "origin", currentBranch]);
yield git(["reset", "--hard", `origin/${currentBranch}`]);
yield git(["stash", "apply", "stash@{0}"]);
}
yield git(["commit", "-m", commitMessage]);
yield git(["pull", "--rebase", "origin", currentBranch]);
if (mode === qodana_12.BRANCH) {
yield git(["push", "origin", currentBranch]);
} else if (mode === qodana_12.PULL_REQUEST) {
const newBranch = `qodana/quick-fixes-${c.runId}`;
yield git(["checkout", "-b", newBranch]);
yield git(["push", "origin", newBranch]);
yield createPr(commitMessage, `${c.repo.owner}/${c.repo.repo}`, currentBranch, newBranch);
}
});
}
Expand Down Expand Up @@ -69513,6 +69494,35 @@ ${comment_tag_pattern}`;
});
}
__name(updateCheck, "updateCheck");
function git(args, options = {}) {
return __awaiter3(this, void 0, void 0, function* () {
return (yield exec.getExecOutput("git", args, options)).exitCode;
});
}
__name(git, "git");
function createPr(title, repo, base, head) {
return __awaiter3(this, void 0, void 0, function* () {
const prBodyFile = path_1.default.join(os.tmpdir(), "pr-body.txt");
fs2.writeFileSync(prBodyFile, (0, output_12.prFixesBody)(getWorkflowRunUrl()));
yield exec.getExecOutput("gh", [
"pr",
"create",
"--repo",
repo,
"--title",
title,
"--body-file",
prBodyFile,
"--base",
base,
"--head",
head
], {
env: Object.assign(Object.assign({}, process.env), { GH_TOKEN: getInputs().githubToken })
});
});
}
__name(createPr, "createPr");
}
});

Expand Down Expand Up @@ -69607,7 +69617,7 @@ function main() {
const exitCode = yield (0, utils_1.qodana)(inputs);
const canUploadCache = (0, utils_1.isNeedToUploadCache)(inputs.useCaches, inputs.cacheDefaultBranchOnly) && (0, qodana_1.isExecutionSuccessful)(exitCode);
yield Promise.all([
(0, utils_1.pushQuickFixes)(),
(0, utils_1.pushQuickFixes)(inputs.pushFixes, inputs.commitMessage),
(0, utils_1.uploadArtifacts)(inputs.resultsDir, inputs.artifactName, inputs.uploadResult),
(0, utils_1.uploadCaches)(inputs.cacheDir, inputs.primaryCacheKey, reservedCacheKey, canUploadCache),
(0, output_1.publishOutput)(exitCode === qodana_1.QodanaExitCode.FailThreshold, inputs.resultsDir, inputs.useAnnotations, inputs.postComment, (0, qodana_1.isExecutionSuccessful)(exitCode))
Expand Down
2 changes: 1 addition & 1 deletion scan/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async function main(): Promise<void> {
isExecutionSuccessful(exitCode)

await Promise.all([
pushQuickFixes(),
pushQuickFixes(inputs.pushFixes, inputs.commitMessage),
uploadArtifacts(
inputs.resultsDir,
inputs.artifactName,
Expand Down
19 changes: 19 additions & 0 deletions scan/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
publishAnnotations
} from './annotations'

export const COMMIT_USER = 'qodana-bot'
export const COMMIT_EMAIL = 'qodana-support@jetbrains.com'
const QODANA_CHECK_NAME = 'Qodana'
const UNKNOWN_RULE_ID = 'Unknown'
const SUMMARY_TABLE_HEADER = '| Inspection name | Severity | Problems |'
Expand Down Expand Up @@ -291,3 +293,20 @@ export function getSummary(
export function getProblemPlural(count: number): string {
return `new problem${count !== 1 ? 's' : ''}`
}

/*
* The pull request with quick-fixes body template.
*/
export function prFixesBody(jobUrl: string): string {
return ` 🖐 Hey there!
This pull request has been auto-generated by the [Qodana Scan workflow](${jobUrl}) configured in your repository.
It has performed code analysis and applied some suggested fixes to improve your code quality 🧹✨
> **Warning**
> It's crucial to review these changes to ensure everything shipshape manually. Please take a moment to examine the changes here. Remember to run your integration tests against this PR to validate the fixes and ensure everything's functioning as expected.
_💻🔍 Happy reviewing and testing!
Best,
[Qodana Scan 🤖](https://github.com/marketplace/actions/qodana-scan)_`
}
Loading

0 comments on commit 2bd1b0c

Please sign in to comment.