Commit fd1b9e88 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Initial RelatedIssuesController#index

parent 8a5b4e2f
module Projects
class RelatedIssuesController < ApplicationController
include IssuesHelper
before_action :authorize_read_issue!, only: [:index]
def index
render json: serialize_as_json
end
def create
end
private
# TODO: Move to service class
def serialize_as_json
related_issues.map do |related_issue|
referenced_issue = related_issue.related_issue == issue ? related_issue.issue : related_issue.related_issue
{
title: referenced_issue.title,
state: referenced_issue.state,
reference: referenced_issue.to_reference(@project),
path: url_for_issue(referenced_issue.iid, @project, only_path: true),
}
end
end
def related_issues
RelatedIssue
.where("issue_id = #{issue.id} OR related_issue_id = #{issue.id}")
.preload(:related_issue, :issue)
.order(:created_at)
end
def issue
@issue ||=
IssuesFinder.new(current_user, project_id: project.id)
.execute
.where(iid: params[:issue_id])
.first!
end
end
end
......@@ -311,6 +311,8 @@ constraints(ProjectUrlConstrainer.new) do
post :bulk_update
post :export_csv
end
resources :related_issues, only: [:index, :create]
end
resources :project_members, except: [:show, :new, :edit], constraints: { id: /[a-zA-Z.\/0-9_\-#%+]+/ }, concerns: :access_requestable do
......
require 'rails_helper'
describe Projects::RelatedIssuesController, type: :controller do
let(:user) { create :user }
let(:project) { create(:project_empty_repo) }
let(:issue) { create :issue, project: project }
describe "GET #index" do
let(:issue_b) { create :issue, project: project }
let(:issue_c) { create :issue, project: project }
let(:issue_d) { create :issue, project: project }
let!(:related_issue_a) do
create(:related_issue, issue: issue,
related_issue: issue_b,
created_at: 2.days.ago)
end
let!(:related_issue_b) do
create(:related_issue, issue: issue,
related_issue: issue_c,
created_at: 1.day.ago)
end
let!(:related_issue_c) do
create(:related_issue, issue: issue_d,
related_issue: issue,
created_at: Date.today)
end
it "returns related issues JSON" do
sign_in user
project.team << [user, :developer]
get :index, namespace_id: issue.project.namespace,
project_id: issue.project,
issue_id: issue,
format: :json
expect(json_response.size).to eq(3)
expect(json_response[0]).to eq(
{
"title" => issue_b.title,
"state" => issue_b.state,
"reference" => issue_b.to_reference(project),
"path" => "/#{project.full_path}/issues/#{issue_b.iid}"
}
)
expect(json_response[1]).to eq(
{
"title" => issue_c.title,
"state" => issue_c.state,
"reference" => issue_c.to_reference(project),
"path" => "/#{project.full_path}/issues/#{issue_c.iid}"
}
)
expect(json_response[2]).to eq(
{
"title" => issue_d.title,
"state" => issue_d.state,
"reference" => issue_d.to_reference(project),
"path" => "/#{project.full_path}/issues/#{issue_d.iid}"
}
)
end
end
xdescribe "GET #create" do
it "returns http success" do
get :create
expect(response).to have_http_status(:success)
end
end
end
FactoryGirl.define do
factory :related_issue do
issue
related_issue factory: :issue
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment