【问题标题】:GitLab WebHook post-receive with WebAPIGitLab WebHook 使用 WebAPI 进行后期接收
【发布时间】:2015-06-06 21:15:45
【问题描述】:

我正在尝试使用 GitLab webhook 'Push events' 在文件发生更改时通知用户。

根据这个GitLab help,我应该收到这个作为请求正文:

{
  "object_kind": "push",
  "before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
  "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
  "ref": "refs/heads/master",
  "user_id": 4,
  "user_name": "John Smith",
  "user_email": "john@example.com",
  "project_id": 15,
  "repository": {
    "name": "Diaspora",
    "url": "git@example.com:mike/diasporadiaspora.git",
    "description": "",
    "homepage": "http://example.com/mike/diaspora", 
    "git_http_url":"http://example.com/mike/diaspora.git",
    "git_ssh_url":"git@example.com:mike/diaspora.git",
    "visibility_level":0
  },
  "commits": [
    {
      "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "message": "Update Catalan translation to e38cb41.",
      "timestamp": "2011-12-12T14:27:31+02:00",
      "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
      "author": {
        "name": "Jordi Mallach",
        "email": "jordi@softcatala.org"
      }
    },
    {
      "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "message": "fixed readme",
      "timestamp": "2012-01-03T23:36:29+02:00",
      "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
      "author": {
        "name": "GitLab dev user",
        "email": "gitlabdev@dv6700.(none)"
      }
    }
  ],
  "total_commits_count": 4
}

但是我的 WebAPI 方法没有正确解决这个请求,我尝试的是

  • public async Task Post()
  • public void Post([FromBody] dynamic value)
  • public void Post([FromBody] PushEvent value)(强类型)

上述方法似乎都不起作用,但是如example method 所示在 ruby​​ 中尝试此方法确实有效。

所以我想这与我使用 Web API 的方式有关。有什么想法吗?

我的 PushEvent 类如下所示:

public class PushEvent
{         
    public string name { get; set; }
    public string url { get; set; }
    public string description { get; set; }
    public string homepage { get; set; }
    public string git_http_url { get; set; }
    public string git_ssh_url { get; set; }
    public int visibility_level { get; set; }
    public string object_kind { get; set; }
    public string before { get; set; }
    public string after { get; set; }
    public int user_id { get; set; }
    public string user_name { get; set; }
    public string user_email { get; set; }
    public int project_id { get; set; }
    public IList<Commit> commits { get; set; }
    public int total_commits_count { get; set; }    

    public class Author
    {
        public string name { get; set; }
        public string email { get; set; }
    }

    public class 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; }
    }    
}

【问题讨论】:

  • 看看你的 PushEvent 对象是什么样子会很有帮助。
  • 另外,当你说,没有正确解决,这是什么意思?你得到一个404?你的断点没有被击中?您的断点被命中,但对象没有任何值等?
  • 所以在我的情况下我无法调试它,因为我不能给 Gitlab webhook localhost url,所以我发布了我的 webapi 并提供了一个类似 http:abc.com/api/hook 的 URL。但我有一些虚拟日志消息,我可以确认该方法根本没有被命中。

标签: c# asp.net-web-api gitlab


【解决方案1】:

我也遇到过同样的问题,我的问题是由 PAI 码引起的

以下是我的代码: 推送事件:

namespace LuckyGitlabStatWebAPI.Models
{
public class PushEvent
{
    public string object_kind { get; set; }
    public string before { get; set; }
    public string after { get; set; }
    public string @ref { get; set; }
    public int user_id { get; set; }
    public string user_name { get; set; }
    public string user_email { get; set; }
    public string user_avatar { get; set; }
    public int project_id { get; set; }
    public project project;
    public repository repository;
    public List<commits> commits;
    public int total_commits_count { get; set; }
}
public class project
{
    public string name { get; set; }
    public string description { get; set; }
    public string web_url { get; set; }
    public string 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 repository
{
    public string name { get; set; }
    public string url { get; set; }
    public string description { get; set; }
    public string homepage { get; set; }
    public string git_http_url { get; set; }
    public string git_ssh_url { get; set; }
    public int visibility_level { get; set; }
}
public class commits
{
    public string id { get; set; }
    public string message { get; set; }
    public string timestamp { get; set; }
    public string url { get; set; }
    public string added { get; set; }
    public Array modified;
    public Array removed { get; set; }
    public author author;
}
public class author
{
    public string name { get; set; }
    public string email { get; set; }
}
 }

API :how to get push info from gitlab hooks with .NET

【讨论】:

    【解决方案2】:

    我确实找到了问题所在,这是由于身份验证。一旦我允许该方法允许匿名,该方法就成功命中了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-21
      • 2015-11-02
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多