From 634d061e91e36a2c74abeeddbbf12511be20c311 Mon Sep 17 00:00:00 2001 From: lindexi Date: Sun, 22 Mar 2020 21:42:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用 https://github.com/yinghualuowu/JsonModelConvert.git 工具 --- .../Business/Check/FileChecker.cs | 4 +- .../Business/GitLabMRCheckerFlow.cs | 3 +- DotNetGitLabWebHook/Business/Notify.cs | 10 +- .../Controllers/GitLabWebHookController.cs | 23 +- .../Model/GitLabMergeRequest.cs | 645 +++++++++++++----- 5 files changed, 512 insertions(+), 173 deletions(-) diff --git a/DotNetGitLabWebHook/Business/Check/FileChecker.cs b/DotNetGitLabWebHook/Business/Check/FileChecker.cs index 0c8ffff..37abf4d 100644 --- a/DotNetGitLabWebHook/Business/Check/FileChecker.cs +++ b/DotNetGitLabWebHook/Business/Check/FileChecker.cs @@ -14,8 +14,8 @@ public FileChecker(RepoManager repoManager) public void Check(GitLabMergeRequest gitLabMergeRequest) { - var repositoryUrl = gitLabMergeRequest.RawProperty.repository.url; - var repoFolder = RepoManager.GetRepo(repositoryUrl, gitLabMergeRequest.RawProperty.repository.name); + var repositoryUrl = gitLabMergeRequest.RawProperty.Repository.Url; + var repoFolder = RepoManager.GetRepo(repositoryUrl, gitLabMergeRequest.RawProperty.Repository.Name); var git = new Git(repoFolder); diff --git a/DotNetGitLabWebHook/Business/GitLabMRCheckerFlow.cs b/DotNetGitLabWebHook/Business/GitLabMRCheckerFlow.cs index 55e16e8..55d8bc0 100644 --- a/DotNetGitLabWebHook/Business/GitLabMRCheckerFlow.cs +++ b/DotNetGitLabWebHook/Business/GitLabMRCheckerFlow.cs @@ -1,5 +1,4 @@ -using DotNetGitLabWebHook.Model; -using DotNetGitLabWebHookToMatterMost.Business.Check; +using DotNetGitLabWebHookToMatterMost.Business.Check; using DotNetGitLabWebHookToMatterMost.Model; using Microsoft.Extensions.Configuration; diff --git a/DotNetGitLabWebHook/Business/Notify.cs b/DotNetGitLabWebHook/Business/Notify.cs index 5b207ac..deaae71 100644 --- a/DotNetGitLabWebHook/Business/Notify.cs +++ b/DotNetGitLabWebHook/Business/Notify.cs @@ -17,7 +17,7 @@ public Notify(IConfiguration configuration) public IConfiguration Configuration { get; } public void NotifyMatterMost(GitLabMergeRequest gitLabMergeRequest) { - var state = gitLabMergeRequest.RawProperty.object_attributes.state; + var state = gitLabMergeRequest.RawProperty.ObjectAttributes.State; string text = ""; @@ -28,7 +28,7 @@ public void NotifyMatterMost(GitLabMergeRequest gitLabMergeRequest) } else if (state == "merged") { - var username = gitLabMergeRequest.RawProperty.user.username; + var username = gitLabMergeRequest.RawProperty.User.Username; text = $"恭喜@{username} [{gitLabMergeRequest.CommonProperty.Title}]({gitLabMergeRequest.CommonProperty.MergeRequestUrl}) 已经合并"; } else @@ -37,11 +37,11 @@ public void NotifyMatterMost(GitLabMergeRequest gitLabMergeRequest) //var action = gitLabMergeRequest.RawProperty.object_attributes.action; // action 有三个值 update open reopen - var assignees = gitLabMergeRequest.RawProperty.assignees; - var assigneeList = new List { gitLabMergeRequest.RawProperty.object_attributes.assignee?.username }; + var assignees = gitLabMergeRequest.RawProperty.Assignees; + var assigneeList = new List { gitLabMergeRequest.RawProperty.ObjectAttributes.Assignee?.Username }; if (assignees != null) { - assigneeList.AddRange(assignees.Select(temp => temp.username)); + assigneeList.AddRange(assignees.Select(temp => temp.Username)); } var assigneeUsername = string.Join(' ', diff --git a/DotNetGitLabWebHook/Controllers/GitLabWebHookController.cs b/DotNetGitLabWebHook/Controllers/GitLabWebHookController.cs index 39e85ab..0cbf735 100644 --- a/DotNetGitLabWebHook/Controllers/GitLabWebHookController.cs +++ b/DotNetGitLabWebHook/Controllers/GitLabWebHookController.cs @@ -1,5 +1,4 @@ -using DotNetGitLabWebHook.Model; -using DotNetGitLabWebHookToMatterMost.Business; +using DotNetGitLabWebHookToMatterMost.Business; using DotNetGitLabWebHookToMatterMost.Model; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; @@ -31,24 +30,24 @@ public IActionResult MergeRequest(object obj) var rootobject = JsonConvert.DeserializeObject(str); - if (rootobject.object_kind == "merge_request") + if (rootobject.ObjectKind == "merge_request") { - var objectAttributes = rootobject.object_attributes; + var objectAttributes = rootobject.ObjectAttributes; - var sourceGitSshUrl = objectAttributes.source.git_ssh_url; - var sourceBranch = objectAttributes.source_branch; + var sourceGitSshUrl = objectAttributes.Source.GitSshUrl; + var sourceBranch = objectAttributes.SourceBranch; // 最后提交号 - var lastCommitId = objectAttributes.last_commit.id; + var lastCommitId = objectAttributes.LastCommit.Id; - var targetGitSshUrl = objectAttributes.target.git_ssh_url; - var targetBranch = objectAttributes.target_branch; + var targetGitSshUrl = objectAttributes.Target.GitSshUrl; + var targetBranch = objectAttributes.TargetBranch; // MR 标题 - var title = objectAttributes.title; + var title = objectAttributes.Title; - var username = rootobject.user.username; - var mergeRequestUrl = objectAttributes.url; + var username = rootobject.User.Username; + var mergeRequestUrl = objectAttributes.Url; var gitLabMergeRequest = new GitLabMergeRequest { diff --git a/DotNetGitLabWebHook/Model/GitLabMergeRequest.cs b/DotNetGitLabWebHook/Model/GitLabMergeRequest.cs index 6be5eee..b2be2cb 100644 --- a/DotNetGitLabWebHook/Model/GitLabMergeRequest.cs +++ b/DotNetGitLabWebHook/Model/GitLabMergeRequest.cs @@ -1,17 +1,23 @@ using System; +using Newtonsoft.Json; namespace DotNetGitLabWebHookToMatterMost.Model { - // 不清真代码,需要修改属性名 public class GitLabMergeRequest { + // 通过 https://github.com/yinghualuowu/JsonModelConvert.git 优化命名 + + /// + /// 通用的属性 + /// public MergeRequestProperty CommonProperty { get; set; } public Rootobject RawProperty { set; get; } public class MergeRequestProperty { - public MergeRequestProperty(string sourceGitSshUrl, string sourceBranch, string lastCommitId, string targetGitSshUrl, string targetBranch, string title, string username, string mergeRequestUrl) + public MergeRequestProperty(string sourceGitSshUrl, string sourceBranch, string lastCommitId, + string targetGitSshUrl, string targetBranch, string title, string username, string mergeRequestUrl) { SourceGitSshUrl = sourceGitSshUrl; SourceBranch = sourceBranch; @@ -35,202 +41,537 @@ public MergeRequestProperty(string sourceGitSshUrl, string sourceBranch, string public class Rootobject { - public string object_kind { get; set; } - public User user { get; set; } - public Project project { get; set; } - public Repository repository { get; set; } - public Object_Attributes object_attributes { get; set; } - public Label[] labels { get; set; } - public Changes changes { get; set; } - public Assignee[] assignees { get; set; } + /// + /// + /// + [JsonProperty("object_kind")] + public string ObjectKind { get; set; } + + /// + /// + /// + [JsonProperty("user")] + public User User { get; set; } + + /// + /// + /// + [JsonProperty("project")] + public Project Project { get; set; } + + /// + /// + /// + [JsonProperty("repository")] + public Repository Repository { get; set; } + + /// + /// + /// + [JsonProperty("object_attributes")] + public ObjectAttributes ObjectAttributes { get; set; } + + /// + /// + /// + [JsonProperty("labels")] + public Label[] Labels { get; set; } + + /// + /// + /// + [JsonProperty("changes")] + public Changes Changes { get; set; } + + /// + /// + /// + [JsonProperty("assignees")] + public Assignee[] Assignees { get; set; } } public class User { - public string name { get; set; } - public string username { get; set; } - public string avatar_url { get; set; } + /// + /// + /// + [JsonProperty("name")] + public string Name { get; set; } + + /// + /// + /// + [JsonProperty("username")] + public string Username { get; set; } + + /// + /// + /// + [JsonProperty("avatar_url")] + public string AvatarUrl { get; set; } } public class Project { - public int? id { get; set; } - public string name { get; set; } - public string description { get; set; } - public string web_url { get; set; } - public object avatar_url { get; set; } - public string git_ssh_url { get; set; } - public string git_http_url { get; set; } - public string _namespace { get; set; } - public int? visibility_level { get; set; } - public string path_with_namespace { get; set; } - public string default_branch { get; set; } - public string homepage { get; set; } - public string url { get; set; } - public string ssh_url { get; set; } - public string http_url { get; set; } + /// + /// + /// + [JsonProperty("name")] + public string Name { get; set; } + + /// + /// + /// + [JsonProperty("description")] + public string Description { get; set; } + + /// + /// + /// + [JsonProperty("web_url")] + public string WebUrl { get; set; } + + /// + /// + /// + [JsonProperty("avatar_url")] + public object AvatarUrl { get; set; } + + /// + /// + /// + [JsonProperty("git_ssh_url")] + public string GitSshUrl { get; set; } + + /// + /// + /// + [JsonProperty("git_http_url")] + public string GitHttpUrl { get; set; } + + /// + /// + /// + [JsonProperty("_namespace")] + public string Namespace { get; set; } + + /// + /// + /// + [JsonProperty("visibility_level")] + public int? VisibilityLevel { get; set; } + + /// + /// + /// + [JsonProperty("path_with_namespace")] + public string PathWithNamespace { get; set; } + + /// + /// + /// + [JsonProperty("default_branch")] + public string DefaultBranch { get; set; } + + /// + /// + /// + [JsonProperty("homepage")] + public string Homepage { get; set; } + + /// + /// + /// + [JsonProperty("url")] + public string Url { get; set; } + + /// + /// + /// + [JsonProperty("ssh_url")] + public string SshUrl { get; set; } + + /// + /// + /// + [JsonProperty("http_url")] + public string HttpUrl { get; set; } } public class Repository { - public string name { get; set; } - public string url { get; set; } - public string description { get; set; } - public string homepage { get; set; } - } - - public class Object_Attributes - { - public int? id { get; set; } - public string target_branch { get; set; } - public string source_branch { get; set; } - public int? source_project_id { get; set; } - public int? author_id { get; set; } - public int? assignee_id { get; set; } - public string title { get; set; } - public string created_at { get; set; } - public string updated_at { get; set; } - public object milestone_id { get; set; } - public string state { get; set; } - public string merge_status { get; set; } - public int? target_project_id { get; set; } - public int? iid { get; set; } - public string description { get; set; } - public Source source { get; set; } - public Target target { get; set; } - public Last_Commit last_commit { get; set; } - public bool? work_in_progress { get; set; } - public string url { get; set; } - public string action { get; set; } - public Assignee assignee { get; set; } - } - - public class Source - { - public string name { get; set; } - public string description { get; set; } - public string web_url { get; set; } - public object avatar_url { get; set; } - public string git_ssh_url { get; set; } - public string git_http_url { get; set; } - public string _namespace { get; set; } - public int visibility_level { get; set; } - public string path_with_namespace { get; set; } - public string default_branch { get; set; } - public string homepage { get; set; } - public string url { get; set; } - public string ssh_url { get; set; } - public string http_url { get; set; } - } - - public class Target - { - public string name { get; set; } - public string description { get; set; } - public string web_url { get; set; } - public object avatar_url { get; set; } - public string git_ssh_url { get; set; } - public string git_http_url { get; set; } - public string _namespace { get; set; } - public int? visibility_level { get; set; } - public string path_with_namespace { get; set; } - public string default_branch { get; set; } - public string homepage { get; set; } - public string url { get; set; } - public string ssh_url { get; set; } - public string http_url { get; set; } - } - - public class Last_Commit - { - public string id { get; set; } - public string message { get; set; } - public DateTime timestamp { get; set; } - public string url { get; set; } - public Author author { get; set; } + /// + /// + /// + [JsonProperty("name")] + public string Name { get; set; } + + /// + /// + /// + [JsonProperty("url")] + public string Url { get; set; } + + /// + /// + /// + [JsonProperty("description")] + public string Description { get; set; } + + /// + /// + /// + [JsonProperty("homepage")] + public string Homepage { get; set; } + } + + public class ObjectAttributes + { + /// + /// + /// + [JsonProperty("id")] + public int? Id { get; set; } + + /// + /// + /// + [JsonProperty("target_branch")] + public string TargetBranch { get; set; } + + /// + /// + /// + [JsonProperty("source_branch")] + public string SourceBranch { get; set; } + + /// + /// + /// + [JsonProperty("source_project_id")] + public int? SourceProjectId { get; set; } + + /// + /// + /// + [JsonProperty("author_id")] + public int? AuthorId { get; set; } + + /// + /// + /// + [JsonProperty("assignee_id")] + public int? AssigneeId { get; set; } + + /// + /// + /// + [JsonProperty("title")] + public string Title { get; set; } + + /// + /// + /// + [JsonProperty("created_at")] + public string CreatedAt { get; set; } + + /// + /// + /// + [JsonProperty("updated_at")] + public string UpdatedAt { get; set; } + + /// + /// + /// + [JsonProperty("milestone_id")] + public object MilestoneId { get; set; } + + /// + /// + /// + [JsonProperty("state")] + public string State { get; set; } + + /// + /// + /// + [JsonProperty("merge_status")] + public string MergeStatus { get; set; } + + /// + /// + /// + [JsonProperty("target_project_id")] + public int? TargetProjectId { get; set; } + + /// + /// + /// + [JsonProperty("iid")] + public int? Iid { get; set; } + + /// + /// + /// + [JsonProperty("description")] + public string Description { get; set; } + + /// + /// + /// + [JsonProperty("source")] + public Source Source { get; set; } + + /// + /// + /// + [JsonProperty("target")] + public Target Target { get; set; } + + /// + /// + /// + [JsonProperty("last_commit")] + public LastCommit LastCommit { get; set; } + + /// + /// + /// + [JsonProperty("work_in_progress")] + public bool? WorkInProgress { get; set; } + + /// + /// + /// + [JsonProperty("url")] + public string Url { get; set; } + + /// + /// + /// + [JsonProperty("action")] + public string Action { get; set; } + + /// + /// + /// + [JsonProperty("assignee")] + public Assignee Assignee { get; set; } + } + + public class Source : Project + { + } + + public class Target : Project + { + } + + public class LastCommit + { + /// + /// + /// + [JsonProperty("id")] + public string Id { get; set; } + + /// + /// + /// + [JsonProperty("message")] + public string Message { get; set; } + + /// + /// + /// + [JsonProperty("timestamp")] + public DateTime Timestamp { get; set; } + + /// + /// + /// + [JsonProperty("url")] + public string Url { get; set; } + + /// + /// + /// + [JsonProperty("author")] + public Author Author { get; set; } } public class Author { - public string name { get; set; } - public string email { get; set; } + /// + /// + /// + [JsonProperty("name")] + public string Name { get; set; } + + /// + /// + /// + [JsonProperty("email")] + public string Email { get; set; } } public class Assignee { - public string name { get; set; } - public string username { get; set; } - public string avatar_url { get; set; } + /// + /// + /// + [JsonProperty("name")] + public string Name { get; set; } + + /// + /// + /// + [JsonProperty("username")] + public string Username { get; set; } + + /// + /// + /// + [JsonProperty("avatar_url")] + public string AvatarUrl { get; set; } } public class Changes { - public Updated_By_Id updated_by_id { get; set; } - public Updated_At updated_at { get; set; } - public Labels labels { get; set; } + /// + /// + /// + [JsonProperty("updated_by_id")] + public UpdatedById UpdatedById { get; set; } + + /// + /// + /// + [JsonProperty("updated_at")] + public UpdatedAt UpdatedAt { get; set; } + + /// + /// + /// + [JsonProperty("labels")] + public Labels Labels { get; set; } } - public class Updated_By_Id + public class UpdatedById { - public object previous { get; set; } - public int? current { get; set; } + /// + /// + /// + [JsonProperty("previous")] + public object Previous { get; set; } + + /// + /// + /// + [JsonProperty("current")] + public int? Current { get; set; } } - public class Updated_At + public class UpdatedAt { - public string previous { get; set; } - public string current { get; set; } + /// + /// + /// + [JsonProperty("previous")] + public string Previous { get; set; } + + /// + /// + /// + [JsonProperty("current")] + public string Current { get; set; } } public class Labels { - public Previou[] previous { get; set; } - public Current[] current { get; set; } + /// + /// + /// + [JsonProperty("previous")] + public Previou[] Previous { get; set; } + + /// + /// + /// + [JsonProperty("current")] + public Current[] Current { get; set; } } - public class Previou + public class Previou : Label { - public int? id { get; set; } - public string title { get; set; } - public string color { get; set; } - public int? project_id { get; set; } - public string created_at { get; set; } - public string updated_at { get; set; } - public bool? template { get; set; } - public string description { get; set; } - public string type { get; set; } - public int? group_id { get; set; } } - public class Current + public class Current : Label { - public int? id { get; set; } - public string title { get; set; } - public string color { get; set; } - public int? project_id { get; set; } - public string created_at { get; set; } - public string updated_at { get; set; } - public bool? template { get; set; } - public string description { get; set; } - public string type { get; set; } - public int? group_id { get; set; } } public class Label { - public int? id { get; set; } - public string title { get; set; } - public string color { get; set; } - public int? project_id { get; set; } - public string created_at { get; set; } - public string updated_at { get; set; } - public bool? template { get; set; } - public string description { get; set; } - public string type { get; set; } - public int? group_id { get; set; } - } + /// + /// + /// + [JsonProperty("id")] + public int? Id { get; set; } + + /// + /// + /// + [JsonProperty("title")] + public string Title { get; set; } + + /// + /// + /// + [JsonProperty("color")] + public string Color { get; set; } + + /// + /// + /// + [JsonProperty("project_id")] + public int? ProjectId { get; set; } + /// + /// + /// + [JsonProperty("created_at")] + public string CreatedAt { get; set; } + /// + /// + /// + [JsonProperty("updated_at")] + public string UpdatedAt { get; set; } + /// + /// + /// + [JsonProperty("template")] + public bool? Template { get; set; } + + /// + /// + /// + [JsonProperty("description")] + public string Description { get; set; } + + /// + /// + /// + [JsonProperty("type")] + public string Type { get; set; } + + /// + /// + /// + [JsonProperty("group_id")] + public int? GroupId { get; set; } + } } } \ No newline at end of file