debian-mirror-gitlab/spec/models/integrations/chat_message/pipeline_message_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

381 lines
13 KiB
Ruby
Raw Normal View History

2019-07-07 11:18:12 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
require 'spec_helper'
2021-06-08 01:23:25 +05:30
RSpec.describe Integrations::ChatMessage::PipelineMessage do
2017-08-17 22:00:37 +05:30
subject { described_class.new(args) }
let(:args) do
{
object_attributes: {
id: 123,
sha: '97de212e80737a608d939f648d959671fb0a0142',
tag: false,
ref: 'develop',
2019-10-12 21:52:04 +05:30
status: 'success',
detailed_status: nil,
duration: 7210,
finished_at: "2019-05-27 11:56:36 -0300"
2017-08-17 22:00:37 +05:30
},
project: {
2019-10-12 21:52:04 +05:30
id: 234,
name: "project_name",
path_with_namespace: 'group/project_name',
web_url: 'http://example.gitlab.com',
avatar_url: 'http://example.com/project_avatar'
},
user: {
id: 345,
name: "The Hacker",
username: "hacker",
email: "hacker@example.gitlab.com",
avatar_url: "http://example.com/avatar"
},
commit: {
id: "abcdef"
2017-08-17 22:00:37 +05:30
},
2019-10-12 21:52:04 +05:30
builds: nil,
markdown: false
2017-08-17 22:00:37 +05:30
}
end
2019-10-12 21:52:04 +05:30
let(:has_yaml_errors) { false }
2022-01-12 12:59:36 +05:30
it_behaves_like Integrations::ChatMessage
2019-10-12 21:52:04 +05:30
before do
test_commit = double("A test commit", committer: args[:user], title: "A test commit message")
test_project = double("A test project", commit_by: test_commit, name: args[:project][:name], web_url: args[:project][:web_url])
allow(test_project).to receive(:avatar_url).with(no_args).and_return("/avatar")
allow(test_project).to receive(:avatar_url).with(only_path: false).and_return(args[:project][:avatar_url])
allow(Project).to receive(:find) { test_project }
test_pipeline = double("A test pipeline", has_yaml_errors?: has_yaml_errors,
yaml_errors: "yaml error description here")
allow(Ci::Pipeline).to receive(:find) { test_pipeline }
allow(Gitlab::UrlBuilder).to receive(:build).with(test_commit).and_return("http://example.com/commit")
allow(Gitlab::UrlBuilder).to receive(:build).with(args[:user]).and_return("http://example.gitlab.com/hacker")
end
2020-05-24 23:13:21 +05:30
it 'returns an empty pretext' do
expect(subject.pretext).to be_empty
end
it "returns the pipeline summary in the activity's title" do
expect(subject.activity[:title]).to eq(
2020-06-23 00:09:42 +05:30
"Pipeline [#123](http://example.gitlab.com/-/pipelines/123)" \
" of branch [develop](http://example.gitlab.com/-/commits/develop)" \
2020-05-24 23:13:21 +05:30
" by The Hacker (hacker) has passed"
)
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context "when the pipeline failed" do
before do
args[:object_attributes][:status] = 'failed'
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns the summary with a 'failed' status" do
2019-10-12 21:52:04 +05:30
expect(subject.activity[:title]).to eq(
2020-06-23 00:09:42 +05:30
"Pipeline [#123](http://example.gitlab.com/-/pipelines/123)" \
" of branch [develop](http://example.gitlab.com/-/commits/develop)" \
2020-05-24 23:13:21 +05:30
" by The Hacker (hacker) has failed"
2019-10-12 21:52:04 +05:30
)
end
2020-05-24 23:13:21 +05:30
end
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
context "when the pipeline passed with warnings" do
before do
args[:object_attributes][:detailed_status] = 'passed with warnings'
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns the summary with a 'passed with warnings' status" do
expect(subject.activity[:title]).to eq(
2020-06-23 00:09:42 +05:30
"Pipeline [#123](http://example.gitlab.com/-/pipelines/123)" \
" of branch [develop](http://example.gitlab.com/-/commits/develop)" \
2020-05-24 23:13:21 +05:30
" by The Hacker (hacker) has passed with warnings"
2019-10-12 21:52:04 +05:30
)
end
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
context 'when no user is provided because the pipeline was triggered by the API' do
2017-08-17 22:00:37 +05:30
before do
2020-05-24 23:13:21 +05:30
args[:user] = nil
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns the summary with 'API' as the username" do
2019-10-12 21:52:04 +05:30
expect(subject.activity[:title]).to eq(
2020-06-23 00:09:42 +05:30
"Pipeline [#123](http://example.gitlab.com/-/pipelines/123)" \
" of branch [develop](http://example.gitlab.com/-/commits/develop)" \
2020-05-24 23:13:21 +05:30
" by API has passed"
2019-10-12 21:52:04 +05:30
)
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns a link to the project in the activity's subtitle" do
expect(subject.activity[:subtitle]).to eq("in [project_name](http://example.gitlab.com)")
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the build duration in the activity's text property" do
expect(subject.activity[:text]).to eq("in 02:00:10")
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the user's avatar image URL in the activity's image property" do
expect(subject.activity[:image]).to eq("http://example.com/avatar")
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context 'when the user does not have an avatar' do
before do
args[:user][:avatar_url] = nil
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns an empty string in the activity's image property" do
expect(subject.activity[:image]).to be_empty
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the pipeline summary as the attachment's fallback property" do
expect(subject.attachments.first[:fallback]).to eq(
"<http://example.gitlab.com|project_name>:" \
2020-06-23 00:09:42 +05:30
" Pipeline <http://example.gitlab.com/-/pipelines/123|#123>" \
" of branch <http://example.gitlab.com/-/commits/develop|develop>" \
2020-05-24 23:13:21 +05:30
" by The Hacker (hacker) has passed in 02:00:10"
)
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns 'good' as the attachment's color property" do
expect(subject.attachments.first[:color]).to eq('good')
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context "when the pipeline failed" do
before do
args[:object_attributes][:status] = 'failed'
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns 'danger' as the attachment's color property" do
expect(subject.attachments.first[:color]).to eq('danger')
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context "when the pipeline passed with warnings" do
before do
args[:object_attributes][:detailed_status] = 'passed with warnings'
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns 'warning' as the attachment's color property" do
expect(subject.attachments.first[:color]).to eq('warning')
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the committer's name and username as the attachment's author_name property" do
expect(subject.attachments.first[:author_name]).to eq('The Hacker (hacker)')
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the committer's avatar URL as the attachment's author_icon property" do
expect(subject.attachments.first[:author_icon]).to eq('http://example.com/avatar')
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the committer's GitLab profile URL as the attachment's author_link property" do
expect(subject.attachments.first[:author_link]).to eq('http://example.gitlab.com/hacker')
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context 'when no user is provided because the pipeline was triggered by the API' do
before do
args[:user] = nil
2017-08-17 22:00:37 +05:30
end
2019-10-12 21:52:04 +05:30
it "returns the committer's name and username as the attachment's author_name property" do
2020-05-24 23:13:21 +05:30
expect(subject.attachments.first[:author_name]).to eq('API')
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns nil as the attachment's author_icon property" do
expect(subject.attachments.first[:author_icon]).to be_nil
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns nil as the attachment's author_link property" do
expect(subject.attachments.first[:author_link]).to be_nil
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the pipeline ID, status, and duration as the attachment's title property" do
expect(subject.attachments.first[:title]).to eq("Pipeline #123 has passed in 02:00:10")
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the pipeline URL as the attachment's title_link property" do
2020-06-23 00:09:42 +05:30
expect(subject.attachments.first[:title_link]).to eq("http://example.gitlab.com/-/pipelines/123")
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns two attachment fields" do
expect(subject.attachments.first[:fields].count).to eq(2)
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the commit message as the attachment's second field property" do
expect(subject.attachments.first[:fields][0]).to eq({
title: "Branch",
2020-06-23 00:09:42 +05:30
value: "<http://example.gitlab.com/-/commits/develop|develop>",
2020-05-24 23:13:21 +05:30
short: true
})
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the ref name and link as the attachment's second field property" do
expect(subject.attachments.first[:fields][1]).to eq({
title: "Commit",
value: "<http://example.com/commit|A test commit message>",
short: true
})
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context "when a job in the pipeline fails" do
before do
args[:builds] = [
{ id: 1, name: "rspec", status: "failed", stage: "test" },
{ id: 2, name: "karma", status: "success", stage: "test" }
]
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns four attachment fields" do
expect(subject.attachments.first[:fields].count).to eq(4)
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns the stage name and link to the 'Failed jobs' tab on the pipeline's page as the attachment's third field property" do
expect(subject.attachments.first[:fields][2]).to eq({
title: "Failed stage",
2020-06-23 00:09:42 +05:30
value: "<http://example.gitlab.com/-/pipelines/123/failures|test>",
2019-10-12 21:52:04 +05:30
short: true
})
end
2020-05-24 23:13:21 +05:30
it "returns the job name and link as the attachment's fourth field property" do
expect(subject.attachments.first[:fields][3]).to eq({
title: "Failed job",
value: "<http://example.gitlab.com/-/jobs/1|rspec>",
2019-10-12 21:52:04 +05:30
short: true
})
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context "when lots of jobs across multiple stages fail" do
before do
args[:builds] = (1..25).map do |i|
{ id: i, name: "job-#{i}", status: "failed", stage: "stage-" + ((i % 3) + 1).to_s }
2019-10-12 21:52:04 +05:30
end
end
2020-05-24 23:13:21 +05:30
it "returns the stage names and links to the 'Failed jobs' tab on the pipeline's page as the attachment's third field property" do
expect(subject.attachments.first[:fields][2]).to eq({
title: "Failed stages",
2020-06-23 00:09:42 +05:30
value: "<http://example.gitlab.com/-/pipelines/123/failures|stage-2>, <http://example.gitlab.com/-/pipelines/123/failures|stage-1>, <http://example.gitlab.com/-/pipelines/123/failures|stage-3>",
2020-05-24 23:13:21 +05:30
short: true
})
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the job names and links as the attachment's fourth field property" do
expected_jobs = 25.downto(16).map do |i|
"<http://example.gitlab.com/-/jobs/#{i}|job-#{i}>"
2019-10-12 21:52:04 +05:30
end
2017-08-17 22:00:37 +05:30
2020-06-23 00:09:42 +05:30
expected_jobs << "and <http://example.gitlab.com/-/pipelines/123/failures|15 more>"
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
expect(subject.attachments.first[:fields][3]).to eq({
title: "Failed jobs",
value: expected_jobs.join(", "),
short: true
})
2017-08-17 22:00:37 +05:30
end
2020-05-24 23:13:21 +05:30
end
2017-08-17 22:00:37 +05:30
2020-05-24 23:13:21 +05:30
context "when jobs succeed on retries" do
before do
args[:builds] = [
{ id: 1, name: "job-1", status: "failed", stage: "stage-1" },
{ id: 2, name: "job-2", status: "failed", stage: "stage-2" },
{ id: 3, name: "job-3", status: "failed", stage: "stage-3" },
{ id: 7, name: "job-1", status: "failed", stage: "stage-1" },
{ id: 8, name: "job-1", status: "success", stage: "stage-1" }
]
end
it "do not return a job which succeeded on retry" do
expected_jobs = [
"<http://example.gitlab.com/-/jobs/3|job-3>",
"<http://example.gitlab.com/-/jobs/2|job-2>"
]
expect(subject.attachments.first[:fields][3]).to eq(
title: "Failed jobs",
value: expected_jobs.join(", "),
short: true
)
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
end
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
context "when jobs failed even on retries" do
before do
args[:builds] = [
{ id: 1, name: "job-1", status: "failed", stage: "stage-1" },
{ id: 2, name: "job-2", status: "failed", stage: "stage-2" },
{ id: 3, name: "job-3", status: "failed", stage: "stage-3" },
{ id: 7, name: "job-1", status: "failed", stage: "stage-1" },
{ id: 8, name: "job-1", status: "failed", stage: "stage-1" }
]
end
it "returns only first instance of the failed job" do
expected_jobs = [
"<http://example.gitlab.com/-/jobs/3|job-3>",
"<http://example.gitlab.com/-/jobs/2|job-2>",
"<http://example.gitlab.com/-/jobs/1|job-1>"
]
expect(subject.attachments.first[:fields][3]).to eq(
title: "Failed jobs",
value: expected_jobs.join(", "),
short: true
)
2020-04-22 19:07:51 +05:30
end
2020-05-24 23:13:21 +05:30
end
2020-04-22 19:07:51 +05:30
2020-05-24 23:13:21 +05:30
context "when the CI config file contains a YAML error" do
let(:has_yaml_errors) { true }
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns three attachment fields" do
expect(subject.attachments.first[:fields].count).to eq(3)
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
it "returns the YAML error deatils as the attachment's third field property" do
expect(subject.attachments.first[:fields][2]).to eq({
title: "Invalid CI config YAML file",
value: "yaml error description here",
short: false
})
2019-10-12 21:52:04 +05:30
end
2020-05-24 23:13:21 +05:30
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the project's name as the attachment's footer property" do
expect(subject.attachments.first[:footer]).to eq("project_name")
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the project's avatar URL as the attachment's footer_icon property" do
expect(subject.attachments.first[:footer_icon]).to eq("http://example.com/project_avatar")
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
it "returns the pipeline's timestamp as the attachment's ts property" do
expected_ts = Time.parse(args[:object_attributes][:finished_at]).to_i
expect(subject.attachments.first[:ts]).to eq(expected_ts)
end
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
context 'when rendering markdown' do
before do
args[:markdown] = true
end
it 'returns the pipeline summary as the attachments in markdown format' do
expect(subject.attachments).to eq(
"[project_name](http://example.gitlab.com):" \
2020-06-23 00:09:42 +05:30
" Pipeline [#123](http://example.gitlab.com/-/pipelines/123)" \
" of branch [develop](http://example.gitlab.com/-/commits/develop)" \
2020-05-24 23:13:21 +05:30
" by The Hacker (hacker) has passed in 02:00:10"
)
2017-08-17 22:00:37 +05:30
end
end
end