diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2016-09-19 10:26:25 +0300 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2016-09-19 10:42:06 +0300 |
commit | 8f6208513a98b33d7edd6ecf1ae6062f266c279f (patch) | |
tree | 4bc003a3e775ab02030a095dac835a84c73b8081 /app | |
parent | b9bf23e07cf886825fc9b0038caae4bfd78d3c05 (diff) |
Test all cycle analytics pre-calculation code.
All the code that pre-calculates metrics for use in the cycle analytics
page.
- Ci::Pipeline -> build start/finish
- Ci::Pipeline#merge_requests
- Issue -> record default metrics after save
- MergeRequest -> record default metrics after save
- Deployment -> Update "first_deployed_to_production_at" for MR metrics
- Git Push -> Update "first commit mention" for issue metrics
- Merge request create/update/refresh -> Update "merge requests closing issues"
Diffstat (limited to 'app')
-rw-r--r-- | app/models/ci/pipeline.rb | 2 | ||||
-rw-r--r-- | app/models/merge_request.rb | 1 | ||||
-rw-r--r-- | app/services/create_deployment_service.rb | 25 | ||||
-rw-r--r-- | app/services/git_push_service.rb | 1 |
4 files changed, 19 insertions, 10 deletions
diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 1f7e6b11bb9..66f43456c06 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -283,7 +283,7 @@ module Ci # the merge request's latest commit. def merge_requests project.merge_requests.where(source_branch: self.ref). - select { |merge_request| merge_request.pipeline.id == self.id } + select { |merge_request| merge_request.pipeline.try(:id) == self.id } end private diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 14e093eed93..6548b84ea7a 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -503,6 +503,7 @@ class MergeRequest < ActiveRecord::Base # running `ReferenceExtractor` on each of them separately. def cache_merge_request_closes_issues!(current_user = self.author) transaction do + self.merge_requests_closing_issues.destroy_all closes_issues(current_user).each do |issue| MergeRequestsClosingIssues.create!(merge_request: self, issue: issue) end diff --git a/app/services/create_deployment_service.rb b/app/services/create_deployment_service.rb index cc63eeae9c3..c126b2fad31 100644 --- a/app/services/create_deployment_service.rb +++ b/app/services/create_deployment_service.rb @@ -23,17 +23,26 @@ class CreateDeploymentService < BaseService private def update_merge_request_metrics(deployment, environment) - # TODO: Test cases: - # 1. Merge request with metrics available and `first_deployed_to_production_at` is nil - # 2. Merge request with metrics available and `first_deployed_to_production_at` is set - # 3. Merge request with metrics unavailable - # 4. Only applies to merge requests merged AFTER the previous production deploy to this branch if environment.name == "production" - merge_requests_deployed_to_production_for_first_time = project.merge_requests.joins("LEFT OUTER JOIN merge_request_metrics ON merge_request_metrics.merge_request_id = merge_requests.id"). - where(target_branch: params[:ref], "merge_request_metrics.first_deployed_to_production_at" => nil). - where("merge_request_metrics.merged_at < ?", deployment.created_at) + query = project.merge_requests.joins("LEFT OUTER JOIN merge_request_metrics ON merge_request_metrics.merge_request_id = merge_requests.id"). + where(target_branch: params[:ref], "merge_request_metrics.first_deployed_to_production_at" => nil) + + previous_deployment = previous_deployment_for_ref(deployment) + merge_requests_deployed_to_production_for_first_time = if previous_deployment + query.where("merge_request_metrics.merged_at < ? AND merge_request_metrics.merged_at > ?", deployment.created_at, previous_deployment.created_at) + else + query.where("merge_request_metrics.merged_at < ?", deployment.created_at) + end merge_requests_deployed_to_production_for_first_time.each { |merge_request| merge_request.metrics.record_production_deploy!(deployment.created_at) } end end + + def previous_deployment_for_ref(current_deployment) + @previous_deployment_for_ref ||= + project.deployments.joins(:environment). + where("environments.name": params[:environment], ref: params[:ref]). + where.not(id: current_deployment.id). + first + end end diff --git a/app/services/git_push_service.rb b/app/services/git_push_service.rb index 0f4f805155f..2aab67b4b9d 100644 --- a/app/services/git_push_service.rb +++ b/app/services/git_push_service.rb @@ -188,7 +188,6 @@ class GitPushService < BaseService @branch_name ||= Gitlab::Git.ref_name(params[:ref]) end - # TODO: What about commits created using the Web UI? Cross references are not created there. def update_issue_metrics(commit, authors) mentioned_issues = commit.all_references(authors[commit]).issues mentioned_issues.each { |issue| issue.metrics.record_commit_mention!(commit.committed_date) if issue.metrics.present? } |