ln.build/ln.build/repositories/gitea/GiteaRelease.cs

48 lines
1.9 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using ln.json;
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)
{
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();
}
public override void AddAttachement(string localPath, string remoteFileName)
{
using (FileStream fs = new FileStream(localPath,FileMode.Open))
{
StreamContent attachmentBytes = new StreamContent(fs);
MultipartFormDataContent formDataContent = new MultipartFormDataContent();
formDataContent.Add(attachmentBytes, "attachment", remoteFileName);
if (HttpStatusCode.Created != GiteaRepository.Client.PostContent(formDataContent,out JSONValue response, "repos", GiteaRepository.Owner, GiteaRepository.Name, "releases", Id.ToString(), String.Format("assets?name={0}", remoteFileName)))
{
throw new Exception(String.Format("could not create attachment to release: {0}", localPath));
}
}
}
public override Attachement[] GetAttachements()
{
throw new System.NotImplementedException();
}
}
}