Skip to content

Commit

Permalink
If no GIT_BRANCH then fall back to vsc root branch property
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister authored and augustoproiete committed Mar 3, 2021
1 parent 71a8852 commit 919dac6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void SetBuildStartTime(string startTime)

public TeamCityPullRequestInfo CreatePullRequestInfo()
{
return new TeamCityPullRequestInfo(Environment);
return new TeamCityPullRequestInfo(Environment, CreateBuildInfo());
}

public TeamCityEnvironmentInfo CreateEnvironmentInfo()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ public sealed class TheIsPullRequestProperty
[InlineData("refs/pull/1/merge", true)]
[InlineData("refs/changes/1/head", true)]
[InlineData("refs/heads/master", false)]
[InlineData("", true)]
public void Should_Return_Correct_Value(string value, bool expected)
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.SetGitBranch(value);
fixture.SetBuildPropertiesContent(Properties.Resources.TeamCity_Build_Properties_Xml);
fixture.SetConfigPropertiesContent(Properties.Resources.TeamCity_Config_Properties_Xml);
var info = fixture.CreatePullRequestInfo();

// When
Expand All @@ -41,11 +44,14 @@ public sealed class TheNumberProperty
[InlineData("refs/changes/3/merge", 3)]
[InlineData("refs/merge-requests/4/merge", 4)]
[InlineData("refs/heads/master", null)]
[InlineData("", 5)]
public void Should_Return_Correct_Value(string value, int? expected)
{
// Given
var fixture = new TeamCityInfoFixture();
fixture.SetGitBranch(value);
fixture.SetBuildPropertiesContent(Properties.Resources.TeamCity_Build_Properties_Xml);
fixture.SetConfigPropertiesContent(Properties.Resources.TeamCity_Config_Properties_Xml);
var info = fixture.CreatePullRequestInfo();

// When
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public TeamCityEnvironmentInfo(ICakeEnvironment environment, IFileSystem fileSys
{
Project = new TeamCityProjectInfo(environment);
Build = new TeamCityBuildInfo(environment, fileSystem);
PullRequest = new TeamCityPullRequestInfo(environment);
PullRequest = new TeamCityPullRequestInfo(environment, Build);
}
}
}
30 changes: 25 additions & 5 deletions src/Cake.Common/Build/TeamCity/Data/TeamCityPullRequestInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ namespace Cake.Common.Build.TeamCity.Data
/// </summary>
public class TeamCityPullRequestInfo : TeamCityInfo
{
private static bool InferIsPullRequest(string gitReferenceName)
private readonly TeamCityBuildInfo _buildInfo;

private bool InferIsPullRequest()
{
var gitReferenceName = GetBranchRef();

if (string.IsNullOrEmpty(gitReferenceName))
{
return false;
Expand All @@ -37,8 +41,10 @@ private static bool InferIsPullRequest(string gitReferenceName)
return false;
}

private static int? GetPullRequestNumber(string gitReferenceName)
private int? GetPullRequestNumber()
{
var gitReferenceName = GetBranchRef();

if (string.IsNullOrEmpty(gitReferenceName))
{
return null;
Expand All @@ -54,6 +60,18 @@ private static bool InferIsPullRequest(string gitReferenceName)
return null;
}

private string GetBranchRef()
{
var gitBranch = GetEnvironmentString("Git_Branch");

if (string.IsNullOrWhiteSpace(gitBranch))
{
gitBranch = _buildInfo.VcsBranchName;
}

return gitBranch;
}

/// <summary>
/// Gets a value indicating whether the current build was started by a pull request.
/// </summary>
Expand All @@ -63,7 +81,7 @@ private static bool InferIsPullRequest(string gitReferenceName)
/// <remarks>
/// <c>env.Git_Branch</c> is a required parameter in TeamCity for this to work.
/// </remarks>
public bool IsPullRequest => InferIsPullRequest(GetEnvironmentString("Git_Branch"));
public bool IsPullRequest => InferIsPullRequest();

/// <summary>
/// Gets the pull request number.
Expand All @@ -74,15 +92,17 @@ private static bool InferIsPullRequest(string gitReferenceName)
/// <remarks>
/// <c>env.Git_Branch</c> is a required parameter in TeamCity for this to work.
/// </remarks>
public int? Number => IsPullRequest ? GetPullRequestNumber(GetEnvironmentString("Git_Branch")) : null;
public int? Number => IsPullRequest ? GetPullRequestNumber() : null;

/// <summary>
/// Initializes a new instance of the <see cref="TeamCityPullRequestInfo"/> class.
/// </summary>
/// <param name="environment">The environment.</param>
public TeamCityPullRequestInfo(ICakeEnvironment environment)
/// <param name="buildInfo">The TeamCity build info.</param>
public TeamCityPullRequestInfo(ICakeEnvironment environment, TeamCityBuildInfo buildInfo)
: base(environment)
{
_buildInfo = buildInfo;
}
}
}

0 comments on commit 919dac6

Please sign in to comment.