diff --git a/gitscord_webhook.php b/gitscord_webhook.php new file mode 100644 index 0000000..fe94c66 --- /dev/null +++ b/gitscord_webhook.php @@ -0,0 +1,208 @@ + "**An error occured with GitScord**\nPlease report in Issues tab on cr@ns gitlab (gitlab.crans.org/elkmaennchen/gitscord-webhook)\n".$message, 'username' => 'GitScord in fail')); +} + +// _layout functions : markdown and JSON encapsulation for Discord + +function push_layout($messageJSON,$JSON,$lang) { + $txt_ligne_push=["fr"=>'nouveau(x) commits',"es"=>' ',"en"=>' ',]; + $txt_title_push=["fr"=>'a push dans',"es"=>' ',"en"=>' ',]; + $txt_add_push=["fr"=>'Ajoutés',"es"=>' ',"en"=>' ',]; + $txt_mod_push=["fr"=>'Modifiés',"es"=>' ',"en"=>' ',]; + $txt_suppr_push=["fr"=>'Supprimés',"es"=>' ',"en"=>' ',]; + $messageJSON['content'] = $JSON['total_commits_count'].' '.$txt_ligne_push[$lang] ; + $messageJSON['embeds'] = array(); + for ($numCommit=$JSON['total_commits_count']-1; $numCommit>=0; $numCommit--) { + // strings of added, modified and deleted files in a single commit + $lst_added = ""; + $lst_modified = ""; + $lst_deleted = ""; + foreach ($JSON['commits'][$numCommit]['added'] as $filename) { + $lst_added = $lst_added.$filename."\n"; + } + foreach ($JSON['commits'][$numCommit]['modified'] as $filename) { + $lst_modified = $lst_modified.$filename."\n"; + } + foreach ($JSON['commits'][$numCommit]['removed'] as $filename) { + $lst_deleted = $lst_deleted.$filename."\n"; + } + // an embed message for each commit + array_push($messageJSON['embeds'], + array('title'=> $txt_title_push[$lang].' *'.$JSON['project']['name'].'*', + 'description'=> "**".$JSON['commits'][$numCommit]['message']."**\n\n__".$txt_add_push[$lang]."__\n".$lst_added."\n__".$txt_mod_push[$lang]."__\n".$lst_modified."\n__".$txt_suppr_push[$lang]."__\n".$lst_deleted, + 'url'=> $JSON['commits'][$numCommit]['url'], + 'timestamp'=> $JSON['commits'][$numCommit]['timestamp'], + 'author'=> array('name'=> $JSON['commits'][$numCommit]['author']['name'])) + ); + } + return($messageJSON); +} + +function tag_layout($messageJSON,$JSON,$lang) { + $txt_ligne_tag=["fr"=>'Nouveau tag',"es"=>' ',"en"=>' ',]; + $txt_title_tag=["fr"=>'a tagué un commit dans',"es"=>' ',"en"=>' ',]; + $messageJSON['content'] = $txt_ligne_tag[$lang] ; + $messageJSON['embeds'] = array(array('title'=> $txt_title_tag[$lang].' *'.$JSON['project']['name'].'*', + 'description'=> $JSON['message'], + 'url'=> $JSON['project']['web_url'].$JSON['ref'], + 'timestamp'=> $JSON['commits'][0]['timestamp'], + 'author'=> array('name'=> $JSON['user_name'])),); + return($messageJSON); +} + +function issue_layout($messageJSON,$JSON,$lang) { + $txt_ligne_issue=["fr"=>'Nouveau problème',"es"=>' ',"en"=>' ',]; + $txt_title_open_issue=["fr"=>'a ouvert une issue dans',"es"=>' ',"en"=>' ',]; + $txt_title_update_issue=["fr"=>'a mis à jour une issue dans',"es"=>' ',"en"=>' ',]; + $txt_title_close_issue=["fr"=>'a fermé une issue dans',"es"=>' ',"en"=>' ',]; + if ($JSON['object_attributes']['action'] == "open") { + $txt_title_issue=$txt_title_open_issue[$lang]; + } + else if ($JSON['object_attributes']['action'] == "close") { + $txt_title_issue=$txt_title_close_issue[$lang]; + } + else { + $txt_title_issue=$txt_title_update_issue[$lang]; + } + $messageJSON['content'] = $txt_ligne_issue[$lang] ; + $messageJSON['embeds'] = array(array('title'=> $txt_title_issue.' *'.$JSON['project']['name'].'*', + 'description'=> "**".$JSON['object_attributes']['title']."**\n".$JSON['object_attributes']['description'], + 'url'=> $JSON['object_attributes']['url'], + 'timestamp'=> $JSON['object_attributes']['last_edited_at'], + 'author'=> array('name'=> $JSON['user']['name'])),); + return($messageJSON); +} + +function note_layout($messageJSON,$JSON,$lang) { + $txt_ligne_note=["fr"=>'Nouvelle note',"es"=>' ',"en"=>' ',]; + $txt_title_note=["fr"=>'a noté un quelque chose dans',"es"=>' ',"en"=>' ',]; + $txt_object_name=""; + if ($JSON['object_attributes']['noteable_type'] == "Issue") { + $txt_object_name=$JSON['issue']['title']; + } + else if ($JSON['object_attributes']['noteable_type'] == "Commit") { + $txt_object_name=$JSON['commit']['message']; + } + else if ($JSON['object_attributes']['noteable_type'] == "MergeRequest") { + $txt_object_name=$JSON['merge_request']['title']; + } + $messageJSON['content'] = $txt_ligne_note[$lang] ; + $messageJSON['embeds'] = array(array('title'=> $txt_title_note[$lang].' *'.$JSON['project']['name'].'*', + 'description'=> "**".$JSON['object_attributes']['noteable_type']." *".$txt_object_name."***\n".$JSON['object_attributes']['note'], + 'url'=> $JSON['object_attributes']['url'], + 'timestamp'=> $JSON['object_attributes']['created_at'], + 'author'=> array('name'=> $JSON['user']['name'])),); + return($messageJSON); +} + +function MR_layout($messageJSON,$JSON,$lang) { + $txt_ligne_MR=["fr"=>'Nouvelle merge request',"es"=>' ',"en"=>' ',]; + $txt_open_action=["fr"=>'a ouvert une MR dans',"es"=>' ',"en"=>' ',]; + $txt_merge_action=["fr"=>'a mergé une MR dans',"es"=>' ',"en"=>' ',]; + $txt_unknown_action=["fr"=>'a modifié une MR dans',"es"=>' ',"en"=>' ',]; + if ($JSON['object_attributes']['action'] == "open") { + $txt_action=$txt_open_action[$lang]; + $txt_action_description=$JSON['object_attributes']['description']; + } + else if ($JSON['object_attributes']['action'] == "merge") { + $txt_action=$txt_merge_action[$lang]; + $txt_action_description=$JSON['object_attributes']['source_branch'].'->'.$JSON['object_attributes']['target_branch']; + } + else { + $txt_action=$txt_unkown_action[$lang]; + $txt_action_description="*Problem with webhook, please report.*"; + } + $messageJSON['content'] = $txt_ligne_MR[$lang] ; + $messageJSON['embeds'] = array(array('title'=> $txt_title_MR[$lang].' *'.$JSON['project']['name'].'*', + 'description'=> "**".$JSON['object_attributes']['title']."**\n".$txt_action_description, + 'url'=> $JSON['object_attributes']['url'], + 'timestamp'=> $JSON['object_attributes']['created_at'], + 'author'=> array('name'=> $JSON['user']['name'])),); + return($messageJSON); +} + +?>