Skip to content

Commit

Permalink
Rename app_name to repo_name for clarity
Browse files Browse the repository at this point in the history
We do have distinct concepts of `app_name` vs `repo_name`; often
they are the same thing, but can be overridden in config, e.g.
https://github.com/alphagov/govuk-developer-docs/blob/91185cc86710ccf39f170f3743a2e3f9b5c12f7d/data/applications.yml#L341-L342

GitHubRepoFetcher does indeed rely on the repo name rather than
the app name, so we should make that clearer in the code.
  • Loading branch information
ChrisBAshton committed Jan 15, 2021
1 parent 751f899 commit 2a7db13
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def from_application_page
end

def repos
data["repos"].to_a.map do |app_name|
repo = GitHubRepoFetcher.instance.repo(app_name)
data["repos"].to_a.map do |repo_name|
repo = GitHubRepoFetcher.instance.repo(repo_name)
Repo.new(repo)
end
end
Expand Down
30 changes: 15 additions & 15 deletions app/github_repo_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@ class GitHubRepoFetcher
include Singleton

# Fetch a repo from GitHub
def repo(app_name)
all_alphagov_repos.find { |repo| repo.name == app_name } || raise("alphagov/#{app_name} not found")
def repo(repo_name)
all_alphagov_repos.find { |repo| repo.name == repo_name } || raise("alphagov/#{repo_name} not found")
end

# Fetch a README for an alphagov application and cache it.
# Note that it is cached as pure markdown and requires further processing.
def readme(app_name)
return nil if repo(app_name).private_repo?
def readme(repo_name)
return nil if repo(repo_name).private_repo?

CACHE.fetch("alphagov/#{app_name} README", expires_in: 1.hour) do
default_branch = repo(app_name).default_branch
HTTP.get("https://raw.githubusercontent.com/alphagov/#{app_name}/#{default_branch}/README.md")
CACHE.fetch("alphagov/#{repo_name} README", expires_in: 1.hour) do
default_branch = repo(repo_name).default_branch
HTTP.get("https://raw.githubusercontent.com/alphagov/#{repo_name}/#{default_branch}/README.md")
rescue Octokit::NotFound
nil
end
end

# Fetch all markdown files under the repo's 'docs' folder
def docs(app_name)
return nil if repo(app_name).private_repo?
def docs(repo_name)
return nil if repo(repo_name).private_repo?

CACHE.fetch("alphagov/#{app_name} docs", expires_in: 1.hour) do
docs = client.contents("alphagov/#{app_name}", path: "docs")
CACHE.fetch("alphagov/#{repo_name} docs", expires_in: 1.hour) do
docs = client.contents("alphagov/#{repo_name}", path: "docs")
docs.select { |doc| doc.name.end_with?(".md") }.map do |doc|
contents = HTTP.get(doc.download_url)
filename = doc.name.match(/(.+)\..+$/)[1]
title = ExternalDoc.title(contents) || filename
{
path: "/apps/#{app_name}/#{filename}.html",
path: "/apps/#{repo_name}/#{filename}.html",
title: title.to_s.force_encoding("UTF-8"),
markdown: contents.to_s.force_encoding("UTF-8"),
relative_path: doc.path,
source_url: doc.html_url,
latest_commit: latest_commit(app_name, doc.path),
latest_commit: latest_commit(repo_name, doc.path),
}
end
rescue Octokit::NotFound
Expand All @@ -53,8 +53,8 @@ def all_alphagov_repos
end
end

def latest_commit(app_name, path)
latest_commit = client.commits("alphagov/#{app_name}", repo(app_name).default_branch, path: path).first
def latest_commit(repo_name, path)
latest_commit = client.commits("alphagov/#{repo_name}", repo(repo_name).default_branch, path: path).first
{
sha: latest_commit.sha,
timestamp: latest_commit.commit.author.date,
Expand Down

0 comments on commit 2a7db13

Please sign in to comment.