gitlab_spec.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'integration/git_integration_base_examples'
  4. RSpec.describe GitLab, integration: true, required_envs: %w[GITLAB_ENDPOINT GITLAB_APITOKEN GITLAB_ISSUE_LINK] do
  5. let(:invalid_issue_url) { "https://#{URI.parse(ENV['GITLAB_ISSUE_LINK']).host}/group/project/-/issues/1" }
  6. let(:issue_data) do
  7. {
  8. id: '1',
  9. title: 'Example issue',
  10. url: ENV['GITLAB_ISSUE_LINK'],
  11. icon_state: 'open',
  12. milestone: 'important milestone',
  13. assignees: ['zammad-robot'],
  14. labels: [
  15. {
  16. color: '#FF0000',
  17. text_color: '#FFFFFF',
  18. title: 'critical'
  19. },
  20. {
  21. color: '#0033CC',
  22. text_color: '#FFFFFF',
  23. title: 'label1'
  24. },
  25. {
  26. color: '#D1D100',
  27. text_color: '#1F1E24',
  28. title: 'special'
  29. }
  30. ],
  31. }
  32. end
  33. let(:instance) { described_class.new(endpoint: ENV['GITLAB_ENDPOINT'], api_token: ENV['GITLAB_APITOKEN']) }
  34. it_behaves_like 'Git Integration Base', issue_type: :gitlab
  35. describe '#issues_by_urls' do
  36. let(:result) { instance.issues_by_urls([ issue_url ]) }
  37. context 'when issue exists' do
  38. let(:issue_url) { ENV['GITLAB_ISSUE_LINK'] }
  39. it 'returns a issues list' do
  40. expect(result[:issues].size).to eq(1)
  41. end
  42. it 'returns issue data in the issues list' do
  43. expect(result[:issues][0]).to eq(issue_data)
  44. end
  45. it 'returns no url replacements' do
  46. expect(result[:url_replacements].size).to eq(0)
  47. end
  48. end
  49. context 'when issue does not exists' do
  50. let(:issue_url) { invalid_issue_url }
  51. it 'returns no issues' do
  52. expect(result[:issues].size).to eq(0)
  53. end
  54. it 'returns no url replacements' do
  55. expect(result[:url_replacements].size).to eq(0)
  56. end
  57. end
  58. end
  59. describe '#issue_by_url' do
  60. let(:result) { instance.issue_by_url(issue_url) }
  61. context 'when issue exists' do
  62. let(:issue_url) { ENV['GITLAB_ISSUE_LINK'] }
  63. it 'returns issue data' do
  64. expect(result).to eq(issue_data)
  65. end
  66. end
  67. context 'when issue does not exists' do
  68. let(:issue_url) { invalid_issue_url }
  69. it 'returns nil' do
  70. expect(result).to be_nil
  71. end
  72. end
  73. end
  74. describe '#variables' do
  75. describe 'Zammad ignores relative GitLab URLs #3830' do
  76. let(:endpoint) { ENV['GITLAB_ENDPOINT'].sub('api/graphql', 'subfolder/api/graphql') }
  77. let(:instance) { described_class.new(endpoint:, api_token: ENV['GITLAB_APITOKEN']) }
  78. let(:issue_url) { "https://#{URI.parse(ENV['GITLAB_ISSUE_LINK']).host}/subfolder/group/project/-/issues/1" }
  79. let(:linked_issue) { GitLab::LinkedIssue.new(instance.client) }
  80. it 'does remove the subfolder from the fullpath to get the issue correctly' do
  81. expect(linked_issue.send(:variables, issue_url)[:fullpath]).to eq('group/project')
  82. end
  83. end
  84. end
  85. end