c# - 如何使用 .NET 从 gitlab hooks 获取推送信息

标签 c# .net gitlab

我需要写一个网站来显示我们团队的推送信息。现在我遇到一个问题,如何从 gitlab 获取信息?

最佳答案

推送信息的类型是JSON,你需要做的是在gitlab中添加一个URL hook,url就是你的API地址。 就像: pic

然后,点击测试按钮检查你的API

以下是我的 push_events API,:

 public int PushEventInfo([FromBody]PushEvent push)
    {
        bool flag = true;
        ProjectController project = new ProjectController();
        List<string> projectName = new List<string>();
        try
        {
            SqlConnection conn = connectLocaldb.ConnectDataBase();
            conn.Open();
            string sql = "INSERT INTO MemberCommitBeforeCompiling(Username,ProjectName,Version,GroupName,CommitTime,Branch) VALUES ('" + push.user_name + "','" + push.project.name + "','" + push.after + "','" + groupname + "',getdate(),'" + push.@ref + "') ";
            SqlCommand cmd = new SqlCommand(sql, conn);
            int result = cmd.ExecuteNonQuery();
            //判断项目是否已存在
            IList<Project> namelist = project.GetAllProjectInfo();
            foreach(var i in namelist)
            {
               //如果MemberProject表中已经存在该项目
                if (i.projectName.Contains(push.project.name)|| push.project.name.Contains(i.projectName))
                    flag = false;
            }
            if (flag==true)
            {
                sql = "INSERT INTO MemberProject(ProjectName,CommitTime,isdelete) VALUES ('" + push.project.name + "',getdate(),'0') ";
                cmd = new SqlCommand(sql, conn);
                result = cmd.ExecuteNonQuery();
            }
            conn.Close();
            return result;
        }
        catch (Exception e)
        {
            FileStream fs = new FileStream("c:\\test\\log.txt", FileMode.Append, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs); // 创建写入流
            sw.WriteLine(e.ToString()); // 写入
            sw.Close();
            return 0;
        }

关于c# - 如何使用 .NET 从 gitlab hooks 获取推送信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40669888/

相关文章:

c# - MSChart 如何更改一系列破折号之间的间距

c# - ML.NET:功能列的架构不匹配 'Features'

Readme.md 中的 GitLab 引用子模块

c# - 如何让 GridView 适应 Windows Store 应用程序中的不同分辨率

c# 从依赖程序集隐式转换抛出 InvalidCastException

c# - 支持通配符 (*) 的 System.StringComparer

docker - Gitlab CI : How do I make `rules.changes` to compare changed file to main branch?

GitLab:有没有办法保护分支不受命令行影响?

c# - =+ 是什么意思,为什么要编译?

.net - 当我使用 dot Net MS Graph SDK 获取自己的用户信息时出现错误代码 406?