webhook的作用:当gitlab server上有任何动作触发时,server端会自动返回一段json数据,记录该动作的所有信息。通过搭建自己写的API来解析json数据,来监控团队内的所有代码及其相关信息动态(提交、编译、issue、评论等)。
获取json数据方法 http://www.ihongguang.cn/?p=99
PushEvents
public int PushEventInfo([FromBody]PushEvent push)
{
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)
{
projectName.Add(i.projectName);
}
if (!projectName.Contains(push.project.name))
{
sql = "INSERT INTO MemberProject(ProjectName,CommitTime,isdelete) VALUES ('" + push.project.name + "',getdate(),'0') ";
cmd = new SqlCommand(sql, conn);
result = cmd.ExecuteNonQuery();
}
else
{
SqlCommand querySingleInfo = conn.CreateCommand();
querySingleInfo.CommandText = "SELECT projectMembers FROM MemberProject where projectName=" + "'" + push.project.name + "'";
SqlDataReader singleInfoReader = querySingleInfo.ExecuteReader();
//有多行数据,用while循环
while (singleInfoReader.Read())
{
username = singleInfoReader["projectMembers"].ToString().Trim();
}
if(!username.Contains(push.user_name))
{
sql = "update MemberProject set ProjectMembers=" + "ProjectMembers+'," + push.user_name + "'";
cmd = new SqlCommand(sql, conn);
result = cmd.ExecuteNonQuery();
}
//关闭查询
singleInfoReader.Close();
}
conn.Close();
return result;
}
catch (Exception e)
{
FileStream fs = new FileStream("c:\\log.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs); // 创建写入流
sw.WriteLine(e.ToString()); // 写入
sw.Close();
return 0;
}
}`</pre>
**BuildEvents**
<pre>` public int BuildEventInfo([FromBody]BuildEvent build)
{
if (build.build_status == "running" || build.build_status == "pending") { return 0; }
else
{
try
{
SqlConnection conn = connectLocaldb.ConnectDataBase();
//打开数据库
conn.Open();
bool buildResult = (build.build_status == "success" ? true : false);
string sql = "insert into MemberCommit(UserName,ProjectName,SpendTime,BeginTime,EndTime,Version,Result,CommitTime,Branch) Values('" + build.user.name + "','" + build.project_name + "','" + build.build_duration + "',DATEADD(hh,8,'" + (build.build_started_at).Substring(0, build.build_started_at.Length - 3) + "'),DATEADD(hh,8,'" + (build.build_finished_at).Substring(0, build.build_finished_at.Length - 3) + "'),'" + build.before_sha + "','" + buildResult + "',getdate(),'" + build.@ref + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
conn.Close();
return result;
}
catch (Exception e)
{
FileStream fs1 = new FileStream("c:\\test\\log.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw1 = new StreamWriter(fs1); // 创建写入流
string s = (build.user.name);
sw1.WriteLine(e.ToString()); // 写入
sw1.Close();
return 0;
}
}
}`</pre>
**IssueEvents**
<pre>` string sql; int result;
public int IssueEventInfo([FromBody]IssueEvent issue)
{
try
{
SqlConnection conn = connectLocaldb.ConnectDataBase();
conn.Open();
sql = "insert into MemberIssue(assignee,projectname,starttime,updatetime,initiator,state,issue) Values('" + issue.assignee.name + "','" + issue.project.name + "',DATEADD(hh,8,'" + (issue.object_attributes.created_at).Substring(0, (issue.object_attributes.created_at).Length - 3) + "'),DATEADD(hh,8,'" + (issue.object_attributes.updated_at).Substring(0, (issue.object_attributes.updated_at).Length - 3) + "'),'" + issue.user.name + "','" + issue.object_attributes.state + "'," + issue.object_attributes.iid + ")";
SqlCommand cmd = new SqlCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception e)
{
FileStream fs = new FileStream("c:\\text\\log.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs); // 创建写入流
sw.WriteLine(e.ToString()); // 写入
//sw.WriteLine("++"); // 写入
//sw.WriteLine(issue.before); // 写入
//sw.WriteLine("++"); // 写入
sw.Close();
result = 0;
}
return result;
}