-
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.
Implement Pipeline Schedule Client (#773)
* Implement Pipeline Schedule Client * Fix detailed PipelineSchedule model deserialization * Implement Schedule's pipelines endpoint * Fix naming convention * Add Last Pipeline information * MR Suggestions
- Loading branch information
1 parent
0a601ce
commit d78dcb9
Showing
15 changed files
with
392 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,70 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Mock.Internals; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Mock.Clients; | ||
|
||
internal class PipelineScheduleClient : ClientBase, IPipelineScheduleClient | ||
{ | ||
private readonly ProjectId _projectId; | ||
|
||
public PipelineScheduleClient(ClientContext context, ProjectId projectId) | ||
: base(context) | ||
{ | ||
_projectId = projectId; | ||
} | ||
|
||
public Models.PipelineSchedule this[int id] | ||
{ | ||
get | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var project = GetProject(_projectId, ProjectPermission.View); | ||
var schedule = project.PipelineSchedules.GetById(id); | ||
if (schedule == null) | ||
throw new GitLabNotFoundException(); | ||
|
||
return schedule.ToPipelineScheduleClient(); | ||
} | ||
} | ||
} | ||
|
||
public IEnumerable<PipelineScheduleBasic> All | ||
{ | ||
get | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var project = GetProject(_projectId, ProjectPermission.View); | ||
return project.PipelineSchedules.Select(s => s.ToPipelineScheduleBasicClient()).ToList(); | ||
} | ||
} | ||
} | ||
|
||
public GitLabCollectionResponse<PipelineScheduleBasic> GetAllAsync() | ||
=> GitLabCollectionResponse.Create(All); | ||
|
||
public GitLabCollectionResponse<PipelineBasic> GetAllSchedulePipelinesAsync(int id) | ||
{ | ||
using (Context.BeginOperationScope()) | ||
{ | ||
var project = GetProject(_projectId, ProjectPermission.View); | ||
var pipelines = project.Pipelines | ||
.Where(p => string.Equals(p.Source, "schedule", System.StringComparison.Ordinal)) | ||
.Select(p => p.ToPipelineBasicClient()); | ||
|
||
return GitLabCollectionResponse.Create(pipelines); | ||
} | ||
} | ||
|
||
public async Task<Models.PipelineSchedule> GetByIdAsync(int id, CancellationToken cancellationToken = default) | ||
{ | ||
await Task.Yield(); | ||
|
||
return this[id]; | ||
} | ||
} |
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,81 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab.Mock; | ||
|
||
public sealed class PipelineSchedule : GitLabObject | ||
{ | ||
public Project Project => (Project)Parent; | ||
|
||
public int Id { get; set; } | ||
|
||
public string Description { get; set; } | ||
|
||
public string Ref { get; set; } | ||
|
||
public string Cron { get; set; } | ||
|
||
public DateTime NextRunAt { get; set; } | ||
|
||
public bool Active { get; set; } | ||
|
||
public DateTime CreatedAt { get; set; } | ||
|
||
public DateTime UpdatedAt { get; set; } | ||
|
||
public User Owner { get; set; } | ||
|
||
public IDictionary<string, string> Variables { get; set; } = new Dictionary<string, string>(StringComparer.Ordinal); | ||
|
||
public PipelineScheduleBasic ToPipelineScheduleBasicClient() | ||
=> new() | ||
{ | ||
Id = Id, | ||
Active = Active, | ||
CreatedAt = CreatedAt, | ||
Cron = Cron, | ||
Description = Description, | ||
NextRunAt = NextRunAt, | ||
Owner = Owner.ToClientUser(), | ||
Ref = Ref, | ||
UpdatedAt = UpdatedAt, | ||
}; | ||
|
||
public Models.PipelineSchedule ToPipelineScheduleClient() | ||
=> new() | ||
{ | ||
Id = Id, | ||
Active = Active, | ||
CreatedAt = CreatedAt, | ||
Cron = Cron, | ||
Description = Description, | ||
NextRunAt = NextRunAt, | ||
Owner = Owner.ToClientUser(), | ||
Ref = Ref, | ||
UpdatedAt = UpdatedAt, | ||
Variables = Variables.Select((kvp) => new PipelineVariable | ||
{ | ||
Key = kvp.Key, | ||
Value = kvp.Value, | ||
}), | ||
}; | ||
|
||
public Pipeline AddNewPipeline() | ||
{ | ||
var pipeline = new Pipeline(Ref) | ||
{ | ||
Source = "schedule", | ||
Variables = Variables.Select((kvp) => new PipelineVariable | ||
{ | ||
Key = kvp.Key, | ||
Value = kvp.Value, | ||
}), | ||
User = Owner, | ||
}; | ||
|
||
Project.Pipelines.Add(pipeline); | ||
return pipeline; | ||
} | ||
} |
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,33 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace NGitLab.Mock; | ||
|
||
public class PipelineScheduleCollection : Collection<PipelineSchedule> | ||
{ | ||
private readonly Project _project; | ||
|
||
public PipelineScheduleCollection(Project project) | ||
: base(project) | ||
{ | ||
_project = project; | ||
} | ||
|
||
public PipelineSchedule GetById(int id) | ||
{ | ||
return this.FirstOrDefault(x => x.Id == id); | ||
} | ||
|
||
public override void Add(PipelineSchedule schedule) | ||
{ | ||
if (schedule is null) | ||
throw new ArgumentNullException(nameof(schedule)); | ||
|
||
if (schedule.Id == default) | ||
{ | ||
schedule.Id = Server.GetNewPipelineScheduleId(); | ||
} | ||
|
||
base.Add(schedule); | ||
} | ||
} |
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
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,43 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using NGitLab.Models; | ||
|
||
namespace NGitLab; | ||
|
||
public interface IPipelineScheduleClient | ||
{ | ||
/// <summary> | ||
/// Gets all the pipeline schedules of the project | ||
/// </summary> | ||
IEnumerable<PipelineScheduleBasic> All { get; } | ||
|
||
/// <summary> | ||
/// Details of single schedule | ||
/// </summary> | ||
/// <param name="id">Schedule Id</param> | ||
/// <returns></returns> | ||
PipelineSchedule this[int id] { get; } | ||
|
||
/// <summary> | ||
/// Gets the details of single schedule | ||
/// </summary> | ||
/// <param name="id">Schedule Id</param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task<PipelineSchedule> GetByIdAsync(int id, CancellationToken cancellationToken = default); | ||
|
||
/// <summary> | ||
/// Gets all the pipeline schedules of the project | ||
/// </summary> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
GitLabCollectionResponse<PipelineScheduleBasic> GetAllAsync(); | ||
|
||
/// <summary> | ||
/// Gets all pipelines triggered by a pipeline schedule in a project. | ||
/// </summary> | ||
/// <param name="id">Schedule Id</param> | ||
/// <returns></returns> | ||
GitLabCollectionResponse<PipelineBasic> GetAllSchedulePipelinesAsync(int id); | ||
} |
Oops, something went wrong.