Skip to content

Commit

Permalink
show running on on builds
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Dec 15, 2017
1 parent bd3ef34 commit 9bb6435
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 128 deletions.
20 changes: 18 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def audited_classes
end
end

def icon_tag(type)
content_tag :i, '', class: "glyphicon glyphicon-#{type}"
def icon_tag(type, options = {})
content_tag :i, '', options.merge(class: "glyphicon glyphicon-#{type}")
end

def link_to_delete(path, options = {})
Expand Down Expand Up @@ -296,4 +296,20 @@ def link_to_chart(name, values)
url = "https://chart.googleapis.com/chart?#{params.to_query}"
link_to icon_tag('signal'), url, target: :blank
end

# show which stages this reference is deploy(ed+ing) to
def deployed_or_running_list(stages, reference)
html = "".html_safe
stages.each do |stage|
next unless deploy = stage.deployed_or_running_deploy
next unless deploy.references?(reference)

text = "".html_safe
text << icon_tag("cloud-upload", title: deploy.status) << " " if deploy.active?
text << stage.name
html << content_tag(:span, text, class: "label label-success release-stage")
html << " "
end
html
end
end
4 changes: 1 addition & 3 deletions app/helpers/stages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ def edit_command_link(command)
end

def stage_template_icon
content_tag :span, '',
class: "glyphicon glyphicon-duplicate",
title: "Template stage, this stage will be used when copying to new Deploy Groups"
icon_tag "duplicate", title: "Template stage, this stage will be used when copying to new Deploy Groups"
end
end
7 changes: 6 additions & 1 deletion app/models/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ def summary_for_timeline
end
end

def references?(ref)
reference == ref || (ref =~ Build::SHA1_REGEX && job&.commit == ref)
end

# TODO: remove this an delegate to job directly, a commit is not a reference
def commit
job.try(:commit).presence || reference
job&.commit.presence || reference
end

def short_reference
Expand Down
4 changes: 0 additions & 4 deletions app/models/release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class Release < ActiveRecord::Base
# This constant is here for convenience - the value that the database uses is in db/schema.rb.
DEFAULT_RELEASE_NUMBER = "1"

def currently_deploying_stages
project.stages.where_reference_being_deployed(version)
end

def changeset
@changeset ||= Changeset.new(project.repository_path, previous_release.try(:commit), commit)
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def last_successful_deploy
@last_successful_deploy = deploys.successful.first
end

# comparing commits might be better ...
def current_release?(release)
last_successful_deploy&.reference == release.version
# last active or successful deploy
def deployed_or_running_deploy
deploys.joins(:job).where("jobs.status" => Job::ACTIVE_STATUSES + ["succeeded"]).first
end

def create_deploy(user, attributes = {})
Expand Down
5 changes: 3 additions & 2 deletions app/views/builds/_build.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<% job = build.docker_build_job %>
<tr>
<td><%= link_to build.nice_name, project_build_path(build.project, build) %></td>
<% unless @project %>
<% if @project %>
<td><%= deployed_or_running_list @project.stages, build.git_sha %></td>
<% else %>
<td><%= link_to build.project.name, build.project %></td>
<% end %>
<td><%= relative_time(build.created_at) %></td>
<td><%= build.creator&.name || "Trigger" %></td>
<td><%= duration_text job.duration if job %></td>
<td><%= git_ref_and_sha_for(build) %></td>
<td><%= build.image_name.presence || build.dockerfile %></td>
Expand Down
5 changes: 3 additions & 2 deletions app/views/builds/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
<thead>
<tr>
<th>Name</th>
<% unless @project %>
<% if @project %>
<th>Deployed to</th>
<% else %>
<th>Project</th>
<% end %>
<th>Created</th>
<th>Created By</th>
<th>
Duration
<%= link_to_chart "Build duration", @builds.map { |b| b.docker_build_job&.duration }.compact %>
Expand Down
9 changes: 0 additions & 9 deletions app/views/releases/_deployed_to.html.erb

This file was deleted.

13 changes: 1 addition & 12 deletions app/views/releases/_release.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@
<td>
<%= release_label(@project, release) %>
</td>
<td>
<% @stages.select {|stage| stage.current_release?(release) }.each do |stage| %>
<span class="label label-success release-stage" title="Currently deployed to <%= stage.name %>">
<%= stage.name %>
</span>
<% end %>
<% release.currently_deploying_stages.each do |stage| %>
<span class="label label-warning release-stage" title="Being deployed to <%= stage.name %>">
<span class="glyphicon glyphicon-cloud-upload"></span> <%= stage.name %>
</span>
<% end %>
</td>
<td><%= deployed_or_running_list @stages, release.version %></td>
<td><%= relative_time(release.created_at) %></td>
<td>
<% if deployer_for_project? %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/releases/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<thead>
<tr>
<th></th>
<th>Currently deployed to</th>
<th>Deployed to</th>
<th>Created</th>
<th></th>
</tr>
Expand Down
1 change: 0 additions & 1 deletion app/views/releases/row_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<div class="tab-pane" id="info-<%= @release.version %>">
Commit: <code><%= @release.commit %></code>
<p>Author: <%= @release.author.name %>
<%= render 'deployed_to', release: @release %>
</div>

<div class="tab-pane" id="pulls-<%= @release.version %>">
Expand Down
4 changes: 3 additions & 1 deletion app/views/releases/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
<%= render 'deploy_to_button', release: @release, stages: @project.stages %>
</div>

<%= render 'deployed_to', release: @release %>
<% if list = deployed_or_running_list(@project.stages, @release.version).presence %>
<p>Deployed to: <%= list %></p>
<% end %>

<label>Created</label>
<%= relative_time(@release.created_at) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def notify
"api_key" => "MY-SECRET",
"deploy" => {
"rails_env" => "staging",
"scm_revision" => "abcabc1",
"scm_revision" => "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1",
"local_username" => "Super Admin",
"scm_repository" => "https://example.com/bar/foo",
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/github/test/models/github_deployment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
production_environment: false,
auto_merge: false,
required_contexts: [],
ref: "abcabc1"
ref: "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1"
}
create = stub_request(:post, endpoint).with(body: body.to_json)
github_deployment.create
Expand Down
4 changes: 2 additions & 2 deletions plugins/jenkins/test/models/samson/jenkins_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def stub_job_detail
def stub_build_with_parameters(update_params)
stub_request(:post, "http://www.test-url.com/job/test_job/buildWithParameters").
with(
body: {"buildStartedBy": "Super Admin", "originatedFrom": "Foo_Staging_staging", "commit": "abcabc1", "deployUrl": "http://www.test-url.com/projects/foo/deploys/#{deploy.id}", "emails" => "[email protected]", "tag" => nil}.merge(update_params),
body: {"buildStartedBy": "Super Admin", "originatedFrom": "Foo_Staging_staging", "commit": "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1", "deployUrl": "http://www.test-url.com/projects/foo/deploys/#{deploy.id}", "emails" => "[email protected]", "tag" => nil}.merge(update_params),
headers: {Authorization: 'Basic dXNlckB0ZXN0LmNvbTpqYXBpa2V5'}
).
to_return(status: 200, body: "", headers: {}).to_timeout
Expand All @@ -30,7 +30,7 @@ def stub_build_with_parameters(update_params)
def stub_build_with_parameters_when_autoconfig_is_enabled(update_params)
stub_request(:post, "http://www.test-url.com/job/test_job/buildWithParameters").
with(
body: {"SAMSON_buildStartedBy": "Super Admin", "SAMSON_commit": "abcabc1", "SAMSON_deployUrl": "http://www.test-url.com/projects/foo/deploys/178003093", "SAMSON_emails": "[email protected]", "SAMSON_originatedFrom": "Foo_Staging_staging", "SAMSON_tag": nil}.merge(update_params),
body: {"SAMSON_buildStartedBy": "Super Admin", "SAMSON_commit": "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1", "SAMSON_deployUrl": "http://www.test-url.com/projects/foo/deploys/178003093", "SAMSON_emails": "[email protected]", "SAMSON_originatedFrom": "Foo_Staging_staging", "SAMSON_tag": nil}.merge(update_params),
headers: {
Authorization: 'Basic dXNlckB0ZXN0LmNvbTpqYXBpa2V5'
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/samson_ledger/test/lib/samson_ledger/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

describe ".post_deployment" do
before do
stub_github_api("repos/bar/foo/compare/abcabc1...staging", "x" => "y")
GITHUB.stubs(:compare).with("bar/foo", "abcabc1", "staging").returns(comparison)
stub_github_api("repos/bar/foo/compare/abcabcaaabcabcaaabcabcaaabcabcaaabcabca1...staging", "x" => "y")
GITHUB.stubs(:compare).with("bar/foo", "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1", "staging").returns(comparison)
Changeset::PullRequest.stubs(:find).with("bar/foo", 42).returns(pull_request)

request_lambda = ->(request) do
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@ succeeded_test:
project: test
status: succeeded
output: This worked!
commit: abcabc1
commit: abcabcaaabcabcaaabcabcaaabcabcaaabcabca1

succeeded_production_test:
command: cap production deploy
user: super_admin
project: test
status: succeeded
output: This worked!
commit: abcabc2
commit: abcabcaaabcabcaaabcabcaaabcabcaaabcabca2

running_test:
command: cap staging deploy
user: super_admin
project: test
status: running
output: This worked!
commit: abcabc3
commit: abcabcaaabcabcaaabcabcaaabcabcaaabcabca3

failed_test:
command: cap staging deploy
user: super_admin
project: test
status: failed
output: Error
commit: staging
commit: staging # failed to resulve the reference so not a valid sha
47 changes: 47 additions & 0 deletions test/helpers/application_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -634,4 +634,51 @@ def render
link_to_chart("Hello world", [1, 2]).must_equal nil
end
end

describe "#icon_tag" do
it "generates simple icons" do
html = icon_tag("foo")
html.must_equal "<i class=\"glyphicon glyphicon-foo\"></i>"
assert html.html_safe?
end

it "generates icons with titles" do
html = icon_tag("foo", title: "bar")
html.must_equal "<i title=\"bar\" class=\"glyphicon glyphicon-foo\"></i>"
assert html.html_safe?
end
end

describe "#deployed_or_running_list" do
let(:stage_list) { [stages(:test_staging)] }

it "produces safe output" do
html = deployed_or_running_list([], "foo")
html.must_equal ""
assert html.html_safe?
end

it "renders succeeded deploys" do
html = deployed_or_running_list(stage_list, "staging")
html.must_equal "<span class=\"label label-success release-stage\">Staging</span> "
end

it "ignores failed deploys" do
deploys(:succeeded_test).job.update_column(:status, 'failed')
html = deployed_or_running_list(stage_list, "staging")
html.must_equal ""
end

it "ignores non-matching deploys" do
deploys(:succeeded_test).update_column(:reference, 'nope')
html = deployed_or_running_list(stage_list, "staging")
html.must_equal ""
end

it "shows active deploys" do
deploys(:succeeded_test).job.update_column(:status, 'running')
html = deployed_or_running_list(stage_list, "staging")
html.must_equal "<span class=\"label label-success release-stage\"><i title=\"running\" class=\"glyphicon glyphicon-cloud-upload\"></i> Staging</span> "
end
end
end
32 changes: 30 additions & 2 deletions test/models/deploy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@

describe "#changeset" do
it "creates a changeset to the previous deploy" do
deploy.changeset.commit.must_equal "abcabc1"
deploy.changeset.commit.must_equal "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1"
end
end

Expand Down Expand Up @@ -443,6 +443,33 @@
end
end

describe "#references?" do
before do
deploy.reference.wont_equal deploy.job.commit
deploy.reload # so sql count assertions are meaningful
end

it "references a matching reference" do
assert deploy.references?(deploy.reference)
end

it "references a matching commit" do
assert deploy.references?(deploy.job.commit)
end

it "does not reference a non-matching reference" do
assert_sql_queries 0 do
refute deploy.references?("foo")
end
end

it "does not reference a non-matching commit" do
assert_sql_queries 1 do
refute deploy.references?("a" * 40)
end
end
end

describe "#validate_stage_is_unlocked" do
def deploy
create_deploy!(job_attributes: { user: user })
Expand Down Expand Up @@ -499,7 +526,8 @@ def deploy

describe "#cache_key" do
it "includes self and commit" do
deploys(:succeeded_test).cache_key.must_equal ["deploys/178003093-20140101201000000000", "abcabc1"]
deploys(:succeeded_test).cache_key.
must_equal ["deploys/178003093-20140101201000000000", "abcabcaaabcabcaaabcabcaaabcabcaaabcabca1"]
end
end

Expand Down
Loading

0 comments on commit 9bb6435

Please sign in to comment.