Skip to content

Commit

Permalink
Add ci lint job (#860)
Browse files Browse the repository at this point in the history
* Improve Asserts on LintClientTests

* Add Jobs details in LintCi response
  • Loading branch information
PaysPlat authored Feb 19, 2025
1 parent b64cb44 commit 3ef67c3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 15 deletions.
80 changes: 65 additions & 15 deletions NGitLab.Tests/LintClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@ public class LintClientTests
private const string ValidCIYaml = @"
variables:
CI_DEBUG_TRACE: ""true""
build:
build-job:
stage: build
tags:
- Runner-Build
before_script:
- echo before start
- echo before end
script:
- echo test
- echo test start
- echo test end
after_script:
- echo after start
- echo after end
when: always
allow_failure: true
";

private const string InvalidCIYaml = @"
variables:
CI_DEBUG_TRACE: ""true""
build:
build-job:
script:
- echo test
this_key_should_not_exist:
Expand All @@ -36,9 +48,38 @@ public async Task LintValidCIYaml()

var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), ValidCIYaml, new(), CancellationToken.None);

Assert.That(result.Valid, Is.True);
Assert.That(result.Errors.Length != 0, Is.False);
Assert.That(result.Warnings.Length != 0, Is.False);
Assert.Multiple(() =>
{
Assert.That(result.Valid, Is.True);
Assert.That(result.Errors, Is.Empty);
Assert.That(result.Warnings, Is.Empty);
});
}

[Test]
[NGitLabRetry]
public async Task LintValidCIYamlWithJobs()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var lintClient = context.Client.Lint;

var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), ValidCIYaml, new() { IncludeJobs = true }, CancellationToken.None);

Assert.That(result.Jobs, Has.Length.EqualTo(1));
var job = result.Jobs[0];
Assert.Multiple(() =>
{
Assert.That(job.Name, Is.EqualTo("build-job"));
Assert.That(job.Stage, Is.EqualTo("build"));
Assert.That(job.BeforeScript, Is.EqualTo(["echo before start", "echo before end"]));
Assert.That(job.Script, Is.EqualTo(["echo test start", "echo test end"]));
Assert.That(job.AfterScript, Is.EqualTo(["echo after start", "echo after end"]));
Assert.That(job.TagList, Is.EqualTo(["Runner-Build"]));
Assert.That(job.Environment, Is.Null);
Assert.That(job.When, Is.EqualTo("always"));
Assert.That(job.AllowFailure, Is.True);
});
}

[Test]
Expand All @@ -51,9 +92,12 @@ public async Task LintInvalidCIYaml()

var result = await context.Client.Lint.ValidateCIYamlContentAsync(project.Id.ToString(), InvalidCIYaml, new(), CancellationToken.None);

Assert.That(result.Valid, Is.False);
Assert.That(result.Errors.Length != 0, Is.True);
Assert.That(result.Warnings.Length != 0, Is.False);
Assert.Multiple(() =>
{
Assert.That(result.Valid, Is.False);
Assert.That(result.Errors, Is.Not.Empty);
Assert.That(result.Warnings, Is.Empty);
});
}

[Test]
Expand All @@ -74,9 +118,12 @@ public async Task LintValidCIProjectYaml()

var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None);

Assert.That(result.Valid, Is.True);
Assert.That(result.Errors.Length != 0, Is.False);
Assert.That(result.Warnings.Length != 0, Is.False);
Assert.Multiple(() =>
{
Assert.That(result.Valid, Is.True);
Assert.That(result.Errors, Is.Empty);
Assert.That(result.Warnings, Is.Empty);
});
}

[Test]
Expand All @@ -97,8 +144,11 @@ public async Task LintInvalidProjectCIYaml()

var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None);

Assert.That(result.Valid, Is.False);
Assert.That(result.Errors.Length != 0, Is.True);
Assert.That(result.Warnings.Length != 0, Is.False);
Assert.Multiple(() =>
{
Assert.That(result.Valid, Is.False);
Assert.That(result.Errors, Is.Not.Empty);
Assert.That(result.Warnings, Is.Empty);
});
}
}
3 changes: 3 additions & 0 deletions NGitLab/Models/LintCI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public class LintCI

[JsonPropertyName("warnings")]
public string[] Warnings { get; set; }

[JsonPropertyName("jobs")]
public LintCIJob[] Jobs { get; set; }
}
34 changes: 34 additions & 0 deletions NGitLab/Models/LintCIJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Text.Json.Serialization;

namespace NGitLab.Models;

public class LintCIJob
{
[JsonPropertyName("name")]
public string Name { get; set; }

[JsonPropertyName("stage")]
public string Stage { get; set; }

[JsonPropertyName("before_script")]
public string[] BeforeScript { get; set; }

[JsonPropertyName("script")]
public string[] Script { get; set; }

[JsonPropertyName("after_script")]
public string[] AfterScript { get; set; }

[JsonPropertyName("tag_list")]
public string[] TagList { get; set; }

[JsonPropertyName("environment")]
public string Environment { get; set; }

[JsonPropertyName("when")]
public string When { get; set; }

[JsonPropertyName("allow_failure")]
public bool AllowFailure { get; set; }

}
22 changes: 22 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2578,13 +2578,35 @@ NGitLab.Models.LineRange.Start.set -> void
NGitLab.Models.LintCI
NGitLab.Models.LintCI.Errors.get -> string[]
NGitLab.Models.LintCI.Errors.set -> void
NGitLab.Models.LintCI.Jobs.get -> NGitLab.Models.LintCIJob[]
NGitLab.Models.LintCI.Jobs.set -> void
NGitLab.Models.LintCI.LintCI() -> void
NGitLab.Models.LintCI.MergedYaml.get -> string
NGitLab.Models.LintCI.MergedYaml.set -> void
NGitLab.Models.LintCI.Valid.get -> bool
NGitLab.Models.LintCI.Valid.set -> void
NGitLab.Models.LintCI.Warnings.get -> string[]
NGitLab.Models.LintCI.Warnings.set -> void
NGitLab.Models.LintCIJob
NGitLab.Models.LintCIJob.AfterScript.get -> string[]
NGitLab.Models.LintCIJob.AfterScript.set -> void
NGitLab.Models.LintCIJob.AllowFailure.get -> bool
NGitLab.Models.LintCIJob.AllowFailure.set -> void
NGitLab.Models.LintCIJob.BeforeScript.get -> string[]
NGitLab.Models.LintCIJob.BeforeScript.set -> void
NGitLab.Models.LintCIJob.Environment.get -> string
NGitLab.Models.LintCIJob.Environment.set -> void
NGitLab.Models.LintCIJob.LintCIJob() -> void
NGitLab.Models.LintCIJob.Name.get -> string
NGitLab.Models.LintCIJob.Name.set -> void
NGitLab.Models.LintCIJob.Script.get -> string[]
NGitLab.Models.LintCIJob.Script.set -> void
NGitLab.Models.LintCIJob.Stage.get -> string
NGitLab.Models.LintCIJob.Stage.set -> void
NGitLab.Models.LintCIJob.TagList.get -> string[]
NGitLab.Models.LintCIJob.TagList.set -> void
NGitLab.Models.LintCIJob.When.get -> string
NGitLab.Models.LintCIJob.When.set -> void
NGitLab.Models.LintCIOptions
NGitLab.Models.LintCIOptions.DryRun.get -> bool?
NGitLab.Models.LintCIOptions.DryRun.set -> void
Expand Down

0 comments on commit 3ef67c3

Please sign in to comment.