Skip to content

Commit

Permalink
Tags management and SpinCandidate (#80)
Browse files Browse the repository at this point in the history
* ignoring local version of docs

* adding tests for taggings

* minor improvements

* more fixes

* tagging

* improvements on the way that API is tested

* fixing the error with 401

* spin_candidates

* deleting routes no longer there
  • Loading branch information
chargio authored and aljesusg committed Feb 6, 2018
1 parent 4cd42f0 commit d238c5f
Show file tree
Hide file tree
Showing 38 changed files with 520 additions and 428 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@

.byebug_history
/.yardoc/

/doc/
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Please notice: ManageIQ Exchange consists on several repos, including one for th
* **Do not open up a GitHub issue if the bug is a security vulnerability
in ManageIQ Exchange**, and instead to refer to our [ManageIQ talk](http://talk.manageiq.org/). If you contact us there we will quickly respond.

* **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/miq-consumption/manageiq-exchange/issues).
* **Ensure the bug was not already reported** by searching on GitHub under [Issues](https://github.com/ManageIQ-Exchange/manageiq-exchange/issues).

* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/miq-consumption/manageiq-exchange/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible, and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring.
* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/ManageIQ-Exchange/manageiq-exchange/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible, and a **code sample** or an **executable test case** demonstrating the expected behavior that is not occurring.

* If possible, use the relevant bug report templates to create the issue, although right now we don't have any (so you can create one)

Expand All @@ -19,7 +19,7 @@ Please notice: ManageIQ Exchange consists on several repos, including one for th

* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.

* Before submitting, please read the [Contributing to ManageIQ Exchange](https://github.com/miq-consumption/manageiq-exchange/contributing.md) guide to know more about coding conventions and benchmarks.
* Before submitting, please read the [Contributing to ManageIQ Exchange](https://github.com/ManageIQ-Exchange/manageiq-exchange/contributing.md) guide to know more about coding conventions and benchmarks.

#### **Do you intend to add a new feature or change an existing one?**

Expand Down
4 changes: 2 additions & 2 deletions Developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ If a valid id and token are included, even when creating new session, authentica

Up to date documentation can be found on the following link:

[Developers documentation](http://www.rubydoc.info/github/miq-consumption/manageiq-exchange)
[Developers documentation](http://www.rubydoc.info/github/ManageIQ-Exchange/manageiq-exchange)

For development, you will need a mean to gather a code and token from GitHub. Users are created automatically when they are authenticated

For instance, you can use [manageiq-exchange-web](https://github.com/miq-consumption/manageiq-exchange-web)
For instance, you can use [manageiq-exchange-web](https://github.com/ManageIQ-Exchange/manageiq-exchange-web)

You will also need to define environment variables:
```bash
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
#

gem 'devise', '~> 4.0'
# gem 'tiddle', :git => 'https://github.com/miq-consumption/tiddle.git'
# gem 'tiddle', :git => 'https://github.com/ManageIQ-Exchange/tiddle.git'
gem 'tiddle', '~> 1.0'

# Github tools
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It has been developed using:

## Getting Help

If you find a bug, please report an [Issue](https://github.com/miq-consumption/manageiq-exchange/issues/new)
If you find a bug, please report an [Issue](https://github.com/ManageIQ-Exchange/manageiq-exchange/issues/new)
and see our [contributing guide](CONTRIBUTING.md).

If you have a question, please [post to Stack Overflow](https://stackoverflow.com/questions/tagged/manageiq-exchange).
Expand All @@ -26,11 +26,11 @@ Thanks!

## Documentation

If you're reading this at https://github.com/miq-consumption/manageiq-exchange you are
If you're reading this at https://github.com/ManageIQ-Exchange/manageiq-exchange you are
reading documentation for our `master`, which is not yet released.

- [0.1.0 (master) Documentation](https://github.com/miq-consumption/manageiq-exchange/tree/master)
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/miq-consumption/manageiq-exchange/master)
- [0.1.0 (master) Documentation](https://github.com/ManageIQ-Exchange/manageiq-exchange/tree/master)
- [![API Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/ManageIQ-Exchange/manageiq-exchange/master)
- [Guides](docs)
## ENVIRONMENT VARIABLES

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/v1/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def version
name = pro.delete(:name)
providers[name] = pro if pro[:enabled]
end
render json: { data: { version: Rails.application.config.api_version, providers: providers } } , status: :ok
render json: { data: { version: Rails.application.config.api_version, providers: providers } }, status: :ok
end

def return_response(collection, state = :ok, metadata = {}, role = nil)
Expand Down
91 changes: 91 additions & 0 deletions app/controllers/v1/spin_candidates_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module V1
###
# Spin Candidates controller
# Provides actions on the Spin Candidates
#
##
class SpinCandidatesController < ApiController
before_action :authenticate_user! # Only authenticated values are valid

###
# Index (search: string - optional )
# Provides an index of all spins in the system
# TODO If you provide a search team, it will return those spins mathing the search
# TODO: Add paging
# users/<user_id>/spins Get all spins of a user
# spins?query=<value> Look spins include value in the name
def index
if params[:user_id]
@user = User.find_by_github_login(params[:user_id])
unless @user
return_response status: :no_content
return
end
@spins = SpinCandidate.where(user_id: @user.id) if @user
else
@spins = SpinCandidate.all
end
@spins = @spins.where('name like? or name like?', "%#{params[:query]}%", "%#{params[:query].downcase}%") if params[:query]
if @spins.count.positive?
return_response @spins, :ok, {}
else
render status: :no_content
end
end

###
# Show (id: identification of the spin)
# Provides a view of the spin
# TODO: When authenticated, provide extended info
# users/<user_id>/spins/<spin_id_or_name> Get a specific spin of user
def show
if params[:user_id]
@user = User.find_by_github_login(params[:user_id])
return_response status: :not_found unless @user
@spin = SpinCandidate.find_by(user_id: @user.id, visible: true, id: params[:id]) || Spin.find_by(user_id: @user.id, visible: true, name: params[:id])
else
@spin = SpinCandidate.find_by(id: params[:id], visible: true) || Spin.find_by(name: params[:id], visible: true)
end
unless @spin
render_error_exchange(:spin_not_found, :not_found)
return
end
return_response @spin, :ok, {}
end

###
# Refresh
# Authenticated only
# Refresh the list of providers for the user.
# Connects to github, gets all repos of the user, and search for spins
#
def refresh
user = current_user.admin? ? User.find(params[:user_id]) : current_user
if user.nil?
render json: { error: 'No user found' }, status: :not_found
return
end
job = RefreshSpinCandidatesJob.perform_later(user: user, token: request.headers['HTTP_X_USER_TOKEN'])
render json: { data: job.job_id, metadata: { queue: job.queue_name, priority: job.priority } }, status: :accepted
end

# Validates the SpinCandidate
# @returns true | false
# updates the log
def validate
# Create Spin with metadata
# Validate the Spin
# Write result in log
# Return true or false
false
end

# Publish the SpinCandidate into a Spin
# @returns :ok or :error
def publish
valid?
# If valid create or update the Spin
# If not valid, the log should be updated.
end
end
end
12 changes: 3 additions & 9 deletions app/controllers/v1/spins_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ def index
# TODO: When authenticated, provide extended info
# users/<user_id>/spins/<spin_id_or_name> Get a specific spin of user
def show
return unless check_params_required(:id)
if params[:user_id]
@user = User.find_by_github_login(params[:user_id])
return_response status: :not_found unless @user
Expand All @@ -60,12 +59,7 @@ def show
# Connects to github, gets all repos of the user, and search for spins
#
def refresh
return unless check_params_required(:user_id)
user = if current_user.admin?
User.find(params[:user_id]) || current_user
else
current_user
end
user = current_user.admin? ? User.find(params[:user_id]) : current_user
if user.nil?
render json: { error: 'No user found' }, status: :not_found
return
Expand All @@ -78,7 +72,7 @@ def visible
return unless check_params_required(:spin_id, :flag)
spin = Spin.find_by(id:params[:spin_id])
if spin
if spin.spin_of?(current_user)
if spin.belongs_to?(current_user)
if spin.visible_to(true?(params[:flag]))
return_response spin, :accepted, {}
else
Expand All @@ -96,7 +90,7 @@ def publish
return unless check_params_required(:spin_id, :flag)
spin = Spin.find_by(id:params[:spin_id])
if spin
if spin.spin_of?(current_user)
if spin.belongs_to?(current_user)
if spin.publish_to( @current_user, true?(params[:flag]))
return_response spin, :accepted, {}
else
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/v1/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UsersController < ApiController
# users?query=<value> get users login include value
def index
logger.debug 'Providing all users'
@users = User.all # TODO: Pagination
@users = User.all # TODO: Pagination
@users = @users.where('github_login like? or github_login like?', "%#{params[:query]}%", "%#{params[:query].downcase}%") if params[:query]

total_users = @users.count
Expand All @@ -37,7 +37,7 @@ def show
logger.debug "Looking for user with github_login #{params[:id]}"
@user = User.find_by(id: params[:id]) || User.find_by(github_login: params[:id])
if @user
return_response @user, :ok
return_response @user, :ok
else
render_error_exchange(:user_not_found, :not_found, { username: params[:id]})
end
Expand Down
49 changes: 49 additions & 0 deletions app/jobs/refresh_spin_candidates_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
###
# __Background Job to refresh user Spin Candidates__
# Will refresh all repos for a user and analyze them
# It will update them if found adfads
#
# @param user: User
# @return boolean
#
class RefreshSpinCandidatesJob < ApplicationJob

queue_as :default

def perform(user:, token:)
logger.info "Refresh Spin Candidates Job for user: #{user.id}"
# Get the client using the application id (only public information)
#
client = Providers::BaseManager.new(user.authentication_tokens.first.provider).get_connector
# Find the spins in the database, store them as an array
user_spin_candidates = user.spin_candidates
app_token = Tiddle::TokenIssuer.build.find_token(user, token)
client.github_access.access_token = app_token.github_token
user_spin_candidates_list = user_spin_candidates.map(&:id)
# Get the list of repos in GitHub (they use the same id in github and local) for the user
repos = client.repos(user: user, github_token: app_token.github_token)
repos_list = repos.map(&:id)
# List of deleted repos
deleted_repos = user_spin_candidates_list - repos_list
# Delete deleted repos
Spin.where(id: deleted_repos).destroy_all
# For each repo:
# Verify that the repo has the file marking is as a candidate
# See if the repo is already in the database
# If it is in the database, update it
# If it is not in the database, add it to the database
repos.each do |repo|
spin_candidate = user_spin_candidates_list.include?(repo.id) ?
SpinCandidate.find_by(id: repo.id) :
SpinCandidate.new(id: repo.id,
full_name: repo.full_name,
user: user,
validation_log: "Pending validation")
if (spin_candidate.is_candidate? client: client)
spin_candidate.save
else
spin_candidate.destroy unless spin_candidate.new_record?
end
end
end
end
7 changes: 5 additions & 2 deletions app/jobs/refresh_spins_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class RefreshSpinsJob < ApplicationJob
def perform(user:, token:)
logger.info "Refresh Spins Job with user: #{user.id}"
# Get the client using the application id (only public information)
client = Provider.new('github_manager').get_connector
#
client = Providers::BaseManager.new(user.authentication_tokens.first.provider).get_connector
# Find the spins in the database, store them as an array
user_spins = user.spins
app_token = Tiddle::TokenIssuer.build.find_token(user, token)
client.github_access.access_token = app_token.github_token
user_spins_list = user_spins.map(&:id)
# Get the list of repos in GitHub (they use the same id in github and local) for the user
repos = client.repos(user: user, github_token: app_token.github_token)
Expand All @@ -32,8 +34,9 @@ def perform(user:, token:)
# If it is not in the database, add it to the database
repos.each do |repo|
spin = user_spins_list.include?(repo.id) ? Spin.find_by(id: repo.id) : Spin.new(id: repo.id, first_import: DateTime.current)
if (candidate_spin?(repo.full_name))
if (spin.acceptable? user)
releases = client.releases(repo.full_name) || []
_metadata_raw, metadata_json = client.metadata(repo.full_name) || {}
spin.update(name: repo.name,
full_name: repo.full_name,
description: repo.description,
Expand Down
16 changes: 16 additions & 0 deletions app/models/providers/github_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,21 @@ def repos(user:, github_token:)
rescue Octokit::NotFound => e
ErrorExchange.new(:github_octokit_not_found, nil, {error: e.to_json})
end

#
# Returns if the repo is a candidate to be a spin
# @param full_name [String] Full name of repo
# @returns true | false
#
def candidate_spin?(full_name)
raise Octokit::NotFound if @github_access.nil?
begin
# GitHub returns false when there is no content, or the content otherwise
not (@github_access.contents(full_name, path: '/.manageiq-spin', accept: 'application/vnd.github.raw') == false)
rescue Octokit::NotFound
return false
end
end

end
end
Loading

0 comments on commit d238c5f

Please sign in to comment.