Class: Zenflow::Github

Inherits:
Object
  • Object
show all
Defined in:
lib/zenflow/helpers/github.rb

Constant Summary collapse

DEFAULT_HUB =
'github.com'
DEFAULT_API_BASE_URL =
'https://api.github.com'
DEFAULT_USER_AGENT_BASE =
'Zencoder'
API_BASE_URL_KEY =
'api.base.url'
USER_KEY =
'github.user'
TOKEN_KEY =
'token'
USER_AGENT_BASE_KEY =
'user.agent.base'
CONFIG_KEYS =
[
  API_BASE_URL_KEY,
  USER_KEY,
  TOKEN_KEY,
  USER_AGENT_BASE_KEY
]
CURRENT =
current

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hub) ⇒ Github



26
27
28
# File 'lib/zenflow/helpers/github.rb', line 26

def initialize(hub)
  @hub = hub
end

Class Method Details

.currentObject



30
31
32
# File 'lib/zenflow/helpers/github.rb', line 30

def self.current
  Github.new(Zenflow::Repo.hub || DEFAULT_HUB)
end

Instance Method Details

#api_base_url(use_default_when_value_is_nil = true) ⇒ Object



42
43
44
45
46
# File 'lib/zenflow/helpers/github.rb', line 42

def api_base_url(use_default_when_value_is_nil=true)
  api_base_url = get_config(API_BASE_URL_KEY)
  api_base_url ||= DEFAULT_API_BASE_URL if use_default_when_value_is_nil
  api_base_url
end

#ask_should_update_base_api_url?Boolean



48
49
50
# File 'lib/zenflow/helpers/github.rb', line 48

def ask_should_update_base_api_url?
  Zenflow::Ask("The GitHub API base URL for this hub is currently #{api_base_url(false)}. Do you want to use that?", :options => ["Y", "n"], :default => "y") == "n"
end

#ask_should_update_user?Boolean



63
64
65
# File 'lib/zenflow/helpers/github.rb', line 63

def ask_should_update_user?
  Zenflow::Ask("The GitHub user for this hub is currently #{user}. Do you want to use that?", :options => ["Y", "n"], :default => "y") == "n"
end

#ask_should_update_user_agent_base?Boolean



102
103
104
# File 'lib/zenflow/helpers/github.rb', line 102

def ask_should_update_user_agent_base?
  Zenflow::Ask("The GitHub User Agent base for this hub is currently #{user_agent_base(false)}. Do you want to use that?", :options => ["Y", "n"], :default => "y") == "n"
end

#ask_should_update_zenflow_token?Boolean



78
79
80
# File 'lib/zenflow/helpers/github.rb', line 78

def ask_should_update_zenflow_token?
  Zenflow::Ask("You already have a token from GitHub. Do you want to set a new one?", :options => ["y", "N"], :default => "n") == "y"
end

#authorizeObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/zenflow/helpers/github.rb', line 82

def authorize
  if zenflow_token.nil? || ask_should_update_zenflow_token?
    Zenflow::Log("Authorizing with GitHub (#{user}@#{@hub})... Enter your GitHub password.")
    oauth_response = JSON.parse(Zenflow::Shell.run(%{curl -u "#{user}" #{api_base_url}/authorizations -d '{"scopes":["repo"], "note":"Zenflow"}' --silent}, silent: true))
    if oauth_response['token']
      set_config(TOKEN_KEY, oauth_response['token'])
      Zenflow::Log("Authorized!")
    else
      Zenflow::Log("Something went wrong. Error from GitHub was: #{oauth_response['message']}")
      return
    end
  end
end

#configObject



36
37
38
39
40
# File 'lib/zenflow/helpers/github.rb', line 36

def config
  set_api_base_url
  set_user
  set_user_agent_base
end

#describeObject



147
148
149
150
151
152
153
154
# File 'lib/zenflow/helpers/github.rb', line 147

def describe
  [
    describe_parameter("API Base URL",    API_BASE_URL_KEY,    api_base_url),
    describe_parameter("User",            USER_KEY,            user),
    describe_parameter("Token",           TOKEN_KEY,           zenflow_token),
    describe_parameter("User Agent Base", USER_AGENT_BASE_KEY, user_agent_base)
  ]
end

#describe_parameter(name, parameter_key, value) ⇒ Object



143
144
145
# File 'lib/zenflow/helpers/github.rb', line 143

def describe_parameter(name, parameter_key, value)
  [name, parameter_key_for_hub(parameter_key), get_config(parameter_key), value]
end

#get_config(base_parameter_key) ⇒ Object



119
120
121
122
# File 'lib/zenflow/helpers/github.rb', line 119

def get_config(base_parameter_key)
  parameter_key_for_hub = parameter_key_for_hub(base_parameter_key)
  get_global_config(parameter_key_for_hub)
end

#get_global_config(key) ⇒ Object



129
130
131
132
133
# File 'lib/zenflow/helpers/github.rb', line 129

def get_global_config(key)
  config = Zenflow::Shell.run("git config --get #{key.to_s}", silent: true)
  config = config.chomp unless config.nil?
  config.to_s == '' ? nil : config
end

#hubObject



22
23
24
# File 'lib/zenflow/helpers/github.rb', line 22

def hub
  @hub
end

#is_default_hub?Boolean



139
140
141
# File 'lib/zenflow/helpers/github.rb', line 139

def is_default_hub?
  @hub == DEFAULT_HUB
end

#parameter_key_for_hub(key) ⇒ Object

If this repo is not hosted on the default github, construct a key prefix containing the hub information



114
115
116
117
# File 'lib/zenflow/helpers/github.rb', line 114

def parameter_key_for_hub(key)
  default_hub_key_prefix = key == USER_KEY ? "" : "zenflow."  # preserves backwards compatibility
  is_default_hub? ? "#{default_hub_key_prefix}#{key}" : "zenflow.hub.#{@hub}.#{key}"
end

#set_api_base_urlObject



52
53
54
55
56
57
# File 'lib/zenflow/helpers/github.rb', line 52

def set_api_base_url
  if api_base_url(false).nil? || ask_should_update_base_api_url?
    api_base_url = Zenflow::Ask("What is the base URL of your Github API?", {:default => DEFAULT_API_BASE_URL})
    set_config(API_BASE_URL_KEY, api_base_url)
  end
end

#set_config(base_parameter_key, value) ⇒ Object



124
125
126
127
# File 'lib/zenflow/helpers/github.rb', line 124

def set_config(base_parameter_key, value)
  parameter_key_for_hub = parameter_key_for_hub(base_parameter_key)
  set_global_config(parameter_key_for_hub, value)
end

#set_global_config(key, value) ⇒ Object



135
136
137
# File 'lib/zenflow/helpers/github.rb', line 135

def set_global_config(key, value)
  Zenflow::Shell.run("git config --global #{key} #{value}", silent: true)
end

#set_userObject



67
68
69
70
71
72
# File 'lib/zenflow/helpers/github.rb', line 67

def set_user
  if user.nil? || ask_should_update_user?
    username = Zenflow::Ask("What is your Github username?")
    set_config(USER_KEY, username)
  end
end

#set_user_agent_baseObject



106
107
108
109
110
111
# File 'lib/zenflow/helpers/github.rb', line 106

def set_user_agent_base
  if user_agent_base(false).nil? || ask_should_update_user_agent_base?
    user_agent_base = Zenflow::Ask("What base string would you like to use for the User Agent header, 'User-Agent: [user-agent-base]/Zenflow-#{VERSION}?", {:default => DEFAULT_USER_AGENT_BASE})
    set_config(USER_AGENT_BASE_KEY, user_agent_base)
  end
end

#userObject



59
60
61
# File 'lib/zenflow/helpers/github.rb', line 59

def user
  get_config(USER_KEY)
end

#user_agent_base(use_default_when_value_is_nil = true) ⇒ Object



96
97
98
99
100
# File 'lib/zenflow/helpers/github.rb', line 96

def user_agent_base(use_default_when_value_is_nil=true)
  user_agent_base = get_config(USER_AGENT_BASE_KEY)
  user_agent_base ||= DEFAULT_USER_AGENT_BASE if use_default_when_value_is_nil
  user_agent_base
end

#zenflow_tokenObject



74
75
76
# File 'lib/zenflow/helpers/github.rb', line 74

def zenflow_token
  get_config(TOKEN_KEY)
end