You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewTicketPage.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2014 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import java.util.Set;
  22. import java.util.TreeSet;
  23. import org.apache.wicket.PageParameters;
  24. import org.apache.wicket.ajax.AjaxRequestTarget;
  25. import org.apache.wicket.ajax.markup.html.form.AjaxButton;
  26. import org.apache.wicket.markup.html.basic.Label;
  27. import org.apache.wicket.markup.html.form.Button;
  28. import org.apache.wicket.markup.html.form.DropDownChoice;
  29. import org.apache.wicket.markup.html.form.Form;
  30. import org.apache.wicket.markup.html.form.TextField;
  31. import org.apache.wicket.markup.html.panel.Fragment;
  32. import org.apache.wicket.model.IModel;
  33. import org.apache.wicket.model.Model;
  34. import org.eclipse.jgit.lib.Repository;
  35. import com.gitblit.Constants;
  36. import com.gitblit.Constants.AccessPermission;
  37. import com.gitblit.Constants.AuthorizationControl;
  38. import com.gitblit.models.RegistrantAccessPermission;
  39. import com.gitblit.models.TicketModel;
  40. import com.gitblit.models.TicketModel.Change;
  41. import com.gitblit.models.TicketModel.Field;
  42. import com.gitblit.models.TicketModel.Status;
  43. import com.gitblit.models.UserModel;
  44. import com.gitblit.tickets.TicketMilestone;
  45. import com.gitblit.tickets.TicketNotifier;
  46. import com.gitblit.tickets.TicketResponsible;
  47. import com.gitblit.utils.StringUtils;
  48. import com.gitblit.wicket.GitBlitWebSession;
  49. import com.gitblit.wicket.WicketUtils;
  50. import com.gitblit.wicket.panels.MarkdownTextArea;
  51. /**
  52. * Page for creating a new ticket.
  53. *
  54. * @author James Moger
  55. *
  56. */
  57. public class NewTicketPage extends RepositoryPage {
  58. private IModel<TicketModel.Type> typeModel;
  59. private IModel<String> titleModel;
  60. private MarkdownTextArea descriptionEditor;
  61. private IModel<String> topicModel;
  62. private IModel<String> mergeToModel;
  63. private IModel<TicketResponsible> responsibleModel;
  64. private IModel<TicketMilestone> milestoneModel;
  65. private Label descriptionPreview;
  66. private IModel<TicketModel.Priority> priorityModel;
  67. private IModel<TicketModel.Severity> severityModel;
  68. public NewTicketPage(PageParameters params) {
  69. super(params);
  70. UserModel currentUser = GitBlitWebSession.get().getUser();
  71. if (currentUser == null) {
  72. currentUser = UserModel.ANONYMOUS;
  73. }
  74. if (!currentUser.isAuthenticated || !app().tickets().isAcceptingNewTickets(getRepositoryModel())) {
  75. // tickets prohibited
  76. setResponsePage(TicketsPage.class, WicketUtils.newRepositoryParameter(repositoryName));
  77. }
  78. typeModel = Model.of(TicketModel.Type.defaultType);
  79. titleModel = Model.of();
  80. topicModel = Model.of();
  81. mergeToModel = Model.of(Repository.shortenRefName(getRepositoryModel().mergeTo));
  82. responsibleModel = Model.of();
  83. milestoneModel = Model.of();
  84. severityModel = Model.of(TicketModel.Severity.defaultSeverity);
  85. priorityModel = Model.of(TicketModel.Priority.defaultPriority);
  86. setStatelessHint(false);
  87. setOutputMarkupId(true);
  88. Form<Void> form = new Form<Void>("editForm");
  89. add(form);
  90. form.add(new DropDownChoice<TicketModel.Type>("type", typeModel, Arrays.asList(TicketModel.Type.choices())));
  91. form.add(new TextField<String>("title", titleModel));
  92. form.add(new TextField<String>("topic", topicModel));
  93. final IModel<String> markdownPreviewModel = Model.of();
  94. descriptionPreview = new Label("descriptionPreview", markdownPreviewModel);
  95. descriptionPreview.setEscapeModelStrings(false);
  96. descriptionPreview.setOutputMarkupId(true);
  97. form.add(descriptionPreview);
  98. descriptionEditor = new MarkdownTextArea("description", markdownPreviewModel, descriptionPreview);
  99. descriptionEditor.setRepository(repositoryName);
  100. form.add(descriptionEditor);
  101. if (currentUser.canAdmin(null, getRepositoryModel())) {
  102. // responsible
  103. Set<String> userlist = new TreeSet<String>();
  104. if (UserModel.ANONYMOUS.canPush(getRepositoryModel())
  105. || AuthorizationControl.AUTHENTICATED == getRepositoryModel().authorizationControl) {
  106. // authorization is ANONYMOUS or AUTHENTICATED (i.e. all users can be set responsible)
  107. userlist.addAll(app().users().getAllUsernames());
  108. } else {
  109. // authorization is by NAMED users (users with PUSH permission can be set responsible)
  110. for (RegistrantAccessPermission rp : app().repositories().getUserAccessPermissions(getRepositoryModel())) {
  111. if (rp.permission.atLeast(AccessPermission.PUSH)) {
  112. userlist.add(rp.registrant);
  113. }
  114. }
  115. }
  116. List<TicketResponsible> responsibles = new ArrayList<TicketResponsible>();
  117. for (String username : userlist) {
  118. UserModel user = app().users().getUserModel(username);
  119. if (user != null && !user.disabled) {
  120. TicketResponsible responsible = new TicketResponsible(user);
  121. responsibles.add(responsible);
  122. }
  123. }
  124. Collections.sort(responsibles);
  125. Fragment responsible = new Fragment("responsible", "responsibleFragment", this);
  126. responsible.add(new DropDownChoice<TicketResponsible>("responsible", responsibleModel, responsibles));
  127. form.add(responsible.setVisible(!responsibles.isEmpty()));
  128. // milestone
  129. List<TicketMilestone> milestones = app().tickets().getMilestones(getRepositoryModel(), Status.Open);
  130. Fragment milestone = new Fragment("milestone", "milestoneFragment", this);
  131. milestone.add(new DropDownChoice<TicketMilestone>("milestone", milestoneModel, milestones));
  132. form.add(milestone.setVisible(!milestones.isEmpty()));
  133. // priority
  134. form.add(new DropDownChoice<TicketModel.Priority>("priority", priorityModel, Arrays.asList(TicketModel.Priority.choices())));
  135. //severity
  136. form.add(new DropDownChoice<TicketModel.Severity>("severity", severityModel, Arrays.asList(TicketModel.Severity.choices())));
  137. // integration branch
  138. List<String> branches = new ArrayList<String>();
  139. for (String branch : getRepositoryModel().getLocalBranches()) {
  140. // exclude ticket branches
  141. if (!branch.startsWith(Constants.R_TICKET)) {
  142. branches.add(Repository.shortenRefName(branch));
  143. }
  144. }
  145. branches.remove(Repository.shortenRefName(getRepositoryModel().HEAD));
  146. branches.add(0, Repository.shortenRefName(getRepositoryModel().HEAD));
  147. Fragment mergeto = new Fragment("mergeto", "mergeToFragment", this);
  148. mergeto.add(new DropDownChoice<String>("mergeto", mergeToModel, branches));
  149. form.add(mergeto.setVisible(!branches.isEmpty()));
  150. } else {
  151. // user does not have permission to assign milestone, responsible, or mergeto
  152. form.add(new Label("responsible").setVisible(false));
  153. form.add(new Label("milestone").setVisible(false));
  154. form.add(new Label("mergeto").setVisible(false));
  155. }
  156. form.add(new AjaxButton("create") {
  157. private static final long serialVersionUID = 1L;
  158. @Override
  159. protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
  160. String title = titleModel.getObject();
  161. if (StringUtils.isEmpty(title)) {
  162. return;
  163. }
  164. String createdBy = GitBlitWebSession.get().getUsername();
  165. Change change = new Change(createdBy);
  166. change.setField(Field.title, title);
  167. change.setField(Field.body, descriptionEditor.getText());
  168. String topic = topicModel.getObject();
  169. if (!StringUtils.isEmpty(topic)) {
  170. change.setField(Field.topic, topic);
  171. }
  172. // type
  173. TicketModel.Type type = TicketModel.Type.defaultType;
  174. if (typeModel.getObject() != null) {
  175. type = typeModel.getObject();
  176. }
  177. change.setField(Field.type, type);
  178. // responsible
  179. TicketResponsible responsible = responsibleModel == null ? null : responsibleModel.getObject();
  180. if (responsible != null) {
  181. change.setField(Field.responsible, responsible.username);
  182. }
  183. // milestone
  184. TicketMilestone milestone = milestoneModel == null ? null : milestoneModel.getObject();
  185. if (milestone != null) {
  186. change.setField(Field.milestone, milestone.name);
  187. }
  188. // severity
  189. TicketModel.Severity severity = TicketModel.Severity.defaultSeverity;
  190. if (severityModel.getObject() != null) {
  191. severity = severityModel.getObject();
  192. }
  193. change.setField(Field.severity, severity);
  194. // priority
  195. TicketModel.Priority priority = TicketModel.Priority.defaultPriority;
  196. if (priorityModel.getObject() != null) {
  197. priority = priorityModel.getObject();
  198. }
  199. change.setField(Field.priority, priority);
  200. // integration branch
  201. String mergeTo = mergeToModel.getObject();
  202. if (!StringUtils.isEmpty(mergeTo)) {
  203. change.setField(Field.mergeTo, mergeTo);
  204. }
  205. TicketModel ticket = app().tickets().createTicket(getRepositoryModel(), 0L, change);
  206. if (ticket != null) {
  207. TicketNotifier notifier = app().tickets().createNotifier();
  208. notifier.sendMailing(ticket);
  209. redirectTo(TicketsPage.class, WicketUtils.newObjectParameter(getRepositoryModel().name, "" + ticket.number));
  210. } else {
  211. // TODO error
  212. }
  213. }
  214. });
  215. Button cancel = new Button("cancel") {
  216. private static final long serialVersionUID = 1L;
  217. @Override
  218. public void onSubmit() {
  219. setResponsePage(TicketsPage.class, getPageParameters());
  220. }
  221. };
  222. cancel.setDefaultFormProcessing(false);
  223. form.add(cancel);
  224. }
  225. @Override
  226. protected String getPageName() {
  227. return getString("gb.newTicket");
  228. }
  229. @Override
  230. protected Class<? extends BasePage> getRepoNavPageClass() {
  231. return TicketsPage.class;
  232. }
  233. }