Skip to content

Commit 55022db

Browse files
Initial commit
0 parents  commit 55022db

File tree

8 files changed

+1275
-0
lines changed

8 files changed

+1275
-0
lines changed

.gitignore

Lines changed: 408 additions & 0 deletions
Large diffs are not rendered by default.

.vscode/launch.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
// Use IntelliSense to find out which attributes exist for C# debugging
6+
// Use hover for the description of the existing attributes
7+
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
// If you have changed target frameworks, make sure to update the program path.
13+
"program": "${workspaceFolder}/bin/Debug/netcoreapp3.1/IT.GiteaComment.dll",
14+
"args": [],
15+
"cwd": "${workspaceFolder}",
16+
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
17+
"console": "internalConsole",
18+
"stopAtEntry": false
19+
},
20+
{
21+
"name": ".NET Core Attach",
22+
"type": "coreclr",
23+
"request": "attach"
24+
}
25+
]
26+
}

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/IT.GiteaComment.csproj",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/IT.GiteaComment.csproj",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/IT.GiteaComment.csproj"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM mcr.microsoft.com/dotnet/core/runtime:3.1.8-buster-slim
2+
RUN dotnet publish -c Release -o artifacts
3+
RUN rm artifacts/*.pdb
4+
ADD artifacts/* /bin/
5+
RUN chmod +x /bin/IT.GiteaComment
6+
CMD "/bin/IT.GiteaComment"

IT.GiteaComment.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" />
10+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.1.8" />
11+
</ItemGroup>
12+
13+
</Project>

LICENSE.md

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

Program.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Gitea Comment
2+
3+
A Drone plugin to post comments on a Gitea Pull Request
4+
5+
Docker Hub: https://hub.docker.com/r/tsakidev/giteacomment
6+
7+
Example reference for pull request with static string:
8+
9+
```yml
10+
steps:
11+
- name: post-to-gitea-pr
12+
image: tsakidev/giteacomment:latest
13+
settings:
14+
gitea_token:
15+
from_secret: gitea_token
16+
gitea_base_url: http://gitea.example.com
17+
comment: "Hello from Drone"
18+
when:
19+
status: [ failure ]
20+
event: pull_request
21+
```
22+
23+
Example reference for pull request with input from file:
24+
25+
```yml
26+
steps:
27+
- name: post-to-gitea-pr
28+
image: tsakidev/giteacomment:latest
29+
settings:
30+
gitea_token:
31+
from_secret: gitea_token
32+
gitea_base_url: http://gitea.example.com
33+
comment_title: "My Title"
34+
comment_from_file: "/path/to/file.txt"
35+
when:
36+
status: [ failure ]
37+
event: pull_request
38+
```

0 commit comments

Comments
 (0)