diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-02-07 03:06:46 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-02-07 03:06:46 +0300 |
commit | 9d7c5e75841eff48217487b3acda56cf265a2aef (patch) | |
tree | d90819e572eab930a8729ed767f6565620922e8a /app/finders/environments_finder.rb | |
parent | b0279cc2239e0b24d59bc80085a9ba42fcf6226a (diff) |
Address feedback
Diffstat (limited to 'app/finders/environments_finder.rb')
-rw-r--r-- | app/finders/environments_finder.rb | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/app/finders/environments_finder.rb b/app/finders/environments_finder.rb new file mode 100644 index 00000000000..5c798a75945 --- /dev/null +++ b/app/finders/environments_finder.rb @@ -0,0 +1,57 @@ +class EnvironmentsFinder + attr_reader :project, :current_user, :params + + def initialize(project, current_user, params = {}) + @project, @current_user, @params = project, current_user, params + end + + def execute(skip_authorization: false) + deployments = project.deployments + deployments = + if ref + deployments_query = params[:with_tags] ? 'ref = :ref OR tag IS TRUE' : 'ref = :ref' + deployments.where(deployments_query, ref: ref.to_s) + elsif commit + deployments.where(sha: commit.sha) + else + deployments.none + end + + environment_ids = deployments + .group(:environment_id) + .select(:environment_id) + + environments = project.environments.available + .where(id: environment_ids).order_by_last_deployed_at.to_a + + if ref && commit + environments.select! do |environment| + environment.includes_commit?(commit) + end + end + + if ref && params[:recently_updated] + environments.select! do |environment| + environment.recently_updated_on_branch?(ref) + end + end + + unless skip_authorization + environments.select! do |environment| + Ability.allowed?(current_user, :read_environment, environment) + end + end + + environments + end + + private + + def ref + params[:ref].try(:to_s) + end + + def commit + params[:commit] + end +end |