コード例 #1
0
ファイル: ProjectModule.php プロジェクト: kalyabin/comitka
 /**
  * Creates contribution review model.
  *
  * Returns model if successfully added.
  *
  * @param Project $project Project model to relate with model
  * @param BaseCommit $commit Contribution model to relate with model
  * @param ContributorInterface $contributor Contributor model
  * @param ContributorInterface $reviewer Reviewer model (null if set to default contributor reviewer)
  *
  * @return ContributionReview|null
  */
 public function createContributionReview(Project $project, BaseCommit $commit, ContributorInterface $contributor, $reviewer = null)
 {
     $model = new ContributionReview();
     $model->setAttributes(['commit_id' => $commit->getId(), 'project_id' => $project->id, 'date' => $commit->getDate()->format('Y-m-d H:i:s'), 'message' => $commit->message, 'contributor_name' => $commit->contributorName, 'contributor_email' => $commit->contributorEmail, 'repo_type' => $project->repo_type, 'contributor_id' => $contributor->getContributorId(), 'reviewer_id' => $reviewer instanceof ContributorInterface ? $reviewer->getContributorId() : $contributor->getDefaultViewerId()]);
     if ($model->save()) {
         return $model;
     }
     return null;
 }
コード例 #2
0
 /**
  * Aggregate contributions by contributor.
  *
  * Returns results as ContributionByContributor array, wich contains contributor interface and contributions count.
  *
  * @return ContributionByContributor[]
  */
 public function aggregateByContributor()
 {
     $query = ContributionReview::find()->select(['repo_type', 'contributor_name', 'contributor_email', 'contributor_id', 'project_id', new Expression('COUNT(*) as cnt')])->andWhere(['or', ['reviewer_id' => $this->reviewerId], ['reviewer_id' => null]])->groupBy(['project_id', 'repo_type', 'contributor_id', 'contributor_name', 'contributor_email']);
     if ($this->projectId) {
         $query->andWhere(['project_id' => $this->projectId]);
     }
     if ($this->type == self::TYPE_FINISHED) {
         $query->andWhere(['not', 'reviewed' => null]);
     } elseif ($this->type == self::TYPE_NOT_FINISHED) {
         $query->andWhere(['reviewed' => null]);
     }
     $res = $query->createCommand()->query();
     /* @var $contributorsApi ContributorApi */
     $contributorsApi = Yii::$app->contributors;
     $result = [];
     foreach ($res as $item) {
         $contributor = null;
         if (!is_null($item['contributor_id'])) {
             $contributor = $contributorsApi->getContributorById($item['contributor_id']);
         } else {
             $contributor = $contributorsApi->getContributor($item['repo_type'], $item['contributor_name'], $item['contributor_email']);
         }
         $result[] = new ContributionByContributor(['contributor' => $contributor, 'cnt' => $item['cnt']]);
     }
     return $result;
 }
コード例 #3
0
 /**
  * Test model validation and save
  */
 public function testValidationSaveAndDelete()
 {
     $model = new ContributionReview();
     $attributes = ['commit_id' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => ['wrong string'], 'isValid' => false], ['value' => str_repeat('A', 41), 'isValid' => false], ['value' => 1, 'isValid' => false], ['value' => md5(uniqid()), 'isValid' => true]], 'project_id' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'string', 'isValid' => false], ['value' => $this->getModule('Yii2')->grabFixture('projects', 'gitProject')->id, 'isValid' => true]], 'date' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'string', 'isValid' => false], ['value' => 1, 'isValid' => false], ['value' => date('Y-m-d H:i:s'), 'isValid' => true]], 'contributor_id' => [['value' => null, 'isValid' => true], ['value' => [], 'isValid' => true], ['value' => '', 'isValid' => true], ['value' => 'wrong integer', 'isValid' => false], ['value' => ['wrong integer'], 'isValid' => false], ['value' => $this->getModule('Yii2')->grabFixture('users', 'activeUser1')->id, 'isValid' => true]], 'reviewer_id' => [['value' => null, 'isValid' => true], ['value' => [], 'isValid' => true], ['value' => '', 'isValid' => true], ['value' => 'wrong integer', 'isValid' => false], ['value' => ['wrong integer'], 'isValid' => false], ['value' => $this->getModule('Yii2')->grabFixture('users', 'activeUser2')->id, 'isValid' => true]], 'message' => [['value' => null, 'isValid' => true], ['value' => 0, 'isValid' => false], ['value' => [], 'isValid' => true], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'test message', 'isValid' => true]], 'contributor_email' => [['value' => null, 'isValid' => true], ['value' => [], 'isValid' => true], ['value' => 0, 'isValid' => false], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'test contributor', 'isValid' => true]], 'contributor_name' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 0, 'isValid' => false], ['value' => '', 'isValid' => false], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'test contributor', 'isValid' => true]], 'repo_type' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 0, 'isValid' => false], ['value' => '', 'isValid' => false], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'wrong repo', 'isValid' => false], ['value' => 'svn', 'isValid' => true], ['value' => 'git', 'isValid' => true], ['value' => 'hg', 'isValid' => true]], 'reviewed' => [['value' => null, 'isValid' => true], ['value' => [], 'isValid' => true], ['value' => ['wrong string'], 'isValid' => false], ['value' => 'string', 'isValid' => false], ['value' => 1, 'isValid' => false], ['value' => date('Y-m-d H:i:s'), 'isValid' => true]]];
     $this->getModule('\\Helper\\Unit')->validateModelAttributes($model, $attributes, $this);
     $this->assertTrue($model->validate());
     $this->assertTrue($model->save());
     $this->assertInstanceOf(Project::className(), $model->project);
     $this->assertEquals($model->project->id, $this->getModule('Yii2')->grabFixture('projects', 'gitProject')->id);
     $this->assertInstanceOf(User::className(), $model->contributor);
     $this->assertEquals($model->contributor->id, $this->getModule('Yii2')->grabFixture('users', 'activeUser1')->id);
     $this->assertInstanceOf(User::className(), $model->reviewer);
     $this->assertEquals($model->reviewer->id, $this->getModule('Yii2')->grabFixture('users', 'activeUser2')->id);
     // test unique model
     $attributes = $model->getAttributes();
     unset($attributes['id']);
     $newModel = new ContributionReview();
     $newModel->setAttributes($attributes);
     $this->assertFalse($newModel->validate());
     $this->assertArrayHasKey('commit_id', $newModel->getErrors());
     // delete test
     $this->assertEquals(1, $model->delete());
 }
コード例 #4
0
 /**
  * Render commit view.
  * If has CommonException - it's may be only not founded commit - generates 404.
  *
  * @return string
  * @throws NotFoundHttpException
  */
 public function run()
 {
     /* @var $commit BaseCommit */
     $commit = null;
     try {
         // get commit model by commit identifier
         $commit = $this->repository->getCommit($this->commitId);
     } catch (CommonException $ex) {
         throw new NotFoundHttpException(Yii::t('app', 'System error: {message}', ['message' => $ex->getMessage()]), $ex->getCode(), $ex);
     }
     // get contribution review model
     $reviewModel = ContributionReview::find()->andWhere(['project_id' => $this->project->id, 'commit_id' => $commit->getId()])->one();
     return $this->controller->render('commit/summary', ['project' => $this->project, 'repository' => $this->repository, 'commit' => $commit, 'reviewModel' => $reviewModel]);
 }
コード例 #5
0
ファイル: CommitPanel.php プロジェクト: kalyabin/comitka
 /**
  * @inheritdoc
  */
 public function run()
 {
     $ret = Html::beginTag('div', ['class' => !$this->reviewModel || !$this->reviewModel->reviewIsFinished() ? 'col-md-12 alert alert-danger' : 'col-md-12 alert alert-info']);
     $ret .= Html::beginTag('div', ['class' => 'row col-md-12']);
     $ret .= Html::tag('strong', Html::encode($this->commit->getId()));
     $ret .= $this->renderParentsIds();
     $ret .= Html::endTag('div');
     $ret .= Html::beginTag('div', ['class' => 'row col-md-12']);
     $ret .= $this->renderCommiterPanel();
     $ret .= Html::endTag('div');
     $ret .= Html::beginTag('div', ['class' => 'row col-md-6']);
     $ret .= $this->renderContributorPanel();
     $ret .= Html::endTag('div');
     $ret .= Html::beginTag('div', ['class' => 'row col-md-6']);
     $ret .= $this->renderReviewPanel();
     $ret .= Html::endTag('div');
     $ret .= Html::endTag('div');
     return $ret;
 }
コード例 #6
0
ファイル: CommitPanelTest.php プロジェクト: kalyabin/comitka
 /**
  * Test panel with finished review model
  *
  * @depends testWithReview
  */
 public function testWithFinishedReview()
 {
     list($contributor, $reviewer, $authModel, $project, $commit) = $this->prepareFixtures();
     $reviewModel = $this->projectModule->createContributionReview($project, $commit, $contributor, $reviewer);
     $this->assertInstanceOf(ContributionReview::className(), $reviewModel);
     $this->assertTrue($reviewModel->finishReview());
     /* @var $contributor User */
     /* @var $reviewer User */
     /* @var $authModel Auth */
     /* @var $project Project */
     /* @var $commit BaseCommit */
     $result = CommitPanel::widget(['project' => $project, 'commit' => $commit, 'contributor' => $contributor, 'authUser' => $authModel, 'reviewModel' => $reviewModel]);
     // contains review model but not contains finish review button
     $this->assertContains($reviewer->name, $result);
     $this->assertNotContains('did not complete a review', $result);
     $expectedString = '/projects/' . $project->id . '/' . $commit->getId() . '/finish-review';
     $this->assertNotContains($expectedString, $result);
     $this->assertNotContains('Finish review', $result);
     // not contains to be a reviewer button
     $this->assertNotContains('(has no review)', $result);
     // has to be review button
     $expectedString = '/projects/' . $project->id . '/' . $commit->getId() . '/create-self-review';
     $this->assertNotContains($expectedString, $result);
     $this->assertNotContains('To be a reviewer', $result);
     // contains finish review date
     $expectedString = 'at ' . $reviewModel->getReviewedDateTime()->format("d\\'M y H:i:s");
     $this->assertContains($expectedString, $result);
 }
コード例 #7
0
ファイル: ProjectApiTest.php プロジェクト: kalyabin/comitka
 /**
  * Test contribution review
  *
  * @depends testGetProjectContributions
  *
  * @param BaseCommit $commit
  */
 public function testCreateContributionReview(BaseCommit $commit)
 {
     /* @var $project Project */
     $project = $this->getModule('Yii2')->grabFixture('projects', 'gitProject');
     /* @var $reviewer User */
     $reviewer = $this->getModule('Yii2')->grabFixture('users', 'activeUser1');
     /* @var $contributor User */
     $contributor = $this->getModule('Yii2')->grabFixture('users', 'activeUser2');
     // create review without users
     $contributionReview = $this->projectApi->createContributionReview($project, $commit, new UnregisteredContributor());
     $this->assertInstanceOf(ContributionReview::className(), $contributionReview);
     $this->assertInstanceOf(Project::className(), $contributionReview->project);
     $this->assertEquals($contributionReview->project->id, $project->id);
     $this->assertNull($contributionReview->reviewer);
     $this->assertInstanceOf(UnregisteredContributor::className(), $contributionReview->contributor);
     // check unique
     $this->assertNull($this->projectApi->createContributionReview($project, $commit, new UnregisteredContributor()));
     // remove review
     $this->assertEquals(1, $contributionReview->delete());
     // create review with users
     $contributionReview = $this->projectApi->createContributionReview($project, $commit, $contributor);
     $this->assertInstanceOf(ContributionReview::className(), $contributionReview);
     $this->assertInstanceOf(Project::className(), $contributionReview->project);
     $this->assertEquals($contributionReview->project->id, $project->id);
     $this->assertNull($contributionReview->reviewer);
     $this->assertInstanceOf(ContributorInterface::class, $contributionReview->contributor);
     $this->assertEquals($contributionReview->contributor->id, $contributor->id);
     $this->assertEquals(1, $contributionReview->delete());
     $contributionReview = $this->projectApi->createContributionReview($project, $commit, $contributor, $reviewer);
     $this->assertInstanceOf(ContributionReview::className(), $contributionReview);
     $this->assertInstanceOf(Project::className(), $contributionReview->project);
     $this->assertEquals($contributionReview->project->id, $project->id);
     $this->assertInstanceOf(ContributorInterface::class, $contributionReview->reviewer);
     $this->assertEquals($contributionReview->reviewer->id, $reviewer->id);
     $this->assertInstanceOf(ContributorInterface::class, $contributionReview->contributor);
     $this->assertEquals($contributionReview->contributor->id, $contributor->id);
 }
コード例 #8
0
 /**
  * To be reviewer
  *
  * @param integer $projectId Project identifier
  * @param string $commitId Commit identifier
  *
  * @return array
  */
 public function actionCreateSelfReview($projectId, $commitId)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $project = $this->findProject($projectId);
     $commit = $this->findCommit($project, $commitId);
     $result = ['success' => false, 'html' => '', 'message' => ''];
     // find existent review model
     $model = ContributionReview::find()->andWhere(['project_id' => $project->id, 'commit_id' => $commit->getId()])->one();
     if ($model && $model->reviewer_id == Yii::$app->user->getId()) {
         // user try to create existent model
         // it's not an error
         $result['success'] = true;
     } elseif ($model && is_null($model->reviewer_id)) {
         // model exists, but reviewer is not set
         $model->reviewer_id = Yii::$app->user->getId();
         if ($model->save()) {
             $result['success'] = true;
         }
     } elseif ($model) {
         // other reviewer already installed
         $result['success'] = false;
         $result['message'] = Yii::t('project', 'Reviewer is already installed to this contribution');
     } else {
         // model is not exists
         // create it
         $contributor = $this->contributorApi->getContributor($project->repo_type, $commit->contributorName, $commit->contributorEmail);
         $reviewer = $this->contributorApi->getContributorById(Yii::$app->user->getId());
         $model = $this->projectApi->createContributionReview($project, $commit, $contributor, $reviewer);
         if (!$model instanceof ContributionReview) {
             $result['success'] = false;
             $result['message'] = Yii::t('project', 'An error occurred during the creation of the review');
         } else {
             $result['success'] = true;
         }
     }
     if ($result['success'] && $model) {
         $result['html'] = CommitPanel::widget(['reviewModel' => $model, 'authUser' => Yii::$app->user, 'contributor' => $model->contributor, 'project' => $project, 'commit' => $commit, 'reviewButtonClass' => 'js-review-button']);
     }
     return $result;
 }