Esempio n. 1
0
 /**
  * @httpMethod POST
  * @path /{type}/{secret}
  */
 public function insertCommit()
 {
     try {
         $type = $this->getUriFragments('type');
         $secret = $this->getUriFragments('secret');
         // parse request
         $type = TypeAbstract::factory($type);
         $project = $type->getRequest(Base::getRawInput());
         // get project
         $con = new Condition(array('secret', '=', $secret));
         $id = $this->sql->select($this->registry['table.vcshook'], array('id'), $con, Sql::SELECT_FIELD);
         if (!empty($id) && $project->hasCommits()) {
             $count = 0;
             foreach ($project->getCommits() as $commit) {
                 try {
                     $record = $this->hm->getHandler('AmunService\\Vcshook\\Commit')->getRecord();
                     $record->setProjectId($id);
                     $record->setAuthor($commit->getAuthor());
                     $record->setUrl($commit->getUrl());
                     $record->setMessage($commit->getMessage());
                     $record->setTimestamp($commit->getTimestamp());
                     // notify listener
                     $this->event->notifyListener('vcshook.commit', array($record));
                     $count++;
                 } catch (\Exception $e) {
                     // import fails we go the next commit and ignore the
                     // error
                 }
             }
             if ($count == 0) {
                 throw new Exception('No commits inserted');
             }
         } else {
             throw new Exception('Invalid project or no commits available');
         }
         $msg = new Message('Inserted ' . $count . ' commits', true);
         $this->setResponse($msg);
     } catch (\Exception $e) {
         $msg = new Message($e->getMessage(), false);
         $this->setResponse($msg, null, 500);
     }
 }
Esempio n. 2
0
 public function create(RecordInterface $record)
 {
     if ($record->hasFields('url', 'type')) {
         $record->globalId = $this->base->getUUID('vcshook:' . uniqid());
         $record->userId = $this->user->getId();
         $record->secret = Security::generateToken(40);
         // check whether project exists
         $type = TypeAbstract::factory($record->type);
         if (!$type->hasProject($record->url)) {
             throw new Exception('Project doesnt exist');
         }
         $date = new DateTime('NOW', $this->registry['core.default_timezone']);
         $record->date = $date->format(DateTime::SQL);
         $this->table->insert($record->getData());
         $record->id = $this->sql->getLastInsertId();
         $this->notify(RecordAbstract::INSERT, $record);
         return $record;
     } else {
         throw new Exception('Missing field in record');
     }
 }