Skip to content

Commit e8297ea

Browse files
committed
🐛 Fix revision ids for analysing pull requests
1 parent f8f97ea commit e8297ea

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

scan/src/utils.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import path from 'path'
4646
import * as fs from 'fs'
4747
import * as os from 'os'
4848
import {COMMIT_EMAIL, COMMIT_USER, prFixesBody} from './output'
49+
import {ExecOutput} from '@actions/exec'
4950

5051
export const ANALYSIS_FINISHED_REACTION = '+1'
5152
export const ANALYSIS_STARTED_REACTION = 'eyes'
@@ -90,16 +91,35 @@ export function getInputs(): Inputs {
9091
}
9192
}
9293

93-
function getPrSha(): string {
94+
async function getPrSha(): Promise<string> {
9495
if (process.env.QODANA_PR_SHA) {
9596
return process.env.QODANA_PR_SHA
9697
}
9798
if (github.context.payload.pull_request !== undefined) {
98-
return github.context.payload.pull_request.base.sha
99+
const output = await gitOutput([
100+
'merge-base',
101+
github.context.payload.pull_request.base.sha,
102+
github.context.payload.pull_request.head.sha
103+
])
104+
if (output.exitCode === 0) {
105+
return output.stdout.trim()
106+
} else {
107+
return github.context.payload.pull_request.base.sha
108+
}
99109
}
100110
return ''
101111
}
102112

113+
function getHeadSha(): string {
114+
if (process.env.QODANA_REVISION) {
115+
return process.env.QODANA_REVISION
116+
}
117+
if (github.context.payload.pull_request !== undefined) {
118+
return github.context.payload.pull_request.head.sha
119+
}
120+
return github.context.sha
121+
}
122+
103123
/**
104124
* Runs the qodana command with the given arguments.
105125
* @param inputs the action inputs.
@@ -113,7 +133,7 @@ export async function qodana(
113133
if (args.length === 0) {
114134
args = getQodanaScanArgs(inputs.args, inputs.resultsDir, inputs.cacheDir)
115135
if (inputs.prMode) {
116-
const sha = getPrSha()
136+
const sha = await getPrSha()
117137
if (sha !== '') {
118138
args.push('--commit', sha)
119139
}
@@ -124,6 +144,7 @@ export async function qodana(
124144
ignoreReturnCode: true,
125145
env: {
126146
...process.env,
147+
QODANA_REVISION: getHeadSha(),
127148
NONINTERACTIVE: '1'
128149
}
129150
})
@@ -594,6 +615,13 @@ async function git(
594615
return (await exec.getExecOutput('git', args, options)).exitCode
595616
}
596617

618+
async function gitOutput(
619+
args: string[],
620+
options: exec.ExecOptions = {}
621+
): Promise<ExecOutput> {
622+
return await exec.getExecOutput('git', args, options)
623+
}
624+
597625
async function createPr(
598626
title: string,
599627
repo: string,

0 commit comments

Comments
 (0)