/** * Add a comment in JIRA with the commit hash * @param Commit $commit The commit for which we're running the Git Hook * @throws GitHookException if requirement fails */ public function run(Commit $commit) { $jiraIssues = $commit->jiras(); $this->logger->debug('Found ' . count($jiraIssues) . " jira issue(s) in {$commit}"); $revision = $commit->revision(); if ($this->isChangeFeatureFlip($revision)) { foreach ($jiraIssues as $jira) { $this->logger->debug("Adding comment to jira {$jira}"); $this->addLabels($jira); } } }
/** * @integrationTest */ public function testRawFileContentsReal() { // Let's make sure the command we're running is legit $expectedContents = trim(shell_exec('git show HEAD:composer.json')); // Replicate the actual shell invocation that would take place Diesel::registerInstantiator('Bart\\Shell\\Command', function () { $shell = new Shell(); $args = func_get_args(); return call_user_func_array([$shell, 'command'], $args); }); $commit = new Commit(new GitRoot(BART_DIR . '/.git'), 'HEAD'); $actualContents = trim($commit->rawFileContents('composer.json')); $this->assertEquals($expectedContents, $actualContents, 'Raw file contents'); }
/** * @param Commit $commit * @throws GitHookException * @throws GitException */ public function run(Commit $commit) { if ($this->job->isHealthy()) { $this->logger->debug('Jenkins job is healthy.'); return; } $this->logger->info('Jenkins job is not healthy...asserting that commit message contains {buildfix} hash'); $messageSubject = $commit->messageSubject(); $buildFixDirective = Directives::BUILD_FIX(); // Check if commit has buildfix directive if (preg_match("/{$buildFixDirective->value()}/", $messageSubject) > 0) { $this->logger->info("Commit has {$buildFixDirective} directive. It attempts to fix build"); return; } throw new GitHookException('Jenkins not healthy and commit does not fix it'); }
/** * Fails if review is not approved & verified in Gerrit * @param Commit $commit A git commit with a Change-Id * @throws GitHookException If Change-Id not found or the review is not approved or verified */ public function run(Commit $commit) { try { $changeId = $commit->gerritChangeId(); } catch (GitException $e) { $this->logger->warn("{$e->getMessage()}. Skipping commit."); throw new GitHookException("Couldn't get Change-Id for {$commit}", $e->getCode(), $e); } /** @var \Bart\Gerrit\Change $change */ $change = Diesel::create('\\Bart\\Gerrit\\Change', $changeId); if (!$change->isReviewedAndVerified()) { $msg = "Could not find an approved & verified change in Gerrit for change {$changeId} in commit {$commit}"; $this->logger->info($msg); throw new GitHookException($msg); } $this->logger->info('Gerrit approved.'); }
/** * Run the hook * @param Commit $commit Commit with Gerrit Change-Id * @throws GitHookException if requirement fails */ public function run(Commit $commit) { try { $changeId = $commit->gerritChangeId(); } catch (GitException $e) { $this->logger->warn("{$e->getMessage()}. Skipping commit."); throw new GitHookException("Couldn't get Change-Id for {$commit}", $e->getCode(), $e); } $change = new Change($changeId); try { if (!$change->exists()) { // This is not a warning, because some repositories do not require code review $this->logger->info('Skipping change b/c it does not exist in Gerrit'); return; } $change->abandon("Abandoning from git hook for commit {$commit}."); } catch (GerritException $e) { $this->logger->error("Problem abandoning Gerrit review {$changeId}", $e); throw new GitHookException("Problem abandoning Gerrit review {$changeId}", $e->getCode(), $e); } }
/** * Run the hook * @param Commit $commit Commit with Gerrit Change-Id * @throws GitHookException if requirement fails */ public function run(Commit $commit) { try { $changeId = $commit->gerritChangeId(); } catch (GitException $e) { $this->logger->warn("{$e->getMessage()}. Skipping commit."); throw new GitHookException("Couldn't get Change-Id for {$commit}", $e->getCode(), $e); } $change = new Change($changeId); try { if (!$change->exists()) { // This is not a warning, because some repositories do not require code review $this->logger->debug('Skipping change b/c it does not exist in Gerrit'); return; } $change->markMerged($commit->revision()); $change->comment("Git hook marking {$changeId} as merged by {$commit}"); } catch (GerritException $e) { $this->logger->error("Failed to mark Gerrit reivew {$changeId} as merged", $e); throw new GitHookException("Failed to mark Gerrit reivew {$changeId} as merged", $e->getCode(), $e); } }
/** * Run the action * @param Commit $commit commit to verify * @throws GitHookException if requirement fails */ public function run(Commit $commit) { $frozenRepos = $this->config->frozenRepoNames(); if (count($frozenRepos) == 0) { $this->logger->debug("No frozen repos."); return; } // Super users are exempt from frozen checks // So hope they don't do anything bad by mistake! if ($this->isSuperUser()) { $this->logger->debug("Superuser exempt from freeze"); return; } if ($frozenRepos === ['all']) { throw new GitHookException('All repositories are frozen.'); } $project = $commit->getProjectName(); $this->logger->debug("Validating if {$project} is frozen"); if (in_array($project, $frozenRepos)) { throw new GitHookException("{$project} repository is frozen."); } }
/** * @param Commit $commit * @return bool If the commit should be skipped by the hook */ private function shouldSkip(Commit $commit, GitHookConfig $gitHookConfig) { $message = $commit->messageBody(); $isEmergency = preg_match('/^EMERGENCY/', $message) === 1; if ($isEmergency) { $to = $gitHookConfig->getEmergencyNotificationEmailAddress(); $subject = $gitHookConfig->getEmergencyNotificationSubject(); $body = $gitHookConfig->getEmergencyNotificationBody(); if (!empty($to) && !empty($subject) && !empty($body)) { GlobalFunctions::mail($to, $subject, $body); } else { $this->logger->error("Invalid mail params. Notification email not sent."); } } return $isEmergency; }