# frozen_string_literal: true require "yaml" require "json" module Groundskeeper # Accesses project details stored in the home directory. class Project attr_reader :details, :servers, :repo_name DETAILS_PATH = "~/.project_details/projects.yml" SERVERS_PATH = "../../servers.yml" JIRA_PREFIX_KEY = "jira_prefix" PROJECT_NAME_KEY = "name" SOURCE_CONTROL_USERNAME_KEY = "scm_username" FULL_DNS_KEY = "full_dns" RVM_RUBY_VERSION_KEY = "ruby_version" DB_ENV_VARIABLES_KEY = "db_env_variables" SENTRY_DSN_KEY = "sentry_dsn" SENTRY_PUBLIC_DSN_KEY = "sentry_public_dsn" SSL_CERTIFICATE_FILE_KEY = "SSLCertificateFile" SSL_CERTIFICATE_CHAIN_FILE_KEY = "SSLCertificateChainFile" SSL_CERTIFICATE_KEY_FILE_KEY = "SSLCertificateKeyFile" WHENEVER_KEY = "uses_whenever" def self.build(repository_name) new( projects_yaml: Document.new(DETAILS_PATH).read, servers_yaml: Document.new(SERVERS_PATH).read, repository_name: repository_name ) end def self.available_applications yaml = Document.new(DETAILS_PATH).read projects = YAML.safe_load(yaml) || {} projects.keys.join(", ") end def initialize(projects_yaml:, servers_yaml:, repository_name:) projects = YAML.safe_load(projects_yaml) || {} @details = projects[repository_name] || {} @servers = YAML.safe_load(servers_yaml) || {} @repo_name = repository_name end def project_name details[PROJECT_NAME_KEY] || "" end def jira_prefix details[JIRA_PREFIX_KEY] || "" end def source_control_username details[SOURCE_CONTROL_USERNAME_KEY] || "" end def full_dns(stage) details.dig(FULL_DNS_KEY, stage.to_s) || "" end def db_env_variables(stage) details.dig(DB_ENV_VARIABLES_KEY, stage.to_s) || "" end def rvm_ruby_version details[RVM_RUBY_VERSION_KEY] || "" end def sentry_dsn details[SENTRY_DSN_KEY] || "" end def sentry_public_dsn details[SENTRY_PUBLIC_DSN_KEY] || "" end def ssl_certificate_file(stage) details.dig(SSL_CERTIFICATE_FILE_KEY, stage.to_s) || "" end def ssl_certificate_chain_file(stage) details.dig(SSL_CERTIFICATE_CHAIN_FILE_KEY, stage.to_s) || "" end def ssl_certificate_key_file(stage) details.dig(SSL_CERTIFICATE_KEY_FILE_KEY, stage.to_s) || "" end def uses_whenever? details[WHENEVER_KEY] == true end def server_config(stage) servers[full_dns(stage)] end def repo_url "git@github.com:#{source_control_username}/#{repo_name}.git" end def deploy_to "/var/www/apps/#{repo_name}" end def tags api_url = "https://api.github.com/repos/" \ "#{source_control_username}/#{repo_name}/tags" authorization_header = "Authorization: token #{ENV['GITHUB_API_TOKEN']}" response = JSON.parse(`curl -s -H "#{authorization_header}" #{api_url}`) unless response.is_a?(Array) message = response["message"] puts "Failed to obtain response: #{message}" return [] end response.map { |h| h["name"] } end end end