bunto-github-metadata 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/bunto-github-metadata.rb +113 -0
- data/lib/bunto-github-metadata/client.rb +108 -0
- data/lib/bunto-github-metadata/ghp_metadata_generator.rb +75 -0
- data/lib/bunto-github-metadata/pages.rb +82 -0
- data/lib/bunto-github-metadata/repository.rb +193 -0
- data/lib/bunto-github-metadata/sanitizer.rb +53 -0
- data/lib/bunto-github-metadata/value.rb +66 -0
- data/lib/bunto-github-metadata/version.rb +5 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 62520af3886612f840412e0ad6ce893282226806
|
4
|
+
data.tar.gz: d20dd9356d26e0825872b9082737d13d5f9254ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4db0a9b06d79251105c12b8ab927c218d404cfa69ba4c66defbe187998b8cf73e06dd416c55354097413e477db3bb1a9c9aa5c121a2515ccdfc3ede73b5b3b36
|
7
|
+
data.tar.gz: f3193317665edfe629b1687a60383df48dc4c98715d3d9e0d55f8d383c2334ffcc578dc8543eea4ae40034666157c2f06ff466fb5e53173effbf33cd353ee1d8
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'octokit'
|
2
|
+
|
3
|
+
module Bunto
|
4
|
+
unless const_defined? :Errors
|
5
|
+
module Errors
|
6
|
+
FatalException = Class.new(::RuntimeError) unless const_defined? :FatalException
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module GitHubMetadata
|
11
|
+
NoRepositoryError = Class.new(Bunto::Errors::FatalException)
|
12
|
+
|
13
|
+
autoload :Client, 'bunto-github-metadata/client'
|
14
|
+
autoload :Pages, 'bunto-github-metadata/pages'
|
15
|
+
autoload :Repository, 'bunto-github-metadata/repository'
|
16
|
+
autoload :Sanitizer, 'bunto-github-metadata/sanitizer'
|
17
|
+
autoload :Value, 'bunto-github-metadata/value'
|
18
|
+
autoload :VERSION, 'bunto-github-metadata/version'
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_accessor :repository
|
22
|
+
attr_writer :client
|
23
|
+
|
24
|
+
def environment
|
25
|
+
Bunto.respond_to?(:env) ? Bunto.env : (Pages.env || 'development')
|
26
|
+
end
|
27
|
+
|
28
|
+
def client
|
29
|
+
@client ||= Client.new
|
30
|
+
end
|
31
|
+
|
32
|
+
def values
|
33
|
+
@values ||= Hash.new
|
34
|
+
end
|
35
|
+
alias_method :to_h, :values
|
36
|
+
alias_method :to_liquid, :to_h
|
37
|
+
|
38
|
+
def clear_values!
|
39
|
+
@values = Hash.new
|
40
|
+
end
|
41
|
+
|
42
|
+
def reset!
|
43
|
+
clear_values!
|
44
|
+
@client = nil
|
45
|
+
@repository = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def [](key)
|
49
|
+
values[key.to_s]
|
50
|
+
end
|
51
|
+
|
52
|
+
def register_value(key, value)
|
53
|
+
values[key.to_s] = Value.new(key.to_s, value)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Reset our values hash.
|
57
|
+
def init!
|
58
|
+
clear_values!
|
59
|
+
|
60
|
+
# Environment-Specific
|
61
|
+
register_value('environment', proc { Pages.env })
|
62
|
+
register_value('hostname', proc { Pages.github_hostname })
|
63
|
+
register_value('pages_env', proc { Pages.env })
|
64
|
+
register_value('pages_hostname', proc { Pages.pages_hostname })
|
65
|
+
register_value('api_url', proc { Pages.api_url })
|
66
|
+
register_value('help_url', proc { Pages.help_url })
|
67
|
+
|
68
|
+
register_value('versions', proc {
|
69
|
+
begin
|
70
|
+
require 'github-pages'
|
71
|
+
GitHubPages.versions
|
72
|
+
rescue LoadError; Hash.new end
|
73
|
+
})
|
74
|
+
|
75
|
+
# The Juicy Stuff
|
76
|
+
register_value('public_repositories', proc { |_,r| r.owner_public_repositories })
|
77
|
+
register_value('organization_members', proc { |_,r| r.organization_public_members })
|
78
|
+
register_value('build_revision', proc {
|
79
|
+
ENV['BUNTO_BUILD_REVISION'] || `git rev-parse HEAD`.strip
|
80
|
+
})
|
81
|
+
register_value('project_title', proc { |_,r| r.name })
|
82
|
+
register_value('project_tagline', proc { |_,r| r.tagline })
|
83
|
+
register_value('owner_name', proc { |_,r| r.owner })
|
84
|
+
register_value('owner_url', proc { |_,r| r.owner_url })
|
85
|
+
register_value('owner_gravatar_url', proc { |_,r| r.owner_gravatar_url })
|
86
|
+
register_value('repository_url', proc { |_,r| r.repository_url })
|
87
|
+
register_value('repository_nwo', proc { |_,r| r.nwo })
|
88
|
+
register_value('repository_name', proc { |_,r| r.name})
|
89
|
+
register_value('zip_url', proc { |_,r| r.zip_url })
|
90
|
+
register_value('tar_url', proc { |_,r| r.tar_url })
|
91
|
+
register_value('clone_url', proc { |_,r| r.repo_clone_url })
|
92
|
+
register_value('releases_url', proc { |_,r| r.releases_url })
|
93
|
+
register_value('issues_url', proc { |_,r| r.issues_url })
|
94
|
+
register_value('wiki_url', proc { |_,r| r.wiki_url })
|
95
|
+
register_value('language', proc { |_,r| r.language })
|
96
|
+
register_value('is_user_page', proc { |_,r| r.user_page? })
|
97
|
+
register_value('is_project_page', proc { |_,r| r.project_page? })
|
98
|
+
register_value('show_downloads', proc { |_,r| r.show_downloads? })
|
99
|
+
register_value('url', proc { |_,r| r.pages_url })
|
100
|
+
register_value('contributors', proc { |_,r| r.contributors })
|
101
|
+
register_value('releases', proc { |_,r| r.releases })
|
102
|
+
|
103
|
+
values
|
104
|
+
end
|
105
|
+
|
106
|
+
if Bunto.const_defined? :Site
|
107
|
+
require_relative 'bunto-github-metadata/ghp_metadata_generator'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
init!
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
module Bunto
|
4
|
+
module GitHubMetadata
|
5
|
+
class Client
|
6
|
+
InvalidMethodError = Class.new(NoMethodError)
|
7
|
+
|
8
|
+
# Whitelisted API calls.
|
9
|
+
API_CALLS = Set.new(%w{
|
10
|
+
repository
|
11
|
+
organization
|
12
|
+
repository?
|
13
|
+
pages
|
14
|
+
contributors
|
15
|
+
releases
|
16
|
+
list_repos
|
17
|
+
organization_public_members
|
18
|
+
})
|
19
|
+
|
20
|
+
def initialize(options = nil)
|
21
|
+
@client = build_octokit_client(options)
|
22
|
+
end
|
23
|
+
|
24
|
+
def safe_require(gem_name)
|
25
|
+
require gem_name
|
26
|
+
true
|
27
|
+
rescue LoadError
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_octokit_client(options = nil)
|
32
|
+
options = options || Hash.new
|
33
|
+
unless options.key? :access_token
|
34
|
+
options.merge! pluck_auth_method
|
35
|
+
end
|
36
|
+
Octokit::Client.new({:auto_paginate => true}.merge(options))
|
37
|
+
end
|
38
|
+
|
39
|
+
def accepts_client_method?(method_name)
|
40
|
+
API_CALLS.include?(method_name.to_s) && @client.respond_to?(method_name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def respond_to?(method_name, include_private = false)
|
44
|
+
accepts_client_method?(method_name) || super
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(method_name, *args, &block)
|
48
|
+
method = method_name.to_s
|
49
|
+
if accepts_client_method?(method_name)
|
50
|
+
key = cache_key(method_name, args)
|
51
|
+
Bunto.logger.debug "GitHub Metadata:", "Calling @client.#{method}(#{args.map(&:inspect).join(", ")})"
|
52
|
+
cache[key] ||= save_from_errors { @client.public_send(method_name, *args, &block) }
|
53
|
+
elsif @client.respond_to?(method_name)
|
54
|
+
raise InvalidMethodError, "#{method_name} is not whitelisted on #{inspect}"
|
55
|
+
else
|
56
|
+
super
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def save_from_errors(default = false, &block)
|
61
|
+
if block.arity == 1
|
62
|
+
block.call(@client)
|
63
|
+
else
|
64
|
+
block.call
|
65
|
+
end
|
66
|
+
rescue Faraday::Error::ConnectionFailed,
|
67
|
+
Octokit::NotFound,
|
68
|
+
Octokit::Unauthorized,
|
69
|
+
Octokit::TooManyRequests
|
70
|
+
default
|
71
|
+
end
|
72
|
+
|
73
|
+
def inspect
|
74
|
+
"#<#{self.class.name} @client=#{client_inspect}>"
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def client_inspect
|
80
|
+
if @client.nil?
|
81
|
+
"nil"
|
82
|
+
else
|
83
|
+
"#<#{@client.class.name} (#{"un" unless @client.access_token}authenticated)>"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def pluck_auth_method
|
88
|
+
if ENV['BUNTO_GITHUB_TOKEN'] || Octokit.access_token
|
89
|
+
{ :access_token => ENV['BUNTO_GITHUB_TOKEN'] || Octokit.access_token }
|
90
|
+
elsif !ENV['NO_NETRC'] && File.exist?(File.join(ENV['HOME'], '.netrc')) && safe_require('netrc')
|
91
|
+
{ :netrc => true }
|
92
|
+
else
|
93
|
+
Bunto.logger.warn "GitHub Metadata:", "No GitHub API authentication could be found." +
|
94
|
+
" Some fields may be missing or have incorrect data."
|
95
|
+
{}.freeze
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def cache_key(method, *args)
|
100
|
+
Digest::SHA1.hexdigest(method.to_s + args.join(", "))
|
101
|
+
end
|
102
|
+
|
103
|
+
def cache
|
104
|
+
@cache ||= {}
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Bunto
|
2
|
+
module GitHubMetadata
|
3
|
+
class GHPMetadataGenerator < Bunto::Generator
|
4
|
+
safe true
|
5
|
+
|
6
|
+
def generate(site)
|
7
|
+
Bunto.logger.debug "Generator:", "Calling GHPMetadataGenerator"
|
8
|
+
initialize_repo! nwo(site)
|
9
|
+
Bunto.logger.debug "GitHub Metadata:", "Generating for #{GitHubMetadata.repository.nwo}"
|
10
|
+
|
11
|
+
site.config['github'] =
|
12
|
+
case site.config['github']
|
13
|
+
when nil
|
14
|
+
GitHubMetadata.to_liquid
|
15
|
+
when Hash
|
16
|
+
Bunto::Utils.deep_merge_hashes(GitHubMetadata.to_liquid, site.config['github'])
|
17
|
+
else
|
18
|
+
site.config['github']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def initialize_repo!(repo_nwo)
|
25
|
+
if GitHubMetadata.repository.nil? || GitHubMetadata.repository.nwo != repo_nwo
|
26
|
+
GitHubMetadata.init!
|
27
|
+
GitHubMetadata.repository = GitHubMetadata::Repository.new(repo_nwo)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def git_remote_url
|
32
|
+
`git remote --verbose`.split("\n").grep(%r{\Aorigin\t}).map do |remote|
|
33
|
+
remote.sub(/\Aorigin\t(.*) \([a-z]+\)/, "\\1")
|
34
|
+
end.uniq.first || ""
|
35
|
+
end
|
36
|
+
|
37
|
+
def nwo_from_git_origin_remote
|
38
|
+
return unless Bunto.env == "development"
|
39
|
+
matches = git_remote_url.match %r{github.com(:|/)([\w-]+)/([\w-]+)}
|
40
|
+
matches[2..3].join("/") if matches
|
41
|
+
end
|
42
|
+
|
43
|
+
def nwo_from_env
|
44
|
+
ENV['PAGES_REPO_NWO']
|
45
|
+
end
|
46
|
+
|
47
|
+
def nwo_from_config(site)
|
48
|
+
repo = site.config['repository']
|
49
|
+
repo if repo && repo.is_a?(String) && repo.include?('/')
|
50
|
+
end
|
51
|
+
|
52
|
+
# Public: fetches the repository name with owner to fetch metadata for.
|
53
|
+
# In order of precedence, this method uses:
|
54
|
+
# 1. the environment variable $PAGES_REPO_NWO
|
55
|
+
# 2. 'repository' variable in the site config
|
56
|
+
# 3. the 'origin' git remote's URL
|
57
|
+
#
|
58
|
+
# site - the Bunto::Site being processed
|
59
|
+
#
|
60
|
+
# Return the name with owner (e.g. 'parkr/my-repo') or raises an
|
61
|
+
# error if one cannot be found.
|
62
|
+
def nwo(site)
|
63
|
+
nwo_from_env || \
|
64
|
+
nwo_from_config(site) || \
|
65
|
+
nwo_from_git_origin_remote || \
|
66
|
+
proc {
|
67
|
+
raise GitHubMetadata::NoRepositoryError, "No repo name found. " \
|
68
|
+
"Specify using PAGES_REPO_NWO environment variables, " \
|
69
|
+
"'repository' in your configuration, or set up an 'origin' " \
|
70
|
+
"git remote pointing to your github.com repository."
|
71
|
+
}.call
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Bunto
|
2
|
+
module GitHubMetadata
|
3
|
+
class Pages
|
4
|
+
class << self
|
5
|
+
DEFAULTS = {
|
6
|
+
'PAGES_ENV' => 'dotcom'.freeze,
|
7
|
+
'PAGES_API_URL' => 'https://api.github.com'.freeze,
|
8
|
+
'PAGES_HELP_URL' => 'https://help.github.com'.freeze,
|
9
|
+
'PAGES_GITHUB_HOSTNAME' => 'github.com'.freeze,
|
10
|
+
'PAGES_PAGES_HOSTNAME' => 'github.io'.freeze,
|
11
|
+
'SSL' => 'false'.freeze,
|
12
|
+
'SUBDOMAIN_ISOLATION' => 'false'.freeze
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
# Whether the GitHub instance supports HTTPS
|
16
|
+
# Note: this will be the same as how sites are served in Enterprise,
|
17
|
+
# but may be different from how sites are served on GitHub.com.
|
18
|
+
# See Repository#url_scheme
|
19
|
+
def ssl?
|
20
|
+
env_var('SSL') == 'true' || test?
|
21
|
+
end
|
22
|
+
|
23
|
+
def scheme
|
24
|
+
ssl? ? "https" : "http"
|
25
|
+
end
|
26
|
+
|
27
|
+
def subdomain_isolation?
|
28
|
+
env_var('SUBDOMAIN_ISOLATION').eql? 'true'
|
29
|
+
end
|
30
|
+
|
31
|
+
def test?; env == 'test' end
|
32
|
+
def dotcom?; env == 'dotcom' end
|
33
|
+
def enterprise?; env == 'enterprise' end
|
34
|
+
|
35
|
+
def custom_domains_enabled?
|
36
|
+
dotcom? || test?
|
37
|
+
end
|
38
|
+
|
39
|
+
def env
|
40
|
+
env_var 'PAGES_ENV'
|
41
|
+
end
|
42
|
+
|
43
|
+
def github_url
|
44
|
+
if dotcom?
|
45
|
+
"https://github.com".freeze
|
46
|
+
else
|
47
|
+
"#{scheme}://#{github_hostname}"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def api_url
|
52
|
+
trim_last_slash env_var('PAGES_API_URL', ENV['API_URL'])
|
53
|
+
end
|
54
|
+
|
55
|
+
def help_url
|
56
|
+
trim_last_slash env_var('PAGES_HELP_URL', ENV['HELP_URL'])
|
57
|
+
end
|
58
|
+
|
59
|
+
def github_hostname
|
60
|
+
trim_last_slash env_var('PAGES_GITHUB_HOSTNAME', ENV['GITHUB_HOSTNAME'])
|
61
|
+
end
|
62
|
+
|
63
|
+
def pages_hostname
|
64
|
+
trim_last_slash env_var('PAGES_PAGES_HOSTNAME', ENV['PAGES_HOSTNAME'])
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def env_var(key, intermediate_default = nil)
|
69
|
+
!ENV[key].to_s.empty? ? ENV[key] : (intermediate_default || DEFAULTS[key])
|
70
|
+
end
|
71
|
+
|
72
|
+
def trim_last_slash(url)
|
73
|
+
if url[-1] == "/"
|
74
|
+
url[0..-2]
|
75
|
+
else
|
76
|
+
url
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
module Bunto
|
2
|
+
module GitHubMetadata
|
3
|
+
class Repository
|
4
|
+
attr_reader :nwo, :owner, :name
|
5
|
+
def initialize(name_with_owner)
|
6
|
+
@nwo = name_with_owner
|
7
|
+
@owner = nwo.split("/").first
|
8
|
+
@name = nwo.split("/").last
|
9
|
+
end
|
10
|
+
|
11
|
+
def git_ref
|
12
|
+
user_page? ? 'master' : 'gh-pages'
|
13
|
+
end
|
14
|
+
|
15
|
+
def repo_info
|
16
|
+
@repo_info ||= (Value.new(proc { |c| c.repository(nwo) }).render || Hash.new)
|
17
|
+
end
|
18
|
+
|
19
|
+
def language
|
20
|
+
repo_info["language"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def tagline
|
24
|
+
repo_info["description"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def owner_url
|
28
|
+
"#{Pages.github_url}/#{owner}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def owner_gravatar_url
|
32
|
+
"#{owner_url}.png"
|
33
|
+
end
|
34
|
+
|
35
|
+
def repo_clone_url
|
36
|
+
"#{repository_url}.git"
|
37
|
+
end
|
38
|
+
|
39
|
+
def repository_url
|
40
|
+
"#{owner_url}/#{name}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def zip_url
|
44
|
+
"#{repository_url}/zipball/#{git_ref}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def tar_url
|
48
|
+
"#{repository_url}/tarball/#{git_ref}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def releases_url
|
52
|
+
"#{repository_url}/releases"
|
53
|
+
end
|
54
|
+
|
55
|
+
def issues_url
|
56
|
+
"#{repository_url}/issues" if repo_info["has_issues"]
|
57
|
+
end
|
58
|
+
|
59
|
+
def wiki_url
|
60
|
+
"#{repository_url}/wiki" if repo_info["has_wiki"]
|
61
|
+
end
|
62
|
+
|
63
|
+
def show_downloads?
|
64
|
+
!!repo_info["has_downloads"]
|
65
|
+
end
|
66
|
+
|
67
|
+
def organization_repository?
|
68
|
+
memoize_value :@is_organization_repository, Value.new(proc { |c| !!c.organization(owner) })
|
69
|
+
end
|
70
|
+
|
71
|
+
def owner_public_repositories
|
72
|
+
memoize_value :@owner_public_repositories, Value.new(proc { |c| c.list_repos(owner, "type" => "public") })
|
73
|
+
end
|
74
|
+
|
75
|
+
def organization_public_members
|
76
|
+
memoize_value :@organization_public_members, Value.new(proc { |c|
|
77
|
+
c.organization_public_members(owner) if organization_repository?
|
78
|
+
})
|
79
|
+
end
|
80
|
+
|
81
|
+
def contributors
|
82
|
+
memoize_value :@contributors, Value.new(proc { |c| c.contributors(nwo) })
|
83
|
+
end
|
84
|
+
|
85
|
+
def releases
|
86
|
+
memoize_value :@releases, Value.new(proc { |c| c.releases(nwo) })
|
87
|
+
end
|
88
|
+
|
89
|
+
def user_page?
|
90
|
+
primary?
|
91
|
+
end
|
92
|
+
|
93
|
+
def project_page?
|
94
|
+
!user_page?
|
95
|
+
end
|
96
|
+
|
97
|
+
def github_repo?
|
98
|
+
!Pages.enterprise? && owner.eql?('github')
|
99
|
+
end
|
100
|
+
|
101
|
+
def primary?
|
102
|
+
if Pages.enterprise?
|
103
|
+
name.downcase == "#{owner.to_s.downcase}.#{Pages.github_hostname}"
|
104
|
+
else
|
105
|
+
user_page_domains.include? name.downcase
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
# In enterprise, the site's scheme will be the same as the instance's
|
110
|
+
# In dotcom, this will be `https` for GitHub-owned sites that end with
|
111
|
+
# `.github.com` and will be `http` for all other sites.
|
112
|
+
# Note: This is not the same as *instance*'s scheme, which may differ
|
113
|
+
def url_scheme
|
114
|
+
if Pages.enterprise?
|
115
|
+
Pages.scheme
|
116
|
+
elsif owner == 'github' && domain.end_with?('.github.com')
|
117
|
+
'https'
|
118
|
+
else
|
119
|
+
'http'
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def default_user_domain
|
124
|
+
if github_repo?
|
125
|
+
"#{owner}.#{Pages.github_hostname}".downcase
|
126
|
+
elsif Pages.enterprise?
|
127
|
+
Pages.pages_hostname.downcase
|
128
|
+
else
|
129
|
+
"#{owner}.#{Pages.pages_hostname}".downcase
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def user_page_domains
|
134
|
+
domains = [default_user_domain]
|
135
|
+
domains.push "#{owner}.github.com".downcase unless Pages.enterprise?
|
136
|
+
domains
|
137
|
+
end
|
138
|
+
|
139
|
+
def user_domain
|
140
|
+
domain = default_user_domain
|
141
|
+
user_page_domains.each do |user_repo|
|
142
|
+
candidate_nwo = "#{owner}/#{user_repo}"
|
143
|
+
next unless Value.new(proc { |client| client.repository? candidate_nwo }).render
|
144
|
+
domain = self.class.new(candidate_nwo).domain
|
145
|
+
end
|
146
|
+
domain
|
147
|
+
end
|
148
|
+
|
149
|
+
def pages_url
|
150
|
+
if !Pages.custom_domains_enabled?
|
151
|
+
path = user_page? ? owner : nwo
|
152
|
+
if Pages.subdomain_isolation?
|
153
|
+
URI.join("#{Pages.scheme}://#{Pages.pages_hostname}/", path).to_s
|
154
|
+
else
|
155
|
+
URI.join("#{Pages.github_url}/pages/", path).to_s
|
156
|
+
end
|
157
|
+
elsif cname || primary?
|
158
|
+
"#{url_scheme}://#{domain}"
|
159
|
+
else
|
160
|
+
URI.join("#{url_scheme}://#{domain}", name).to_s
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def cname
|
165
|
+
memoize_value :@cname, Value.new(proc { |c|
|
166
|
+
if Pages.custom_domains_enabled?
|
167
|
+
(c.pages(nwo) || {'cname' => nil})['cname']
|
168
|
+
end
|
169
|
+
})
|
170
|
+
end
|
171
|
+
|
172
|
+
def domain
|
173
|
+
@domain ||=
|
174
|
+
if !Pages.custom_domains_enabled?
|
175
|
+
Pages.github_hostname
|
176
|
+
elsif cname # explicit CNAME
|
177
|
+
cname
|
178
|
+
elsif primary? # user/org repo
|
179
|
+
default_user_domain
|
180
|
+
else # project repo
|
181
|
+
user_domain
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
private
|
186
|
+
|
187
|
+
def memoize_value(var_name, value)
|
188
|
+
return instance_variable_get(var_name) if instance_variable_defined?(var_name)
|
189
|
+
instance_variable_set(var_name, value.render)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Bunto
|
2
|
+
module GitHubMetadata
|
3
|
+
module Sanitizer
|
4
|
+
extend self
|
5
|
+
# Sanitize an object.
|
6
|
+
# When the resource is either `false`, `true`, `nil` or a number,
|
7
|
+
# it returns the resource as-is. When the resource is an array,
|
8
|
+
# it maps over the entire array, sanitizing each of its values.
|
9
|
+
# When the resource responds to the #to_hash method, it returns
|
10
|
+
# the value of #sanitize_resource (see below). If none of the
|
11
|
+
# aforementioned conditions are met, the return value of #to_s
|
12
|
+
# is used.
|
13
|
+
#
|
14
|
+
# resource - an Object
|
15
|
+
#
|
16
|
+
# Returns the sanitized resource.
|
17
|
+
def sanitize(resource)
|
18
|
+
case resource
|
19
|
+
when Array
|
20
|
+
resource.map { |item| sanitize(item) }
|
21
|
+
when Numeric
|
22
|
+
resource
|
23
|
+
when FalseClass
|
24
|
+
false
|
25
|
+
when TrueClass
|
26
|
+
true
|
27
|
+
when NilClass
|
28
|
+
nil
|
29
|
+
else
|
30
|
+
if resource.respond_to?(:to_hash)
|
31
|
+
sanitize_resource(resource)
|
32
|
+
else
|
33
|
+
resource.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Sanitize the Sawyer Resource or Hash
|
39
|
+
# Note: the object must respond to :to_hash for this to work.
|
40
|
+
# Consider using #sanitize instead of this method directly.
|
41
|
+
#
|
42
|
+
# resource - an Object which responds to :to_hash
|
43
|
+
#
|
44
|
+
# Returns the sanitized sawyer resource or hash as a hash.
|
45
|
+
def sanitize_resource(resource)
|
46
|
+
resource.to_hash.inject({}) do |result, (k, v)|
|
47
|
+
result[k.to_s] = sanitize(v)
|
48
|
+
result
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Bunto
|
4
|
+
module GitHubMetadata
|
5
|
+
class Value
|
6
|
+
attr_reader :key, :value
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
case args.size
|
10
|
+
when 1
|
11
|
+
@key = '{anonymous}'
|
12
|
+
@value = args.first
|
13
|
+
when 2
|
14
|
+
@key = args.first.to_s
|
15
|
+
@value = args.last
|
16
|
+
else
|
17
|
+
raise ArgumentError.new("#{args.size} args given but expected 1 or 2")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def render
|
22
|
+
@value = if @value.respond_to?(:call)
|
23
|
+
case @value.arity
|
24
|
+
when 0
|
25
|
+
@value.call
|
26
|
+
when 1
|
27
|
+
@value.call(GitHubMetadata.client)
|
28
|
+
when 2
|
29
|
+
@value.call(GitHubMetadata.client, GitHubMetadata.repository)
|
30
|
+
else
|
31
|
+
raise ArgumentError.new("Whoa, arity of 0, 1, or 2 please in your procs.")
|
32
|
+
end
|
33
|
+
else
|
34
|
+
@value
|
35
|
+
end
|
36
|
+
@value = Sanitizer.sanitize(@value)
|
37
|
+
rescue RuntimeError, NameError => e
|
38
|
+
Bunto.logger.error "GitHubMetadata:", "Error processing value '#{key}':"
|
39
|
+
raise e
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
render.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_json(*)
|
47
|
+
render.to_json
|
48
|
+
end
|
49
|
+
|
50
|
+
def to_liquid
|
51
|
+
case render
|
52
|
+
when nil
|
53
|
+
nil
|
54
|
+
when true, false
|
55
|
+
value
|
56
|
+
when Hash
|
57
|
+
value
|
58
|
+
when String, Numeric, Array
|
59
|
+
value
|
60
|
+
else
|
61
|
+
to_json
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bunto-github-metadata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Parker Moore
|
8
|
+
- Suriyaa Kudo
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-05-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: octokit
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '4.0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '4.0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '1.5'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '1.5'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: netrc
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rspec
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: bunto
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '1.0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '1.0'
|
98
|
+
description:
|
99
|
+
email:
|
100
|
+
- parkrmoore@gmail.com
|
101
|
+
- SuriyaaKudoIsc@users.noreply.github.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- lib/bunto-github-metadata.rb
|
107
|
+
- lib/bunto-github-metadata/client.rb
|
108
|
+
- lib/bunto-github-metadata/ghp_metadata_generator.rb
|
109
|
+
- lib/bunto-github-metadata/pages.rb
|
110
|
+
- lib/bunto-github-metadata/repository.rb
|
111
|
+
- lib/bunto-github-metadata/sanitizer.rb
|
112
|
+
- lib/bunto-github-metadata/value.rb
|
113
|
+
- lib/bunto-github-metadata/version.rb
|
114
|
+
homepage: https://github.com/bunto/github-metadata
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.5.1
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: The site.github namespace
|
138
|
+
test_files: []
|