Last active
June 4, 2020 05:49
-
-
Save monis0395/4ff5d1f0f15cadf5cbedd807d133d9bf to your computer and use it in GitHub Desktop.
Integrate Gitlab with Flock. This allows users to: 1. Send messages to the flock channel via webhook. 2 Send json payload to any webhook url 3. Only send message when MR is merged with master branch only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function isGetParamPresent($param) | |
{ | |
return isset($_GET[$param]) && !empty($_GET[$param]); | |
} | |
$rawData = trim(file_get_contents("php://input")); | |
$decoded_data = json_decode($rawData, true); | |
$FLOCK_API_URL = "https://api.flock.com/hooks/sendMessage/"; | |
$repoName = isGetParamPresent('repoName') ? $_GET['repoName'] : $decoded_data['repository']['name']; | |
$projectMessage = isGetParamPresent('skipProjectMessage') && $_GET['skipProjectMessage'] == "true" ? "" : " to {$repoName}"; | |
$title = $decoded_data["object_attributes"]["title"]; | |
$merge_request_creator = $decoded_data["object_attributes"]["last_commit"]["author"]["name"]; | |
$upload_message = "Uploading{$projectMessage}: {$title} (@{$merge_request_creator})"; | |
$event = $decoded_data["object_kind"]; | |
$state = $decoded_data["object_attributes"]["state"]; | |
$targetBranch = $decoded_data["object_attributes"]["target_branch"]; | |
$API_URL = ""; | |
$JSON_MESSAGE_KEY = "text"; | |
if (isGetParamPresent('flockApiKey')) { | |
$API_URL = $FLOCK_API_URL . $_GET['flockApiKey']; | |
} | |
if (isGetParamPresent('apiUrl')) { | |
$API_URL = $_GET['apiUrl']; | |
} | |
if (isGetParamPresent('apiMessageKey')) { | |
$JSON_MESSAGE_KEY = $_GET['apiMessageKey']; | |
} | |
if ($API_URL != "" && $JSON_MESSAGE_KEY != "" && $event == "merge_request" && $state == "merged" && $targetBranch == "master") { | |
$data = array( | |
$JSON_MESSAGE_KEY => $upload_message | |
); | |
$content = json_encode($data); | |
echo $content; | |
$curl = curl_init($API_URL); | |
curl_setopt($curl, CURLOPT_HEADER, false); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json")); | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, $content); | |
$result = curl_exec($curl); | |
var_dump($result); | |
curl_close($curl); | |
} else { | |
$message = "Reasons for not sending message:"; | |
if ($API_URL == "") { | |
$message .= "\n- API URL is empty. Please send query-param: flockApiKey or apiUrl"; | |
} | |
if ($JSON_MESSAGE_KEY == "") { | |
$message .= "\n- apiMessageKey is empty"; | |
} | |
if ($event !== "merge_request") { | |
$message .= "\n- eventType: {$event} != merge_request"; | |
} | |
if ($state !== "merged") { | |
$message .= "\n- state: {$state} != merged"; | |
} | |
if ($targetBranch !== "master") { | |
$message .= "\n- targetBranch: {$targetBranch} != master"; | |
} | |
echo $message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment