From 6abddd3905e811d5ac3b54b73127a9b95b18f767 Mon Sep 17 00:00:00 2001 From: D Chua Date: Thu, 17 Aug 2023 15:24:10 +0100 Subject: [PATCH] Include non default branch deploys in commit log The commit log on an application summary page only lists commits for the default branch (main) and does not include manual deploys from other branches. Whilst this is included in the "What's where" section at the bottom of the page, it's helpful to also include them in the commit log section where it is easier to see if an environment is currently out of sync with `main`. If any environments have not yet been rendered to the table with its latest deployed commit, we get the last deployed commit for those environments and add them as final entries to the commit log table. --- app/models/application.rb | 4 ++ app/views/applications/show.html.erb | 34 +++++++++++++++- .../applications_controller_test.rb | 39 ++++++++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/app/models/application.rb b/app/models/application.rb index a8a9cfabf..7d40720c8 100644 --- a/app/models/application.rb +++ b/app/models/application.rb @@ -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) diff --git a/app/views/applications/show.html.erb b/app/views/applications/show.html.erb index 97462fd90..02c6df757 100644 --- a/app/views/applications/show.html.erb +++ b/app/views/applications/show.html.erb @@ -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 %>

<%= deployment.environment.humanize %> at <%= time_tag(deployment.created_at, human_datetime(deployment.created_at)) %> @@ -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) %> + + +

+ <%= environment.humanize %> + at <%= time_tag(deployment.created_at, human_datetime(deployment.created_at)) %> +

+ Not on default branch + + + +

+ <% latest_deployed_commit = @application.latest_commit(@application, deployment.version) %> + <%= latest_deployed_commit[:commit][:message] %> + <% if latest_deployed_commit[:commit][:author] %> + + <%= latest_deployed_commit[:commit][:author][:name] %> + + <% end %> +

+ + +

+ <%= link_to(deployment.version[0..8], "#{@application.repo_url}/commit/#{deployment.version}", target: "_blank", class: "release__commit-hash govuk-link govuk-body-s") %> +

+ + + <% end %> + <% end %> <% end %> <% end %> diff --git a/test/functional/applications_controller_test.rb b/test/functional/applications_controller_test.rb index 0f16663b4..ebab06185 100644 --- a/test/functional/applications_controller_test.rb +++ b/test/functional/applications_controller_test.rb @@ -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(