From a9294fca24e05353df7a38cee0ec0d5b3cb2cc51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gr=C3=BCnwald?= Date: Fri, 3 Jan 2025 15:57:40 +0100 Subject: [PATCH] Add source and target branch to GitLabCIPullRequestInfo Add properties "SourceBranch" and "TargetBranch" to GitLabCIPullRequestInfo. --- .../Fixtures/Build/GitLabCIInfoFixture.cs | 2 ++ .../Data/GitLabCIPullRequestInfoTests.cs | 32 +++++++++++++++++++ .../GitLabCI/Data/GitLabCIPullRequestInfo.cs | 16 ++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/Cake.Common.Tests/Fixtures/Build/GitLabCIInfoFixture.cs b/src/Cake.Common.Tests/Fixtures/Build/GitLabCIInfoFixture.cs index e8a0e846d4..9e8bc2a8d7 100644 --- a/src/Cake.Common.Tests/Fixtures/Build/GitLabCIInfoFixture.cs +++ b/src/Cake.Common.Tests/Fixtures/Build/GitLabCIInfoFixture.cs @@ -65,6 +65,8 @@ public GitLabCIInfoFixture(bool versionNineOrNewer = false) Environment.GetEnvironmentVariable("CI_SERVER_VERSION").Returns("8.9.0"); Environment.GetEnvironmentVariable("GITLAB_USER_ID").Returns("42"); Environment.GetEnvironmentVariable("GITLAB_USER_EMAIL").Returns("anthony@warwickcontrol.com"); + Environment.GetEnvironmentVariable("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME").Returns("source-branch"); + Environment.GetEnvironmentVariable("CI_MERGE_REQUEST_TARGET_BRANCH_NAME").Returns("main"); } public GitLabCIBuildInfo CreateBuildInfo() diff --git a/src/Cake.Common.Tests/Unit/Build/GitLabCI/Data/GitLabCIPullRequestInfoTests.cs b/src/Cake.Common.Tests/Unit/Build/GitLabCI/Data/GitLabCIPullRequestInfoTests.cs index 93d4d59fcf..97d588dff2 100644 --- a/src/Cake.Common.Tests/Unit/Build/GitLabCI/Data/GitLabCIPullRequestInfoTests.cs +++ b/src/Cake.Common.Tests/Unit/Build/GitLabCI/Data/GitLabCIPullRequestInfoTests.cs @@ -61,5 +61,37 @@ public void Should_Return_Correct_Value() Assert.Equal(1, result); } } + + public sealed class TheSourceBranchProperty + { + [Fact] + public void Should_Return_Correct_Value() + { + // Given + var info = new GitLabCIInfoFixture().CreatePullRequestInfo(); + + // When + var result = info.SourceBranch; + + // Then + Assert.Equal("source-branch", result); + } + } + + public sealed class TheTargetBranchProperty + { + [Fact] + public void Should_Return_Correct_Value() + { + // Given + var info = new GitLabCIInfoFixture().CreatePullRequestInfo(); + + // When + var result = info.TargetBranch; + + // Then + Assert.Equal("main", result); + } + } } } \ No newline at end of file diff --git a/src/Cake.Common/Build/GitLabCI/Data/GitLabCIPullRequestInfo.cs b/src/Cake.Common/Build/GitLabCI/Data/GitLabCIPullRequestInfo.cs index 9db900b131..8105eca950 100644 --- a/src/Cake.Common/Build/GitLabCI/Data/GitLabCIPullRequestInfo.cs +++ b/src/Cake.Common/Build/GitLabCI/Data/GitLabCIPullRequestInfo.cs @@ -43,5 +43,21 @@ public GitLabCIPullRequestInfo(ICakeEnvironment environment) /// The pull request id. /// public int IId => GetEnvironmentInteger("CI_MERGE_REQUEST_IID"); + + /// + /// Gets the source branch of the pull request. + /// + /// + /// The source branch of the pull request. + /// + public string SourceBranch => GetEnvironmentString("CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"); + + /// + /// Gets the target branch of the pull request. + /// + /// + /// The target branch name of the pull request. + /// + public string TargetBranch => GetEnvironmentString("CI_MERGE_REQUEST_TARGET_BRANCH_NAME"); } } \ No newline at end of file