Skip to content

Commit ee92f55

Browse files
authored
Merging pull request #86 from fga-gpp-mds/US22_Manter_Issue
Adding Issue support
2 parents 675024f + cd5928b commit ee92f55

File tree

7 files changed

+322
-0
lines changed

7 files changed

+322
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class IssuesController < ApplicationController
2+
before_action :set_authorization, :set_path
3+
4+
def index
5+
@issues = @client.list_issues(@path)
6+
7+
convert_form_params(@issues)
8+
9+
render json: @form_params
10+
end
11+
12+
def create
13+
@issue = @client.create_issue(@path, issue_params[:name], issue_params[:body], options = { assignee: issue_params[:assignee], labels: issue_params[:labels] })
14+
15+
convert_form_params(@issue)
16+
17+
render json: @form_params, status: :created
18+
end
19+
20+
def update
21+
@issue = @client.update_issue(@path, issue_params[:number], issue_params[:name], issue_params[:body], options = { assignee: issue_params[:assignee], labels: [issue_params[:labels]] })
22+
23+
convert_form_params(@issue)
24+
25+
render json: @form_params
26+
end
27+
28+
def close
29+
@issue = @client.close_issue(@path, issue_params[:number])
30+
31+
render status: 200
32+
end
33+
34+
private
35+
36+
def set_authorization
37+
@current_user = AuthorizeApiRequest.call(request.headers).result
38+
@client = Octokit::Client.new(access_token: @current_user.access_token)
39+
end
40+
41+
def set_path
42+
@project = Project.find(params[:id])
43+
44+
if @project.name.include? "/"
45+
@path = @project.name
46+
else
47+
@name = @client.user.login
48+
@repo = @project.name
49+
@path = @name + "/" + @repo
50+
end
51+
52+
@path
53+
end
54+
55+
def convert_form_params(issue)
56+
@form_params = { issues_infos: [] }
57+
if issue.kind_of?(Array)
58+
@issues.each do |issue|
59+
@form_params[:issues_infos].push(name: issue.title, number: issue.number, body: issue.body)
60+
end
61+
else
62+
@form_params[:issues_infos].push(name: issue.title, number: issue.number, body: issue.body)
63+
end
64+
@form_params
65+
end
66+
67+
def issue_params
68+
params.require(:issue).permit(:name, :body, :assignee, :labels, :number)
69+
end
70+
end

app/models/issue.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Issue < ApplicationRecord
2+
end

config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
get "repos", to: "projects#github_projects_list"
77

8+
get "projects/:id/issues", to: "issues#index"
9+
post "projects/:id/issues", to: "issues#create"
10+
put "projects/:id/issues", to: "issues#update"
11+
patch "projects/:id/issues", to: "issues#update"
12+
delete "projects/:id/issues", to: "issues#close"
13+
814
resources :users, shallow: true do
915
resources :projects do
1016
resources :releases do
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateIssues < ActiveRecord::Migration[5.1]
2+
def change
3+
create_table :issues do |t|
4+
t.string :title
5+
t.text :body
6+
t.string :assignee
7+
t.integer :milestone
8+
t.string :labels
9+
t.string :assignees
10+
11+
t.timestamps
12+
end
13+
end
14+
end
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
require "test_helper"
2+
3+
class IssuesControllerTest < ActionDispatch::IntegrationTest
4+
def setup
5+
@user = User.create(name: "Ronaldo", email: "Ronaldofenomeno@gmail.com", password: "123456789", password_confirmation: "123456789", github: "ronaldobola")
6+
@token = AuthenticateUser.call(@user.email, @user.password)
7+
@project = Project.create(name: "Falko", description: "Descrição do projeto.", user_id: @user.id, is_project_from_github: true)
8+
@project2 = Project.create(name: "Falko", description: "Descrição do projeto.", user_id: @user.id, is_project_from_github: false)
9+
end
10+
11+
test "should see issues if user is loged in" do
12+
13+
mock = Minitest::Mock.new
14+
15+
def mock.user()
16+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
17+
end
18+
19+
def mock.list_issues(name)
20+
[ Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), title: "issue", number: "3", body: "This is a template body") ]
21+
end
22+
23+
24+
Octokit::Client.stub :new, mock do
25+
get "/projects/#{@project.id}/issues", headers: { Authorization: @token.result }
26+
27+
assert response.parsed_body["issues_infos"][0]["name"] == "issue"
28+
assert response.parsed_body["issues_infos"][0]["number"] == "3"
29+
assert response.parsed_body["issues_infos"][0]["body"] == "This is a template body"
30+
assert_response :success
31+
end
32+
end
33+
34+
test "should not see issues if user is not loged in" do
35+
36+
mock = Minitest::Mock.new
37+
38+
def mock.user()
39+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
40+
end
41+
42+
def mock.list_issues(name)
43+
[ Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), title: "issue", number: "3", body: "This is a template body") ]
44+
end
45+
46+
47+
Octokit::Client.stub :new, mock do
48+
get "/projects/#{@project.id}/issues"
49+
50+
assert_response :unauthorized
51+
end
52+
end
53+
54+
test "should create issues if user is loged in" do
55+
56+
mock = Minitest::Mock.new
57+
58+
def mock.user()
59+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
60+
end
61+
62+
def mock.create_issue(path, name, body, options)
63+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), title: name, body: body, number: "3")
64+
end
65+
66+
67+
Octokit::Client.stub :new, mock do
68+
post "/projects/#{@project.id}/issues", headers: { Authorization: @token.result }, params: {
69+
issue: {
70+
"name": "New Issue",
71+
"body": "New Body"
72+
}
73+
}
74+
75+
assert response.parsed_body["issues_infos"][0]["name"] == "New Issue"
76+
assert response.parsed_body["issues_infos"][0]["body"] == "New Body"
77+
assert_response :created
78+
end
79+
end
80+
81+
test "should not create issues if user is not loged in" do
82+
83+
mock = Minitest::Mock.new
84+
85+
def mock.user()
86+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
87+
end
88+
89+
def mock.create_issue(path, name, body, options)
90+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), title: name, body: body, number: "3")
91+
end
92+
93+
94+
Octokit::Client.stub :new, mock do
95+
post "/projects/#{@project.id}/issues", params: {
96+
issue: {
97+
"name": "New Issue",
98+
"body": "New Body"
99+
}
100+
}
101+
102+
assert_response :unauthorized
103+
end
104+
end
105+
106+
test "should update issues if user is loged in" do
107+
108+
mock = Minitest::Mock.new
109+
110+
def mock.user()
111+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
112+
end
113+
114+
def mock.update_issue(path, number, name, body, options)
115+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), title: name, body: body, number: "3")
116+
end
117+
118+
119+
Octokit::Client.stub :new, mock do
120+
put "/projects/#{@project.id}/issues", headers: { Authorization: @token.result }, params: {
121+
issue: {
122+
"number": "3",
123+
"name": "Updated Issue",
124+
"body": "Updated Body"
125+
}
126+
}
127+
assert response.parsed_body["issues_infos"][0]["name"] == "Updated Issue"
128+
assert response.parsed_body["issues_infos"][0]["body"] == "Updated Body"
129+
assert_response :success
130+
end
131+
end
132+
133+
test "should not update issues if user is not loged in" do
134+
135+
mock = Minitest::Mock.new
136+
137+
def mock.user()
138+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
139+
end
140+
141+
def mock.update_issue(path, number, name, body, options)
142+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), title: name, body: body, number: "3")
143+
end
144+
145+
146+
Octokit::Client.stub :new, mock do
147+
put "/projects/#{@project.id}/issues", params: {
148+
issue: {
149+
"number": "3",
150+
"name": "Updated Issue",
151+
"body": "Updated Body"
152+
}
153+
}
154+
155+
assert_response :unauthorized
156+
end
157+
end
158+
159+
test "should close issue if user is loged in" do
160+
161+
mock = Minitest::Mock.new
162+
163+
def mock.user()
164+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
165+
end
166+
167+
def mock.close_issue(path, number)
168+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), number: "3")
169+
end
170+
171+
172+
Octokit::Client.stub :new, mock do
173+
delete "/projects/#{@project.id}/issues", headers: { Authorization: @token.result }, params: {
174+
issue: {
175+
"number": "3"
176+
}
177+
}
178+
179+
assert_response :success
180+
end
181+
end
182+
183+
test "should not close issue if user is not loged in" do
184+
185+
mock = Minitest::Mock.new
186+
187+
def mock.user()
188+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), login: "username_test")
189+
end
190+
191+
def mock.close_issue(path, number)
192+
Sawyer::Resource.new(Sawyer::Agent.new("/issues_test"), number: "3")
193+
end
194+
195+
196+
Octokit::Client.stub :new, mock do
197+
delete "/projects/#{@project.id}/issues", params: {
198+
issue: {
199+
"number": "3"
200+
}
201+
}
202+
203+
assert_response :unauthorized
204+
end
205+
end
206+
end

test/fixtures/issues.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
title: MyString
5+
body: MyText
6+
assignee: MyString
7+
milestone: 1
8+
labels:
9+
assignees:
10+
11+
two:
12+
title: MyString
13+
body: MyText
14+
assignee: MyString
15+
milestone: 1
16+
labels:
17+
assignees:

test/models/issue_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "test_helper"
2+
3+
class IssueTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)