gitlab_spec.rb 2.6 KB

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