Skip to content

Commit

Permalink
Merge pull request #1391 from arturcic/fix/1389
Browse files Browse the repository at this point in the history
#1389 - fix setting the build version in azure devops
  • Loading branch information
arturcic authored Dec 10, 2024
2 parents 1a151a8 + d049a7d commit 6e906e1
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 5 deletions.
5 changes: 5 additions & 0 deletions dist/tools/azure/agent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ class BuildAgent extends BuildAgentBase {
setFailed = (message, done) => this._setResult(TaskResult.Failed, message, done);
setOutput = (name, value) => this._setVariable(name, value, true);
setVariable = (name, value) => this._setVariable(name, value);
updateBuildNumber = (version) => this._updateBuildNumber(version);
_updateBuildNumber(version) {
this.debug(`build number: ${version}`);
issueCommand("build.updatebuildnumber", {}, version);
}
_setResult(result, message, done) {
this.debug(`task result: ${TaskResult[result]}`);
if (result === TaskResult.Failed && message) {
Expand Down
2 changes: 1 addition & 1 deletion dist/tools/azure/agent.mjs.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/tools/github/agent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class BuildAgent extends BuildAgentBase {
}
issueCommand("set-env", { name }, convertedVal);
};
updateBuildNumber = (version) => this.debug(`updateBuildNumber - ${version}`);
}

export { BuildAgent };
Expand Down
2 changes: 1 addition & 1 deletion dist/tools/github/agent.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tools/libs/agents.mjs.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/tools/libs/gitversion.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class GitVersionTool extends DotnetTool {
this.buildAgent.error(`Unable to set output/variable for ${property}`);
}
}
if (output.FullSemVer.endsWith("+0")) {
output.FullSemVer = output.FullSemVer.slice(0, -2);
}
this.buildAgent.updateBuildNumber(output.FullSemVer);
}
async getRepoDir(settings) {
return await super.getRepoPath(settings.targetPath);
Expand Down
2 changes: 1 addition & 1 deletion dist/tools/libs/gitversion.mjs.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/tools/local/agent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BuildAgent extends BuildAgentBase {
this.debug(`setVariable - ${name} - ${value}`);
process.env[name] = value;
}
updateBuildNumber = (version) => this.debug(`updateBuildNumber - ${version}`);
}

export { BuildAgent };
Expand Down
2 changes: 1 addition & 1 deletion dist/tools/local/agent.mjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/__tests__/agents/azure/build-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,14 @@ describe.skipIf(isAzurePipelines)('build-agent/azure', () => {
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(`##vso[task.setvariable variable=test;isOutput=true;issecret=false;]value${os.EOL}`)
})

it('should update build number', () => {
const spyDebug = vi.spyOn(agent, 'debug')
const spyWrite = vi.spyOn(process.stdout, 'write')

agent.updateBuildNumber('test')
expect(spyDebug).toHaveBeenCalledTimes(1)
expect(spyDebug).toHaveBeenCalledWith('build number: test')
expect(spyWrite).toHaveBeenCalledWith(`##vso[build.updatebuildnumber]test${os.EOL}`)
})
})
8 changes: 8 additions & 0 deletions src/__tests__/agents/github/build-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,12 @@ describe.skipIf(isGitHubActions)('build-agent/github', () => {
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(`::set-env name=name::value${os.EOL}`)
})

it('should update build number', () => {
const spy = vi.spyOn(process.stdout, 'write')

agent.updateBuildNumber('test')
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(`::debug::updateBuildNumber - test${os.EOL}`)
})
})
8 changes: 8 additions & 0 deletions src/__tests__/agents/local/build-agent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ describe('build-agent/local', () => {
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('setOutput - test - value')
})

it('should update build number', () => {
const spy = vi.spyOn(agent, 'debug')

agent.updateBuildNumber('test')
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('updateBuildNumber - test')
})
})
3 changes: 3 additions & 0 deletions src/__tests__/tools/gitversion/tool.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ describe('GitVersionTool', () => {
},
setVariable(name: string, value: string) {
variables.set(name, value)
},
updateBuildNumber(_version: string): void {
return
}
} as IBuildAgent
tool = new TestGitVersionTool(buildAgent)
Expand Down
7 changes: 7 additions & 0 deletions src/agents/azure/build-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export class BuildAgent extends BuildAgentBase implements IBuildAgent {

setVariable = (name: string, value: string): void => this._setVariable(name, value)

updateBuildNumber = (version: string): void => this._updateBuildNumber(version)

private _updateBuildNumber(version: string): void {
this.debug(`build number: ${version}`)
issueCommand('build.updatebuildnumber', {}, version)
}

private _setResult(result: TaskResult, message: string, done?: boolean): void {
this.debug(`task result: ${TaskResult[result]}`)
// add an error issue
Expand Down
4 changes: 4 additions & 0 deletions src/agents/common/build-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export interface IBuildAgent {

setVariable(name: string, value: string): void

updateBuildNumber: (version: string) => void

which(tool: string, check?: boolean): Promise<string>
}

Expand All @@ -86,6 +88,8 @@ export abstract class BuildAgentBase implements IBuildAgent {

abstract setVariable(name: string, value: string): void

abstract updateBuildNumber: (version: string) => void

get sourceDir(): string {
return this.getVariableAsPath(this.sourceDirVariable)?.replace(/\\/g, '/')
}
Expand Down
2 changes: 2 additions & 0 deletions src/agents/github/build-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ export class BuildAgent extends BuildAgentBase implements IBuildAgent {

issueCommand('set-env', { name }, convertedVal)
}

updateBuildNumber = (version: string): void => this.debug(`updateBuildNumber - ${version}`)
}
2 changes: 2 additions & 0 deletions src/agents/local/build-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ export class BuildAgent extends BuildAgentBase implements IBuildAgent {
this.debug(`setVariable - ${name} - ${value}`)
process.env[name] = value
}

updateBuildNumber = (version: string): void => this.debug(`updateBuildNumber - ${version}`)
}
5 changes: 5 additions & 0 deletions src/tools/gitversion/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export class GitVersionTool extends DotnetTool {
this.buildAgent.error(`Unable to set output/variable for ${property}`)
}
}

if (output.FullSemVer.endsWith('+0')) {
output.FullSemVer = output.FullSemVer.slice(0, -2)
}
this.buildAgent.updateBuildNumber(output.FullSemVer)
}

protected async getRepoDir(settings: ExecuteSettings | CommandSettings): Promise<string> {
Expand Down

0 comments on commit 6e906e1

Please sign in to comment.