From 45fd0977284e713d6ff7eaf0701afbb9ed57acc1 Mon Sep 17 00:00:00 2001 From: Guan Xiying Date: Mon, 21 Jun 2021 11:16:37 +0800 Subject: [PATCH] * Add parse webhook functions. --- module/gitlab/config.php | 2 +- module/gitlab/control.php | 17 +++++++++++++++-- module/gitlab/model.php | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/module/gitlab/config.php b/module/gitlab/config.php index 275bf96a15..a4bff19207 100644 --- a/module/gitlab/config.php +++ b/module/gitlab/config.php @@ -17,7 +17,7 @@ $config->gitlab->bugLabel->description = "bug label from zentao"; $config->gitlab->bugLabel->color = "#D10069"; $config->gitlab->bugLabel->priority = "0"; -$config->gitlab->zentaoApiWebhookUrl = "%s/api.php?m=gitlab&f=webhook&product=%s&gitlab=%s&project=%s"; +$config->gitlab->zentaoApiWebhookUrl = "%s/api.php?m=gitlab&f=webhook&product=%s&gitlab=%s"; $config->gitlab->zentaoApiWebhookToken = ""; diff --git a/module/gitlab/control.php b/module/gitlab/control.php index 4919d0bf15..7002a860d8 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -204,6 +204,19 @@ class gitlab extends control $gitlab = $this->get->gitlab; $project = $this->get->project; + $requestBody = json_decode(file_get_contents('php://input')); + $request = $this->gitlab->parseWebhookBody($requestBody); + + switch($request->type) + { + case 'issue_create': + $this->createTaskFromIssue($request); + break; + case 'issue_update': + $this->updateTaskFromIssue($request); + break; + } + $logFile = $this->app->getLogRoot() . 'webhook.'. date('Ymd') . '.log.php'; if(!file_exists($logFile)) file_put_contents($logFile, ''); @@ -211,8 +224,8 @@ class gitlab extends control if($fh) { fwrite($fh, date('Ymd H:i:s') . ": " . $this->app->getURI() . "\n"); - if(!empty($_POST)) fwrite($fh, "data: " . var_export($_POST, true) . "\n"); - //fwrite($fh, "results:" . print_r($response, true) . "\n"); + fwrite($fh, "GET: " . var_export($_GET, true) . "\n"); + fwrite($fh, "Body: " . var_export($requestBody, true) . "\n"); if(!empty($errors)) fwrite($fh, "errors: " . $errors . "\n"); fclose($fh); } diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 987f453162..3e1af87946 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -522,4 +522,22 @@ class gitlabModel extends model $response = $this->apiCreateIssue($gitlabID, $projectID, $bug); return $response; } + + public function parseWebhookBody($body) + { + $type = zget($body, 'object_kind', ''); + if(!$type or !is_callable(array($this, "parse{$type}Webhook"))) return false; + return call_user_func_array(array($this, "parse{$type}Webhook"), array('body' => $body)); + } + + public function parseIssueWebhook($body) + { + $request = new stdclass; + $request->type = $body->object_kind; + $issue = $body->object_attributes; + + $request->labels = $body->labels; + $request->project = $body->project->id; + } + }