-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tags management and SpinCandidate (#80)
* 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
Showing
38 changed files
with
520 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ | |
|
||
.byebug_history | ||
/.yardoc/ | ||
|
||
/doc/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.