Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get job artifact by Ref #564

Merged
merged 18 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions NGitLab.Mock/Clients/JobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public byte[] GetJobArtifact(int jobId, string path)
throw new NotImplementedException();
}

public byte[] GetJobArtifact(JobArtifactQuery query)
{
throw new NotImplementedException();
}

public IEnumerable<Models.Job> GetJobs(JobScopeMask scope)
{
return GetJobs(new JobQuery { Scope = scope });
Expand Down
27 changes: 27 additions & 0 deletions NGitLab.Tests/JobTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,5 +226,32 @@ public async Task Test_get_job_artifact()
Assert.AreEqual("test", content);
}
}

[Test]
[NGitLabRetry]
public async Task Test_get_job_artifact_query()
{
using var context = await GitLabTestContext.CreateAsync();
var project = context.CreateProject();
var jobsClient = context.Client.GetJobs(project.Id);
using (await context.StartRunnerForOneJobAsync(project.Id))
{
AddGitLabCiFile(context.Client, project);
var jobs = await GitLabTestContext.RetryUntilAsync(() => jobsClient.GetJobs(JobScopeMask.Success), jobs => jobs.Any(), TimeSpan.FromMinutes(2));
var job = jobs.Single();
Assert.AreEqual(JobStatus.Success, job.Status);

var query = new JobArtifactQuery();
query.RefName = project.DefaultBranch;
query.JobName = job.Name;
query.ArtifactPath = "file0.txt";

var artifact = jobsClient.GetJobArtifact(query);
Assert.IsNotEmpty(artifact);

var content = Encoding.ASCII.GetString(artifact).Trim();
Assert.AreEqual("test", content);
}
}
}
}
2 changes: 2 additions & 0 deletions NGitLab/IJobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface IJobClient

byte[] GetJobArtifact(int jobId, string path);

byte[] GetJobArtifact(JobArtifactQuery query);

string GetTrace(int jobId);

Task<string> GetTraceAsync(int jobId, CancellationToken cancellationToken = default);
Expand Down
12 changes: 12 additions & 0 deletions NGitLab/Impl/JobClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ public byte[] GetJobArtifact(int jobId, string path)
return result;
}

public byte[] GetJobArtifact(JobArtifactQuery query)
{
byte[] result = null;
_api.Get().Stream($"{_jobsPath}/artifacts/{query.RefName}/raw/{query.ArtifactPath}?job={query.JobName}", s =>
{
using var ms = new MemoryStream();
s.CopyTo(ms);
result = ms.ToArray();
});
return result;
}

public string GetTrace(int jobId)
{
var result = string.Empty;
Expand Down
16 changes: 16 additions & 0 deletions NGitLab/Models/JobArtifactQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace NGitLab.Models
{
public class JobArtifactQuery
shivan marked this conversation as resolved.
Show resolved Hide resolved
{
[JsonPropertyName("ref_name")]
public string RefName;

[JsonPropertyName("artifact_path")]
public string ArtifactPath;

[JsonPropertyName("job")]
public string JobName;
shivan marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 7 additions & 0 deletions NGitLab/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ NGitLab.IJobClient
NGitLab.IJobClient.Get(int jobId) -> NGitLab.Models.Job
NGitLab.IJobClient.GetAsync(int jobId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.Job>
NGitLab.IJobClient.GetJobArtifact(int jobId, string path) -> byte[]
NGitLab.IJobClient.GetJobArtifact(NGitLab.Models.JobArtifactQuery query) -> byte[]
NGitLab.IJobClient.GetJobArtifacts(int jobId) -> byte[]
NGitLab.IJobClient.GetJobs(NGitLab.Models.JobQuery query) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Job>
NGitLab.IJobClient.GetJobs(NGitLab.Models.JobScopeMask scope) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Job>
Expand Down Expand Up @@ -593,6 +594,7 @@ NGitLab.Impl.JobClient
NGitLab.Impl.JobClient.Get(int jobId) -> NGitLab.Models.Job
NGitLab.Impl.JobClient.GetAsync(int jobId, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task<NGitLab.Models.Job>
NGitLab.Impl.JobClient.GetJobArtifact(int jobId, string path) -> byte[]
NGitLab.Impl.JobClient.GetJobArtifact(NGitLab.Models.JobArtifactQuery query) -> byte[]
NGitLab.Impl.JobClient.GetJobArtifacts(int jobId) -> byte[]
NGitLab.Impl.JobClient.GetJobs(NGitLab.Models.JobQuery query) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Job>
NGitLab.Impl.JobClient.GetJobs(NGitLab.Models.JobScopeMask scope) -> System.Collections.Generic.IEnumerable<NGitLab.Models.Job>
Expand Down Expand Up @@ -2031,6 +2033,11 @@ NGitLab.Models.JobAction.Cancel = 0 -> NGitLab.Models.JobAction
NGitLab.Models.JobAction.Erase = 2 -> NGitLab.Models.JobAction
NGitLab.Models.JobAction.Play = 3 -> NGitLab.Models.JobAction
NGitLab.Models.JobAction.Retry = 1 -> NGitLab.Models.JobAction
NGitLab.Models.JobArtifactQuery
NGitLab.Models.JobArtifactQuery.ArtifactPath -> string
NGitLab.Models.JobArtifactQuery.JobArtifactQuery() -> void
NGitLab.Models.JobArtifactQuery.JobName -> string
NGitLab.Models.JobArtifactQuery.RefName -> string
NGitLab.Models.JobBasic
NGitLab.Models.JobBasic.Commit.get -> NGitLab.Models.Commit
NGitLab.Models.JobBasic.JobBasic() -> void
Expand Down