Class: KnifeGithubRepoFork::GithubRepoFork

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/github_repo_fork.rb

Instance Method Summary collapse

Instance Method Details

#get_body_json(target) ⇒ Object

Create the json body with repo config for POST information

Parameters:

  • target (String)

    oragnization target name



86
87
88
89
90
# File 'lib/chef/knife/github_repo_fork.rb', line 86

def get_body_json(target)
  body = {
    "organization" => target,
  }.to_json
end

#get_github_tokenObject

Get the OAuth authentication token from config or command line

Parameters:

  • nil


94
95
96
97
98
99
100
101
# File 'lib/chef/knife/github_repo_fork.rb', line 94

def get_github_token()
  token = locate_config_value('github_token')
  if token.nil? || token.empty?
    Chef::Log.error("Please specify a github token")
    exit 1
  end
  token
end

#rest_request(params = {}) ⇒ Object

Post Get the OAuth authentication token from config or command line

Parameters:

  • url (String)

    target url (organization or user) body [JSON] json data with repo configuration token [String] token sring



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef/knife/github_repo_fork.rb', line 107

def rest_request(params = {})
  url    = params['url'].to_s
  token  = params['token'].to_s
  code   = params['response_code'] || 200
  body   = params['body'] || nil
  action = params['action'] || 'POST'

  if @github_ssl_verify_mode == "verify_none"
    config[:ssl_verify_mode] = :verify_none
  elsif @github_ssl_verify_mode == "verify_peer"
    config[:ssl_verify_mode] = :verify_peer
  end

  Chef::Log.debug("URL: " + url)

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host,uri.port)
  if uri.scheme == "https"
    http.use_ssl = true
    if  @github_ssl_verify_mode == "verify_none"
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    else
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    end
  end
   
  req = Net::HTTP::Post.new(uri.path, initheader = {"Authorization" => "token #{token}"})
  req.body = body unless body.nil?
  response = http.request(req)
  
  unless response.code.to_s == code.to_s then
    puts "Error #{response.code}: #{response.message}"
    puts JSON.pretty_generate(JSON.parse(response.body))
    puts "URL: #{url}"
    exit 1
  end

  begin
    json = JSON.parse(response.body)
  rescue
    ui.warn "The result on the RESTRequest is not in json format"
    ui.warn "Output: " + response.body
    exit 1
  end
  json
end

#runObject



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
76
77
78
79
80
81
82
# File 'lib/chef/knife/github_repo_fork.rb', line 49

def run
  params = {}
  # validate base options from base module.
  validate_base_options      

  # Display information if debug mode is on.
  display_debug_info

  # Get the name_args from the command line
  name = name_args[0]
  name_args[1].nil? ? owner = locate_config_value('github_organizations').first : owner = name_args[1]
  target = name_args[2] unless name_args[2].nil?
  
  if owner.nil? || name.nil? || owner.empty? || name.empty? 
    Chef::Log.error("Please specify a repository name like: name ")
    exit 1
  end 

  # Set params for the rest request
  params['url'] = @github_url + "/api/" + @github_api_version + "/repos/#{owner}/#{name}/forks"
  params['body'] = get_body_json(target) unless target.nil?
  params['token'] = get_github_token()
  params['response_code']  = 202

  # Execute the rest request
  username = ENV['USER']

  rest_request(params)
  if target
    puts "Fork of #{name} is created in #{target}"
  else
    puts "Fork of #{name} is created in #{username}"
  end
end