Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure DevOps should support pr-mode flag #427

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
⬆️ Azure DevOps should support pr-mode flag
  • Loading branch information
hybloid committed Dec 20, 2024
commit 1cde38f204a335337bcded8d1f05840ff5440a65
50 changes: 50 additions & 0 deletions vsts/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import {

// eslint-disable-next-line @typescript-eslint/no-require-imports
import path = require('path')
import * as exec from '@actions/exec'
import {ExecOutput} from '@actions/exec'

export function setFailed(message: string): void {
tl.setResult(tl.TaskResult.Failed, message)
Expand Down Expand Up @@ -77,6 +79,12 @@ export async function qodana(args: string[] = []): Promise<number> {
if (args.length === 0) {
const inputs = getInputs()
args = getQodanaScanArgs(inputs.args, inputs.resultsDir, inputs.cacheDir)
if (inputs.prMode && tl.getVariable('Build.Reason') === 'PullRequest') {
const sha = await getPrSha()
if (sha !== '') {
args.push('--commit', sha)
}
}
}
return await tl.execAsync(EXECUTABLE, args, {
ignoreReturnCode: true,
Expand Down Expand Up @@ -171,3 +179,45 @@ export function uploadSarif(resultsDir: string, execute: boolean): void {
tl.warning(`Failed to upload SARIF – ${(error as Error).message}`)
}
}

async function getPrSha(): Promise<string> {
if (process.env.QODANA_PR_SHA) {
return process.env.QODANA_PR_SHA
}
const sourceBranch = tl
.getVariable('System.PullRequest.SourceBranch')
?.replace('refs/heads/', '')
const targetBranch = tl
.getVariable('System.PullRequest.TargetBranch')
?.replace('refs/heads/', '')

if (sourceBranch && targetBranch) {
await git(['fetch', 'origin'])
const output = await gitOutput(
['merge-base', 'origin/' + sourceBranch, 'origin/' + targetBranch],
{
ignoreReturnCode: true
}
)
if (output.exitCode === 0) {
return output.stdout.trim()
} else {
return ''
}
}
return ''
}

async function git(
args: string[],
options: exec.ExecOptions = {}
): Promise<number> {
return (await exec.getExecOutput('git', args, options)).exitCode
}

async function gitOutput(
args: string[],
options: exec.ExecOptions = {}
): Promise<ExecOutput> {
return exec.getExecOutput('git', args, options)
}