Skip to main content

nerdbank_gitversioning/cloud_build_services/
team_city.rs

1// Copyright (c) .NET Foundation and Contributors. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4//! [TeamCity](https://www.jetbrains.com/help/teamcity/predefined-build-parameters.html) support.
5
6use std::io::{self, Write};
7
8use crate::cloud::{
9    CloudBuild, EnvironmentVariables, empty_variables, env, if_starts_with, is_nonempty,
10};
11
12/// TeamCity cloud-build integration.
13pub struct TeamCity;
14
15impl CloudBuild for TeamCity {
16    fn name(&self) -> &'static str {
17        "TeamCity"
18    }
19
20    fn is_applicable(&self) -> bool {
21        is_nonempty(self.git_commit_id().as_deref())
22    }
23
24    fn is_pull_request(&self) -> bool {
25        false
26    }
27
28    fn building_branch(&self) -> Option<String> {
29        if_starts_with(env("BUILD_GIT_BRANCH"), "refs/heads/")
30    }
31
32    fn building_tag(&self) -> Option<String> {
33        if_starts_with(env("BUILD_GIT_BRANCH"), "refs/tags/")
34    }
35
36    fn git_commit_id(&self) -> Option<String> {
37        env("BUILD_VCS_NUMBER")
38    }
39
40    fn set_cloud_build_number(
41        &self,
42        build_number: &str,
43        stdout: &mut dyn Write,
44        _stderr: &mut dyn Write,
45    ) -> io::Result<EnvironmentVariables> {
46        writeln!(stdout, "##teamcity[buildNumber '{}']", escape(build_number))?;
47        Ok(empty_variables())
48    }
49
50    fn set_cloud_build_variable(
51        &self,
52        name: &str,
53        value: &str,
54        stdout: &mut dyn Write,
55        _stderr: &mut dyn Write,
56    ) -> io::Result<EnvironmentVariables> {
57        let name = escape(name);
58        let value = escape(value);
59        writeln!(
60            stdout,
61            "##teamcity[setParameter name='{name}' value='{value}']"
62        )?;
63        writeln!(
64            stdout,
65            "##teamcity[setParameter name='system.{name}' value='{value}']"
66        )?;
67        Ok(empty_variables())
68    }
69}
70
71fn escape(value: &str) -> String {
72    value
73        .replace('|', "||")
74        .replace('\'', "|'")
75        .replace('\n', "|n")
76        .replace('\r', "|r")
77        .replace('[', "|[")
78        .replace(']', "|]")
79}