Skip to content

Commit

Permalink
Add clean tokens for gitlab CI and more tests (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
dicko2 authored Jun 27, 2023
1 parent 7ec3160 commit a5059ee
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="4.7.2" />
<InternalsVisibleTo Include="Agoda.DevFeedback.Common.Tests" />
</ItemGroup>

</Project>
12 changes: 11 additions & 1 deletion src/Agoda.DevFeedback.Common/GitContextReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static GitContext GetGitContext()
throw new GitContextException("Unable to get git branch.");
}

url = CleanGitlabCIToken(url);
return new GitContext
{
RepositoryUrl = url,
Expand Down Expand Up @@ -59,13 +60,22 @@ static string RunCommand(string args)
return process.StandardOutput.ReadLine();
}

static string GetRepositoryNameFromUrl(string url)
internal static string GetRepositoryNameFromUrl(string url)
{
var repositoryName = url.Substring(url.LastIndexOf('/') + 1);

return repositoryName.EndsWith(".git")
? repositoryName.Substring(0, repositoryName.LastIndexOf('.'))
: repositoryName;
}

internal static string CleanGitlabCIToken(string url)
{
if (url.Contains("@") && url.StartsWith("https"))
{
url = "https://" + url.Split('@')[1];
}
return url;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>


<ItemGroup>
<PackageReference Include="Agoda.Analyzers" Version="1.0.523">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="NSubstitute" Version="4.2.2" />
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Agoda.DevFeedback.Common\Agoda.DevFeedback.Common.csproj" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions src/Agoda.DevFeedback.CommonTests/GitContextReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Agoda.DevFeedback.Common;
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Shouldly;

namespace Agoda.DevFeedback.Common.Tests
{
[TestFixture()]
public class GitContextReaderTests
{
[Test()]
[TestCase("https://gitlab-ci-token:[email protected]/full-stack/ycs/revenue-management", "64_-_v7W68Q4uMG1sXIeh-W")]
// Note: this is not a real token, it is just a random string
public void WhenUrlHasToken_ShouldCleanTokenFromUrl(string url, string token)
{
var result = GitContextReader.CleanGitlabCIToken(url);
result.ShouldNotContain(token);
result.ShouldNotContain("@");
result.ShouldNotContain("gitlab-ci-token:");
}

[Test()]
[TestCase("https://gitlab.agodadev.io/full-stack/ycs/revenue-management")]
public void WhenGetRepoNameFromUrl_ShouldReturnTheGitlabNamespacePathForTheGitlabProject(string url)
{
GitContextReader.GetRepositoryNameFromUrl(url)
.ShouldBe("full-stack/ycs/revenue-management");
}
}
}
10 changes: 8 additions & 2 deletions src/DotnetBuildMetrics.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.DevFeedback.Common",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.DevFeedback.AspNetStartup", "Agoda.DevFeedback.AspNetStartup\Agoda.DevFeedback.AspNetStartup.csproj", "{1CDE58A7-3243-4564-93DA-59EF30475334}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agoda.Tests.Metrics.NUnit", "Agoda.Tests.Metrics.NUnit\Agoda.Tests.Metrics.NUnit.csproj", "{E54150A6-5E21-47C7-947C-7AB6C362A1C9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.Tests.Metrics.NUnit", "Agoda.Tests.Metrics.NUnit\Agoda.Tests.Metrics.NUnit.csproj", "{E54150A6-5E21-47C7-947C-7AB6C362A1C9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agoda.Tests.Metrics.NUnit.Tests", "Agoda.Tests.Metrics.NUnit.Tests\Agoda.Tests.Metrics.NUnit.Tests.csproj", "{C3FD34EE-C8E1-4A48-885D-1E9AC37B3561}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Agoda.Tests.Metrics.NUnit.Tests", "Agoda.Tests.Metrics.NUnit.Tests\Agoda.Tests.Metrics.NUnit.Tests.csproj", "{C3FD34EE-C8E1-4A48-885D-1E9AC37B3561}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Agoda.DevFeedback.Common.Tests", "Agoda.DevFeedback.CommonTests\Agoda.DevFeedback.Common.Tests.csproj", "{A78ACD7D-E485-4FAE-8669-D6A672CF6D3E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -39,6 +41,10 @@ Global
{C3FD34EE-C8E1-4A48-885D-1E9AC37B3561}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3FD34EE-C8E1-4A48-885D-1E9AC37B3561}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3FD34EE-C8E1-4A48-885D-1E9AC37B3561}.Release|Any CPU.Build.0 = Release|Any CPU
{A78ACD7D-E485-4FAE-8669-D6A672CF6D3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A78ACD7D-E485-4FAE-8669-D6A672CF6D3E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A78ACD7D-E485-4FAE-8669-D6A672CF6D3E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A78ACD7D-E485-4FAE-8669-D6A672CF6D3E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit a5059ee

Please sign in to comment.