Skip to content

Commit

Permalink
Merge pull request #1401 from alphagov/get-duplicate-fields-from-dev-…
Browse files Browse the repository at this point in the history
…docs

Get duplicate fields from the Developer Docs
  • Loading branch information
deborahchua authored Feb 16, 2024
2 parents 88ed365 + 6aa7f79 commit f7ef692
Show file tree
Hide file tree
Showing 26 changed files with 238 additions and 230 deletions.
4 changes: 1 addition & 3 deletions app/controllers/applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def deploy

if @production_deploy
comparison = Services.github.compare(
@application.repo,
@application.repo_path,
@production_deploy.version,
@release_tag,
)
Expand Down Expand Up @@ -113,9 +113,7 @@ def application_params
:archived,
:id,
:name,
:repo,
:default_branch,
:shortname,
:status_notes,
:task,
:deploy_freeze,
Expand Down
23 changes: 3 additions & 20 deletions app/controllers/deployments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def create
private

def application_by_repo
existing_apps = Application.where(repo: repo_path)
existing_apps = Application.where(name: repo_path)

case existing_apps.length
when 0
Application.create!(name: normalize_app_name(repo_path), repo: repo_path)
Application.create!(name: normalize_app_name(repo_path))
when 1
existing_apps[0]
else
Expand All @@ -48,24 +48,14 @@ def application_by_repo
end

def repo_path
if params[:repo].start_with?("http")
URI.parse(params[:repo]).path.gsub(%r{^/}, "")
elsif params[:repo].start_with?("git@")
params[:repo].split(":")[-1].gsub(/.git$/, "")
else
params[:repo]
end
params[:repo].split("/")[-1]
end

def normalize_app_name(unnormalized_app_name)
normalized_app_name = unnormalized_app_name.split("/")[-1].tr("-", " ").humanize.titlecase
normalized_app_name.gsub(/\bApi\b/, "API")
end

def push_notification?
params[:repo].present?
end

def deployment_params
params.require(:deployment).permit(
:application,
Expand All @@ -81,13 +71,6 @@ def deployment_params
)
end

def new_deployment_params
params.permit(
:application_id,
:environment,
)
end

def recent_deployment_params
params.permit(
:environment_filter,
Expand Down
12 changes: 8 additions & 4 deletions app/helpers/url_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ def smokey_url(environment)
"https://deploy.#{suffix}/job/Smokey"
end

def github_repo_url(app)
Repo.url(app_name: app.name).nil? ? "https://github.com/alphagov/#{app.name.parameterize}" : Repo.url(app_name: app.name)
end

def github_dependency_link_to(app, text)
link_to(text, "#{app.repo_url}/pulls?q=is%3Apr+state%3Aopen+label%3Adependencies", target: "_blank", rel: "noopener", class: "govuk-link")
link_to(text, "#{github_repo_url(app)}/pulls?q=is%3Apr+state%3Aopen+label%3Adependencies", target: "_blank", rel: "noopener", class: "govuk-link")
end

def github_tag_link_to(app, git_ref)
link_to(git_ref.truncate(15), "#{app.repo_url}/tree/#{git_ref}", target: "_blank", rel: "noopener", class: "govuk-link")
link_to(git_ref.truncate(15), "#{github_repo_url(app)}/tree/#{git_ref}", target: "_blank", rel: "noopener", class: "govuk-link")
end

def github_compare_to_default(application, deploy)
"#{application.repo_url}/compare/#{deploy.version}...#{application.default_branch}"
def github_compare_to_default(app, deploy)
"#{github_repo_url(app)}/compare/#{deploy.version}...#{app.default_branch}"
end

def govuk_domain_suffix(environment)
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/post_out_of_sync_deploys_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def app_list(apps)
end

def app_info(app)
"- <#{Plek.external_url_for('release')}/applications/#{app.shortname}|#{app.name}> – #{app.status.to_s.humanize} (<https://github.com/#{app.repo}/actions/workflows/deploy.yml|Deploy GitHub action>)"
"- <#{Plek.external_url_for('release')}/applications/#{app.shortname}|#{app.name}> – #{app.status.to_s.humanize} (<https://github.com/#{app.repo_path}/actions/workflows/deploy.yml|Deploy GitHub action>)"
end
end
25 changes: 13 additions & 12 deletions app/models/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ class Application < ApplicationRecord
friendly_id :fallback_shortname, use: :slugged, slug_column: :shortname

validates :name, presence: { message: "is required" }
validates :repo, presence: { message: "is required" }

validates :name, :repo, :status_notes, :shortname, length: { maximum: 255 }

validates :repo, format: { with: /\A[^\s\/]+\/[^\s\/]+\Z/i }, allow_blank: true
validates :name, :status_notes, length: { maximum: 255 }

validates :name, uniqueness: { case_sensitive: true }

Expand Down Expand Up @@ -48,15 +45,19 @@ def status
end

def fallback_shortname
repo.split("/")[-1] unless repo.nil?
Repo.shortname(app_name: name).nil? ? name.parameterize : Repo.shortname(app_name: name)
end

def repo_path
"alphagov/#{name.parameterize}"
end

def repo_url
"https://github.com/#{repo}"
Repo.url(app_name: name).nil? ? "https://github.com/#{repo_path}" : Repo.url(app_name: name)
end

def repo_compare_url(from, to)
"https://github.com/#{repo}/compare/#{from}...#{to}"
"#{repo_url}/compare/#{from}...#{to}"
end

def self.cd_statuses
Expand All @@ -79,19 +80,19 @@ def deployed_to_ec2?
end

def dependency_pull_requests
Services.github.search_issues("repo:#{repo} is:pr state:open label:dependencies")
Services.github.search_issues("repo:#{repo_path} is:pr state:open label:dependencies")
end

def commits
Services.github.commits(repo)
Services.github.commits(repo_path)
end

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

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

tags.each_with_object({}) do |tag, hash|
sha = tag[:commit][:sha]
Expand All @@ -104,7 +105,7 @@ def undeployed_commits
production_deployment = deployments.last_deploy_to(live_environment)

comparison = Services.github.compare(
repo,
repo_path,
production_deployment.version,
default_branch,
)
Expand Down
2 changes: 1 addition & 1 deletion app/models/commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(commit_data, application)
end

def github_url
"#{application.repo_url}/commit/#{commit_data[:sha]}"
"#{Repo.url(app_name: application.name)}/commit/#{commit_data[:sha]}"
end

def sha
Expand Down
2 changes: 1 addition & 1 deletion app/models/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def previous_version
def commits
@commits ||=
begin
Services.github.compare(application.repo, previous_version, version).commits.reverse.map do |commit|
Services.github.compare(application.repo_path, previous_version, version).commits.reverse.map do |commit|
Commit.new(commit.to_h, application)
end
rescue Octokit::NotFound
Expand Down
20 changes: 15 additions & 5 deletions app/models/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ class Repo
base_uri "docs.publishing.service.gov.uk"

def self.all
response ||= get("/apps.json")
JSON.parse(response.body)
rescue HTTParty::Error, JSON::ParserError => e
Rails.logger.debug "Error fetching govuk repos: #{e.message}"
[]
Rails.cache.fetch("apps_json_data", expires_in: 12.hours) do
response ||= get("/apps.json")
JSON.parse(response.body)
rescue HTTParty::Error, JSON::ParserError => e
Rails.logger.debug "Error fetching govuk repos: #{e.message}"
[]
end
end

def self.find_by(app_name:)
all.find { |app| app["app_name"] == app_name.parameterize }
end

def self.url(app_name:)
find_by(app_name:)&.dig("links", "repo_url")
end

def self.shortname(app_name:)
find_by(app_name:)&.dig("shortname")
end
end
22 changes: 1 addition & 21 deletions app/views/applications/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@
error_message: @application.errors[:name].first,
} %>

<%= render "govuk_publishing_components/components/input", {
label: {
text: "GitHub repository path"
},
name: "application[repo]",
hint: "Example: alphagov/publisher",
value: @application.repo,
error_message: @application.errors[:repo].first,
} %>

<%= render "govuk_publishing_components/components/select", {
id: "default_branch",
label: "GitHub repository default branch",
Expand All @@ -32,16 +22,6 @@
end
} %>

<%= render "govuk_publishing_components/components/input", {
label: {
text: "Short name"
},
name: "application[shortname]",
hint: "For use in graphite metrics. Example: whitehall",
value: @application.shortname,
error_message: @application.errors[:shortname].first,
} %>

<%= render "govuk_publishing_components/components/textarea", {
label: {
text: "Status notes"
Expand Down Expand Up @@ -72,7 +52,7 @@
<span class="govuk-warning-text__icon" aria-hidden="true">!</span>
<strong class="govuk-warning-text__text">
<span class="govuk-warning-text__assistive">Warning</span>
Continuous deployment between each environment has to be disabled or enabled via <%= link_to "GitHub action", "https://github.com/#{@application.repo}/actions/workflows/set-automatic-deploys.yml", class: "govuk-link" %>.
Continuous deployment between each environment has to be disabled or enabled via <%= link_to "GitHub action", "#{@application.repo_url}/actions/workflows/set-automatic-deploys.yml", class: "govuk-link" %>.
</strong>
</div>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/applications/deploy.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<%= render "govuk_publishing_components/components/button", {
text: "Deploy this release",
href: "https://github.com/#{@application.repo}/actions/workflows/deploy.yml",
href: "#{@application.repo_url}/actions/workflows/deploy.yml",
target: "_blank",
margin_bottom: true
} %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class RemoveRepoColumnFromApplication < ActiveRecord::Migration[7.1]
def change
change_table :applications, bulk: true do |t|
t.remove_index :repo
t.remove :repo, type: :string
end
end
end
4 changes: 1 addition & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2022_09_23_151609) do
ActiveRecord::Schema[7.1].define(version: 2024_02_08_111830) do
create_table "applications", id: :integer, charset: "latin1", force: :cascade do |t|
t.string "name"
t.string "repo"
t.datetime "created_at", precision: nil, null: false
t.datetime "updated_at", precision: nil, null: false
t.string "status_notes"
Expand All @@ -22,7 +21,6 @@
t.boolean "deploy_freeze", default: false, null: false
t.string "default_branch", default: "main", null: false
t.index ["name"], name: "index_applications_on_name", unique: true
t.index ["repo"], name: "index_applications_on_repo"
t.index ["shortname"], name: "index_applications_on_shortname"
end

Expand Down
28 changes: 14 additions & 14 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
example_note = "Here is a note that will hopefully demonstrate how somebody might use the notes field to write a long note."

applications = [
{ name: "Calendars", repo: "alphagov/calendars" },
{ name: "Content store", repo: "alphagov/content-store" },
{ name: "Feedback", repo: "alphagov/feedback" },
{ name: "Frontend", repo: "alphagov/frontend", status_notes: example_note },
{ name: "Imminence", repo: "alphagov/imminence" },
{ name: "Licensify", repo: "alphagov/licensify" },
{ name: "Publisher", repo: "alphagov/publisher" },
{ name: "Rummager", repo: "alphagov/rummager" },
{ name: "Signon", repo: "alphagov/signon" },
{ name: "Smart answers", repo: "alphagov/smart-answers", shortname: "smartanswers" },
{ name: "Static", repo: "alphagov/static" },
{ name: "Support", repo: "alphagov/support" },
{ name: "Whitehall", repo: "alphagov/whitehall" },
{ name: "Data insight non-govuk reach collector", repo: "alphagov/datainsight-nongovuk-reach-collector", archived: true },
{ name: "Calendars" },
{ name: "Content store" },
{ name: "Feedback" },
{ name: "Frontend", status_notes: example_note },
{ name: "Imminence" },
{ name: "Licensify" },
{ name: "Publisher" },
{ name: "Rummager" },
{ name: "Signon" },
{ name: "Smart answers", shortname: "smartanswers" },
{ name: "Static" },
{ name: "Support" },
{ name: "Whitehall" },
{ name: "Data insight non-govuk reach collector", archived: true },
]

applications.each do |application_hash|
Expand Down
1 change: 0 additions & 1 deletion test/factories/factories.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FactoryBot.define do
factory :application do
sequence(:name) { |n| "Application #{n}" }
sequence(:repo) { |n| "alphagov/application-#{n}" }
status_notes { "" }
end

Expand Down
Loading

0 comments on commit f7ef692

Please sign in to comment.