diff --git a/app/controllers/github_repos_controller.rb b/app/controllers/github_repos_controller.rb index 2efbe5e97..e5d3084ba 100644 --- a/app/controllers/github_repos_controller.rb +++ b/app/controllers/github_repos_controller.rb @@ -1,14 +1,23 @@ class GithubReposController < ApplicationController def create @client = create_octokit_client - @repo = GithubRepo.find_or_create(github_repo_params[:url], fetched_repo_params) - redirect_to "/settings/integrations", notice: "GitHub repo added" + @repo = GithubRepo.find_or_create(fetched_repo_params) + if @repo.valid? + redirect_to "/settings/integrations", notice: "GitHub repo added" + else + redirect_to "/settings/integrations", + error: "There was an error adding your Github repo" + end end def update @repo = GithubRepo.find(params[:id]) - @repo.update(featured: false) - redirect_to "/settings/integrations", notice: "GitHub repo added" + if @repo.update(featured: false) + redirect_to "/settings/integrations", notice: "GitHub repo added" + else + redirect_to "/settings/integrations", + error: "There was an error removing your Github repo" + end end private diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 58c020bc5..0dfb697a5 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -29,14 +29,14 @@ class UsersController < ApplicationController @user = current_user @tab_list = tab_list(@user) @tab = params["user"]["tab"] || "profile" - if @user.update(user_params) - RssReader.new.delay.fetch_user(@user) if @user.feed_url.present? - notice = "Your profile was successfully updated." - follow_hiring_tag(@user) - redirect_to "/settings/#{@tab}", notice: notice - else - render :edit - end + if @user.update(user_params) + RssReader.new.delay.fetch_user(@user) if @user.feed_url.present? + notice = "Your profile was successfully updated." + follow_hiring_tag(@user) + redirect_to "/settings/#{@tab}", notice: notice + else + render :edit + end end def onboarding_update diff --git a/app/models/github_repo.rb b/app/models/github_repo.rb index e63710992..3f7895402 100644 --- a/app/models/github_repo.rb +++ b/app/models/github_repo.rb @@ -1,31 +1,37 @@ class GithubRepo < ApplicationRecord belongs_to :user - validates :name, :url, presence: true + validates :name, :url, :github_id_code, presence: true validates :url, uniqueness: true validates :github_id_code, uniqueness: true after_save :clear_caches before_destroy :clear_caches - def self.find_or_create(github_url, params = {}) - repo = where(url: github_url).first_or_initialize + def self.find_or_create(params) + repo = where(github_id_code: params[:github_id_code]).or(where(url: params[:url])). + first_or_initialize repo.update(params) repo end def self.update_to_latest + # TODO: this is a very intensive process. Definitely not a good approach on the long run. where("updated_at < ?", 1.day.ago).find_each do |repo| user_token = User.find_by_id(repo.user_id).identities.where(provider: "github").last.token client = Octokit::Client.new(access_token: user_token) - fetched_repo = client.repositories.select do |fresh_repo| - if repo[:github_id_code] - fresh_repo.id == repo[:github_id_code] - else - fresh_repo.html_url == repo[:url] - end - end.first - repo.update( + + fetched_repo = if repo[:github_id_code] + client.repositories.select do |fresh_repo| + fresh_repo.id == repo[:github_id_code] + end.first + else + client.repositories.select do |fresh_repo| + fresh_repo.html_url == repo[:url] + end.first + end + + repo.update!( github_id_code: fetched_repo.id, name: fetched_repo.name, description: fetched_repo.description, diff --git a/app/views/users/_integrations.html.erb b/app/views/users/_integrations.html.erb index fad2060e8..978e07d64 100644 --- a/app/views/users/_integrations.html.erb +++ b/app/views/users/_integrations.html.erb @@ -2,7 +2,7 @@ <% if @client %>