/** * Retrieve project commits from specified date and time. * * @param Project $project Project model * @param DateTime $dateFrom Date and time from get a commits. * * @return BaseCommit[] */ public function getProjectContributions(Project $project, DateTime $dateFrom) { $pageLimit = 10; $skip = 0; /* @var $repository BaseRepository */ $repository = $project->getRepositoryObject(); $currentDate = new DateTime(); do { /* @var $commits BaseCommit[] */ $commits = $repository->getHistory($pageLimit, $skip); foreach ($commits as $commit) { if ($commit->getDate()->getTimestamp() > $dateFrom->getTimestamp()) { /* @var $commit BaseCommit */ (yield $commit); } // if current timestamp less than commit timestamp $currentDate = $commit->getDate(); if ($currentDate->getTimestamp() <= $dateFrom->getTimestamp()) { break; } } $skip += count($commits); // reached last page, break if (count($commits) < $pageLimit) { break; } } while ($currentDate->getTimestamp() > $dateFrom->getTimestamp()); }
/** * Find project model by identifier. * * Throws 404 if not found. * * @param integer $id * @return Project * @throws NotFoundHttpException */ protected function findModel($id) { $model = is_scalar($id) ? Project::findOne($id) : null; if (!$model instanceof Project) { throw new NotFoundHttpException(); } return $model; }
/** * @inheritdoc */ public function init() { // find a project using id request variable $projectId = Yii::$app->request->get('id', null); if (is_scalar($projectId)) { $this->project = Project::findOne((int) $projectId); } if (!$this->project instanceof Project) { throw new NotFoundHttpException(); } $this->repository = $this->project->getRepositoryObject(); return parent::init(); }
/** * Check create a project * * @return Project */ public function testCreateProject() { $model = new Project(); $this->assertFalse($model->validate(), 'Check error validation'); // check title validation $this->assertArrayHasKey('title', $model->getErrors(), 'Check title error validation'); $model->title = str_repeat('ttt', Project::MAX_TITLE_LENGTH); $this->assertFalse($model->validate()); $this->assertArrayHasKey('title', $model->getErrors(), 'Check title error validation'); $model->title = 'Test repo'; $this->assertFalse($model->validate()); $this->assertArrayNotHasKey('title', $model->getErrors()); $this->checkRepoPath($model, Project::REPO_HG); $this->checkRepoPath($model, Project::REPO_GIT); $this->assertTrue($model->save()); return $model; }
/** * @inheritdoc */ public function init() { if (!$this->project instanceof Project) { throw new InvalidParamException('Project variable must be an instance of ' . Project::className()); } if (!$this->repository instanceof BaseRepository) { throw new InvalidParamException('Repository variable must be an instance of ' . BaseRepository::className()); } if (!$this->commit instanceof BaseCommit) { throw new InvalidParamException('Commit variable must be an instance of ' . BaseCommit::className()); } if (!$this->file instanceof File) { throw new InvalidParamException('File variable must be an instance of ' . File::className()); } $this->id = $this->commit->getId() . md5($this->file->getPathname()); }
/** * 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()); }
/** * Renders project title * * @return string */ protected function renderTitle() { $content = '<h1 class="inline-title">' . Html::encode($this->project->title) . '</h1>' . "\n"; $content .= '<span class="' . $this->project->getRepoLabelCss() . '">' . $this->project->getRepoTypeName() . '</span>' . "\n"; return $content; }
/** * Retrieve a project model * * @return ActiveQuery */ public function getProject() { return $this->hasOne(Project::className(), ['id' => 'project_id']); }
/** * 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); }
/** * Get project model * * Throws 404 if project not found. * * @param integer $projectId Project identifier * * @return Project * * @throws NotFoundHttpException */ protected function findProject($projectId) { $project = Project::findOne((int) $projectId); if (!$project instanceof Project) { throw new NotFoundHttpException(Yii::t('project', 'Project not found')); } return $project; }
/** * Collect contributions for specified project and date. * * If projectId is not set, collect contributions for any project. * If dateFrom is not set, collect contributions from last check point. * If last check point is not set, collect contributions from 2 last days. * * @param string $dateFrom Date from in Y-m-d H:i:s format. Null if need collect from last check point * @param integer $projectId Project identifier or null if need collect for any project */ public function actionCollectContributions($dateFrom = null, $projectId = null) { $checkpointFile = Yii::getAlias('@runtime/contribution-collector-date.txt'); if (is_null($dateFrom) && is_file($checkpointFile)) { $dateFrom = trim(file_get_contents($checkpointFile)); } elseif (is_null($dateFrom)) { $dateFrom = date('Y-m-d H:i:s', strtotime('-2 day')); } file_put_contents($checkpointFile, date('Y-m-d H:i:s')); $dateFrom = DateTime::createFromFormat('Y-m-d H:i:s', $dateFrom); if ($dateFrom === false) { $this->stderr('Wrong date time format', Console::FG_RED); return 1; } /* @var $projectApi ProjectModule */ $projectApi = Yii::$app->getModule('project'); /* @var $contributorApi ContributorApi */ $contributorApi = Yii::$app->contributors; $resProject = Project::find(); if (!is_null($projectId) && is_scalar($projectId)) { $resProject->andWhere(['id' => (int) $projectId]); } $this->logInfo('Total projects count: ', $resProject->count()); foreach ($resProject->each() as $project) { /* @var $project Project */ $this->logInfo('Collect contributions for: ', $project->title); // calculate sum $cntCollected = 0; $cntErrors = 0; foreach ($projectApi->getProjectContributions($project, $dateFrom) as $commit) { /* @var $commit BaseCommit */ $contributor = $contributorApi->getContributor($project->repo_type, $commit->contributorName, $commit->contributorEmail); $model = $projectApi->createContributionReview($project, $commit, $contributor); if ($model) { $this->stdout('.', Console::FG_GREEN); $cntCollected++; } else { $this->stdout('x', Console::FG_RED); $cntErrors++; } } $this->stdout("\n"); $this->logInfo('Collected: ', $cntCollected); $this->logInfo('Errors: ', $cntErrors); } }