-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
398 additions
and
6 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
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,63 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Collections.Generic; | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
using CKAN.NetKAN.Services; | ||
|
||
namespace CKAN.NetKAN.Sources.Gitlab | ||
{ | ||
/// <summary> | ||
/// Provides convenient access to the GitLab API | ||
/// https://docs.gitlab.com/ee/api/ | ||
/// </summary> | ||
internal sealed class GitlabApi : IGitlabApi | ||
{ | ||
/// <summary> | ||
/// Initialize the API object | ||
/// </summary> | ||
/// <param name="http">HTTP service for getting URLs</param> | ||
/// <param name="token">GitLab API token</param> | ||
public GitlabApi(IHttpService http, string token = null) | ||
{ | ||
this.http = http; | ||
this.token = token; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve info about a GitLab project from the API | ||
/// </summary> | ||
/// <param name="reference">Specification of which project to retrieve</param> | ||
/// <returns>A project object</returns> | ||
public GitlabProject GetProject(GitlabRef reference) | ||
{ | ||
// https://docs.gitlab.com/ee/api/projects | ||
return JsonConvert.DeserializeObject<GitlabProject>( | ||
http.DownloadText( | ||
new Uri(apiBase, $"{reference.Account}%2F{reference.Project}"), | ||
token, null)); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve info about a GitLab project's releases from the API | ||
/// </summary> | ||
/// <param name="reference">Specification of which project's releases to retrieve</param> | ||
/// <returns>Sequence of release objects from the API</returns> | ||
public IEnumerable<GitlabRelease> GetAllReleases(GitlabRef reference) | ||
{ | ||
// https://docs.gitlab.com/ee/api/releases/ | ||
return JArray.Parse( | ||
http.DownloadText( | ||
new Uri(apiBase, $"{reference.Account}%2F{reference.Project}/releases"), | ||
token, null)) | ||
.Select(releaseJson => releaseJson.ToObject<GitlabRelease>()); | ||
} | ||
|
||
private IHttpService http; | ||
private string token; | ||
|
||
private static readonly Uri apiBase = new Uri("https://gitlab.com/api/v4/projects/"); | ||
} | ||
} |
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,18 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace CKAN.NetKAN.Sources.Gitlab | ||
{ | ||
/// <summary> | ||
/// Represents the x_netkan_gitlab object from a netkan | ||
/// </summary> | ||
internal sealed class GitlabOptions | ||
{ | ||
/// <summary> | ||
/// True to use source ZIP for a release. | ||
/// Note that this MUST be true because GitLab only provides source ZIPs! | ||
/// If they add other assets in the future, this requirement can be relaxed. | ||
/// </summary> | ||
[JsonProperty("use_source_archive")] | ||
public readonly bool UseSourceArchive = false; | ||
} | ||
} |
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,25 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace CKAN.NetKAN.Sources.Gitlab | ||
{ | ||
/// <summary> | ||
/// Represents a project from the GitLab API | ||
/// </summary> | ||
public sealed class GitlabProject | ||
{ | ||
[JsonProperty("name")] | ||
public readonly string Name; | ||
|
||
[JsonProperty("description")] | ||
public readonly string Description; | ||
|
||
[JsonProperty("web_url")] | ||
public readonly string WebURL; | ||
|
||
[JsonProperty("issues_enabled")] | ||
public readonly bool IssuesEnabled; | ||
|
||
[JsonProperty("readme_url")] | ||
public readonly string ReadMeURL; | ||
} | ||
} |
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,44 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
using CKAN.NetKAN.Model; | ||
|
||
namespace CKAN.NetKAN.Sources.Gitlab | ||
{ | ||
/// <summary> | ||
/// Represents a GitLab $kref | ||
/// </summary> | ||
internal sealed class GitlabRef : RemoteRef | ||
{ | ||
/// <summary> | ||
/// Initialize the GitLab reference | ||
/// </summary> | ||
/// <param name="reference">The base $kref object from a netkan</param> | ||
public GitlabRef(RemoteRef reference) | ||
: base(reference) | ||
{ | ||
var match = Pattern.Match(reference.Id); | ||
if (match.Success) | ||
{ | ||
Account = match.Groups["account"].Value; | ||
Project = match.Groups["project"].Value; | ||
} | ||
else | ||
{ | ||
throw new Kraken(string.Format(@"Could not parse reference: ""{0}""", reference)); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The first part of the "account/project" path from GitLab | ||
/// </summary> | ||
public readonly string Account; | ||
/// <summary> | ||
/// The second part of the "account/project" path from GitLab | ||
/// </summary> | ||
public readonly string Project; | ||
|
||
private static readonly Regex Pattern = new Regex( | ||
@"^(?<account>[^/]+)/(?<project>[^/]+)$", | ||
RegexOptions.Compiled); | ||
} | ||
} |
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,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
using Newtonsoft.Json; | ||
|
||
namespace CKAN.NetKAN.Sources.Gitlab | ||
{ | ||
/// <summary> | ||
/// Represents a release from the GitLab API | ||
/// </summary> | ||
internal sealed class GitlabRelease | ||
{ | ||
[JsonProperty("tag_name")] | ||
public readonly string TagName; | ||
|
||
[JsonProperty("author")] | ||
public readonly GitlabReleaseAuthor Author = new GitlabReleaseAuthor(); | ||
|
||
[JsonProperty("released_at")] | ||
public readonly DateTime ReleasedAt; | ||
|
||
[JsonProperty("assets")] | ||
public readonly GitlabReleaseAssets Assets = new GitlabReleaseAssets(); | ||
} | ||
|
||
/// <summary> | ||
/// Represents an author from the GitLab API | ||
/// </summary> | ||
internal sealed class GitlabReleaseAuthor | ||
{ | ||
[JsonProperty("name")] | ||
public readonly string Name; | ||
} | ||
|
||
/// <summary> | ||
/// Represents an assets object from the GitLab API | ||
/// </summary> | ||
internal sealed class GitlabReleaseAssets | ||
{ | ||
[JsonProperty("sources")] | ||
public readonly List<GitlabReleaseAssetSource> Sources = new List<GitlabReleaseAssetSource>(); | ||
} | ||
|
||
/// <summary> | ||
/// Represents an assets source object from the GitLab API | ||
/// </summary> | ||
internal sealed class GitlabReleaseAssetSource | ||
{ | ||
[JsonProperty("format")] | ||
public readonly string Format; | ||
|
||
[JsonProperty("url")] | ||
public readonly string 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,25 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace CKAN.NetKAN.Sources.Gitlab | ||
{ | ||
/// <summary> | ||
/// Interface for classes providing access to the GitLab API | ||
/// Allows mocking up in tests | ||
/// </summary> | ||
internal interface IGitlabApi | ||
{ | ||
/// <summary> | ||
/// Retrieve info about a GitLab project from the API | ||
/// </summary> | ||
/// <param name="reference">Specification of which project to retrieve</param> | ||
/// <returns>A project object</returns> | ||
GitlabProject GetProject(GitlabRef reference); | ||
|
||
/// <summary> | ||
/// Retrieve info about a GitLab project's releases from the API | ||
/// </summary> | ||
/// <param name="reference">Specification of which project's releases to retrieve</param> | ||
/// <returns>Sequence of release objects from the API</returns> | ||
IEnumerable<GitlabRelease> GetAllReleases(GitlabRef reference); | ||
} | ||
} |
Oops, something went wrong.