.Net 创建webhooks Demo,可用于Gitlab等Workflow
框架:.Net 6 、webapi
package:GitLabApiClient
定义body参数DTO,不同event有不同的参数,具体可参考gitlab文档:https://docs.gitlab.com/ee/user/project/integrations/webhook_events.html
public class GitLabWebhookData
{
public string Ref { get; set; }
public string Before { get; set; }
public string object_kind { get; set; }
public string event_type { get; set; }
public string After { get; set; }
public string UserName { get; set; }
public string UserEmail { get; set; }
public List<Commit> Commits { get; set; }
public ObjectAttributes object_attributes { get; set; }
public User User { get; set; }
}
public class Commit
{
public string Id { get; set; }
public string Message { get; set; }
}
public class ObjectAttributes
{
public string Note { get; set; }
public string noteable_type { get; set; }
}
public class User
{
public string Name { get; set; }
public string Email { get; set; }
// Add more properties as needed
}
定义controller,一定要是Post
请求
[HttpPost("webhooks"),AllowAnonymous]
public async Task<string> Demo([FromBody] GitLabWebhookData data)
{
_logger.LogInformation(JsonConvert.SerializeObject(data));
### Add your workflow
return JsonConvert.SerializeObject(data);
}
部署之后,把 http:XXXXX/api/webhooks
放入到Url中,并选择要触发的事件
触发comment event返回的结果如下:
{"Ref":null,"Before":null,"object_kind":"note","event_type":"note","After":null,"UserName":null,"UserEmail":null,"Commits":null,"object_attributes":{"Note":"656565","noteable_type":"MergeRequest"},"User":{"Name":"XXXX","Email":"[REDACTED]"}}
若有需要,我们也可以主动操作gitlab,透过api请求,下面使用packageGitLabApiClient
来做Demo。首先需要设置Access Tokens,
// if you have auth token:
GitLabClient client = new GitLabClient("https://gitlab.XXX.com.cn", "U2WPMRgfXUydn1QKafcS");
//使用GitLabClient 创建comment event
var result = await client.MergeRequests.CreateNoteAsync(2583, 1, new CreateMergeRequestNoteRequest("8787887887788788787"));
return JsonConvert.SerializeObject(result);
我们也可以通过api请求,本质上package也是透过api请求的
HttpClient client = new HttpClient();
var gitLabUrl = "https://gitlab-k8s.XXXX.com.cn";
var privateToken = "U2WPMRgfXUydn1QKafcS";
var projectId = "2583";
var mergeRequestId = "1";
var comment = "your-comment";
client.DefaultRequestHeaders.Add("PRIVATE-TOKEN", privateToken);
var tempc = JsonConvert.SerializeObject(new { body = comment });
var content = new StringContent(tempc, Encoding.UTF8, "application/json");
var response = await client.PostAsync($"{gitLabUrl}/api/v4/projects/{projectId}/merge_requests/{mergeRequestId}/notes", content);
Console.WriteLine(await response.Content.ReadAsStringAsync());
//{"id":133288,"type":null,"body":"your-comment","attachment":null,"author":{"id":1129,"username":"project_2583_bot","name":"****","state":"active","avatar_url":null,"web_url":"https://gitlab-k8s.wzs.wistron.com.cn/project_2583_bot"},"created_at":"2024-03-08T11:49:33.891+08:00","updated_at":"2024-03-08T11:49:33.891+08:00","system":false,"noteable_id":46213,"noteable_type":"MergeRequest","resolvable":false,"confidential":false,"internal":false,"noteable_iid":1,"commands_changes":{}}