Skip to content

Commit

Permalink
Changes in Workflows.
Browse files Browse the repository at this point in the history
Changed implementation based on SonarCloud recommendation.
  • Loading branch information
amsga committed Dec 14, 2024
1 parent f9b3c24 commit 2fe6426
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 48 deletions.
44 changes: 40 additions & 4 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,58 @@ on:
jobs:
build:

runs-on: ubuntu-latest
runs-on: windows-latest

strategy:
matrix:
dotnet: [ '8.0.x' ]
name: .NET ${{ matrix.dotnet }}

steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 17
distribution: 'zulu' # Alternative distribution options are available.
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ matrix.dotnet }}
- name: Cache SonarQube Cloud packages
uses: actions/cache@v4
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarQube Cloud scanner
id: cache-sonar-scanner
uses: actions/cache@v4
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarQube Cloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: powershell
run: |
New-Item -Path .\.sonar\scanner -ItemType Directory
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
- name: Restore dependencies
run: dotnet restore
- name: SonarCloudPrepare
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: .\.sonar\scanner\dotnet-sonarscanner begin /k:"TensionDev_UUIDUtil" /o:"tensiondev" /d:sonar.token="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.scanner.scanAll=false /d:sonar.cs.opencover.reportsPaths=**/coverage.opencover.xml
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
run: dotnet test --no-build --verbosity normal --collect "XPlat Code Coverage;Format=opencover"
- name: SonarCloudAnalyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: .\.sonar\scanner\dotnet-sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
4 changes: 1 addition & 3 deletions UUIDUtil/UUIDUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageId>TensionDev.UUID</PackageId>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<Authors>TensionDev amsga</Authors>
<Company>TensionDev</Company>
<Product>TensionDev.UUID</Product>
Expand All @@ -21,8 +21,6 @@
<PackageReleaseNotes>Change in license to Apache License 2.0.
Release with UUID / GUID Version 1, Version 3, Version 4 and Version 5, and Draft Versions 6 and 7.</PackageReleaseNotes>
<NeutralLanguage>en-SG</NeutralLanguage>
<AssemblyVersion>2.1.0.0</AssemblyVersion>
<FileVersion>2.1.0.0</FileVersion>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
4 changes: 4 additions & 0 deletions UUIDUtil/UUIDv1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class UUIDv1
protected internal static readonly Object s_initLock = new Object();
protected internal static readonly Object s_clockLock = new Object();

protected UUIDv1()
{
}

/// <summary>
/// Initialises a new GUID/UUID based on Version 1 (date-time and MAC address)
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions UUIDUtil/UUIDv3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ namespace TensionDev.UUID
/// </summary>
public class UUIDv3

Check warning on line 26 in UUIDUtil/UUIDv3.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x

Rename class 'UUIDv3' to match pascal case naming rules, consider using 'UuiDv3'. (https://rules.sonarsource.com/csharp/RSPEC-101)
{
protected UUIDv3()
{
}

/// <summary>
/// Initialises a new GUID/UUID based on Version 3 (MD5 namespace name-based)
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions UUIDUtil/UUIDv4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ namespace TensionDev.UUID
/// </summary>
public class UUIDv4

Check warning on line 24 in UUIDUtil/UUIDv4.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x

Rename class 'UUIDv4' to match pascal case naming rules, consider using 'UuiDv4'. (https://rules.sonarsource.com/csharp/RSPEC-101)
{
protected UUIDv4()
{
}

/// <summary>
/// Initialises a new GUID/UUID based on Version 4 (random)
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions UUIDUtil/UUIDv5.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ namespace TensionDev.UUID
/// </summary>
public class UUIDv5

Check warning on line 26 in UUIDUtil/UUIDv5.cs

View workflow job for this annotation

GitHub Actions / .NET 8.0.x

Rename class 'UUIDv5' to match pascal case naming rules, consider using 'UuiDv5'. (https://rules.sonarsource.com/csharp/RSPEC-101)
{
protected UUIDv5()
{
}

/// <summary>
/// Initialises a new GUID/UUID based on Version 5 (SHA-1 namespace name-based)
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions UUIDUtil/UUIDv6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class UUIDv6
protected internal static readonly Object s_initLock = new Object();
protected internal static readonly Object s_clockLock = new Object();

protected UUIDv6()
{
}

/// <summary>
/// Initialises a new GUID/UUID based on Version 6 (date-time)
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions UUIDUtil/UUIDv7.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class UUIDv7
protected internal static UInt16 s_counter = 0;
protected internal static readonly Object s_counterLock = new Object();

protected UUIDv7()
{
}

public enum GenerationMethod
{
/// <summary>
Expand Down
63 changes: 33 additions & 30 deletions UUIDUtil/Uuid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ namespace TensionDev.UUID
{
public sealed class Uuid : IComparable<Uuid>, IEquatable<Uuid>
{
private uint _time_low;
private ushort _time_mid;
private ushort _time_hi_and_version;
private const string INVALID_FORMAT_STRING = "The format of s is invalid";
private const string HEX_FORMAT_STRING = "{0:x2}";
private readonly uint _time_low;
private readonly ushort _time_mid;
private readonly ushort _time_hi_and_version;
private byte _clock_seq_hi_and_reserved;
private byte _clock_seq_low;
private byte[] _node;
private readonly byte _clock_seq_low;
private readonly byte[] _node;

/// <summary>
/// A read-only instance of the Uuid object whose value is all zeros.
Expand All @@ -37,7 +39,7 @@ public sealed class Uuid : IComparable<Uuid>, IEquatable<Uuid>
/// <summary>
/// A read-only instance of the Uuid object whose value is all ones.
/// </summary>
public static readonly Uuid Max = new Uuid(uint.MaxValue, ushort.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff});
public static readonly Uuid Max = new Uuid(uint.MaxValue, ushort.MaxValue, ushort.MaxValue, byte.MaxValue, byte.MaxValue, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });

public Uuid()
{
Expand Down Expand Up @@ -84,20 +86,17 @@ public Uuid(byte[] b) : this()
/// <exception cref="System.OverflowException">The format of s is invalid</exception>
public Uuid(string s) : this()
{
if (s == null)
throw new ArgumentNullException(nameof(s));

if (String.IsNullOrEmpty(s))
throw new FormatException("The format of s is invalid");
throw new ArgumentNullException(nameof(s), INVALID_FORMAT_STRING);

if (s.Length != 32 && s.Length != 36 && s.Length != 38)
throw new FormatException("The format of s is invalid");
throw new FormatException(INVALID_FORMAT_STRING);

if (s.Length == 38)
if (s.Length == 38 &&
!(s.StartsWith("{") && s.EndsWith("}")) &&
!(s.StartsWith("(") && s.EndsWith(")")))
{
if (!(s.StartsWith("{") && s.EndsWith("}")) &&
!(s.StartsWith("(") && s.EndsWith(")")))
throw new FormatException("The format of s is invalid");
throw new FormatException(INVALID_FORMAT_STRING);
}

string vs = s.Replace("{", "");
Expand All @@ -107,15 +106,15 @@ public Uuid(string s) : this()

if (vs.Length == 36)
{
Regex regex = new Regex(@"\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b", RegexOptions.IgnoreCase);
Regex regex = new Regex(@"\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(100));
MatchCollection matches = regex.Matches(vs);
if (matches.Count == 0)
throw new FormatException("The format of s is invalid");
throw new FormatException(INVALID_FORMAT_STRING);
}
vs = vs.Replace("-", "");

if (vs.Length != 32)
throw new FormatException("The format of s is invalid");
throw new FormatException(INVALID_FORMAT_STRING);

Byte[] b = new Byte[16];
for (Int32 i = 0; i < vs.Length; i += 2)
Expand All @@ -126,11 +125,11 @@ public Uuid(string s) : this()
}
catch (FormatException)
{
throw new FormatException("The format of s is invalid");
throw new FormatException(INVALID_FORMAT_STRING);
}
catch (OverflowException)
{
throw new OverflowException("The format of s is invalid");
throw new OverflowException(INVALID_FORMAT_STRING);
}
}

Expand Down Expand Up @@ -356,9 +355,10 @@ public Guid ToVariant2()
{
byte newClockSeq = (byte)(_clock_seq_hi_and_reserved & 0x1F);
newClockSeq = (byte)(newClockSeq | 0xC0);
Uuid variant2 = new Uuid(this.ToByteArray());

variant2._clock_seq_hi_and_reserved = newClockSeq;
Uuid variant2 = new Uuid(this.ToByteArray())
{
_clock_seq_hi_and_reserved = newClockSeq
};

return variant2.ToGuid();
}
Expand All @@ -370,10 +370,13 @@ public Guid ToVariant2()
/// <returns>A TensionDev.UUID.Uuid object.</returns>
public static Uuid ToVariant1(Guid guid)
{
Uuid variant1 = new Uuid(guid.ToString());
byte newClockSeq = (byte)(variant1._clock_seq_hi_and_reserved & 0x3F);
Uuid variant2 = new Uuid(guid.ToString());
byte newClockSeq = (byte)(variant2._clock_seq_hi_and_reserved & 0x3F);
newClockSeq = (byte)(newClockSeq | 0x80);
variant1._clock_seq_hi_and_reserved = newClockSeq;
Uuid variant1 = new Uuid(variant2.ToByteArray())
{
_clock_seq_hi_and_reserved = newClockSeq
};

return variant1;
}
Expand Down Expand Up @@ -432,8 +435,8 @@ private string ToStringCannonical()
sb.Append("-");
sb.Append(BitConverter.ToString(BitConverter.GetBytes(_time_hi_and_version)).Replace("-", ""));
sb.Append("-");
sb.AppendFormat("{0:x2}", _clock_seq_hi_and_reserved);
sb.AppendFormat("{0:x2}", _clock_seq_low);
sb.AppendFormat(HEX_FORMAT_STRING, _clock_seq_hi_and_reserved);
sb.AppendFormat(HEX_FORMAT_STRING, _clock_seq_low);
sb.Append("-");
sb.Append(BitConverter.ToString(_node).Replace("-", ""));

Expand All @@ -447,8 +450,8 @@ private string ToHexString()
sb.Append(BitConverter.ToString(BitConverter.GetBytes(_time_low)).Replace("-", ""));
sb.Append(BitConverter.ToString(BitConverter.GetBytes(_time_mid)).Replace("-", ""));
sb.Append(BitConverter.ToString(BitConverter.GetBytes(_time_hi_and_version)).Replace("-", ""));
sb.AppendFormat("{0:x2}", _clock_seq_hi_and_reserved);
sb.AppendFormat("{0:x2}", _clock_seq_low);
sb.AppendFormat(HEX_FORMAT_STRING, _clock_seq_hi_and_reserved);
sb.AppendFormat(HEX_FORMAT_STRING, _clock_seq_low);
sb.Append(BitConverter.ToString(_node).Replace("-", ""));

return sb.ToString().ToLower();
Expand Down
5 changes: 3 additions & 2 deletions XUnitTestProjectUUID/UnitTestUUIDv1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class UnitTestUUIDv1
[Fact]
public void TestGetNodeID()
{
IList<byte[]> expectedNodeIDs = new List<byte[]>();
List<byte[]> expectedNodeIDs = new List<byte[]>();
int expectedLength = 6;
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface nic in nics)
{
Expand All @@ -24,7 +25,7 @@ public void TestGetNodeID()
if (nics.Length > 0)
Assert.Contains(nodeID, expectedNodeIDs);
else
Assert.True(nodeID.Length == 6);
Assert.Equal(expectedLength, nodeID.Length);
}

[Fact]
Expand Down
5 changes: 3 additions & 2 deletions XUnitTestProjectUUID/UnitTestUUIDv6.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public class UnitTestUUIDv6
[Fact]
public void TestGetNodeID()
{
int expectedLength = 6;
byte[] nodeID = TensionDev.UUID.UUIDv6.GetNodeID();

Assert.True(nodeID.Length == 6);
Assert.Equal(expectedLength, nodeID.Length);
}

[Fact]
Expand Down Expand Up @@ -64,7 +65,7 @@ public void TestGetClockSequence()
[Fact]
public void TestUUIDVariantField()
{
IList<char> expectedVariantField = new List<char>() { '8', '9', 'a', 'b' };
List<char> expectedVariantField = new List<char>() { '8', '9', 'a', 'b' };

ConcurrentBag<String> concurrentBag = new ConcurrentBag<String>();

Expand Down
Loading

0 comments on commit 2fe6426

Please sign in to comment.