diff options
author | Brian Williams <bwilliams@gitlab.com> | 2022-06-23 15:47:22 +0300 |
---|---|---|
committer | Achilleas Pipinellis <axil@gitlab.com> | 2022-06-23 18:34:08 +0300 |
commit | 1422aaf82e1b9eb4175eb6e2d368578677853b06 (patch) | |
tree | 54ba8182b9deeb7af6faae743b19feb8ac4c79a0 | |
parent | e1664f8a4c7306351bac017c08832d78f7ef62bc (diff) |
Refactor edit_on_gitlab helper
Rather than having a bunch of case statements with URLs, this changes
the `edit_on_gitlab` helper so that it builds urls from a project path,
a branch name, and a docs file path. This is easier to maintain since
it's a data-driven approach. The PRODUCT_REPOS data can be edited
without changing the program logic.
-rw-r--r-- | lib/helpers/edit_on_gitlab.rb | 99 | ||||
-rw-r--r-- | spec/helpers/edit_on_gitlab_spec.rb | 12 |
2 files changed, 68 insertions, 43 deletions
diff --git a/lib/helpers/edit_on_gitlab.rb b/lib/helpers/edit_on_gitlab.rb index 47d94d26..cbaac803 100644 --- a/lib/helpers/edit_on_gitlab.rb +++ b/lib/helpers/edit_on_gitlab.rb @@ -2,51 +2,76 @@ module Nanoc::Helpers module EditOnGitLab + PRODUCT_REPOS = { + "omnibus" => { + project: "gitlab-org/omnibus-gitlab", + default_branch_name: "master", + doc_directory: "doc" + }, + "runner" => { + project: "gitlab-org/gitlab-runner", + default_branch_name: "main", + doc_directory: "docs" + }, + "charts" => { + project: "gitlab-org/charts/gitlab", + default_branch_name: "master", + doc_directory: "doc" + }, + "operator" => { + project: "gitlab-org/cloud-native/gitlab-operator", + default_branch_name: "master", + doc_directory: "doc" + }, + "ee" => { + project: "gitlab-org/gitlab", + default_branch_name: "master", + doc_directory: "doc" + } + }.freeze + def edit_on_gitlab(item, editor: :simple) - # Make an array out of the content's source path. - content_filename_array = item.identifier.to_s.split('/') - # remove first empty item - content_filename_array.shift - # Get the product name. - product = content_filename_array.shift - # This should be the path from the doc/ directory for a given file. - docs_content_filename = content_filename_array.join("/") - - case product - when "omnibus" - # omnibus-gitlab repo - gitlab_url = "https://gitlab.com/gitlab-org/#{product}-gitlab/blob/master/doc/#{docs_content_filename}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/#{product}-gitlab/edit/master/-/doc/#{docs_content_filename}" - when "runner" - # gitlab-runner repo - gitlab_url = "https://gitlab.com/gitlab-org/gitlab-#{product}/blob/main/docs/#{docs_content_filename}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/gitlab-#{product}/edit/main/-/docs/#{docs_content_filename}" - when "charts" - # GitLab Helm chart repo - gitlab_url = "https://gitlab.com/gitlab-org/#{product}/gitlab/blob/master/doc/#{docs_content_filename}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/#{product}/gitlab/edit/master/-/doc/#{docs_content_filename}" - when "operator" - # GitLab Operator repo - gitlab_url = "https://gitlab.com/gitlab-org/cloud-native/gitlab-#{product}/blob/master/doc/#{docs_content_filename}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/cloud-native/gitlab-#{product}/edit/master/-/doc/#{docs_content_filename}" - when "ee" - # gitlab-foss and gitlab repos - gitlab_url = "https://gitlab.com/gitlab-org/gitlab/blob/master/doc/#{docs_content_filename}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/gitlab/edit/master/-/doc/#{docs_content_filename}" - else - # gitlab-docs pages - gitlab_url = "https://gitlab.com/gitlab-org/gitlab-docs/blob/main/#{item[:content_filename]}" - gitlab_ide_url = "https://gitlab.com/-/ide/project/gitlab-org/gitlab-docs/edit/main/-/#{item[:content_filename]}" - end + resource = resource_from_item(item) case editor when :simple - gitlab_url + blob_url(resource) when :webide - gitlab_ide_url + ide_url(resource) else raise "Unknown editor: #{editor}" end end + + private + + def resource_from_item(item) + # The item identifier is the file path of the current docs page. + # Ex: "/ee/user/ssh.md" + # + # We can use the first path segement to determine which project the docs + # reside in. If it's not a known project, then we'll assume that it's a + # content file inside gitlab-docs. + identifier_path = item.identifier.to_s.delete_prefix("/") + product, _, repo_doc_path = identifier_path.partition("/") + if repo = PRODUCT_REPOS[product] + file_path = "#{repo[:doc_directory]}/#{repo_doc_path}" + return repo.merge({ file_path: file_path }) + end + + { + project: "gitlab-org/gitlab-docs", + default_branch_name: "main", + file_path: item[:content_filename] + } + end + + def blob_url(resource) + "https://gitlab.com/#{resource[:project]}/-/blob/#{resource[:default_branch_name]}/#{resource[:file_path]}" + end + + def ide_url(resource) + "https://gitlab.com/-/ide/project/#{resource[:project]}/edit/#{resource[:default_branch_name]}/-/#{resource[:file_path]}" + end end end diff --git a/spec/helpers/edit_on_gitlab_spec.rb b/spec/helpers/edit_on_gitlab_spec.rb index 77114593..a813e66c 100644 --- a/spec/helpers/edit_on_gitlab_spec.rb +++ b/spec/helpers/edit_on_gitlab_spec.rb @@ -20,17 +20,17 @@ RSpec.describe Nanoc::Helpers::EditOnGitLab do using RSpec::Parameterized::TableSyntax where(:identifier, :editor, :expected_url) do - "/omnibus/index.md" | :simple | "https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/index.md" + "/omnibus/index.md" | :simple | "https://gitlab.com/gitlab-org/omnibus-gitlab/-/blob/master/doc/index.md" "/omnibus/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/omnibus-gitlab/edit/master/-/doc/index.md" - "/runner/index.md" | :simple | "https://gitlab.com/gitlab-org/gitlab-runner/blob/main/docs/index.md" + "/runner/index.md" | :simple | "https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/docs/index.md" "/runner/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/gitlab-runner/edit/main/-/docs/index.md" - "/charts/index.md" | :simple | "https://gitlab.com/gitlab-org/charts/gitlab/blob/master/doc/index.md" + "/charts/index.md" | :simple | "https://gitlab.com/gitlab-org/charts/gitlab/-/blob/master/doc/index.md" "/charts/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/charts/gitlab/edit/master/-/doc/index.md" - "/operator/index.md" | :simple | "https://gitlab.com/gitlab-org/cloud-native/gitlab-operator/blob/master/doc/index.md" + "/operator/index.md" | :simple | "https://gitlab.com/gitlab-org/cloud-native/gitlab-operator/-/blob/master/doc/index.md" "/operator/index.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/cloud-native/gitlab-operator/edit/master/-/doc/index.md" - "/ee/user/ssh.md" | :simple | "https://gitlab.com/gitlab-org/gitlab/blob/master/doc/user/ssh.md" + "/ee/user/ssh.md" | :simple | "https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/user/ssh.md" "/ee/user/ssh.md" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/gitlab/edit/master/-/doc/user/ssh.md" - "/content/404.html" | :simple | "https://gitlab.com/gitlab-org/gitlab-docs/blob/main/content/404.html" + "/content/404.html" | :simple | "https://gitlab.com/gitlab-org/gitlab-docs/-/blob/main/content/404.html" "/content/404.html" | :webide | "https://gitlab.com/-/ide/project/gitlab-org/gitlab-docs/edit/main/-/content/404.html" end |