Exemplo n.º 1
0
 public function Save()
 {
     $this->StartTime = pdo_real_escape_string($this->StartTime);
     $this->EndTime = pdo_real_escape_string($this->EndTime);
     $this->SubmitTime = pdo_real_escape_string($this->SubmitTime);
     $this->Command = pdo_real_escape_string(trim($this->Command));
     $this->Log = pdo_real_escape_string(trim($this->Log));
     // Compute the number of errors and warnings.
     // This speeds up the display of the main table.
     $nbuilderrors = -1;
     $nbuildwarnings = -1;
     if ($this->InsertErrors) {
         $nbuilderrors = 0;
         $nbuildwarnings = 0;
         foreach ($this->Errors as $error) {
             if ($error->Type == 0) {
                 $nbuilderrors++;
             } else {
                 $nbuildwarnings++;
             }
         }
     }
     if (!$this->Exists()) {
         $id = '';
         $idvalue = '';
         if ($this->Id) {
             $id = 'id,';
             $idvalue = qnum($this->Id) . ',';
         }
         if (strlen($this->Type) == 0) {
             $this->Type = extract_type_from_buildstamp($this->Stamp);
         }
         $this->Name = pdo_real_escape_string($this->Name);
         $this->Stamp = pdo_real_escape_string($this->Stamp);
         $this->Type = pdo_real_escape_string($this->Type);
         $this->Generator = pdo_real_escape_string($this->Generator);
         $this->SetParentId(0);
         $justCreatedParent = false;
         if ($this->SubProjectName) {
             $this->SetParentId($this->LookupParentBuildId());
             if ($this->ParentId == 0) {
                 // This is the first subproject to submit for a new build.
                 // Create a new parent build for it.
                 $justCreatedParent = $this->CreateParentBuild($nbuilderrors, $nbuildwarnings);
             }
         }
         $this->Uuid = Build::GenerateUuid($this->Stamp, $this->Name, $this->SiteId, $this->ProjectId, $this->SubProjectName);
         $query = 'INSERT INTO build
             (' . $id . 'siteid, projectid, stamp, name, type, generator,
              starttime, endtime, submittime, command, log, builderrors,
              buildwarnings, parentid, uuid, changeid)
             VALUES
             (' . $idvalue . "'{$this->SiteId}', '{$this->ProjectId}',\n                 '{$this->Stamp}', '{$this->Name}', '{$this->Type}',\n                 '{$this->Generator}', '{$this->StartTime}', '{$this->EndTime}',\n                 '{$this->SubmitTime}', '{$this->Command}', '{$this->Log}',\n                 {$nbuilderrors}, {$nbuildwarnings}, {$this->ParentId},\n                 '{$this->Uuid}', '{$this->PullRequest}')";
         if (!pdo_query($query)) {
             $error = pdo_error(null, false);
             // This error might be due to a unique constraint violation
             // for this UUID.  Query for such a previously existing build.
             $existing_id_result = pdo_single_row_query("SELECT id FROM build WHERE uuid = '{$this->Uuid}'");
             if ($existing_id_result && array_key_exists('id', $existing_id_result)) {
                 $this->Id = $existing_id_result['id'];
                 // If a previously existing build with this UUID was found
                 // call UpdateBuild() on it.  This also sets ParentId
                 // if an existing parent was found.
                 $this->UpdateBuild($this->Id, $nbuilderrors, $nbuildwarnings);
                 // Does the parent still need to be created?
                 if ($this->SubProjectName && $this->ParentId < 1) {
                     if (!$this->CreateParentBuild($nbuilderrors, $nbuildwarnings)) {
                         // Someone else created the parent after we called
                         // UpdateBuild(this->Id,...).
                         // In this case we also need to manually update
                         // the parent as well.
                         $this->UpdateBuild($this->ParentId, $nbuilderrors, $nbuildwarnings);
                     }
                 }
                 // Now that the existing build and its parent (if any) have
                 // been updated we can return early.
                 return true;
             }
             add_log("SQL error: {$error}", 'Build Insert', LOG_ERR, $this->ProjectId, $this->Id);
             return false;
         }
         if (!$this->Id) {
             $this->Id = pdo_insert_id('build');
         }
         // Add the groupid
         if ($this->GroupId) {
             $query = "INSERT INTO build2group (groupid,buildid) VALUES ('{$this->GroupId}','{$this->Id}')";
             if (!pdo_query($query)) {
                 add_last_sql_error('Build2Group Insert', $this->ProjectId, $this->Id);
             }
             // Associate the parent with this group too.
             if ($this->ParentId > 0) {
                 $result = pdo_query('SELECT groupid FROM build2group WHERE buildid=' . qnum($this->ParentId));
                 if (pdo_num_rows($result) == 0) {
                     global $CDASH_DB_TYPE;
                     $duplicate_sql = '';
                     if ($CDASH_DB_TYPE !== 'pgsql') {
                         $duplicate_sql = 'ON DUPLICATE KEY UPDATE groupid=groupid';
                     }
                     $query = "INSERT INTO build2group (groupid,buildid)\n                            VALUES ('{$this->GroupId}','{$this->ParentId}')\n                            {$duplicate_sql}";
                     if (!pdo_query($query)) {
                         add_last_sql_error('Parent Build2Group Insert', $this->ProjectId, $this->ParentId);
                     }
                 }
             }
         }
         // Add the subproject2build relationship:
         if ($this->SubProjectId) {
             $query = "INSERT INTO subproject2build (subprojectid,buildid) VALUES ('{$this->SubProjectId}','{$this->Id}')";
             if (!pdo_query($query)) {
                 add_last_sql_error('SubProject2Build Insert', $this->ProjectId, $this->Id);
             }
         }
         // Save the information
         if (!empty($this->Information)) {
             $this->Information->BuildId = $this->Id;
             $this->Information->Save();
         }
         // Update parent's tally of total build errors & warnings.
         if (!$justCreatedParent) {
             $this->UpdateBuild($this->ParentId, $nbuilderrors, $nbuildwarnings);
         } elseif ($this->ParentId > 0) {
             // If we just created a child build, associate it with
             // the parent's updates (if any).
             require_once 'models/buildupdate.php';
             BuildUpdate::AssignUpdateToChild($this->Id, $this->ParentId);
         }
     } else {
         // Build already exists.
         // Update this build and its parent (if necessary).
         $this->UpdateBuild($this->Id, $nbuilderrors, $nbuildwarnings);
     }
     // Add errors/warnings
     foreach ($this->Errors as $error) {
         $error->BuildId = $this->Id;
         $error->Insert();
     }
     // Add ErrorDiff
     foreach ($this->ErrorDiffs as $diff) {
         $diff->BuildId = $this->Id;
         $diff->Insert();
     }
     // Add label associations regardless of how Build::Save gets called:
     //
     $this->InsertLabelAssociations();
     // Should we post build errors to a pull request?
     if (isset($this->PullRequest)) {
         $hasErrors = false;
         foreach ($this->Errors as $error) {
             if ($error->Type == 0) {
                 $hasErrors = true;
                 break;
             }
         }
         if ($hasErrors) {
             $message = 'This build experienced errors';
             $url = get_server_URI(false) . "/viewBuildError.php?buildid={$this->Id}";
             $this->NotifyPullRequest($message, $url);
         }
     }
     return true;
 }