Skip to content

Commit 122062f

Browse files
committed
Ensures GitVersion output is valid
Checks the result code before processing GitVersion's output. This prevents attempting to parse invalid JSON when the tool fails, ensuring a smoother build process.
1 parent 7aa3ada commit 122062f

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

dist/tools/libs/gitversion.mjs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,25 @@ class Runner extends RunnerBase {
210210
return this.safeExecute(async () => await this.tool.executeCommand(), "GitVersion executed successfully");
211211
}
212212
processGitVersionOutput(result) {
213-
const stdout = result.stdout;
214-
if (stdout.lastIndexOf("{") === -1 || stdout.lastIndexOf("}") === -1) {
215-
const errorMessage = "GitVersion output is not valid JSON, see output details";
216-
this.buildAgent.debug(errorMessage);
217-
this.buildAgent.setFailed(errorMessage, true);
218-
return {
219-
code: -1,
220-
error: new Error(errorMessage)
221-
};
213+
if (result.code === 0) {
214+
const stdout = result.stdout;
215+
if (stdout.lastIndexOf("{") === -1 || stdout.lastIndexOf("}") === -1) {
216+
const errorMessage = "GitVersion output is not valid JSON, see output details";
217+
this.buildAgent.debug(errorMessage);
218+
this.buildAgent.setFailed(errorMessage, true);
219+
return {
220+
code: -1,
221+
error: new Error(errorMessage)
222+
};
223+
} else {
224+
const jsonOutput = stdout.substring(stdout.lastIndexOf("{"), stdout.lastIndexOf("}") + 1);
225+
const gitVersionOutput = JSON.parse(jsonOutput);
226+
this.tool.writeGitVersionToAgent(gitVersionOutput);
227+
this.tool.updateBuildNumber();
228+
this.buildAgent.setSucceeded("GitVersion executed successfully", true);
229+
return result;
230+
}
222231
} else {
223-
const jsonOutput = stdout.substring(stdout.lastIndexOf("{"), stdout.lastIndexOf("}") + 1);
224-
const gitVersionOutput = JSON.parse(jsonOutput);
225-
this.tool.writeGitVersionToAgent(gitVersionOutput);
226-
this.tool.updateBuildNumber();
227-
this.buildAgent.setSucceeded("GitVersion executed successfully", true);
228232
return result;
229233
}
230234
}

0 commit comments

Comments
 (0)