Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor app doc classes & build task #2918

Merged
merged 15 commits into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ To run the app locally:

The app will appear at [http://localhost:4567/](http://localhost:4567/)

You may need a GitHub auth token if you find yourself rate limited. You can create one here:
### GitHub token

You will need a GitHub auth token to build the project or run the full test suite,
otherwise you will find yourself rate limited. You can create one here:

https://github.com/settings/tokens/new

Expand All @@ -53,6 +56,12 @@ export GITHUB_TOKEN=somethingsomething
./startup.sh
```

You may find it easier to save the token to a file and then refer to it dynamically:

```sh
GITHUB_TOKEN=$(cat ~/github_token.txt) ./startup.sh
```

### Building the project

Build the site with:
Expand Down
27 changes: 21 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ end
namespace :assets do
desc "Build the static site"
task :precompile do
sh "git clone https://github.com/alphagov/govuk-content-schemas.git /tmp/govuk-content-schemas --depth=1 && NO_CONTRACTS=true GOVUK_CONTENT_SCHEMAS_PATH=/tmp/govuk-content-schemas middleman build"
sh "rm -rf /tmp/govuk-content-schemas; git clone https://github.com/alphagov/govuk-content-schemas.git /tmp/govuk-content-schemas --depth=1 && NO_CONTRACTS=true GOVUK_CONTENT_SCHEMAS_PATH=/tmp/govuk-content-schemas middleman build"
end
end

desc "Find deployable applications that are not in this repo"
task :verify_deployable_apps do
common_yaml = HTTP.get_yaml("https://raw.githubusercontent.com/alphagov/govuk-puppet/master/hieradata/common.yaml")
common_yaml = suppress_output do
HTTP.get_yaml("https://raw.githubusercontent.com/alphagov/govuk-puppet/master/hieradata/common.yaml")
end
deployable_applications = common_yaml["deployable_applications"].map { |k, v| v["repository"] || k }
our_applications = AppDocs.pages.map(&:github_repo_name)
our_applications = Applications.all.map(&:github_repo_name)

intentionally_missing =
%w[
Expand Down Expand Up @@ -64,9 +66,10 @@ end

desc "Check all puppet names are valid, will error when not"
task :check_puppet_names do
puts "Checking Puppet manifests..."
invalid_puppet_names = []
AppDocs.pages.reject(&:retired?).each do |app|
HTTP.get(app.puppet_url) unless app.puppet_url.nil?
Applications.active.each do |app|
suppress_output { HTTP.get(app.puppet_url) unless app.puppet_url.nil? }
rescue Octokit::NotFound
invalid_puppet_names << app.puppet_url
end
Expand All @@ -81,4 +84,16 @@ end
desc "Clear out and rebuild the build/ folder"
task build: %i[cache:clear assets:precompile]

task default: %i[verify_deployable_apps check_puppet_names lint lint_markdown spec build]
task default: %i[verify_deployable_apps lint lint_markdown spec check_puppet_names build]

# https://gist.github.com/moertel/11091573
def suppress_output
original_stderr = $stderr.clone
original_stdout = $stdout.clone
$stderr.reopen(File.new("/dev/null", "w"))
$stdout.reopen(File.new("/dev/null", "w"))
yield
ensure
$stdout.reopen(original_stdout)
$stderr.reopen(original_stderr)
end
226 changes: 226 additions & 0 deletions app/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
class App
attr_reader :app_data

def initialize(app_data)
@app_data = app_data
end

def api_payload
{
app_name: app_name,
team: team,
dependencies_team: dependencies_team,
puppet_name: puppet_name,
production_hosted_on: production_hosted_on,
links: {
self: "https://docs.publishing.service.gov.uk/apps/#{app_name}.json",
html_url: html_url,
repo_url: repo_url,
sentry_url: sentry_url,
},
}
end

def aws_puppet_class
Applications.aws_machines.each do |puppet_class, keys|
if keys["apps"].include?(app_name) || keys["apps"].include?(puppet_name)
return puppet_class
end
end
"Unknown - have you configured and merged your app in govuk-puppet/hieradata_aws/common.yaml"
end

def carrenza_machine
Applications.carrenza_machines.each do |puppet_class, keys|
if keys["apps"].include?(app_name) || keys["apps"].include?(puppet_name)
return puppet_class
end
end
"Unknown - have you configured and merged your app in govuk-puppet/hieradata/common.yaml"
end

def production_hosted_on_aws?
production_hosted_on == "aws"
end

def machine_class
app_data["machine_class"] || (production_hosted_on_aws? ? aws_puppet_class : carrenza_machine)
end

def production_hosted_on
app_data["production_hosted_on"]
end

def hosting_name
Applications::HOSTERS.fetch(production_hosted_on)
end

def html_url
"https://docs.publishing.service.gov.uk/apps/#{app_name}.html"
end

def retired?
app_data["retired"]
end

def private_repo?
app_data["private_repo"]
end

def page_title
if retired?
"Application: #{app_name} (retired)"
else
"Application: #{app_name}"
end
end

def app_name
app_data["app_name"] || github_repo_name
end

def example_published_pages
Applications.app_data.publishing_examples[app_name]
end

def example_rendered_pages
Applications.app_data.rendering_examples[app_name]
end

def github_repo_name
app_data.fetch("github_repo_name")
end

def management_url
app_data["management_url"]
end

def repo_url
app_data["repo_url"] || "https://github.com/alphagov/#{github_repo_name}"
end

def sentry_url
if app_data["sentry_url"] == false
nil
elsif app_data["sentry_url"]
app_data["sentry_url"]
else
"https://sentry.io/govuk/app-#{app_name}"
end
end

def puppet_url
return unless production_hosted_on.in?(%w[aws carrenza])

return app_data["puppet_url"] if app_data["puppet_url"]

"https://github.com/alphagov/govuk-puppet/blob/master/modules/govuk/manifests/apps/#{puppet_name}.pp"
end

def deploy_url
return if app_data["deploy_url"] == false || production_hosted_on == "heroku"

if production_hosted_on == "paas"
app_data["deploy_url"]
else
"https://github.com/alphagov/govuk-app-deployment/blob/master/#{github_repo_name}/config/deploy.rb"
end
end

def dashboard_url
return if app_data["dashboard_url"] == false

app_data["dashboard_url"] || (
if production_hosted_on_aws?
"https://grafana.production.govuk.digital/dashboard/file/#{app_name}.json"
else
"https://grafana.publishing.service.gov.uk/dashboard/file/#{app_name}.json"
end
)
end

def publishing_e2e_tests_url
if ["Frontend apps", "Publishing apps"].include?(type)
"https://github.com/alphagov/publishing-e2e-tests/search?q=%22#{app_name.underscore}%3A+true%22+path%3A%2Fspec%2F"
end
end

def api_docs_url
app_data["api_docs_url"]
end

def component_guide_url
app_data["component_guide_url"]
end

def metrics_dashboard_url
app_data["metrics_dashboard_url"]
end

def type
app_data.fetch("type")
end

def team
app_data["team"]
end

def dependencies_team
app_data
.fetch("dependencies_team", team)
end

def description
app_data["description"] || description_from_github
end

def production_url
app_data["production_url"] || (type.in?(["Publishing app", "Admin app"]) ? "https://#{app_name}.publishing.service.gov.uk" : nil)
end

def readme
github_readme
end

def can_run_rake_tasks_in_jenkins?
production_hosted_on.in?(%w[aws carrenza])
end

def rake_task_url(environment, rake_task = "")
query_params = "?TARGET_APPLICATION=#{app_name}&MACHINE_CLASS=#{machine_class}&RAKE_TASK=#{rake_task}"

case production_hosted_on
when "aws"
if environment == "integration"
"https://deploy.#{environment}.publishing.service.gov.uk/job/run-rake-task/parambuild/#{query_params}"
else
"https://deploy.blue.#{environment}.govuk.digital/job/run-rake-task/parambuild/#{query_params}"
end
when "carrenza"
environment_prefix = environment == "production" ? "" : ".#{environment}"
"https://deploy#{environment_prefix}.publishing.service.gov.uk/job/run-rake-task/parambuild/#{query_params}"
end
end

private

def puppet_name
app_data["puppet_name"] || app_name.underscore
end

def description_from_github
github_repo_data["description"]
end

def github_repo_data
return {} if private_repo?

@github_repo_data ||= GitHubRepoFetcher.instance.repo(github_repo_name)
end

def github_readme
return nil if private_repo?

@github_readme ||= GitHubRepoFetcher.instance.readme(github_repo_name)
end
end
Loading