-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Lint endpoints (#460)
* Add Lint Client * Update PublicAPI.Unshipped.txt * Update 7 files * Update PublicAPI.Unshipped.txt * Put the tests yamls in constants
- Loading branch information
1 parent
3fc47f9
commit 5ac8552
Showing
10 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Mock.Clients | ||
{ | ||
internal class LintClient : ILintClient | ||
{ | ||
private readonly ClientContext _context; | ||
|
||
public LintClient(ClientContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public Task<LintCI> ValidateCIYamlContentAsync(string projectId, string yamlContent, LintCIOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Task<LintCI> ValidateProjectCIConfigurationAsync(string projectId, LintCIOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
using NGitLab.Tests.Docker; | ||
using NUnit.Framework; | ||
|
||
namespace NGitLab.Tests | ||
{ | ||
public class LintClientTests | ||
{ | ||
private const string ValidCIYaml = @" | ||
variables: | ||
CI_DEBUG_TRACE: ""true"" | ||
build: | ||
script: | ||
- echo test | ||
"; | ||
|
||
private const string InvalidCIYaml = @" | ||
variables: | ||
CI_DEBUG_TRACE: ""true"" | ||
build: | ||
script: | ||
- echo test | ||
this_key_should_not_exist: | ||
- this should fail the linting | ||
"; | ||
|
||
[Test] | ||
[NGitLabRetry] | ||
public async Task LintValidCIYaml() | ||
{ | ||
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(), CancellationToken.None); | ||
|
||
Assert.True(result.Valid); | ||
Assert.False(result.Errors.Any()); | ||
Assert.False(result.Warnings.Any()); | ||
} | ||
|
||
[Test] | ||
[NGitLabRetry] | ||
public async Task LintInvalidCIYaml() | ||
{ | ||
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(), InvalidCIYaml, new(), CancellationToken.None); | ||
|
||
Assert.False(result.Valid); | ||
Assert.True(result.Errors.Any()); | ||
Assert.False(result.Warnings.Any()); | ||
} | ||
|
||
[Test] | ||
[NGitLabRetry] | ||
public async Task LintValidCIProjectYaml() | ||
{ | ||
using var context = await GitLabTestContext.CreateAsync(); | ||
var project = context.CreateProject(); | ||
var lintClient = context.Client.Lint; | ||
|
||
context.Client.GetRepository(project.Id).Files.Create(new FileUpsert | ||
{ | ||
Branch = project.DefaultBranch, | ||
CommitMessage = "test", | ||
Path = ".gitlab-ci.yml", | ||
Content = ValidCIYaml, | ||
}); | ||
|
||
var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None); | ||
|
||
Assert.True(result.Valid); | ||
Assert.False(result.Errors.Any()); | ||
Assert.False(result.Warnings.Any()); | ||
} | ||
|
||
[Test] | ||
[NGitLabRetry] | ||
public async Task LintInvalidProjectCIYaml() | ||
{ | ||
using var context = await GitLabTestContext.CreateAsync(); | ||
var project = context.CreateProject(); | ||
var lintClient = context.Client.Lint; | ||
|
||
context.Client.GetRepository(project.Id).Files.Create(new FileUpsert | ||
{ | ||
Branch = project.DefaultBranch, | ||
CommitMessage = "test", | ||
Path = ".gitlab-ci.yml", | ||
Content = InvalidCIYaml, | ||
}); | ||
|
||
var result = await context.Client.Lint.ValidateProjectCIConfigurationAsync(project.Id.ToString(), new(), CancellationToken.None); | ||
|
||
Assert.False(result.Valid); | ||
Assert.True(result.Errors.Any()); | ||
Assert.False(result.Warnings.Any()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab | ||
{ | ||
/// <summary> | ||
/// https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/lint.md | ||
/// </summary> | ||
public interface ILintClient | ||
{ | ||
Task<LintCI> ValidateCIYamlContentAsync(string projectId, string yamlContent, LintCIOptions options, CancellationToken cancellationToken = default); | ||
|
||
Task<LintCI> ValidateProjectCIConfigurationAsync(string projectId, LintCIOptions options, CancellationToken cancellationToken = default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Impl | ||
{ | ||
public class LintClient : ILintClient | ||
{ | ||
private readonly API _api; | ||
|
||
public LintClient(API api) | ||
{ | ||
_api = api; | ||
} | ||
|
||
public Task<LintCI> ValidateCIYamlContentAsync(string projectId, string yamlContent, LintCIOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
var url = BuildLintCIUrl(projectId, options); | ||
var data = new | ||
{ | ||
content = yamlContent, | ||
}; | ||
|
||
return _api.Post().With(data).ToAsync<LintCI>(url, cancellationToken); | ||
} | ||
|
||
public Task<LintCI> ValidateProjectCIConfigurationAsync(string projectId, LintCIOptions options, CancellationToken cancellationToken = default) | ||
{ | ||
var url = BuildLintCIUrl(projectId, options); | ||
|
||
return _api.Get().ToAsync<LintCI>(url, cancellationToken); | ||
} | ||
|
||
private static string BuildLintCIUrl(string projectId, LintCIOptions options) | ||
{ | ||
var url = Project.Url + "/" + WebUtility.UrlEncode(projectId) + LintCI.Url; | ||
|
||
url = Utils.AddParameter(url, "dry_run", options.DryRun); | ||
url = Utils.AddParameter(url, "ref", options.Ref); | ||
url = Utils.AddParameter(url, "include_jobs", options.IncludeJobs); | ||
|
||
return url; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace NGitLab.Models | ||
{ | ||
public class LintCI | ||
{ | ||
public const string Url = "/ci/lint"; | ||
|
||
[JsonPropertyName("valid")] | ||
public bool Valid { get; set; } | ||
|
||
[JsonPropertyName("merged_yaml")] | ||
public string MergedYaml { get; set; } | ||
|
||
[JsonPropertyName("errors")] | ||
public string[] Errors { get; set; } | ||
|
||
[JsonPropertyName("warnings")] | ||
public string[] Warnings { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace NGitLab.Models | ||
{ | ||
public class LintCIOptions | ||
{ | ||
/// <summary> | ||
/// Run pipeline creation simulation, or only do static check. | ||
/// </summary> | ||
public bool? DryRun { get; set; } | ||
|
||
/// <summary> | ||
/// If the list of jobs that would exist in a static check or pipeline simulation should be included in the response. | ||
/// </summary> | ||
public bool? IncludeJobs { get; set; } | ||
|
||
/// <summary> | ||
/// When dry_run is true, sets the branch or tag to use. | ||
/// </summary> | ||
public string Ref { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters