|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Net.Http; |
| 4 | +using Microsoft.Extensions.Configuration; |
| 5 | + |
| 6 | +namespace IT.GiteaComment |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + |
| 11 | + |
| 12 | + static async System.Threading.Tasks.Task Main(string[] args) |
| 13 | + { |
| 14 | + var builder = new ConfigurationBuilder().AddEnvironmentVariables(); |
| 15 | + var configuration = builder.Build(); |
| 16 | + |
| 17 | + |
| 18 | + string GITEA_BASE_URL = Environment.GetEnvironmentVariable("PLUGIN_GITEA_BASE_URL"); |
| 19 | + string GITEA_TOKEN = Environment.GetEnvironmentVariable("PLUGIN_GITEA_TOKEN"); |
| 20 | + string DRONE_REPO_NAMESPACE = Environment.GetEnvironmentVariable("DRONE_REPO_NAMESPACE"); |
| 21 | + string DRONE_REPO_NAME = Environment.GetEnvironmentVariable("DRONE_REPO_NAME"); |
| 22 | + string DRONE_PULL_REQUEST = Environment.GetEnvironmentVariable("DRONE_PULL_REQUEST"); |
| 23 | + string COMMENT_TITLE = Environment.GetEnvironmentVariable("PLUGIN_COMMENT_TITLE"); |
| 24 | + string TEXT_COMMENT = Environment.GetEnvironmentVariable("PLUGIN_COMMENT"); |
| 25 | + string COMMENT_FROM_FILE = Environment.GetEnvironmentVariable("PLUGIN_COMMENT_FROM_FILE"); |
| 26 | + |
| 27 | + Console.WriteLine(COMMENT_FROM_FILE); |
| 28 | + Console.WriteLine(TEXT_COMMENT); |
| 29 | + if (string.IsNullOrEmpty(COMMENT_FROM_FILE) && string.IsNullOrWhiteSpace(COMMENT_FROM_FILE) && string.IsNullOrEmpty(TEXT_COMMENT) && string.IsNullOrWhiteSpace(TEXT_COMMENT)) |
| 30 | + { |
| 31 | + throw new Exception("COMMENT_FROM_FILE and TEXT_COMMENT cannot both be empty!"); |
| 32 | + } |
| 33 | + |
| 34 | + if ( |
| 35 | + (string.IsNullOrEmpty(GITEA_TOKEN) && string.IsNullOrWhiteSpace(GITEA_TOKEN)) || |
| 36 | + (string.IsNullOrEmpty(GITEA_BASE_URL) && string.IsNullOrWhiteSpace(GITEA_BASE_URL)) || |
| 37 | + (string.IsNullOrEmpty(DRONE_REPO_NAMESPACE) && string.IsNullOrWhiteSpace(DRONE_REPO_NAMESPACE)) || |
| 38 | + (string.IsNullOrEmpty(DRONE_REPO_NAME) && string.IsNullOrWhiteSpace(DRONE_REPO_NAME)) || |
| 39 | + (string.IsNullOrEmpty(DRONE_PULL_REQUEST) && string.IsNullOrWhiteSpace(DRONE_PULL_REQUEST)) |
| 40 | + ) |
| 41 | + { |
| 42 | + throw new Exception("GITEA_TOKEN, GITEA_BASE_URL, DRONE_REPO_NAMESPACE, DRONE_REPO_NAME and DRONE_PULL_REQUEST cannot be empty!"); |
| 43 | + } |
| 44 | + |
| 45 | + string comment = string.Empty; |
| 46 | + if (!string.IsNullOrEmpty(COMMENT_FROM_FILE) && !string.IsNullOrWhiteSpace(COMMENT_FROM_FILE)) |
| 47 | + { |
| 48 | + string title = COMMENT_TITLE.ToString().Trim(); |
| 49 | + title = !string.IsNullOrEmpty(title) ? title : "Gitea Comment"; |
| 50 | + Console.WriteLine("Reading from file"); |
| 51 | + comment = $"## {title}\\n```text\\n"+System.IO.File.ReadAllText(COMMENT_FROM_FILE.ToString()).Trim().Replace(Environment.NewLine, "\\n")+"\\n```"; |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + Console.WriteLine("Reading from specified text"); |
| 56 | + comment = TEXT_COMMENT.ToString().Trim(); |
| 57 | + } |
| 58 | + |
| 59 | + string postBody = $"{{\"Body\":\"{comment}\"}}"; |
| 60 | + |
| 61 | + HttpClient client = new HttpClient(); |
| 62 | + client.BaseAddress = new Uri(GITEA_BASE_URL.ToString().Trim()); |
| 63 | + client.DefaultRequestHeaders.Add("Authorization", $"token {GITEA_TOKEN.ToString().Trim()}"); |
| 64 | + HttpContent content = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json"); |
| 65 | + await client.PostAsync($"/api/v1/repos/{DRONE_REPO_NAMESPACE}/{DRONE_REPO_NAME}/issues/{DRONE_PULL_REQUEST}/comments", content); |
| 66 | + content.Dispose(); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments