This commit refines the JIRA request handling by restructuring the `jira_resolve_categorie_interne` function to better manage the internal category based on the provided input. It introduces a new function, `jira_set_categorie_interne`, to update the category for JIRA issues, enhancing the integration's functionality. Additionally, it improves error logging for user creation and ticket submission processes, providing clearer feedback on failures. These changes aim to enhance the robustness and user experience of the JIRA integration.
529 lines
18 KiB
PHP
529 lines
18 KiB
PHP
<?php
|
|
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
// Lire le JSON si nécessaire
|
|
if ($_SERVER['CONTENT_TYPE'] === 'application/json' || str_contains($_SERVER['CONTENT_TYPE'], 'application/json')) {
|
|
$raw = file_get_contents("php://input");
|
|
$json = json_decode($raw, true);
|
|
if (is_array($json)) {
|
|
$_POST = $json;
|
|
} else {
|
|
http_response_code(400);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode(['success' => false, 'message' => 'Mauvais JSON']);
|
|
exit;
|
|
}
|
|
}
|
|
|
|
require_once "php/inc_fonctions.php";
|
|
|
|
global $vDomaine;
|
|
$strAction = $intEvenement = $intNewNoBib = $intEpreuveCommandee = $intEpreuve = $intPartipcipant = $intQuantite = $strLangue = '';
|
|
|
|
//$_POST['request_type'] ="check_email";
|
|
//$_POST['email'] ="stepan@progiweb.ca";
|
|
//$_POST['nom']="stephan test";
|
|
//$_POST['description']="description ";
|
|
//$_POST['typedemande']="test sl";
|
|
//$_POST['summary']="sujet";
|
|
|
|
const JIRA_REQUEST_TYPE_SOUTIEN_TECHNIQUE = '4';
|
|
|
|
if (isset($_POST['request_type'] )) {
|
|
$strAction = $_POST['request_type'];
|
|
}
|
|
|
|
$tabRetour = array();
|
|
$tabRetour['success']=false;
|
|
switch($strAction) {
|
|
case 'check_email':
|
|
$tabRetour = jira_submit_contact_ticket($_POST);
|
|
break;
|
|
case 'no_equipe':
|
|
case 'no_bib':
|
|
|
|
break;
|
|
case 'teammates':
|
|
break;
|
|
}
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
echo json_encode($tabRetour);
|
|
exit;
|
|
|
|
function jira_get_type_demande_row(array $post)
|
|
{
|
|
global $objDatabase;
|
|
|
|
if (empty($post['typedemande']) || !isset($objDatabase)) {
|
|
return null;
|
|
}
|
|
|
|
$sqlCol = "SELECT info_texte, info_option2, info_option3 FROM info WHERE info_actif = 1 AND info_clef = 'type_de_demande' AND info_option2 = '" . $objDatabase->fxEscape($post['typedemande']) . "' LIMIT 1";
|
|
$row = $objDatabase->fxGetResults($sqlCol);
|
|
|
|
return !empty($row) ? reset($row) : null;
|
|
}
|
|
|
|
function jira_resolve_request_type_id(array $post)
|
|
{
|
|
$row = jira_get_type_demande_row($post);
|
|
if (!empty($row['info_texte']) && fxJiraIsAutreDemande($row['info_texte'])) {
|
|
return (string)($post['typedemande'] ?? JIRA_REQUEST_TYPE_SOUTIEN_TECHNIQUE);
|
|
}
|
|
|
|
return JIRA_REQUEST_TYPE_SOUTIEN_TECHNIQUE;
|
|
}
|
|
|
|
function jira_resolve_categorie_interne(array $post)
|
|
{
|
|
$strCategorie = '';
|
|
|
|
if (isset($post['categorie_interne'])) {
|
|
$strCategorie = trim((string)$post['categorie_interne']);
|
|
} else {
|
|
$row = jira_get_type_demande_row($post);
|
|
if (!empty($row['info_option3'])) {
|
|
$strCategorie = trim((string)$row['info_option3']);
|
|
} elseif (!empty($row['info_texte'])) {
|
|
$strCategorie = fxJiraMapCategorieInterne($row['info_texte']);
|
|
}
|
|
}
|
|
|
|
// Seul « Vente » est géré pour l'instant ; les autres choix n'envoient pas de catégorie interne.
|
|
return ($strCategorie === 'Vente') ? 'Vente' : '';
|
|
}
|
|
|
|
function jira_log_contact_error($context, $detail = '')
|
|
{
|
|
$line = 'jira contact [' . $context . ']';
|
|
if ($detail !== '') {
|
|
$line .= ': ' . (is_string($detail) ? $detail : json_encode($detail, JSON_UNESCAPED_UNICODE));
|
|
}
|
|
error_log($line);
|
|
}
|
|
|
|
function jira_wait_for_account_id($email, $maxAttempts = 4)
|
|
{
|
|
for ($i = 0; $i < $maxAttempts; $i++) {
|
|
if ($i > 0) {
|
|
usleep(500000);
|
|
}
|
|
$result = jira_recherche_email($email);
|
|
if (!empty($result['success']) && !empty($result['jira']['accountId'])) {
|
|
return $result['jira']['accountId'];
|
|
}
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
function jira_submit_contact_ticket(array $post)
|
|
{
|
|
$tabRetour = array('success' => false, 'accountId' => '');
|
|
$email = trim((string)($post['email'] ?? ''));
|
|
$nom = trim((string)($post['nom'] ?? ''));
|
|
$userJustCreated = false;
|
|
|
|
$result = jira_recherche_email($email);
|
|
$accountId = !empty($result['success']) ? ($result['jira']['accountId'] ?? '') : '';
|
|
|
|
if ($accountId === '') {
|
|
$result = jira_creer_user($email, $nom);
|
|
if (empty($result['success'])) {
|
|
jira_log_contact_error('user_creation', $result['statut'] ?? '');
|
|
return $tabRetour;
|
|
}
|
|
$userJustCreated = true;
|
|
if (is_array($result['statut'])) {
|
|
$accountId = $result['statut']['accountId'] ?? '';
|
|
}
|
|
if ($accountId === '') {
|
|
$accountId = jira_wait_for_account_id($email);
|
|
}
|
|
}
|
|
|
|
$tabRetour['accountId'] = $accountId;
|
|
|
|
$data = array(
|
|
'serviceDeskId' => 2,
|
|
'requestTypeId' => jira_resolve_request_type_id($post),
|
|
'summary' => $post['summary'] ?? '',
|
|
'description' => $post['description'] ?? '',
|
|
'categorie_interne' => jira_resolve_categorie_interne($post),
|
|
'raiseOnBehalfOf' => $accountId !== '' ? $accountId : $email,
|
|
);
|
|
|
|
$ticket = jira_create_ticket($data);
|
|
if (empty($ticket['success']) && ($userJustCreated || $accountId === '')) {
|
|
$accountId = jira_wait_for_account_id($email, 3);
|
|
if ($accountId !== '') {
|
|
$tabRetour['accountId'] = $accountId;
|
|
$data['raiseOnBehalfOf'] = $accountId;
|
|
$ticket = jira_create_ticket($data);
|
|
}
|
|
}
|
|
|
|
if (empty($ticket['success'])) {
|
|
jira_log_contact_error('ticket_creation', $ticket['statut'] ?? '');
|
|
return $tabRetour;
|
|
}
|
|
|
|
$categorieInterne = $data['categorie_interne'] ?? '';
|
|
if ($categorieInterne === '') {
|
|
$tabRetour['success'] = true;
|
|
return $tabRetour;
|
|
}
|
|
|
|
$issueKey = '';
|
|
if (is_array($ticket['statut']) && !empty($ticket['statut']['issueKey'])) {
|
|
$issueKey = $ticket['statut']['issueKey'];
|
|
}
|
|
if ($issueKey === '') {
|
|
jira_log_contact_error('categorie_interne', 'issueKey manquant après création');
|
|
return $tabRetour;
|
|
}
|
|
|
|
$update = jira_set_categorie_interne($issueKey, $categorieInterne);
|
|
if (empty($update['success'])) {
|
|
jira_log_contact_error('categorie_interne', $update['statut'] ?? '');
|
|
return $tabRetour;
|
|
}
|
|
|
|
$tabRetour['success'] = true;
|
|
return $tabRetour;
|
|
}
|
|
|
|
function jira_recherche_email($param)
|
|
{
|
|
// recherche user
|
|
$jiraDomain = 'https://progiwebinc.atlassian.net'; // Remplacez par le domaine de votre Jira
|
|
// API que vous voulez appeler
|
|
$admin_email = 'stephan@progiweb.ca'; // Remplacez par votre email
|
|
//$api_token = 'ATATT3xFfGF0DuhOhRUxFlj2hFpLuzYGapJIrhMETB35NnLFi1TyxDszp3tTa8MaadTpT72GfDk9FOpDm07m6HbdXoQ9plDeRvB6OwRnD19VrIqA8M3HmDGgf_MWR7Pwk5731ahbgb4UBdRgj-ICBqt9A0SjJPnnoTfu4ECVh51HjM0RsekOmeM=908B030C'; // Remplacez par votre jeton d'API
|
|
//ms1inscription v4
|
|
$api_token = 'ATATT3xFfGF0Z91qESp8j7uO783qK0E4BWsY8rhPWEmexSa4Rw3c21Spg6hYIkmVgy6Vwzb34RxqFl3sR00Kby_DuY1NdcUJyfPQYZ0_JbeEjAk9ybFgNY8-MBSABMCxzaDk4szTbWupfjxHT8sJxJMvSNxgHPds5FxXaGo2DnZICZu5dXuvWbk=F73E15AE';
|
|
|
|
|
|
$apiEndpoint = '/rest/api/3/user/search?query=email@example.com';
|
|
|
|
// URL de votre instance Jira
|
|
|
|
|
|
// Email à rechercher
|
|
$email = $param;
|
|
|
|
|
|
|
|
// Initialiser cURL
|
|
$ch = curl_init();
|
|
|
|
// Point de terminaison pour rechercher un utilisateur
|
|
$url = $jiraDomain . '/rest/api/3/user/search?query=' . urlencode($email);
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Authorization: Basic ' . base64_encode($admin_email . ':' . $api_token),
|
|
'Content-Type: application/json',
|
|
]);
|
|
|
|
// Exécuter la requête et obtenir la réponse
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
// Décoder la réponse JSON
|
|
$result = json_decode($response, true);
|
|
$sortie=array();
|
|
$sortie['success']=false;
|
|
// Filtrer pour une correspondance exacte sur l'email
|
|
$exact_match = null;
|
|
if (!empty($result)) {
|
|
|
|
foreach ($result as $user) {
|
|
if (isset($user['emailAddress']) && strtolower($user['emailAddress']) === strtolower($email)) {
|
|
$sortie['success']=true;
|
|
$sortie['jira']=$user;
|
|
$exact_match = $user;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $sortie ;
|
|
}
|
|
function jira_creer_user($email,$displayName)
|
|
{
|
|
$jiraDomain = 'https://progiwebinc.atlassian.net'; // Remplacez par le domaine de votre Jira
|
|
// API que vous voulez appeler
|
|
$userEmail = 'stephan@progiweb.ca'; // Remplacez par votre email
|
|
$apiToken = 'ATATT3xFfGF0Z91qESp8j7uO783qK0E4BWsY8rhPWEmexSa4Rw3c21Spg6hYIkmVgy6Vwzb34RxqFl3sR00Kby_DuY1NdcUJyfPQYZ0_JbeEjAk9ybFgNY8-MBSABMCxzaDk4szTbWupfjxHT8sJxJMvSNxgHPds5FxXaGo2DnZICZu5dXuvWbk=F73E15AE'; // Remplacez par votre jeton d'API
|
|
$apiEndpoint = '/rest/servicedeskapi/customer';
|
|
|
|
|
|
|
|
|
|
// Encodage de l'authentification en Base64
|
|
$authHeader = base64_encode("$userEmail:$apiToken");
|
|
|
|
// Corps de la requête
|
|
$postData = json_encode([
|
|
'email' => $email,
|
|
'displayName' => $displayName
|
|
]);
|
|
|
|
// Initialisation de cURL
|
|
$ch = curl_init("$jiraDomain$apiEndpoint");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
"Authorization: Basic $authHeader",
|
|
"Content-Type: application/json",
|
|
"Accept: application/json"
|
|
]);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
|
|
// Exécuter la requête
|
|
$response = curl_exec($ch);
|
|
$result = json_decode($response, true);
|
|
$sortie=array();
|
|
$sortie['statut']=false;
|
|
// Gérer les erreurs
|
|
if (curl_errno($ch)) {
|
|
// echo 'Erreur cURL : ' . curl_error($ch);
|
|
$sortie['statut']=curl_error($ch);
|
|
} else {
|
|
// Vérifier le code HTTP pour voir si le client existe déjà
|
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if ($httpcode == 201) {
|
|
// echo "Le client a été créé avec succès.";
|
|
$sortie['success']=true;
|
|
$sortie['statut']= $result;
|
|
} elseif ($httpcode == 400) {
|
|
// Traiter le cas où l'email existe déjà
|
|
$responseData = json_decode($response, true);
|
|
|
|
$sortie['success']=true;
|
|
if (isset($responseData['errors']) && strpos($responseData['errors'][0]['message'], 'already exists') !== false) {
|
|
// echo "Le client avec cet email existe déjà.";
|
|
$sortie['success']=true;
|
|
$sortie['existe']=true;
|
|
$sortie['statut']= $result;
|
|
} else {
|
|
//echo "Une erreur s'est produite : " . $response;
|
|
$sortie['statut']= $result;
|
|
$sortie['success']=true;
|
|
$sortie['existe']=true;
|
|
}
|
|
} else {
|
|
$sortie['success']=false;
|
|
// echo "Erreur inattendue : " . $response;
|
|
$sortie['statut']= $result;
|
|
}
|
|
}
|
|
|
|
// Fermer la session cURL
|
|
curl_close($ch);
|
|
return($sortie);
|
|
}
|
|
|
|
function jira_set_categorie_interne($issueKey, $categorieInterne)
|
|
{
|
|
$jiraDomain = 'https://progiwebinc.atlassian.net';
|
|
$userEmail = 'stephan@progiweb.ca';
|
|
$apiToken = 'ATATT3xFfGF0Z91qESp8j7uO783qK0E4BWsY8rhPWEmexSa4Rw3c21Spg6hYIkmVgy6Vwzb34RxqFl3sR00Kby_DuY1NdcUJyfPQYZ0_JbeEjAk9ybFgNY8-MBSABMCxzaDk4szTbWupfjxHT8sJxJMvSNxgHPds5FxXaGo2DnZICZu5dXuvWbk=F73E15AE';
|
|
|
|
$payload = json_encode(array(
|
|
'fields' => array(
|
|
'customfield_11179' => array(
|
|
'value' => $categorieInterne,
|
|
),
|
|
),
|
|
));
|
|
|
|
$ch = curl_init($jiraDomain . '/rest/api/3/issue/' . rawurlencode($issueKey));
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
|
|
'Authorization: Basic ' . base64_encode($userEmail . ':' . $apiToken),
|
|
'Content-Type: application/json',
|
|
));
|
|
|
|
$response = curl_exec($ch);
|
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
$sortie = array('success' => false, 'statut' => '');
|
|
if ($httpcode === 204) {
|
|
$sortie['success'] = true;
|
|
return $sortie;
|
|
}
|
|
|
|
$sortie['statut'] = ($response !== false && $response !== '') ? $response : ('HTTP ' . $httpcode);
|
|
return $sortie;
|
|
}
|
|
|
|
function jira_create_ticket( $param)
|
|
{
|
|
|
|
// Domaine Jira
|
|
$jiraDomain = 'https://progiwebinc.atlassian.net'; // Remplacez par le domaine de votre Jira
|
|
// Informations d'authentification
|
|
$userEmail = 'stephan@progiweb.ca'; // Remplacez par votre email
|
|
$apiToken = 'ATATT3xFfGF0Z91qESp8j7uO783qK0E4BWsY8rhPWEmexSa4Rw3c21Spg6hYIkmVgy6Vwzb34RxqFl3sR00Kby_DuY1NdcUJyfPQYZ0_JbeEjAk9ybFgNY8-MBSABMCxzaDk4szTbWupfjxHT8sJxJMvSNxgHPds5FxXaGo2DnZICZu5dXuvWbk=F73E15AE'; // Remplacez par votre jeton d'API
|
|
// Les informations du ticket
|
|
$data = [
|
|
'serviceDeskId' => $param['serviceDeskId'],
|
|
'requestTypeId' => $param['requestTypeId'],
|
|
'requestFieldValues' => [
|
|
'summary' => $param['summary'],
|
|
'description' => $param['description'],
|
|
],
|
|
'raiseOnBehalfOf' => $param['raiseOnBehalfOf'], // Remplacez par l'accountId du client (facultatif)
|
|
] ;
|
|
$jsonData = json_encode($data);
|
|
// if ($jsonData === false) {
|
|
// echo "Erreur dans l'encodage JSON : " . json_last_error_msg();
|
|
// } else {
|
|
// echo $jsonData; // Afficher les données encodées
|
|
// }
|
|
// Initialiser cURL
|
|
$ch = curl_init();
|
|
// Point de terminaison pour créer une demande
|
|
$url = $jiraDomain . '/rest/servicedeskapi/request';
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Authorization: Basic ' . base64_encode($userEmail . ':' . $apiToken),
|
|
'Content-Type: application/json; charset=UTF-8',
|
|
]);
|
|
// Exécuter la requête et obtenir la réponse
|
|
$response = curl_exec($ch);
|
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
$sortie['success']=false;
|
|
|
|
$sortie['statut']="";
|
|
// Vérifier si le contenu de la réponse est vide
|
|
if ($response === false || empty($response)) {
|
|
// Gérer les réponses vides ou erreurs
|
|
if ($httpcode == 204) {
|
|
$sortie['success']=true;
|
|
$sortie['statut'] = "Le ticket a été créé avec succès, mais la réponse est vide (204 No Content).";
|
|
|
|
} else {
|
|
$sortie['success']=false;
|
|
$sortie['statut'] = "Erreur : Le serveur a renvoyé une réponse vide. Code HTTP : " . $httpcode;
|
|
|
|
}
|
|
} else {
|
|
// Décoder la réponse JSON seulement si elle n'est pas vide
|
|
$decoded_response = json_decode($response, true);
|
|
|
|
if ($httpcode == 201) {
|
|
$sortie['success']=true;
|
|
$sortie['statut']= $decoded_response;
|
|
|
|
} else {
|
|
$sortie['success']=false;
|
|
$sortie['statut']= $response;
|
|
|
|
}
|
|
}
|
|
|
|
return( $sortie);
|
|
}
|
|
function jira_create_ticketold( $param)
|
|
{
|
|
|
|
// Domaine Jira
|
|
$jiraDomain = 'https://progiwebinc.atlassian.net'; // Remplacez par le domaine de votre Jira
|
|
|
|
// Informations d'authentification
|
|
$userEmail = 'stephan@progiweb.ca'; // Remplacez par votre email
|
|
$apiToken = 'ATATT3xFfGF0Z91qESp8j7uO783qK0E4BWsY8rhPWEmexSa4Rw3c21Spg6hYIkmVgy6Vwzb34RxqFl3sR00Kby_DuY1NdcUJyfPQYZ0_JbeEjAk9ybFgNY8-MBSABMCxzaDk4szTbWupfjxHT8sJxJMvSNxgHPds5FxXaGo2DnZICZu5dXuvWbk=F73E15AE'; // Remplacez par votre jeton d'API
|
|
|
|
|
|
|
|
// Les informations du ticket
|
|
$data = [
|
|
'serviceDeskId' => $param['serviceDeskId'],
|
|
'requestTypeId' => $param['requestTypeId'],
|
|
|
|
'requestFieldValues' => [
|
|
'summary' => $param['summary'],
|
|
'description' => $param['description'],
|
|
|
|
],
|
|
'raiseOnBehalfOf' => $param['raiseOnBehalfOf'], // Remplacez par l'accountId du client (facultatif)
|
|
] ;
|
|
|
|
$jsonData = json_encode($data);
|
|
|
|
// if ($jsonData === false) {
|
|
// echo "Erreur dans l'encodage JSON : " . json_last_error_msg();
|
|
// } else {
|
|
// echo $jsonData; // Afficher les données encodées
|
|
// }
|
|
|
|
|
|
// Initialiser cURL
|
|
$ch = curl_init();
|
|
|
|
// Point de terminaison pour créer une demande
|
|
$url = $jiraDomain . '/rest/servicedeskapi/request';
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Authorization: Basic ' . base64_encode($userEmail . ':' . $apiToken),
|
|
'Content-Type: application/json; charset=UTF-8',
|
|
]);
|
|
|
|
// Exécuter la requête et obtenir la réponse
|
|
$response = curl_exec($ch);
|
|
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
$sortie['success']=0;
|
|
|
|
$sortie['statut']="";
|
|
// Vérifier si le contenu de la réponse est vide
|
|
if ($response === false || empty($response)) {
|
|
// Gérer les réponses vides ou erreurs
|
|
if ($httpcode == 204) {
|
|
$sortie['success']=1;
|
|
$sortie['statut'] = "Le ticket a été créé avec succès, mais la réponse est vide (204 No Content).";
|
|
|
|
} else {
|
|
$sortie['success']=0;
|
|
$sortie['statut'] = "Erreur : Le serveur a renvoyé une réponse vide. Code HTTP : " . $httpcode;
|
|
|
|
}
|
|
} else {
|
|
// Décoder la réponse JSON seulement si elle n'est pas vide
|
|
$decoded_response = json_decode($response, true);
|
|
|
|
if ($httpcode == 201) {
|
|
$sortie['success']=1;
|
|
$sortie['statut']= $decoded_response;
|
|
|
|
} else {
|
|
$sortie['success']=0;
|
|
$sortie['statut']= $response;
|
|
|
|
}
|
|
}
|
|
|
|
return( $sortie);
|
|
}
|
|
|