134 lines
5.0 KiB
C#
134 lines
5.0 KiB
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Reflection.Emit;
|
|
using ln.json;
|
|
using ln.json.attributes;
|
|
using ln.type;
|
|
|
|
namespace ln.build.repositories.gitea
|
|
{
|
|
public class GiteaRelease : Release
|
|
{
|
|
public override Repository Repository => GiteaRepository;
|
|
public GiteaRepository GiteaRepository { get; }
|
|
|
|
public GiteaRelease(GiteaRepository repository)
|
|
{
|
|
GiteaRepository = repository;
|
|
}
|
|
public GiteaRelease(GiteaRepository repository,JSONObject jsonRelease) : this(repository)
|
|
{
|
|
UpdateFromJson(jsonRelease);
|
|
}
|
|
|
|
public void UpdateFromJson(JSONObject jsonRelease)
|
|
{
|
|
Id = (int)(long)jsonRelease["id"].ToNative();
|
|
Name = jsonRelease["name"].ToNative().ToString();
|
|
TagName = jsonRelease["tag_name"].ToNative().ToString();
|
|
Body = jsonRelease["body"].ToNative().ToString();
|
|
IsDraft = (bool)jsonRelease["draft"].ToNative();
|
|
IsPreRelease = (bool)jsonRelease["prerelease"].ToNative();
|
|
TargetCommit = jsonRelease["target_commitish"].ToNative().ToString();
|
|
}
|
|
|
|
public JSONObject ToJson()
|
|
{
|
|
JSONObject jsonRelease = new JSONObject();
|
|
jsonRelease.Add("name", Name);
|
|
jsonRelease.Add("tag_name", TagName);
|
|
jsonRelease.Add("target_comittish", TargetCommit);
|
|
jsonRelease.Add("prerelease",IsPreRelease);
|
|
jsonRelease.Add("draft",IsDraft);
|
|
jsonRelease.Add("body",Body);
|
|
|
|
return jsonRelease;
|
|
}
|
|
|
|
public override Attachment CreateOrReplaceAttachment(string localPath, string remoteFileName)
|
|
{
|
|
Attachment attachment = FindAttachmentByName(remoteFileName) ?? new GiteaAttachment(this){ Name = remoteFileName, };
|
|
attachment.Create(localPath);
|
|
return attachment;
|
|
}
|
|
|
|
public override Attachment[] GetAttachments()
|
|
{
|
|
if (HttpStatusCode.OK == GiteaRepository.Client.GetJson(out JSONValue jsonAssets, "repos", GiteaRepository.Owner, GiteaRepository.Name, "releases", Id.ToString(), "assets" ))
|
|
{
|
|
List<Attachment> attachments = new List<Attachment>();
|
|
|
|
foreach (JSONObject jsonAsset in (jsonAssets as JSONArray).Children)
|
|
attachments.Add(new GiteaAttachment(this, jsonAsset));
|
|
|
|
return attachments.ToArray();
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public class GiteaAttachment : Attachment
|
|
{
|
|
public override Release Release => GiteaRelease;
|
|
public GiteaRelease GiteaRelease { get; }
|
|
|
|
int id = -1;
|
|
string downloadURL;
|
|
|
|
public override int Id { get => id; set => throw new NotImplementedException(); }
|
|
public override string Name { get; set; }
|
|
public override string DownloadURL { get => downloadURL; set => throw new NotImplementedException(); }
|
|
|
|
|
|
public GiteaAttachment(GiteaRelease giteaRelease)
|
|
{
|
|
GiteaRelease = giteaRelease;
|
|
}
|
|
public GiteaAttachment(GiteaRelease giteaRelease,JSONObject jsonAttachment) :this(giteaRelease)
|
|
{
|
|
LoadJson(jsonAttachment);
|
|
}
|
|
|
|
private void LoadJson(JSONObject jsonAttachment)
|
|
{
|
|
id = (int)(long)jsonAttachment["id"].ToNative();
|
|
Name = jsonAttachment["name"].ToNative().ToString();
|
|
downloadURL = jsonAttachment["browser_download_url"].ToNative().ToString();
|
|
}
|
|
|
|
public override void Delete()
|
|
{
|
|
if (id != -1)
|
|
{
|
|
if (HttpStatusCode.NoContent != GiteaRelease.GiteaRepository.Client.Delete("repos", GiteaRelease.GiteaRepository.Owner, GiteaRelease.GiteaRepository.Name, "releases", GiteaRelease.Id.ToString(), "assets", id.ToString()))
|
|
throw new Exception(String.Format("could not delete attachment {0}/{1}", GiteaRelease.Id, id));
|
|
|
|
id = -1;
|
|
}
|
|
}
|
|
|
|
public override void Create(string localPath)
|
|
{
|
|
using (FileStream fs = new FileStream(localPath,FileMode.Open))
|
|
{
|
|
StreamContent attachmentBytes = new StreamContent(fs);
|
|
MultipartFormDataContent formDataContent = new MultipartFormDataContent();
|
|
formDataContent.Add(attachmentBytes, "attachment", Name);
|
|
if (HttpStatusCode.Created != GiteaRelease.GiteaRepository.Client.PostContent(formDataContent,out JSONValue response, "repos", GiteaRelease.GiteaRepository.Owner, GiteaRelease.GiteaRepository.Name, "releases", GiteaRelease.Id.ToString(), string.Format("assets?name={0}", Name)))
|
|
{
|
|
throw new Exception(String.Format("could not create attachment to release: {0}", localPath));
|
|
}
|
|
|
|
if (id != -1)
|
|
Delete();
|
|
|
|
LoadJson(response as JSONObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
} |