Skip to content

Commit 7518093

Browse files
authored
Merging pull request #99 from fga-gpp-mds/us07_visualizar_gpa
2 parents 320fe9e + 447d51e commit 7518093

File tree

5 files changed

+52
-3
lines changed

5 files changed

+52
-3
lines changed

app/controllers/projects_controller.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require "rest-client"
12
class ProjectsController < ApplicationController
23
include ValidationsHelper
34

@@ -16,6 +17,16 @@ def index
1617
render json: @projects
1718
end
1819

20+
def get_gpa
21+
project = Project.find(params[:id])
22+
github_slug = project.github_slug
23+
result = RestClient.get("http://api.codeclimate.com/v1/repos?github_slug=#{github_slug}")
24+
result_json = JSON.parse(result)
25+
score = result_json["data"][0]["attributes"]["score"]
26+
27+
render json: score
28+
end
29+
1930
def github_projects_list
2031
@current_user = AuthorizeApiRequest.call(request.headers).result
2132
@client = Octokit::Client.new(access_token: @current_user.access_token)
@@ -55,6 +66,8 @@ def create
5566
@project = Project.create(project_params)
5667
@project.user_id = @current_user.id
5768

69+
puts @project.github_slug
70+
5871
if @project.save
5972
render json: @project, status: :created
6073
else
@@ -80,6 +93,6 @@ def set_project
8093
end
8194

8295
def project_params
83-
params.require(:project).permit(:name, :description, :is_project_from_github, :user_id)
96+
params.require(:project).permit(:name, :description, :user_id, :is_project_from_github, :github_slug)
8497
end
8598
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
post "request_github_token", to: "users#request_github_token"
55

66
get "repos", to: "projects#github_projects_list"
7+
get "projects/:id/gpa", to: "projects#get_gpa"
78

89
get "projects/:id/issues", to: "issues#index"
910
post "projects/:id/issues", to: "issues#create"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddGithubSlugToProject < ActiveRecord::Migration[5.1]
2+
def change
3+
add_column :projects, :github_slug, :string
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13+
1314
ActiveRecord::Schema.define(version: 20171107172605) do
1415

1516
# These are extensions that must be enabled in order to support this database
@@ -21,6 +22,7 @@
2122
t.datetime "created_at", null: false
2223
t.datetime "updated_at", null: false
2324
t.bigint "user_id"
25+
t.string "github_slug"
2426
t.boolean "is_project_from_github"
2527
t.index ["user_id"], name: "index_projects_on_user_id"
2628
end

test/controllers/projects_controller_test.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ def setup
1414
"name": "Falko",
1515
"description": "Some project description 1.",
1616
"user_id": @user.id,
17-
"is_project_from_github": true
17+
"is_project_from_github": true,
18+
"github_slug": "alaxalves/Falko"
1819
)
1920

2021
@project2 = Project.create(
2122
"name": "Falko",
2223
"description": "Some project description 2.",
2324
"user_id": @user.id,
24-
"is_project_from_github": false
25+
"is_project_from_github": false,
26+
"github_slug": "alaxalves/LabBancos"
2527
)
2628

2729
@token = AuthenticateUser.call(@user.email, @user.password)
@@ -221,6 +223,32 @@ def mock.organization_repositories(login)
221223
end
222224
end
223225

226+
test "should receive an numeric score" do
227+
@token = AuthenticateUser.call(@user.email, @user.password)
228+
codeclimate_response = '{
229+
"data": [{
230+
"id": "696a76232df2736347000001",
231+
"type": "repos",
232+
"attributes": {
233+
"analysis_version": 3385,
234+
"badge_token": "16096d266f46b7c68dd4",
235+
"branch": "master",
236+
"created_at": "2017-07-15T20:08:03.732Z",
237+
"github_slug": "twinpeaks\/ranchorosa",
238+
"human_name": "ranchorosa",
239+
"last_activity_at": "2017-07-15T20:09:41.846Z",
240+
"score": 2.92
241+
}
242+
}]
243+
}'
244+
245+
RestClient.stub :get, codeclimate_response do
246+
get "/projects/#{@project.id}/gpa", headers: { Authorization: @token.result }
247+
assert_response :success
248+
assert response.parsed_body == 2.92
249+
end
250+
end
251+
224252
test "should not import a project from github if the is_project_from_github is invalid" do
225253
post "/users/#{@user.id}/projects", params: {
226254
"project": {

0 commit comments

Comments
 (0)