File: repository_finder.rb

package info (click to toggle)
ruby-jekyll-github-metadata 2.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: ruby: 2,355; javascript: 107; sh: 41; makefile: 6
file content (75 lines) | stat: -rw-r--r-- 2,275 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

module Jekyll
  module GitHubMetadata
    class RepositoryFinder
      NoRepositoryError = Class.new(Jekyll::Errors::FatalException)

      attr_reader :site
      def initialize(site)
        @site = site
      end

      # Public: fetches the repository name with owner to fetch metadata for.
      # In order of precedence, this method uses:
      # 1. the environment variable $PAGES_REPO_NWO
      # 2. 'repository' variable in the site config
      # 3. the 'origin' git remote's URL
      #
      # site - the Jekyll::Site being processed
      #
      # Return the name with owner (e.g. 'parkr/my-repo') or raises an
      # error if one cannot be found.
      def nwo
        @nwo ||= begin
          nwo_from_env || \
            nwo_from_config || \
            nwo_from_git_origin_remote || \
            proc do
              raise NoRepositoryError, "No repo name found. " \
                "Specify using PAGES_REPO_NWO environment variables, " \
                "'repository' in your configuration, or set up an 'origin' " \
                "git remote pointing to your github.com repository."
            end.call
        end
      end

      private

      def nwo_from_env
        ENV["PAGES_REPO_NWO"]
      end

      def nwo_from_config
        repo = site.config["repository"]
        repo if repo&.is_a?(String) && repo&.include?("/")
      end

      def git_remotes
        _process, output = Jekyll::Utils::Exec.run("git", "remote", "--verbose")
        output.to_s.strip.split("\n")
      rescue Errno::ENOENT => e
        Jekyll.logger.warn "Not Installed:", e.message
        []
      end

      def git_remote_url
        git_remotes.grep(%r!\Aorigin\t!).map do |remote|
          remote.sub(%r!\Aorigin\t(.*) \([a-z]+\)!, "\\1")
        end.uniq.first || ""
      end

      def nwo_from_git_origin_remote
        return unless Jekyll.env == "development" || Jekyll.env == "test"

        matches = git_remote_url.chomp(".git").match github_remote_regex
        matches[2..3].join("/") if matches
      end

      def github_remote_regex
        github_host_regex = Regexp.escape(Jekyll::GitHubMetadata::Pages.github_hostname)
        %r!#{github_host_regex}(:|/)([\w-]+)/([\w\.-]+)!
      end
    end
  end
end