docbrown/app/controllers/github_repos_controller.rb
Andy Zhao cd9f6d9ada [Done] Add BDEFGI Policies and Specs (#487)
* Add comment for public controllers

* Add block policy and specs

* Add policy for authorizing dashboard

* Add follow policy and specs

* Refactor a tiny bit

* Prevent banned users from following anything

* Add a note for email sub controller about auth

* Add image upload policies

* Add policy for github repos

* Fix typo and use correct github repo variable

* Fix image uploader and use regular params

* Add authenticate_user before action back in

* Rename test

* Update slack bot message formatting and fix reported URL
2018-06-26 09:23:07 -04:00

55 lines
1.6 KiB
Ruby

class GithubReposController < ApplicationController
after_action :verify_authorized
def create
authorize GithubRepo
@client = create_octokit_client
@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])
authorize @repo
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
def create_octokit_client
current_user_token = current_user.identities.where(provider: "github").last.token
client = Octokit::Client.new(access_token: current_user_token)
client.repositories.sort_by!(&:name) if client
client
end
def fetched_repo_params
fetched_repo = @client.repositories.select do |repo|
repo.id == permitted_attributes(GithubRepo)[:github_id_code].to_i
end.first
{
github_id_code: fetched_repo.id,
user_id: current_user.id,
name: fetched_repo.name,
description: fetched_repo.description,
language: fetched_repo.language,
fork: fetched_repo.fork,
url: fetched_repo.html_url,
bytes_size: fetched_repo.size,
watchers_count: fetched_repo.watchers,
stargazers_count: fetched_repo.stargazers_count,
featured: true,
info_hash: fetched_repo.to_hash,
}
end
end