Skip to content

Commit

Permalink
Support pipeline retry endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Sep 18, 2023
1 parent a6333c1 commit daa8d27
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
14 changes: 14 additions & 0 deletions NGitLab.Mock/Clients/PipelineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,19 @@ public GitLabCollectionResponse<PipelineVariable> GetVariablesAsync(int pipeline
{
return GitLabCollectionResponse.Create(GetVariables(pipelineId));
}

public Task<Models.Pipeline> RetryAsync(int pipelineId, CancellationToken cancellationToken = default)
{
using (Context.BeginOperationScope())
{
var jobs = _jobClient.GetJobs(JobScopeMask.Failed).Where(j => j.Pipeline.Id == pipelineId);
foreach (var job in jobs)
{
_jobClient.RunAction(job.Id, JobAction.Retry);
}

return Task.FromResult(this[pipelineId]);
}
}
}
}
23 changes: 20 additions & 3 deletions NGitLab.Tests/PipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,30 @@ public async Task Test_get_triggered_pipeline_variables()
var trigger = triggers.Create("Test Trigger");
var ciJobToken = trigger.Token;

var pipeline = pipelineClient.CreatePipelineWithTrigger(ciJobToken, project.DefaultBranch, new Dictionary<string, string>(StringComparer.InvariantCulture) { { "Test", "HelloWorld" } });
var pipeline = pipelineClient.CreatePipelineWithTrigger(ciJobToken, project.DefaultBranch, new Dictionary<string, string>(StringComparer.Ordinal) { { "Test", "HelloWorld" } });

var variables = pipelineClient.GetVariables(pipeline.Id);

Assert.IsTrue(variables.Any(v =>
v.Key.Equals("Test", StringComparison.InvariantCulture) &&
v.Value.Equals("HelloWorld", StringComparison.InvariantCulture)));
v.Key.Equals("Test", StringComparison.Ordinal) &&
v.Value.Equals("HelloWorld", StringComparison.Ordinal)));
}

[Test]
[NGitLabRetry]
public async Task Test_retry()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var pipelineClient = context.Client.GetPipelines(project.Id);
JobTests.AddGitLabCiFile(context.Client, project);

var triggers = context.Client.GetTriggers(project.Id);
var trigger = triggers.Create("Test Trigger");
var ciJobToken = trigger.Token;

var pipeline = pipelineClient.CreatePipelineWithTrigger(ciJobToken, project.DefaultBranch, new Dictionary<string, string>(StringComparer.Ordinal) { { "Test", "HelloWorld" } });
var retriedPipeline = pipelineClient.RetryAsync(pipeline.Id);
}
}
}
2 changes: 2 additions & 0 deletions NGitLab/IPipelineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@ public interface IPipelineClient
/// </summary>
/// <param name="query"></param>
GitLabCollectionResponse<Bridge> GetBridgesAsync(PipelineBridgeQuery query);

Task<Pipeline> RetryAsync(int pipelineId, CancellationToken cancellationToken = default);
}
}
6 changes: 6 additions & 0 deletions NGitLab/Impl/PipelineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,11 @@ private string CreateGetBridgesUrl(PipelineBridgeQuery query)
url = Utils.AddParameter(url, "scope", query.Scope);
return url;
}

public Task<Pipeline> RetryAsync(int pipelineId, CancellationToken cancellationToken = default)
{
var url = $"{_pipelinesPath}/{pipelineId.ToStringInvariant()}/retry";
return _api.Post().ToAsync<Pipeline>(url, cancellationToken);
}
}
}
2 changes: 2 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ NGitLab.Impl.PipelineClient.GetTestReportsSummary(int pipelineId) -> NGitLab.Mod
NGitLab.Impl.PipelineClient.GetVariables(int pipelineId) -> System.Collections.Generic.IEnumerable<NGitLab.Models.PipelineVariable>
NGitLab.Impl.PipelineClient.GetVariablesAsync(int pipelineId) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.PipelineVariable>
NGitLab.Impl.PipelineClient.PipelineClient(NGitLab.Impl.API api, int projectId) -> void
NGitLab.Impl.PipelineClient.RetryAsync(int pipelineId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.Pipeline>
NGitLab.Impl.PipelineClient.Search(NGitLab.Models.PipelineQuery query) -> System.Collections.Generic.IEnumerable<NGitLab.Models.PipelineBasic>
NGitLab.Impl.PipelineClient.SearchAsync(NGitLab.Models.PipelineQuery query) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.PipelineBasic>
NGitLab.Impl.PipelineClient.this[int id].get -> NGitLab.Models.Pipeline
Expand Down Expand Up @@ -857,6 +858,7 @@ NGitLab.IPipelineClient.GetTestReports(int pipelineId) -> NGitLab.Models.TestRep
NGitLab.IPipelineClient.GetTestReportsSummary(int pipelineId) -> NGitLab.Models.TestReportSummary
NGitLab.IPipelineClient.GetVariables(int pipelineId) -> System.Collections.Generic.IEnumerable<NGitLab.Models.PipelineVariable>
NGitLab.IPipelineClient.GetVariablesAsync(int pipelineId) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.PipelineVariable>
NGitLab.IPipelineClient.RetryAsync(int pipelineId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.Pipeline>
NGitLab.IPipelineClient.Search(NGitLab.Models.PipelineQuery query) -> System.Collections.Generic.IEnumerable<NGitLab.Models.PipelineBasic>
NGitLab.IPipelineClient.SearchAsync(NGitLab.Models.PipelineQuery query) -> NGitLab.GitLabCollectionResponse<NGitLab.Models.PipelineBasic>
NGitLab.IPipelineClient.this[int id].get -> NGitLab.Models.Pipeline
Expand Down

0 comments on commit daa8d27

Please sign in to comment.