Skip to content

Commit

Permalink
Merge pull request #1265 from alphagov/include-deploys-from-non-defau…
Browse files Browse the repository at this point in the history
…lt-branch

Include non default branch deploys in commit log
  • Loading branch information
deborahchua authored Aug 30, 2023
2 parents d85e230 + 6abddd3 commit 8a5ee94
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/models/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def commits
Services.github.commits(repo)
end

def latest_commit(application, commit_sha)
Services.github.commit(application.repo, commit_sha)
end

def tag_names_by_commit
tags = Services.github.tags(repo)

Expand Down
34 changes: 33 additions & 1 deletion app/views/applications/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@
<% end %>
<%= t.body do %>
<% latest_deploy_on_default_branch = [] %>
<% @commits.each do |commit| %>
<%= t.row do %>
<% tags = @tag_names_by_commit.fetch(commit[:sha], []) %>
<% commit_deployments = capture do %>
<% @application.latest_deploy_to_each_environment.each do |_, deployment| %>
<% @application.latest_deploy_to_each_environment.each do |environment, deployment| %>
<% if tags.include?(deployment.version) || deployment.commit_match?(commit[:sha]) %>
<% latest_deploy_on_default_branch << environment %>
<p class="govuk-body-xs govuk-!-margin-bottom-1 release__commits-message">
<span class="release__commits-label release__commits-label--<%= 'production' if deployment.to_live_environment? %>"><%= deployment.environment.humanize %></span>
<span>at <%= time_tag(deployment.created_at, human_datetime(deployment.created_at)) %></span>
Expand Down Expand Up @@ -67,6 +69,36 @@
<%= t.cell link_to(commit[:sha][0..8], "#{@application.repo_url}/commit/#{commit[:sha]}", target: "_blank", class: "release__commit-hash govuk-link govuk-body-s") %>
<% end %>
<% end %>
<% @application.latest_deploy_to_each_environment.each do |environment, deployment| %>
<% if !latest_deploy_on_default_branch.include?(environment) %>
<tr class="govuk-table__row">
<td class="govuk-table__cell">
<p class="govuk-body-xs govuk-!-margin-bottom-1 release__commits-message">
<span class="release__commits-label release__commits-label--<%= 'production' if deployment.to_live_environment? %>"><%= environment.humanize %></span>
<span>at <%= time_tag(deployment.created_at, human_datetime(deployment.created_at)) %></span>
</p>
<span class="release__badge release__badge--orange">Not on default branch</span>
</td>
<td class="govuk-table__cell"/>
<td class="govuk-table__cell">
<p class="govuk-body-s govuk-!-margin-bottom-0">
<% latest_deployed_commit = @application.latest_commit(@application, deployment.version) %>
<%= latest_deployed_commit[:commit][:message] %>
<% if latest_deployed_commit[:commit][:author] %>
<span class="release__commits-author">
<%= latest_deployed_commit[:commit][:author][:name] %>
</span>
<% end %>
</p>
</td>
<td class="govuk-table__cell">
<p class="govuk-body-s govuk-!-margin-bottom-1">
<%= link_to(deployment.version[0..8], "#{@application.repo_url}/commit/#{deployment.version}", target: "_blank", class: "release__commit-hash govuk-link govuk-body-s") %>
</p>
</td>
</tr>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
Expand Down
39 changes: 38 additions & 1 deletion test/functional/applications_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,50 @@ class ApplicationsControllerTest < ActionController::TestCase
assert_select ".gem-c-notice", "Do not deploy this without talking to core team first!"
end

context "with manual deployment" do
setup do
version = "release_42"
@first_commit = stub_commit
@base_commit = stub_commit
@deployed_sha = @first_commit[:sha]
@manual_deploy = SecureRandom.hex(40)
@latest_commit = { sha: @manual_deploy, commit: { author: { name: "Winston Churchill" }, message: "We shall fight on the beaches" } }

FactoryBot.create(:deployment, application: @app, environment: "production EKS", version:, deployed_sha: @deployed_sha)
FactoryBot.create(:deployment, application: @app, environment: "staging EKS", version:, deployed_sha: @deployed_sha)
FactoryBot.create(:deployment, application: @app, environment: "integration EKS", version: @manual_deploy)

stub_request(:get, "https://api.github.com/repos/#{@app.repo}/commits/#{@manual_deploy}").to_return(headers: { "Content-Type" => "application/json" }, body: JSON.generate(@latest_commit))

Octokit::Client.any_instance.stubs(:compare)
.with(@app.repo, version, @app.default_branch)
.returns(stub(
"comparison",
commits: [@first_commit],
base_commit: @base_commit,
))
end

should "show the manual deployment commit" do
get :show, params: { id: @app.id }
assert_select ".release__commits-label", { text: "Integration eks", count: 1 }
assert_select "p", text: @latest_commit[:message]
assert_select ".release__commit-hash", { text: @manual_deploy.first(9), count: 1 }
end

should "show 'not on default branch' status" do
get :show, params: { id: @app.id }
assert_select ".release__badge--orange", { text: "Not on default branch", count: 1 }
end
end

context "GET show with a production deployment" do
setup do
version = "release_42"
FactoryBot.create(:deployment, application: @app, version:)
@first_commit = stub_commit
@second_commit = stub_commit
@base_commit = stub_commit
FactoryBot.create(:deployment, application: @app, version:, deployed_sha: @first_commit[:sha])
Octokit::Client.any_instance.stubs(:compare)
.with(@app.repo, version, @app.default_branch)
.returns(stub(
Expand Down

0 comments on commit 8a5ee94

Please sign in to comment.