Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed authentication method to pass client id and secret through he… #100

Merged
merged 1 commit into from Dec 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions app/services/github.rb
Expand Up @@ -5,34 +5,33 @@ def initialize

# Returns an array of hashes of all (public so far) unepwcmc repositories with the first element as a hash of link header info
# Pass in a page number for github pagination, defaults to 1

CLIENT_CREDENTIALS = "client_id=#{Rails.application.secrets.github_key}&client_secret=#{Rails.application.secrets.github_secret}"
CLIENT_CREDENTIALS = { username: Rails.application.secrets.github_key, password: Rails.application.secrets.github_secret }

def get_all_repos page = 1
url = "#{Rails.application.secrets.github_api_base_url}orgs/unepwcmc/repos?#{CLIENT_CREDENTIALS}&page=#{page}"
response = HTTParty.get(url, headers: {"User-Agent" => "Labs"})
url = "#{Rails.application.secrets.github_api_base_url}orgs/unepwcmc/repos?page=#{page}"
response = HTTParty.get(url, basic_auth: CLIENT_CREDENTIALS, headers: {"User-Agent" => "Labs"})
repos_array = JSON.parse(response.body).map { |repo| OpenStruct.new(repo) }
@repos = repos_array.unshift(parse_link_headers(response.headers['link']))
end

# Takes a repo name and returns its information in an openstruct hash
def get_single_repo name
url = "#{Rails.application.secrets.github_api_base_url}repos/unepwcmc/#{name}?#{CLIENT_CREDENTIALS}"
response = HTTParty.get(url, headers: {"User-Agent" => "Labs"})
url = "#{Rails.application.secrets.github_api_base_url}repos/unepwcmc/#{name}"
response = HTTParty.get(url, basic_auth: CLIENT_CREDENTIALS, headers: {"User-Agent" => "Labs"})
OpenStruct.new(JSON.parse(response.body))
end

def get_rails_version repo
url = "#{Rails.application.secrets.github_api_base_url}repos/unepwcmc/#{repo}/contents/Gemfile?#{CLIENT_CREDENTIALS}"
response = HTTParty.get(url, headers: {"User-Agent" => "Labs"})
url = "#{Rails.application.secrets.github_api_base_url}repos/unepwcmc/#{repo}/contents/Gemfile"
response = HTTParty.get(url, basic_auth: CLIENT_CREDENTIALS, headers: {"User-Agent" => "Labs"})
return '' if response.body.match(/Not\sFound/)
content = Base64.decode64(JSON.parse(response.body)["content"])
content.match(/gem 'rails',\s'(~>\s)?(\d\.[\d+|\.]*)'/).try(:captures).try(:last) || ''
end

def get_ruby_version repo
url = "#{Rails.application.secrets.github_api_base_url}repos/unepwcmc/#{repo}/contents/.ruby-version?#{CLIENT_CREDENTIALS}"
response = HTTParty.get(url, headers: {"User-Agent" => "Labs"})
url = "#{Rails.application.secrets.github_api_base_url}repos/unepwcmc/#{repo}/contents/.ruby-version"
response = HTTParty.get(url, basic_auth: CLIENT_CREDENTIALS, headers: {"User-Agent" => "Labs"})
return '' if response.body.match(/Not\sFound/)
Base64.decode64(JSON.parse(response.body)["content"]).delete!("\n")
end
Expand Down