Commit 93bf7173 authored by Alexandru Croitor's avatar Alexandru Croitor

Expose all Jira projects endpoint through a GraphQL endpoint

Jira Server API does not have a paginated projects endpoint,
so we will cover that with a new `all_projects` GraphQL endpoint.

re https://gitlab.com/gitlab-org/gitlab/-/issues/220232
parent 811599d1
......@@ -10,6 +10,12 @@ module Types
authorize :admin_project
field :all_projects,
[Types::Projects::Services::JiraProjectType],
null: true,
connection: false,
description: 'List of all Jira projects fetched through Jira REST API. Latest Jira Server API version ([8.9.0](https://docs.atlassian.com/software/jira/docs/api/REST/8.9.0/)) compatible.'
field :projects,
Types::Projects::Services::JiraProjectType.connection_type,
null: true,
......@@ -17,6 +23,13 @@ module Types
extensions: [Gitlab::Graphql::Extensions::ExternallyPaginatedArrayExtension],
description: 'List of Jira projects fetched through Jira REST API',
resolver: Resolvers::Projects::JiraProjectsResolver
def all_projects
raise Gitlab::Graphql::Errors::BaseError, _('Jira service not configured.') unless object&.active?
raise Gitlab::Graphql::Errors::BaseError, _('Unable to connect to the Jira instance. Please check your Jira integration configuration.') unless object.test(nil)[:success]
object.client.Project.all
end
end
end
end
......
---
title: Expose all Jira projects endpoint through a GraphQL
merge_request: 33861
author:
type: added
......@@ -6258,6 +6258,12 @@ type JiraService implements Service {
"""
active: Boolean
"""
List of all Jira projects fetched through Jira REST API. Latest Jira Server API version
([8.9.0](https://docs.atlassian.com/software/jira/docs/api/REST/8.9.0/)) compatible.
"""
allProjects: [JiraProject!]
"""
List of Jira projects fetched through Jira REST API
"""
......
......@@ -17302,6 +17302,28 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "allProjects",
"description": "List of all Jira projects fetched through Jira REST API. Latest Jira Server API version ([8.9.0](https://docs.atlassian.com/software/jira/docs/api/REST/8.9.0/)) compatible.",
"args": [
],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "JiraProject",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "projects",
"description": "List of Jira projects fetched through Jira REST API",
......@@ -925,6 +925,7 @@ Autogenerated return type of JiraImportUsers
| Name | Type | Description |
| --- | ---- | ---------- |
| `active` | Boolean | Indicates if the service is active |
| `allProjects` | JiraProject! => Array | List of all Jira projects fetched through Jira REST API. Latest Jira Server API version ([8.9.0](https://docs.atlassian.com/software/jira/docs/api/REST/8.9.0/)) compatible. |
| `projects` | JiraProjectConnection | List of Jira projects fetched through Jira REST API |
| `type` | String | Class name of the service |
......
......@@ -6,7 +6,7 @@ describe GitlabSchema.types['JiraService'] do
specify { expect(described_class.graphql_name).to eq('JiraService') }
it 'has basic expected fields' do
expect(described_class).to have_graphql_fields(:type, :active, :projects)
expect(described_class).to have_graphql_fields(:type, :active, :projects, :all_projects)
end
specify { expect(described_class).to require_graphql_authorizations(:admin_project) }
......
# frozen_string_literal: true
require 'spec_helper'
describe 'query Jira projects' do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
include_context 'jira projects request context'
let(:services) { graphql_data_at(:project, :services, :edges) }
let(:all_jira_projects) { services.first.dig('node', 'allProjects') }
let(:query) do
%(
query {
project(fullPath: "#{project.full_path}") {
services(type: JIRA_SERVICE) {
edges {
node {
... on JiraService {
allProjects {
key
name
projectId
}
}
}
}
}
}
}
)
end
context 'when user does not have access' do
it_behaves_like 'unauthorized users cannot read services'
end
context 'when user can access project services' do
before do
project.add_maintainer(current_user)
end
context 'when jira service enabled and working' do
before do
post_graphql(query, current_user: current_user)
end
it_behaves_like 'a working graphql query'
it 'returns list of all jira projects' do
project_keys = all_jira_projects.map { |jp| jp['key'] }
project_names = all_jira_projects.map { |jp| jp['name'] }
project_ids = all_jira_projects.map { |jp| jp['projectId'] }
expect(all_jira_projects.size).to eq(2)
expect(project_keys).to eq(%w(EX ABC))
expect(project_names).to eq(%w(Example Alphabetical))
expect(project_ids).to eq([10000, 10001])
end
end
context 'when connection to jira fails' do
before do
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo').to_raise(Errno::ECONNREFUSED)
end
it 'returns error', :aggregate_failures do
post_graphql(query, current_user: current_user)
expect(all_jira_projects).to be_nil
expect(graphql_errors).to include(a_hash_including('message' => 'Unable to connect to the Jira instance. Please check your Jira integration configuration.'))
end
end
context 'when jira service is not active' do
before do
jira_service.update!(active: false)
end
it 'returns error', :aggregate_failures do
post_graphql(query, current_user: current_user)
expect(all_jira_projects).to be_nil
expect(graphql_errors).to include(a_hash_including('message' => 'Jira service not configured.'))
end
end
end
end
......@@ -74,6 +74,48 @@ shared_context 'jira projects request context' do
}'
end
let_it_be(:all_jira_projects_json) do
'[{
"expand": "description,lead,issueTypes,url,projectKeys,permissions,insight",
"self": "https://gitlab-jira.atlassian.net/rest/api/2/project/10000",
"id": "10000",
"key": "EX",
"name": "Example",
"avatarUrls": {
"48x48": "https://gitlab-jira.atlassian.net/secure/projectavatar?pid=10000&avatarId=10425",
"24x24": "https://gitlab-jira.atlassian.net/secure/projectavatar?size=small&s=small&pid=10000&avatarId=10425",
"16x16": "https://gitlab-jira.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10000&avatarId=10425",
"32x32": "https://gitlab-jira.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10000&avatarId=10425"
},
"projectTypeKey": "software",
"simplified": false,
"style": "classic",
"isPrivate": false,
"properties": {
}
},
{
"expand": "description,lead,issueTypes,url,projectKeys,permissions,insight",
"self": "https://gitlab-jira.atlassian.net/rest/api/2/project/10001",
"id": "10001",
"key": "ABC",
"name": "Alphabetical",
"avatarUrls": {
"48x48": "https://gitlab-jira.atlassian.net/secure/projectavatar?pid=10001&avatarId=10405",
"24x24": "https://gitlab-jira.atlassian.net/secure/projectavatar?size=small&s=small&pid=10001&avatarId=10405",
"16x16": "https://gitlab-jira.atlassian.net/secure/projectavatar?size=xsmall&s=xsmall&pid=10001&avatarId=10405",
"32x32": "https://gitlab-jira.atlassian.net/secure/projectavatar?size=medium&s=medium&pid=10001&avatarId=10405"
},
"projectTypeKey": "software",
"simplified": true,
"style": "next-gen",
"isPrivate": false,
"properties": {
},
"entityId": "14935009-f8aa-481e-94bc-f7251f320b0e",
"uuid": "14935009-f8aa-481e-94bc-f7251f320b0e"
}]'
end
let_it_be(:empty_jira_projects_json) do
'{
"self": "https://your-domain.atlassian.net/rest/api/2/project/search?startAt=0&maxResults=2",
......@@ -86,10 +128,32 @@ shared_context 'jira projects request context' do
}'
end
let(:server_info_json) do
'{
"baseUrl": "https://gitlab-jira.atlassian.net",
"version": "1001.0.0-SNAPSHOT",
"versionNumbers": [
1001,
0,
0
],
"deploymentType": "Cloud",
"buildNumber": 100128,
"buildDate": "2020-06-03T01:58:44.000-0700",
"serverTime": "2020-06-04T06:15:13.686-0700",
"scmInfo": "e736ab140ddb281c7cf5dcf9062c9ce2c08b3c1c",
"serverTitle": "Jira",
"defaultLocale": {
"locale": "en_US"
}
}'
end
let(:test_url) { "#{url}/rest/api/2/project/search?maxResults=50&query=&startAt=0" }
let(:start_at_20_url) { "#{url}/rest/api/2/project/search?maxResults=50&query=&startAt=20" }
let(:start_at_1_url) { "#{url}/rest/api/2/project/search?maxResults=50&query=&startAt=1" }
let(:max_results_1_url) { "#{url}/rest/api/2/project/search?maxResults=1&query=&startAt=0" }
let(:all_projects_url) { "#{url}/rest/api/2/project" }
before do
WebMock.stub_request(:get, test_url).with(basic_auth: [username, password])
......@@ -100,5 +164,9 @@ shared_context 'jira projects request context' do
.to_return(body: jira_projects_json, headers: { "Content-Type": "application/json" })
WebMock.stub_request(:get, max_results_1_url).with(basic_auth: [username, password])
.to_return(body: jira_projects_json, headers: { "Content-Type": "application/json" })
WebMock.stub_request(:get, all_projects_url).with(basic_auth: [username, password])
.to_return(body: all_jira_projects_json, headers: { "Content-Type": "application/json" })
WebMock.stub_request(:get, 'https://jira.example.com/rest/api/2/serverInfo')
.to_return(status: 200, body: server_info_json, headers: {})
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