Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-3144: TeamCityPullRequestInfo without GIT_BRANCH #3145

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
}
}