ninegitea/src/Repository/ScrumRepository.php

284 lines
13 KiB
PHP
Raw Normal View History

2021-07-20 13:04:47 +02:00
<?php
namespace App\Repository;
use App\Entity\Scrum;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
2022-01-22 11:18:42 +01:00
use App\Service\giteaService;
use App\Entity\Scrumissue as Scrumissue;
2023-08-25 14:44:12 +02:00
use Symfony\Component\HttpFoundation\Session\SessionInterface;
2021-07-20 13:04:47 +02:00
class ScrumRepository extends ServiceEntityRepository
{
2023-08-25 14:44:12 +02:00
private $session;
2023-08-29 17:07:47 +02:00
private $firstcolumn;
private $datescrumupdate;
2023-08-25 14:44:12 +02:00
public function __construct(ManagerRegistry $registry,giteaService $giteaservice,SessionInterface $session)
2021-07-20 13:04:47 +02:00
{
parent::__construct($registry, Scrum::class);
2022-01-22 11:18:42 +01:00
$this->giteaservice = $giteaservice;
2023-08-25 14:44:12 +02:00
$this->session = $session;
2022-01-22 11:18:42 +01:00
}
2022-06-29 11:50:27 +02:00
public function findByUser($user) {
if($user->hasRole("ROLE_MASTER") || $user->hasRole("ROLE_MODO") || $user->hasRole("ROLE_ADMIN"))
return $this->_em->getRepository("App:Scrum")->findBy([],["name"=>"ASC"]);
else {
$scrums=$this ->createQueryBuilder('s')
->from('App:User','u')
->Where('u.id = :id')
->andWhere('u MEMBER OF s.users')
->setParameter('id',$user->getId())
->orderBy('s.name')
->getQuery()->getResult();
return $scrums;
}
}
2023-11-07 17:03:54 +01:00
public function getGitea($scrum,&$giteaassignees,&$giteacolumns,&$giteamilestones,&$giteateams,&$giteaprioritys,&$giteatypes,&$gitealabels, $forcereload=false) {
2023-08-25 14:44:12 +02:00
$viewclosed = $this->session->get("viewclosed");
// Récupérer les labels de gitea
$gitealabels=$this->giteaservice->getLabels($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"]);
2024-02-16 16:40:41 +01:00
if(!is_array($gitealabels)) die("Probleme de connexion avec gitea veuillez vous <a href='/logout'>reconnecter</a>");
// S'assurer que nos colonnes existes toujours
$scrumcolumns=$scrum->getScrumcolumns();
foreach($scrumcolumns as $scrumcolumn) {
$gitealabel=$this->giteaservice->getLabel($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$scrumcolumn->getGiteaid());
if(!$gitealabel) {
$this->_em->remove($scrumcolumn);
$this->_em->flush();
}
}
// S'assurer que nos teams existes toujours
$scrumteams=$scrum->getScrumteams();
foreach($scrumteams as $scrumteam) {
$gitealabel=$this->giteaservice->getLabel($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$scrumteam->getGiteaid());
if(!$gitealabel) {
$this->_em->remove($scrumteam);
$this->_em->flush();
}
}
// S'assurer que nos prioritys existes toujours
$scrumprioritys=$scrum->getScrumprioritys();
foreach($scrumprioritys as $scrumpriority) {
$gitealabel=$this->giteaservice->getLabel($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$scrumpriority->getGiteaid());
if(!$gitealabel) {
$this->_em->remove($scrumpriority);
$this->_em->flush();
}
}
// S'assurer que nos types existes toujours
$scrumtypes=$scrum->getScrumtypes();
foreach($scrumtypes as $scrumtype) {
$gitealabel=$this->giteaservice->getLabel($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$scrumtype->getGiteaid());
if(!$gitealabel) {
$this->_em->remove($scrumtype);
$this->_em->flush();
}
}
// S'assurer que nos sprint sont toujours lié à un jalon existant
$scrumsprints=$scrum->getScrumsprints();
foreach($scrumsprints as $scrumsprint) {
$giteamilestone=$this->giteaservice->getMilestone($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$scrumsprint->getGiteamilestone());
if(!$giteamilestone) {
$this->_em->remove($scrumsprint);
$this->_em->flush();
}
}
2022-01-22 11:18:42 +01:00
// Récupérer le dernier order
$last = $this->_em->getRepository('App:Scrumissue')->findOneBy(["scrum"=>$scrum], ['rowid' => 'DESC']);
if(!$last) $lastrowid=-1;
else $lastrowid=$last->getRowid();
// Récupérer la premier column scrum
2023-08-29 17:07:47 +02:00
$this->firstcolumn=$this->_em->getRepository('App:Scrumcolumn')->findOneBy(["scrum"=>$scrum], ['rowid' => 'ASC']);
if(!$this->firstcolumn) return $this->redirectToRoute($this->route."_update",["id"=>$scrum->getId()]);
2022-01-22 11:18:42 +01:00
// Récupérer la premier column scrum
$columns=$this->_em->getRepository('App:Scrumcolumn')->findBy(["scrum"=>$scrum], ['rowid' => 'ASC']);
$giteacolumns=[];
foreach($columns as $column) {
array_push($giteacolumns,$column->getGiteaid());
}
2022-01-28 16:54:46 +01:00
// Récupérer les teams
$teams=$this->_em->getRepository('App:Scrumteam')->findBy(["scrum"=>$scrum], ['rowid' => 'ASC']);
$giteateams=[];
foreach($teams as $team) {
array_push($giteateams,$team->getGiteaid());
}
2022-02-03 17:26:35 +01:00
// Récupérer les prioritys
$prioritys=$this->_em->getRepository('App:Scrumpriority')->findBy(["scrum"=>$scrum], ['rowid' => 'ASC']);
$giteaprioritys=[];
foreach($prioritys as $priority) {
array_push($giteaprioritys,$priority->getGiteaid());
}
2023-08-30 11:15:33 +02:00
// Récupérer les types
$types=$this->_em->getRepository('App:Scrumtype')->findBy(["scrum"=>$scrum], ['rowid' => 'ASC']);
$giteatypes=[];
foreach($types as $type) {
array_push($giteatypes,$type->getGiteaid());
}
2022-01-22 11:18:42 +01:00
// Récupérer la orga de gitea
if(!empty($scrum->getGiteajson()["owner"]["email"]))
$giteaassignees=[$scrum->getGiteajson()["owner"]];
else
$giteaassignees=[];
2022-01-28 16:54:46 +01:00
$giteacollaborators=$this->giteaservice->getOrgateams($scrum->getGiteajson()["owner"]["login"]);
if($giteacollaborators&&is_array($giteacollaborators)) {
foreach($giteacollaborators as $team) {
2022-01-22 11:18:42 +01:00
$giteamembers=$this->giteaservice->getTeammembers($team->id);
if($giteamembers&&is_array($giteamembers)) {
foreach($giteamembers as $giteamember) {
if(!in_array($giteamember,$giteaassignees))
array_push($giteaassignees,$giteamember);
}
}
}
}
$giteacollaborators=$this->giteaservice->getCollaborators($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"]);
2024-02-16 16:40:41 +01:00
if(!is_array($giteacollaborators)) die("Probleme de connexion avec gitea veuillez vous <a href='/logout'>reconnecter</a>");
2022-01-22 11:18:42 +01:00
foreach($giteacollaborators as $giteacollaborator) {
if(!in_array($giteacollaborator,$giteaassignees))
array_push($giteaassignees,$giteacollaborator);
}
// Récupérer les milestones de gitea
2023-08-25 14:44:12 +02:00
$giteamilestones=$this->giteaservice->getMilestones($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],"?state=".($viewclosed=="true"?"all":"open"));
2024-02-16 16:40:41 +01:00
if(!is_array($giteamilestones)) die("Probleme de connexion avec gitea veuillez vous <a href='/logout'>reconnecter</a>");
2022-01-22 11:18:42 +01:00
$keysort = array_column($giteamilestones, 'title');
array_multisort($keysort, SORT_DESC, $giteamilestones);
2023-08-25 14:44:12 +02:00
2022-01-22 11:18:42 +01:00
// Récupérer les labels de gitea
$gitealabels=$this->giteaservice->getLabels($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"]);
2023-08-24 13:22:38 +02:00
// Formatter les labels non colomn/team/priority/type
2022-01-22 11:18:42 +01:00
foreach($gitealabels as $key => $gitealabel) {
2023-08-30 11:15:33 +02:00
if(in_array($gitealabel->id,$giteacolumns)||in_array($gitealabel->id,$giteateams)||in_array($gitealabel->id,$giteaprioritys)||in_array($gitealabel->id,$giteatypes))
2022-01-22 11:18:42 +01:00
unset($gitealabels[$key]);
}
// Récupérer les issues de gitea
2023-08-25 14:44:12 +02:00
$giteaissues=$this->giteaservice->getIssues($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],"?state=".($viewclosed=="true"?"all":"open"));
2024-02-16 16:40:41 +01:00
if(!is_array($giteaissues)) die("Probleme de connexion avec gitea veuillez vous <a href='/logout'>reconnecter</a>");
2022-01-22 11:18:42 +01:00
// Génération des issues
$tbgiteaissues=[];
$fgscrumupdate=false;
2023-08-29 17:07:47 +02:00
$this->datescrumupdate=new \DateTime();
2022-01-22 11:18:42 +01:00
foreach($giteaissues as $giteaissue) {
$scrumissue=$this->_em->getRepository("App:Scrumissue")->findOneBy(["scrum"=>$scrum,"giteaid"=>$giteaissue->id]);
if(!$scrumissue) {
$lastrowid++;
$scrumissue=new Scrumissue();
$scrumissue->setScrum($scrum);
$scrumissue->setRowid($lastrowid);
$scrumissue->setGiteaid($giteaissue->id);
$this->majissue($scrumissue,$giteaissue,true);
2022-01-22 11:18:42 +01:00
}
else {
$fgissueupdated=false;
if($scrumissue->getGiteajson()["updated_at"]!=json_decode(json_encode($giteaissue), true)["updated_at"])
$fgissueupdated=true;
if($scrumissue->getGiteajson()["closed_at"]!=json_decode(json_encode($giteaissue), true)["closed_at"])
$fgissueupdated=true;
if($giteaissue->milestone&&$scrumissue->getGiteamilestonename()!=$giteaissue->milestone->title)
$fgissueupdated=true;
if(!$giteaissue->milestone&&!is_null($scrumissue->getGiteamilestone()))
$fgissueupdated=true;
2022-01-22 11:18:42 +01:00
if($forcereload) {
$fgissueupdated=true;
}
if($fgissueupdated) {
$fgscrumupdate=true;
$this->majissue($scrumissue,$giteaissue,false);
}
2022-01-22 11:18:42 +01:00
}
array_push($tbgiteaissues,$giteaissue->number);
}
// Placer à closed l'ensemble des issues vu comme open mais qui ne le sont plus
2023-08-29 17:07:47 +02:00
if($viewclosed=="false") {
$scrumissues=$scrum->getScrumissues();
foreach($scrumissues as $scrumissue) {
if($scrumissue->getGiteastate()=="open"&&!in_array($scrumissue->getGiteanumber(),$tbgiteaissues)) {
$giteaissue=$this->giteaservice->getIssue($scrum->getGiteajson()["owner"]["login"],$scrum->getGiteajson()["name"],$scrumissue->getGiteanumber());
$fgscrumupdate=true;
// S'il ne trouve pas d'issue c'est qu'elle n'existe plus
if(!$giteaissue) {
// Faudrait supprimer mais pour l'instant pas touche
//dump("netrouvepasissue alors");
//dump($scrumissue->getGiteanumber());
}
// Sinon mise
else $this->majissue($scrumissue,$giteaissue,false);
2023-08-29 17:07:47 +02:00
}
2022-01-22 11:18:42 +01:00
}
}
2023-08-29 17:07:47 +02:00
2022-01-22 11:18:42 +01:00
if($fgscrumupdate) {
2023-08-29 17:07:47 +02:00
$scrum->setUpdatedate($this->datescrumupdate);
2022-01-22 11:18:42 +01:00
$this->_em->persist($scrum);
$this->_em->flush();
}
2021-07-20 13:04:47 +02:00
}
2023-08-29 17:07:47 +02:00
function majissue($scrumissue,$giteaissue,$issubmit) {
2023-08-29 17:07:47 +02:00
$scrumissue->setGiteanumber($giteaissue->number);
$scrumissue->setGiteastate($giteaissue->state);
$scrumissue->setGiteatitle($giteaissue->title);
2023-08-29 17:07:47 +02:00
if($giteaissue->milestone) {
$scrumissue->setGiteamilestone($giteaissue->milestone->id);
$scrumissue->setGiteamilestonename($giteaissue->milestone->title);
}
else {
$scrumissue->setGiteamilestone(null);
$scrumissue->setGiteamilestonename(null);
}
$scrumissue->setGiteajson(json_decode(json_encode($giteaissue), true));
$this->_em->persist($scrumissue);
$this->_em->flush();
$havecolumn=false;
foreach($giteaissue->labels as $gitealabel) {
2023-11-07 17:03:54 +01:00
$scrumcolumn=$this->_em->getRepository('App:Scrumcolumn')->findOneBy(["scrum"=>$scrumissue->getScrum(),"giteaid"=>$gitealabel->id]);
2023-08-29 17:07:47 +02:00
if($scrumcolumn) {
$havecolumn=true;
$scrumissue->setScrumcolumn($scrumcolumn);
break;
}
}
if(!$havecolumn) {
$scrumissue->setScrumcolumn($this->firstcolumn);
// Affecter le label backlog en tant que gitealabel
$backlog=array($this->firstcolumn->getGiteaid());
$this->giteaservice->putIssuelabels($scrumissue->getScrum()->getGiteajson()["owner"]["login"],$scrumissue->getScrum()->getGiteajson()["name"],$scrumissue->getGiteanumber(),$backlog);
}
// Sauvegarde de l'issue
$this->_em->persist($scrumissue);
$this->_em->flush();
}
2021-07-20 13:04:47 +02:00
}