├── README.md └── index.php /README.md: -------------------------------------------------------------------------------- 1 | # Gitoscdingtalkapi 2 | Git@osc dingtalk api git@osc 使用钉钉机器人提示api接口 3 | 4 | (表情包功能暂未开放) 5 | 6 | 需要设置两个地方 index.php   7 | 8 | 设置请求密码 9 | $pwd = "Northme"; //Set password 10 | 11 | 设置钉钉机器人access\_token可在钉钉机器人详情页获取 12 | $access_token = "xxx"; // Set Api token 13 | 14 | ![使用效果图](https://static.dingtalk.com/media/lADPACOG83gk3SbNBiHNAtA_720_1569.jpg_220x10000q90s150.jpg_.webp) 15 | 16 | ## 版权 17 | 18 | 禁止用于商业用途 19 | 20 | Hangzhou Northme Technology Co.,Ltd. 21 |
22 | @@@@@@@@@@@@~~:-:~:::::~~:;;;@@@@@@@@@@@@@@@@,@@@,@@@@@@@@@@,@@,,,,,,,,@@@ ,,,,,,@-,@@@,@@@@@@,@@@@@@,,@@@@@@@@@@@@@@@@@
23 | @@@@@@@@@@@~:~~~~::;:::::~:;;;@@@@@@@@@@@@@@@,@@@,@@@@@@@@@,,@@@@@@@@,-@@@-,,,,@@@@,,@@,@@@@@@,@@@@@@,,@@@@@@@@@@@@@@@@@
24 | @@@@@@@@@@:::;::~~:~::::;~:;;;!@@@@@@@@@@@@@@,@@@,@@@@@@@@,,@@@@@@@@,,@@@@@@@,,@@@@@,,@,@@@@@@,@@,,,,,,,,,,@@@@@@@@@@@@@
25 | @@@@@@@@@::::::::;:;:;;;;;!;;!;;@@@@@@@@@@@@@,@@@,@@@@,@@,,@@@@@@@@,,@@@@@@@@,,@@@@@@,@,@@@@,,,,,@@@@,,@@@@@@@@@@@@@@@@@
26 | @@@@@@@@::@@@@@@:::::;;;;;!:;;;;@@@@@@@@,,,,,,@@@,@@,,,@@,@@@,@@@@,,,,@@@@,,,,,,,@,@@@@,@@@@@@,@@@@@@,,@@@@@@@@@@@@@@@@@
27 | @@@@@@@@:~;:;:@@@;;:;:;!;;;;;!;;*@@@@@@@@@@@@,@@@,,,,@@@@@@@,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,@@@@@@@@@@@@@@
36 | 
37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | password != $pwd) { // password 46 | header('HTTP/1.0 404 Not Found'); 47 | exit(); 48 | } 49 | 50 | // Start to Get the Git@osc data analysis 51 | 52 | $title = $hook_name = $git_Data->hook_name; // Get the action 53 | 54 | 55 | /* 56 | * Function the action 57 | * Push note merge_request issue tag 58 | * 59 | **/ 60 | 61 | 62 | // Push 63 | function fnPush($data) 64 | { 65 | // Get the Commit 66 | $commit_arr = $data->commits; 67 | $commit_messages = ""; 68 | 69 | //decompose Commit 70 | foreach ($commit_arr as $commit) { 71 | 72 | // Processing character 73 | $commit->id = substr($commit->id, 0, 7); 74 | $commit->timestamp = date($commit->timestamp); 75 | 76 | // Add character 77 | $commit_messages .= <<id} 79 | NHL Time: {$commit_time} 80 | NHL > {$commit->message} 81 | NHL ------- 82 | EOF; 83 | } 84 | 85 | $messages = <<< EOF 86 | \n ## {$data->repository->name} Push 提示 87 | \n 分支:{$data->ref} 88 | \n 提交者:[{$data->user_name}]({$data->user->url}) 89 | NHL 共{$data->total_commits_count}个 Commit 90 | NHL #### Commit 详情 91 | {$commit_messages} 92 | NHL #### 仓库 93 | NHL 仓库名称:{$data->repository->name} 94 | NHL > {$data->repository->description} 95 | NHL \n [仓库地址]( {$data->repository->url} ) 96 | NHL \n [仓库主页]( {$data->repository->homepage} ) 97 | EOF; 98 | return $messages; 99 | } 100 | 101 | // Note 102 | function fn_Note($data) 103 | { 104 | $Note_msg= str_replace(array("\r\n", "\r"), 'NHL', $data->note); 105 | $messages = <<project->name} {$data->noteable_type} 提示 107 | NHL Id: {$data->noteable_id} 108 | NHL 作者:{$data->author->user_name} 109 | NHL --内容-- 110 | NHL {$Note_msg} 111 | NHL --内容-- 112 | NHL #### 仓库 113 | NHL 仓库名称:{$data->repository->name} 114 | NHL [仓库链接]({$data->repository->url}) 115 | NHL 仓库描述: 116 | NHL > {$data->repository->description} 117 | EOF; 118 | return $messages; 119 | } 120 | 121 | // PullRequest 122 | function fnMerge_request($data) 123 | { 124 | $Merge_request_body= str_replace(array("\r\n", "\r"), 'NHL', $data->body); 125 | $messages = <<source_repo->repository->name} PullRequest 提示 127 | \n iid: #{$data->iid} 128 | NHL 标题: {$data->title} 129 | NHL --说明--- 130 | NHL {$Merge_request_body} 131 | NHL \n --说明--- 132 | NHL \n 状态: {$data->state} 133 | NHL 合并状态: {$data->merge_status} 134 | NHL 提起者:{$data->author->user_name} 135 | NHL > {$data->source_repo->project->path_with_namespace} : {$data->source_branch} 136 | NHL ↓↓↓↓↓↓↓↓↓↓↓ 137 | NHL {$data->target_repo->project->path_with_namespace} : {$data->target_branch} 138 | 139 | NHL ### 源项目 140 | NHL 项目名称:{$data->source_repo->project->name} 141 | NHL > {$data->source_repo->project->description} 142 | NHL [项目地址]({$data->source_repo->project->url}) 143 | NHL ### 源仓库 144 | NHL 仓库名称:{$data->source_repo->repository->name} 145 | NHL > {$data->source_repo->repository->description} 146 | NHL \n [仓库链接]({$data->source_repo->repository->url}) 147 | NHL ### 目标项目 148 | NHL 目标项目名称:{$data->target_repo->project->name} 149 | NHL > {$data->target_repo->project->description} 150 | NHL \n [目标项目地址]({$data->target_repo->project->url}) 151 | NHL ### 目标仓库 152 | NHL 目标仓库名称:{$data->target_repo->repository->name} 153 | NHL > {$data->target_repo->repository->description} 154 | NHL \n [目标仓库链接]({$data->target_repo->repository->url}) 155 | 156 | EOF; 157 | return $messages; 158 | 159 | } 160 | 161 | /* Issue */ 162 | function fn_Issue($data) 163 | { 164 | $assignee=empty($data->assignee) ? "未指定":$data->assignee->user_name; 165 | $milestone=empty($data->milestone) ? "没有里程碑":$data->milestone; 166 | $Issue_request_body= str_replace(array("\r\n", "\r"), 'NHL', $data->description); 167 | $messages = <<project->name} Issue 提示 169 | NHL iid: {$data->iid} 170 | NHL 名称:{$data->title} 171 | NHL --描述-- 172 | NHL {$Issue_request_body} 173 | NHL --描述-- 174 | NHL 状态:{$data->state} 175 | NHL 指定: {$assignee} 176 | NHL 里程碑:{$milestone} 177 | NHL 提起者:{$data->user->username} 178 | 179 | NHL ### 项目 180 | NHL 项目名称:{$data->project->name} 181 | NHL 项目分支:{$data->project->default_branch} 182 | NHL [项目地址]({$data->project->url}) 183 | 184 | NHL ### 仓库 185 | NHL 仓库名称:{$data->repository->name} 186 | NHL [仓库链接]({$data->repository->url}) 187 | NHL > {$data->repository->description} 188 | 189 | EOF; 190 | 191 | 192 | return $messages; 193 | } 194 | 195 | /*Tag*/ 196 | function fn_Tag($data) 197 | { 198 | 199 | $messages = <<project->name} 新增 Tag 提示 201 | NHL ref:{$data->ref} 202 | NHL Before 203 | NHL > {$data->before} 204 | NHL \n After 205 | NHL > {$data->after} 206 | NHL ### 项目 207 | NHL 项目名称:{$data->project->name} 208 | NHL [项目地址]({$data->project->url}) 209 | NHL 项目分支:{$data->project->default_branch} 210 | NHL ### 仓库 211 | NHL 名称:{$data->repository->name} 212 | NHL [链接]({$data->repository->url}) 213 | NHL > {$data->repository->description} 214 | EOF; 215 | return $messages; 216 | } 217 | 218 | 219 | 220 | 221 | switch ($hook_name) { 222 | case "push_hooks": 223 | $messages = fnPush($git_Data); 224 | $title = $git_Data->repository->name ." Push"; 225 | break; 226 | case "note_hooks": 227 | $messages = fn_Note($git_Data); 228 | $title = $git_Data->project->name ." ". $git_Data->noteable_type; 229 | break; 230 | case "merge_request_hooks": 231 | $messages = fnMerge_request($git_Data); 232 | $title = $git_Data->source_repo->repository->name . " PullRequest"; 233 | break; 234 | case "issue_hooks": 235 | $messages = fn_Issue($git_Data); 236 | $title = $git_Data->project->name . " issue"; 237 | break; 238 | case "tag_push_hooks": 239 | $messages = fn_Tag($git_Data); 240 | $title = $git_Data->project->name . " New Tag"; 241 | break; 242 | 243 | default: 244 | $messages = "Error ! The ip is" . $_SERVER["REMOTE_ADDR"]; 245 | break; 246 | } 247 | 248 | 249 | /****** 250 | * 251 | * 252 | * Happy !!!!!!!!!!!! 253 | * By keac 254 | * 255 | * */ 256 | $messages .= << 'text', 'text' => array('content' => $messages,), 'at' => array('atMobiles' => array(), 'isAtAll' => $isAtAll,),); 276 | 277 | 278 | $data = array('msgtype' => 'markdown', 'markdown' => array('title' => $title, 'text' => $messages,),); 279 | 280 | 281 | curlDing($data, $access_token); 282 | 283 | 284 | 285 | /* 286 | * 287 | * Post to DingTalk 288 | * 289 | * */ 290 | 291 | 292 | function curlDing($data, $access_token){ 293 | 294 | 295 | $data_string = json_encode($data); // To json 296 | $urls = "https://oapi.dingtalk.com/robot/send?access_token=" . $access_token; // To DingTalk API 297 | /* POST */ 298 | $ch = curl_init(); 299 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8')); 300 | curl_setopt($ch, CURLOPT_URL, "$urls"); 301 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 302 | curl_setopt($ch, CURLOPT_POST, true); 303 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 304 | $retBase = curl_exec($ch); 305 | curl_close($ch); 306 | 307 | /* End of Post */ 308 | 309 | return $retBase; 310 | } --------------------------------------------------------------------------------