diff options
author | tiagonbotelho <tiagonbotelho@hotmail.com> | 2016-08-09 23:08:59 +0100 |
---|---|---|
committer | tiagonbotelho <tiagonbotelho@hotmail.com> | 2016-08-26 13:38:57 +0100 |
commit | d87fdbeb5775f6b7262c5977229164dfe0d705e7 (patch) | |
tree | 69db9aeb4d198f3cc2f94c4e958fef7a8a9e4225 | |
parent | a89e6bbf4b48865d0caf455e2428786fcfe12dc0 (diff) | |
download | gitlab-ce-d87fdbeb5775f6b7262c5977229164dfe0d705e7.tar.gz |
adds test to check whether or not an email is sent to label subscribers after creating a new issue through the api
-rw-r--r-- | lib/api/issues.rb | 25 | ||||
-rw-r--r-- | spec/requests/api/issues_spec.rb | 13 |
2 files changed, 19 insertions, 19 deletions
diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 1121285f0af..9a042e6e70d 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -154,35 +154,22 @@ module API render_api_error!({ labels: errors }, 400) end + # Find or create labels if params[:labels].present? - params[:labels] = params[:labels].split(",").each { |word| word.strip! } - attrs[:label_ids] = [] - - params[:labels].each do |label| - existing_label = user_project.labels.where(title: label).first - - unless existing_label.nil? - attrs[:label_ids] << existing_label.id - params[:labels].delete(label) - end + attrs[:label_ids] = params[:labels].split(",").map do |label_name| + user_project.labels.create_with(color: Label::DEFAULT_COLOR) + .find_or_create_by(title: label_name.strip) + .id end end - project = user_project - - issue = ::Issues::CreateService.new(project, current_user, attrs.merge(request: request, api: true)).execute + issue = ::Issues::CreateService.new(user_project, current_user, attrs.merge(request: request, api: true)).execute if issue.spam? render_api_error!({ error: 'Spam detected' }, 400) end if issue.valid? - # create new labels and attach to issue. Labels are valid because - # we already checked its name, so there can't be an error here - if params[:labels].present? - issue.add_labels_by_names(params[:labels]) - end - present issue, with: Entities::Issue, current_user: current_user else render_validation_error!(issue) diff --git a/spec/requests/api/issues_spec.rb b/spec/requests/api/issues_spec.rb index b8038fc85a1..a4c91252472 100644 --- a/spec/requests/api/issues_spec.rb +++ b/spec/requests/api/issues_spec.rb @@ -2,6 +2,7 @@ require 'spec_helper' describe API::API, api: true do include ApiHelpers + let(:user) { create(:user) } let(:user2) { create(:user) } let(:non_member) { create(:user) } @@ -478,6 +479,18 @@ describe API::API, api: true do expect(json_response['labels']).to eq(['label', 'label2']) end + it "emails label subscribers" do + clear_enqueued_jobs + label = project.labels.first + label.toggle_subscription(user2) + + expect do + post api("/projects/#{project.id}/issues", user), + title: 'new issue', labels: label.title + end.to change{enqueued_jobs.size}.by(1) + expect(response.status).to eq(201) + end + it "returns a 400 bad request if title not given" do post api("/projects/#{project.id}/issues", user), labels: 'label, label2' expect(response).to have_http_status(400) |