Commit af4201c9 authored by Ezekiel Kigbo's avatar Ezekiel Kigbo

Merge branch '340529-append-url-issue-description-to-template' into 'master'

Append URL issue description to template

See merge request gitlab-org/gitlab!80554
parents 9d6efd35 3a6ed05d
...@@ -78,7 +78,12 @@ export default class TemplateSelector { ...@@ -78,7 +78,12 @@ export default class TemplateSelector {
setEditorContent(file, { skipFocus } = {}) { setEditorContent(file, { skipFocus } = {}) {
if (!file) return; if (!file) return;
const newValue = file.content; let newValue = file.content;
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.has('issue[description]')) {
newValue += `\n${urlParams.get('issue[description]')}`;
}
this.editor.setValue(newValue, 1); this.editor.setValue(newValue, 1);
......
...@@ -160,6 +160,8 @@ To regenerate the email address: ...@@ -160,6 +160,8 @@ To regenerate the email address:
### Using a URL with prefilled values ### Using a URL with prefilled values
> Ability to use both `issuable_template` and `issue[description]` in the same URL [introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/340529) in GitLab 14.8.
To link directly to the new issue page with prefilled fields, use query To link directly to the new issue page with prefilled fields, use query
string parameters in a URL. You can embed a URL in an external string parameters in a URL. You can embed a URL in an external
HTML page to create issues with certain fields prefilled. HTML page to create issues with certain fields prefilled.
...@@ -168,8 +170,8 @@ HTML page to create issues with certain fields prefilled. ...@@ -168,8 +170,8 @@ HTML page to create issues with certain fields prefilled.
| -------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- | | -------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| Title | `issue[title]` | Must be [URL-encoded](../../../api/index.md#namespaced-path-encoding). | | Title | `issue[title]` | Must be [URL-encoded](../../../api/index.md#namespaced-path-encoding). |
| Issue type | `issue[issue_type]` | Either `incident` or `issue`. | | Issue type | `issue[issue_type]` | Either `incident` or `issue`. |
| Description template | `issuable_template` | Cannot be used at the same time as `issue[description]`. Must be [URL-encoded](../../../api/index.md#namespaced-path-encoding). | | Description template | `issuable_template` | Must be [URL-encoded](../../../api/index.md#namespaced-path-encoding). |
| Description | `issue[description]` | Cannot be used at the same time as `issuable_template`. Must be [URL-encoded](../../../api/index.md#namespaced-path-encoding). | | Description | `issue[description]` | Must be [URL-encoded](../../../api/index.md#namespaced-path-encoding). If used in combination with `issuable_template` or a [default issue template](../description_templates.md#set-a-default-template-for-merge-requests-and-issues), the `issue[description]` value is appended to the template. |
| Confidential | `issue[confidential]` | If `true`, the issue is marked as confidential. | | Confidential | `issue[confidential]` | If `true`, the issue is marked as confidential. |
Adapt these examples to form your new issue URL with prefilled fields. Adapt these examples to form your new issue URL with prefilled fields.
......
...@@ -8,8 +8,12 @@ module EE ...@@ -8,8 +8,12 @@ module EE
def issue_params_from_template def issue_params_from_template
return {} unless project.feature_available?(:issuable_default_templates) return {} unless project.feature_available?(:issuable_default_templates)
if project.issues_template.present? && params.include?(:description)
{ description: project.issues_template + "\n" + params.delete(:description) }
else
{ description: project.issues_template } { description: project.issues_template }
end end
end
# Issue params can be built from 3 types of passed params, # Issue params can be built from 3 types of passed params,
# They take precedence over eachother like this # They take precedence over eachother like this
......
...@@ -27,6 +27,12 @@ RSpec.describe Issues::BuildService do ...@@ -27,6 +27,12 @@ RSpec.describe Issues::BuildService do
expect(issue.description).to eq('Work hard, play hard!') expect(issue.description).to eq('Work hard, play hard!')
end end
it 'fills in the template, followed by the query parameter, in the description' do
issue = build_issue(description: 'Travailler dur, jouer dur!')
expect(issue.description).to eq("Work hard, play hard!\nTravailler dur, jouer dur!")
end
end end
end end
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
RSpec.describe 'New/edit issue', :js do RSpec.describe 'New/edit issue', :js do
include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::JavaScriptHelper
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:user2) { create(:user) } let_it_be(:user2) { create(:user) }
let_it_be(:milestone) { create(:milestone, project: project) } let_it_be(:milestone) { create(:milestone, project: project) }
...@@ -310,6 +310,53 @@ RSpec.describe 'New/edit issue', :js do ...@@ -310,6 +310,53 @@ RSpec.describe 'New/edit issue', :js do
end end
end end
describe 'new issue with query parameters' do
before do
project.repository.create_file(
current_user,
'.gitlab/issue_templates/test_template.md',
'description from template',
message: 'Add test_template.md',
branch_name: project.default_branch_or_main
)
end
after do
project.repository.delete_file(
current_user,
'.gitlab/issue_templates/test_template.md',
message: 'Remove test_template.md',
branch_name: project.default_branch_or_main
)
end
it 'leaves the description blank if no query parameters are specified' do
visit new_project_issue_path(project)
expect(find('#issue_description').value).to be_empty
end
it 'fills the description from the issue[description] query parameter' do
visit new_project_issue_path(project, issue: { description: 'description from query parameter' })
expect(find('#issue_description').value).to match('description from query parameter')
end
it 'fills the description from the issuable_template query parameter' do
visit new_project_issue_path(project, issuable_template: 'test_template')
wait_for_requests
expect(find('#issue_description').value).to match('description from template')
end
it 'fills the description from the issuable_template and issue[description] query parameters' do
visit new_project_issue_path(project, issuable_template: 'test_template', issue: { description: 'description from query parameter' })
wait_for_requests
expect(find('#issue_description').value).to match('description from template\ndescription from query parameter')
end
end
describe 'edit issue' do describe 'edit issue' do
before do before do
visit edit_project_issue_path(project, issue) visit edit_project_issue_path(project, issue)
......
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