Eclipseplugins
CherryPickPage.java
1 package com.proalpha.pds.gitutils.cherrypick;
2 
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.net.URL;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.LinkedList;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19 
20 import org.eclipse.core.runtime.FileLocator;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.core.runtime.Platform;
23 import org.eclipse.jface.dialogs.IDialogSettings;
24 import org.eclipse.jface.dialogs.IMessageProvider;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.viewers.ArrayContentProvider;
27 import org.eclipse.jface.viewers.ComboViewer;
28 import org.eclipse.jface.viewers.ISelectionChangedListener;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.StructuredSelection;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.jface.viewers.ViewerFilter;
33 import org.eclipse.jface.wizard.WizardPage;
34 import org.eclipse.jgit.lib.Repository;
35 import org.eclipse.mylyn.tasks.core.ITaskActivityManager;
36 import org.eclipse.mylyn.tasks.ui.TasksUi;
37 import org.eclipse.swt.SWT;
38 import org.eclipse.swt.custom.ScrolledComposite;
39 import org.eclipse.swt.events.FocusEvent;
40 import org.eclipse.swt.events.FocusListener;
41 import org.eclipse.swt.events.KeyEvent;
42 import org.eclipse.swt.events.KeyListener;
43 import org.eclipse.swt.events.SelectionAdapter;
44 import org.eclipse.swt.events.SelectionEvent;
45 import org.eclipse.swt.graphics.Point;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.swt.widgets.Group;
51 import org.eclipse.swt.widgets.Label;
52 import org.eclipse.swt.widgets.Text;
53 import org.eclipse.wb.swt.ResourceManager;
54 
55 import com.proalpha.git.PaGitConfig;
56 import com.proalpha.git.model.server.GitServerRepository;
57 import com.proalpha.git.util.PaBranchName;
58 import com.proalpha.git.util.PaRepository;
59 import com.proalpha.git.util.bitbucket.BitbucketRestCallGetRepoList;
60 import com.proalpha.pds.gitutils.Activator;
61 import com.proalpha.pds.gitutils.PreferencesConstants;
62 
71 class PrefixMatchFilter extends ViewerFilter {
72 
73  private String input = "";
74 
75  public void setInput(String s) {
76  this.input = s.toLowerCase();
77  }
78 
79  public String getInput() {
80  return this.input;
81  }
82 
83  @Override
88  public boolean select(Viewer viewer, Object parentElement, Object element) {
89  if (this.input == null || this.input.length() == 0) {
90  return true;
91  }
92  String branchInList = (String) element;
93 
94  return (branchInList.toLowerCase().startsWith(this.input));
95  }
96 }
97 
102 public class CherryPickPage extends WizardPage {
103 
104  private Repository srcRepository;
105  private List<GitServerRepository> currRepos;
106  private Map<String, Set<String>> projectRepositoryMap;
107 
108  private Button btnUpdateTargetBranchPoint;
109  private ComboViewer cvSrcProject;
110  private ComboViewer cvSrcRepository;
111  private ComboViewer cvSrcBranch;
112  private Text txtSrcFeature;
113  private Text txtTargetBranchPoint;
114  private Text txtTargetBranch;
115  private Text txtTargetFeature;
116  private Button btnFetchCommits;
117  private Button btnEditBranchname;
118  private Button btnOnlyShowMaster;
119 
120  ScrolledComposite scrolledComposite;
121  private Label infoLabel;
122 
123  private CherryPickSettings cherryPickSettings;
124  private static final String PAGE_NAME = "pAGitCherryPickPage";
125 
126  public CherryPickPage(Repository repository) {
127  super(CherryPickPage.class.getName());
128 
129  this.srcRepository = repository;
130  setTitle("proALPHA CherryPick");
131  setMessage("Select repository and branch which needs to get picked into your current repository", INFORMATION);
132  URL url = FileLocator.find(Platform.getBundle(Activator.PLUGIN_ID), new Path("icons/pA-logox72.png"),
133  Collections.emptyMap());
134 
135  setImageDescriptor(ImageDescriptor.createFromURL(url));
136 
137  }
138 
139  @Override
140  public void createControl(Composite parent) {
141  cherryPickSettings = ((CherryPickWizard) getWizard()).getSettings();
142  currRepos = getServerRepositories();
143  projectRepositoryMap = fillProjectRepositoryMap();
144 
145  Composite container = new Composite(parent, SWT.NULL);
146  setControl(container);
147  container.setLayout(new GridLayout(2, false));
148 
149  this.createSourceControls(container);
150  this.createTargetControls(container);
151 
152  setControl(container);
153 
154  GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1);
155  Composite container_bottom = new Composite(container, SWT.None);
156  container_bottom.setLayoutData(gridData);
157  gridData.widthHint = 488;
158  gridData.heightHint = 100;
159 
160  scrolledComposite = new ScrolledComposite(container_bottom, SWT.V_SCROLL | SWT.H_SCROLL);
161  scrolledComposite.setExpandHorizontal(true);
162  scrolledComposite.setExpandVertical(true);
163  scrolledComposite.setBounds(0, 0, 408, 100);
164 
165  infoLabel = new Label(scrolledComposite, SWT.NONE);
166  infoLabel.setBounds(0, 0, 408, 100);
167  infoLabel.setText("");
168 
169  btnFetchCommits = new Button(container_bottom, SWT.NONE);
170  btnFetchCommits.setBounds(443, 0, 120, 30);
171  btnFetchCommits.setText("Fetch Commits");
172  btnFetchCommits.setEnabled(false);
173 
174  addSrcWidgetListeners();
175  addTargetWidgetListeners();
176  addButtonWidgetListeners();
177 
179 
180  // Propose a feature name based on the activated Mylyn task/issue
181  ITaskActivityManager taskActivityManager = TasksUi.getTaskActivityManager();
182  if (taskActivityManager != null && taskActivityManager.getActiveTask() != null
183  && taskActivityManager.getActiveTask().getTaskKey() != null) {
184  setTxtFeatureText(taskActivityManager.getActiveTask().getTaskKey());
185  txtTargetFeature.setText(taskActivityManager.getActiveTask().getTaskKey());
186  }
187 
188  setPageComplete(false);
189  }
190 
196  private void createTargetControls(Composite container) {
197  Group grpTarget = new Group(container, SWT.NONE);
198  GridData gdGrpTarget = new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1);
199  gdGrpTarget.widthHint = 450;
200  gdGrpTarget.heightHint = 110;
201  grpTarget.setLayoutData(gdGrpTarget);
202  grpTarget.setText("Target");
203 
204  Label label1 = new Label(grpTarget, SWT.NONE);
205  label1.setText("Repository");
206  label1.setBounds(10, 22, 80, 15);
207 
208  Text txtTargetRepository;
209  txtTargetRepository = new Text(grpTarget, SWT.NONE);
210  txtTargetRepository.setBounds(96, 23, 347, 21);
211  txtTargetRepository.setText(srcRepository.getDirectory().toString());
212  txtTargetRepository.setEnabled(false);
213 
214  Label lblBranchPoint = new Label(grpTarget, SWT.NONE);
215  lblBranchPoint.setText("Branch Point");
216  lblBranchPoint.setBounds(10, 49, 80, 15);
217 
218  txtTargetBranchPoint = new Text(grpTarget, SWT.BORDER);
219  txtTargetBranchPoint.setBounds(96, 46, 212, 21);
220  try {
221  // Try to determine the main branch of the repository
222 
223  List<String> branches = PaRepository.getMasterBranches(srcRepository);
224 
225  String mainBranch = null;
226  if (branches != null && !branches.isEmpty())
227  mainBranch = branches.get(0);
228  else {
229  branches = PaRepository.getCodeFreezeBranches(srcRepository);
230  if (branches != null && !branches.isEmpty())
231  mainBranch = branches.get(0);
232  }
233 
234  if (mainBranch == null)
235  // Fallback: Load checked out branch, if no master or code-freeze branch is
236  // available.
237  mainBranch = srcRepository.getFullBranch();
238 
239  String remoteTrackingRef = PaRepository.getRemoteTrackingBranch(srcRepository, mainBranch);
240 
241  if (remoteTrackingRef == null || remoteTrackingRef.isEmpty())
242  // Fallback to HEAD if no remote tracking branch could be determined
243  // Hopefully never happens
244  remoteTrackingRef = "HEAD";
245 
246  txtTargetBranchPoint.setText(remoteTrackingRef);
247  cherryPickSettings.setTargetBranchPoint(remoteTrackingRef);
248  } catch (IOException ex) {
249  setMessage("Could not determine the branch name to be used as branching point!", IMessageProvider.ERROR);
250  }
251 
252  btnUpdateTargetBranchPoint = new Button(grpTarget, SWT.CHECK);
253  btnUpdateTargetBranchPoint.setBounds(314, 48, 129, 16);
254  btnUpdateTargetBranchPoint.setText("update/fetch");
255  btnUpdateTargetBranchPoint.setSelection(cherryPickSettings.isUpdateTargetBranchPoint());
256 
257  Label label2 = new Label(grpTarget, SWT.NONE);
258  label2.setText("Branch");
259  label2.setBounds(10, 76, 80, 15);
260 
261  txtTargetBranch = new Text(grpTarget, SWT.BORDER);
262  txtTargetBranch.setBounds(96, 73, 212, 21);
263  txtTargetBranch.setEnabled(false);
264 
265  btnEditBranchname = new Button(grpTarget, SWT.CHECK);
266  btnEditBranchname.setBounds(314, 75, 129, 16);
267  btnEditBranchname.setText("modify branch name");
268 
269  Label label3 = new Label(grpTarget, SWT.NONE);
270  label3.setText("Feature");
271  label3.setBounds(10, 103, 80, 15);
272 
273  txtTargetFeature = new Text(grpTarget, SWT.BORDER);
274  txtTargetFeature.setBounds(96, 101, 162, 21);
275  }
276 
282  private void createSourceControls(Composite container) {
283  String gitDefaultRepo = Activator.getDefault().getPreferenceStore()
284  .getString(PreferencesConstants.GIT_DEFAULT_REPO);
285 
286  String gitDefaultProject = "";
287  String gitShortDefaultRepo = "";
288  if (gitDefaultRepo.contains("/")) {
289  String[] splitGitDefaultRepo = gitDefaultRepo.split("/");
290  gitDefaultProject = splitGitDefaultRepo[0];
291  gitShortDefaultRepo = splitGitDefaultRepo[1];
292  }
293 
294  Group grpSource = new Group(container, SWT.NONE);
295  GridData gdGrpSource = new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1);
296  gdGrpSource.heightHint = 111;
297  gdGrpSource.widthHint = 450;
298  grpSource.setLayoutData(gdGrpSource);
299  grpSource.setText("Source");
300 
301  Label lblProject = new Label(grpSource, SWT.NONE);
302  lblProject.setText("Project");
303  lblProject.setBounds(10, 22, 80, 15);
304 
305  Label lblRepository = new Label(grpSource, SWT.NONE);
306  lblRepository.setText("Repository");
307  lblRepository.setBounds(10, 49, 80, 15);
308 
309  Label lblBranch = new Label(grpSource, SWT.NONE);
310  lblBranch.setText("Branch");
311  lblBranch.setBounds(10, 76, 80, 15);
312 
313  Label lblFeature = new Label(grpSource, SWT.NONE);
314  lblFeature.setText("Feature");
315  lblFeature.setBounds(10, 103, 80, 15);
316 
317  // Set the default value for the project Combo Box
318  PrefixMatchFilter projectFilter = new PrefixMatchFilter();
319  cvSrcProject = new ComboViewer(grpSource, SWT.BORDER);
320  cvSrcProject.setContentProvider(ArrayContentProvider.getInstance());
321  cvSrcProject.getCombo().setBounds(96, 19, 212, 21);
322  cvSrcProject.addFilter(projectFilter);
323 
324  Set<String> projectsSet = projectRepositoryMap.keySet();
325  String[] projects = new String[projectsSet.size()];
326  projectsSet.toArray(projects);
327  Arrays.sort(projects);
328  cvSrcProject.setInput(projects);
329  cvSrcProject.setSelection(new StructuredSelection(gitDefaultProject));
330 
331  // Set the default value for the repository Combo Box
332  PrefixMatchFilter repositoryFilter = new PrefixMatchFilter();
333  cvSrcRepository = new ComboViewer(grpSource, SWT.BORDER);
334  cvSrcRepository.setContentProvider(ArrayContentProvider.getInstance());
335  cvSrcRepository.getCombo().setBounds(96, 46, 212, 21);
336  cvSrcRepository.addFilter(repositoryFilter);
337 
338  Set<String> repositorySet = projectRepositoryMap.get(gitDefaultProject);
339  if (repositorySet == null) {
340  repositorySet = new HashSet<String>();
341  }
342  String[] repositories = new String[repositorySet.size()];
343  repositorySet.toArray(repositories);
344  Arrays.sort(repositories);
345  cvSrcRepository.setInput(repositories);
346  cvSrcRepository.setSelection(new StructuredSelection(gitShortDefaultRepo));
347 
348  PrefixMatchFilter branchFilter = new PrefixMatchFilter();
349  cvSrcBranch = new ComboViewer(grpSource, SWT.BORDER);
350  cvSrcBranch.setContentProvider(ArrayContentProvider.getInstance());
351  cvSrcBranch.getCombo().setBounds(96, 73, 212, 21);
352  cvSrcBranch.addFilter(branchFilter);
353  cvSrcBranch.addSelectionChangedListener(new ISelectionChangedListener() {
354 
355  @Override
356  public void selectionChanged(SelectionChangedEvent event) {
357  if (cvSrcBranch.getSelection() != event.getSelection()) {
358  setPageComplete(false);
359  }
360 
361  }
362  });
363 
364  txtSrcFeature = new Text(grpSource, SWT.BORDER);
365  txtSrcFeature.setBounds(96, 100, 162, 21);
366 
367  Button btnRefreshRepos = new Button(grpSource, SWT.NONE);
368  btnRefreshRepos.setToolTipText("Reload repositories from server");
369  btnRefreshRepos.setImage(ResourceManager.getPluginImage("com.proalpha.pds.gitutils", "icons/refresh.gif"));
370  btnRefreshRepos.setBounds(314, 18, 28, 23);
371 
372  IDialogSettings settings = getDialogSettings();
373  btnOnlyShowMaster = new Button(grpSource, SWT.CHECK);
374  btnOnlyShowMaster.setBounds(314, 75, 129, 16);
375  btnOnlyShowMaster.setText("only show master");
376  btnOnlyShowMaster.setSelection(settings.getBoolean("ONLY_SHOW_MASTER"));
377 
378  }
379 
383  private void addSrcWidgetListeners() {
384 
385  cvSrcProject.getCombo().addModifyListener(modifyEvent -> proposeRepositories());
386  cvSrcRepository.getCombo().addModifyListener(modifyEvent -> checkRepoAndProposeBranches());
387  cvSrcRepository.getCombo().addFocusListener(new FocusListener() {
388  @Override
389  public void focusGained(FocusEvent e) {
390  setMessage("Choose your source repository");
391  }
392 
393  @Override
394  public void focusLost(FocusEvent e) {
395  if (cvSrcBranch.getCombo().getText().length() != 0)
396  checkBranch();
397  }
398  });
399 
400  cvSrcBranch.getCombo().addFocusListener(new FocusListener() {
401  @Override
402  public void focusGained(FocusEvent e) {
403  setMessage("Choose your source branch");
404  }
405 
406  @Override
407  public void focusLost(FocusEvent e) {
408  if (cvSrcBranch.getCombo().getText().length() != 0 && checkBranch()) {
409  String featureByBranch = getFeatureText(cvSrcBranch.getCombo().getText());
410  if (featureByBranch != null)
411  setTxtFeatureText(featureByBranch);
412  }
413  }
414  });
415 
416  txtSrcFeature.addModifyListener(modifyEvent -> {
417  if (cvSrcBranch.getCombo().getText().length() != 0) {
418  checkBranch();
419  txtTargetFeature.setText(txtSrcFeature.getText());
420  }
421  });
422  txtSrcFeature.addFocusListener(new FocusListener() {
423  @Override
424  public void focusGained(FocusEvent e) {
425  setMessage("Type your feature to pick");
426  }
427 
428  @Override
429  public void focusLost(FocusEvent e) {
430  if (cvSrcBranch.getCombo().getText().length() != 0 && txtSrcFeature.getText().length() != 0) {
431  btnFetchCommits.setEnabled(true);
432  }
433  String feature = txtSrcFeature.getText();
434  check_caused_issues(feature);
435  }
436  });
437 
438  KeyListener projectKeyListener = createKeyListener(cvSrcProject);
439  cvSrcProject.getCombo().addKeyListener(projectKeyListener);
440 
441  KeyListener repositoryKeyListener = createKeyListener(cvSrcRepository);
442  cvSrcRepository.getCombo().addKeyListener(repositoryKeyListener);
443 
444  KeyListener branchKeyListener = createKeyListener(cvSrcBranch);
445  cvSrcBranch.getCombo().addKeyListener(branchKeyListener);
446 
447  cvSrcBranch.getCombo().addModifyListener(modifyListener -> {
448  if (cvSrcBranch.getCombo().getText().length() != 0 && checkBranch()) {
449  String featureByBranch = getFeatureText(cvSrcBranch.getCombo().getText());
450  if (featureByBranch != null)
451  setTxtFeatureText(featureByBranch);
452  }
453  });
454  }
455 
456  private void check_caused_issues(String issue) {
457  List<String> command = new LinkedList<String>();
458  command.add("python");
459  command.add("-m");
460  command.add("pa_common.pds_functions.jira_issue_links");
461  command.add("-i");
462  command.add(issue);
463  command.add("-l");
464  command.add("INFO");
465 
466  infoLabel.setText("");
467  run_command(command);
468  }
469 
470  private boolean run_command(List<String> command) {
471  boolean result = false;
472  ProcessBuilder builder = new ProcessBuilder();
473  builder.command(command);
474  Process p;
475  try {
476  p = builder.start();
477 
478  InputStream inputStream = p.getErrorStream();
479  BufferedReader bufRead = new BufferedReader(new InputStreamReader(inputStream));
480  String line;
481  while ((line = bufRead.readLine()) != null) {
482  String text = infoLabel.getText();
483  infoLabel.setText(text + "\n" + line);
484  this.getShell().getDisplay().update();
485  }
486 
487  } catch (IOException e) {
488  e.printStackTrace();
489  }
490  return result;
491  }
492 
493  private void setTxtFeatureText(String text) {
494  txtSrcFeature.setText(text);
495  txtSrcFeature.setFocus();
496  getShell().setFocus();
497  }
498 
499  private KeyListener createKeyListener(ComboViewer viewer) {
500  return new KeyListener() {
501 
502  @Override
503  public void keyPressed(final KeyEvent e) {
504  // Must be implemented in KeyListener
505  }
506 
507  @Override
508  public void keyReleased(final KeyEvent e) {
509  String currentInput = viewer.getCombo().getText();
510  String inputFilter = ((PrefixMatchFilter) viewer.getFilters()[0]).getInput();
511 
512  // Only refresh the filter if the old search string was shortened or
513  // a printable character was inserted
514  if (currentInput.length() < inputFilter.length() || Character.isDigit(e.character)
515  || Character.isAlphabetic(e.character)) {
516  if (e.keyCode == SWT.ESC) {
517  ((PrefixMatchFilter) viewer.getFilters()[0]).setInput("");
518  currentInput = "";
519  } else {
520  ((PrefixMatchFilter) viewer.getFilters()[0]).setInput(currentInput);
521  }
522 
523  // Run the filter by refreshing and "drop-down" the list
524  viewer.refresh(true);
525  viewer.getCombo().setListVisible(e.keyCode != SWT.ESC);
526 
527  // SetListVisible selects the first matching item, so we have to set
528  // the old string again and even position the cursor.
529  viewer.getCombo().setText(currentInput);
530  viewer.getCombo().setSelection(new Point(currentInput.length(), currentInput.length()));
531 
532  }
533  }
534  };
535  }
536 
540  private void addTargetWidgetListeners() {
541 
542  txtTargetBranchPoint.addModifyListener(modifyEvent -> {
543  if (txtTargetBranchPoint.getText().length() != 0) {
544  cherryPickSettings.setTargetBranchPoint(txtTargetBranchPoint.getText());
545  }
546  });
547 
548  txtTargetBranchPoint.addFocusListener(new FocusListener() {
549  @Override
550  public void focusGained(FocusEvent e) {
551  setMessage("Type your new branch point");
552  }
553 
554  @Override
555  public void focusLost(FocusEvent e) {
556  // Must be implemented in FocusListener
557  }
558  });
559 
560  txtTargetBranch.addModifyListener(modifyEvent -> {
561  if (txtTargetBranch.getText().length() != 0) {
562  cherryPickSettings.setTargetBranch(txtTargetBranch.getText());
563  }
564  });
565 
566  txtTargetBranch.addFocusListener(new FocusListener() {
567  @Override
568  public void focusGained(FocusEvent e) {
569  setMessage("Type your new branchname");
570  }
571 
572  @Override
573  public void focusLost(FocusEvent e) {
574  if (txtTargetBranch.getText().length() != 0) {
575  String featureByBranch = getFeatureText(txtTargetBranch.getText());
576  if (featureByBranch != null)
577  txtTargetFeature.setText(featureByBranch);
578  }
579  }
580  });
581 
582  txtTargetFeature.addModifyListener(modifyEvent -> {
583  if (txtTargetFeature.getText().length() != 0) {
584  cherryPickSettings.setTargetIssue(txtTargetFeature.getText());
585  }
586  });
587  txtTargetFeature.addFocusListener(new FocusListener() {
588  @Override
589  public void focusGained(FocusEvent e) {
590  setMessage("Choose a target feature which is used for first line of commit message");
591  }
592 
593  @Override
594  public void focusLost(FocusEvent e) {
595  if (txtTargetFeature.getText().length() == 0) {
596  String featureByBranch = getFeatureText(txtTargetBranch.getText());
597  if (featureByBranch != null)
598  txtTargetFeature.setText(featureByBranch);
599  }
600 
601  cherryPickSettings.setTargetIssue(txtTargetFeature.getText());
602  }
603  });
604 
605  btnUpdateTargetBranchPoint.addSelectionListener(new SelectionAdapter() {
606  @Override
607  public void widgetSelected(SelectionEvent e) {
608  cherryPickSettings.setUpdateTargetBranchPoint(btnUpdateTargetBranchPoint.getSelection());
609  }
610  });
611  }
612 
616  private void addButtonWidgetListeners() {
617 
618  btnEditBranchname.addSelectionListener(new SelectionAdapter() {
619  @Override
620  public void widgetSelected(SelectionEvent e) {
621  txtTargetBranch.setEnabled(btnEditBranchname.getSelection());
622  if (!btnEditBranchname.getSelection())
623  checkBranch();
624  }
625  });
626 
627  btnFetchCommits.addSelectionListener(new SelectionAdapter() {
628  @Override
629  public void widgetSelected(SelectionEvent e) {
630  fetchCommits();
631  }
632  });
633 
634  btnOnlyShowMaster.addSelectionListener(new SelectionAdapter() {
635  @Override
636  public void widgetSelected(SelectionEvent e) {
637  IDialogSettings settings = getDialogSettings();
638  settings.put("ONLY_SHOW_MASTER", btnOnlyShowMaster.getSelection());
640  }
641  });
642  }
643 
647  protected void fetchCommits() {
648 
649  cherryPickSettings.setSourceIssue(txtSrcFeature.getText());
650  cherryPickSettings.setSourceBranch(cvSrcBranch.getCombo().getText());
651  cherryPickSettings.setTargetIssue(txtTargetFeature.getText());
652 
653  String projectname = cvSrcProject.getCombo().getText();
654  String repoName = cvSrcRepository.getCombo().getText();
655 
656  for (GitServerRepository actRepo : currRepos) {
657  if (actRepo.getProject().equals(projectname) && actRepo.getName().equals(repoName)) {
658  cherryPickSettings.setSourceRepoString(projectname, repoName);
659  break;
660  }
661  }
662 
663  Boolean commitsfound = ((CherryPickWizard) getWizard()).gatherCommits();
664  if (!commitsfound)
665  setMessage("No commits found!", IMessageProvider.WARNING);
666  else
667  setMessage("Commits found! Push 'Next' to view or 'Finish' to pick them", INFORMATION);
668  setPageComplete(commitsfound);
669  }
670 
676  private List<GitServerRepository> getServerRepositories() {
677 
678  ArrayList<GitServerRepository> serverRepositories;
679 
680  BitbucketRestCallGetRepoList restcall = new BitbucketRestCallGetRepoList();
681  restcall.setSubUrl("repos");
682  restcall.execute();
683  serverRepositories = restcall.getRestResponse();
684 
685  return serverRepositories;
686  }
687 
693  private String getFeatureText(String branchText) {
694 
695  Pattern p = Pattern.compile("(.*\\.)?(" + PaGitConfig.getStringFromConfig("issue_key_format") + ")$");
696  Matcher m = p.matcher(branchText);
697 
698  if (m.find())
699  return m.group(2);
700 
701  return null;
702 
703  }
704 
710  private boolean checkBranch() {
711 
712  boolean foundIt = false;
713  String srcBranchName = cvSrcBranch.getCombo().getText();
714  setMessage(null, IMessageProvider.INFORMATION);
715  setErrorMessage(null);
716 
717  if (srcBranchName.length() == 0) {
718  setPageComplete(false);
719  return false;
720  }
721 
722  String projectname = cvSrcProject.getCombo().getText();
723  String repoName = cvSrcRepository.getCombo().getText();
724 
725  for (GitServerRepository repository : currRepos) {
726  if (repository.getProject().equals(projectname) && repository.getName().equals(repoName)) {
727  for (String actBranch : repository.getBranches()) {
728  if (actBranch.equals(srcBranchName)) {
729  foundIt = true;
730  }
731  }
732  }
733  if (foundIt)
734  break;
735  }
736 
737  if (!foundIt) {
738  setMessage("Branch not found!", IMessageProvider.WARNING);
739  setPageComplete(false);
740  btnFetchCommits.setEnabled(false);
741  return false;
742  }
743 
744  cherryPickSettings.setSourceBranch(srcBranchName);
745 
746  proposeTargetBranchName(srcBranchName);
747 
748  btnFetchCommits.setEnabled(true);
749  return true;
750 
751  }
752 
758  private void proposeTargetBranchName(String srcBranchName) {
759  String targetBranch = srcBranchName;
760  try {
761  // Try to guess the version of the target branch
762  // In case, this is a pick with a non-proALPHA conform repo, the
763  // version can not be extracted from the branch name
764  String versionPrefix = PaBranchName.getVersion(this.srcRepository.getBranch());
765  if (versionPrefix != null)
766  versionPrefix = versionPrefix + ".";
767  else
768  versionPrefix = "";
769 
770  if (this.txtSrcFeature.getText() != null && !this.txtSrcFeature.getText().isEmpty()) {
771  targetBranch = versionPrefix + this.txtSrcFeature.getText();
772  } else {
773  // we can only pre-insert the version. the feature is not known
774  targetBranch = versionPrefix;
775  }
776  } catch (IOException e) {
777  // Just ignore... if the exception is thrown, we can't identify a valid branch
778  // name
779  }
780 
781  txtTargetBranch.setText(targetBranch);
782  cherryPickSettings.setTargetBranch(targetBranch);
783  }
784 
789  protected void checkRepoAndProposeBranches() {
790  List<String> allBranches;
791  boolean foundIt = false;
792  setMessage(null, IMessageProvider.INFORMATION);
793  setErrorMessage(null);
794 
795  if (cvSrcRepository.getCombo().getText().length() == 0) {
796  setPageComplete(false);
797  return;
798  }
799 
800  String projectname = cvSrcProject.getCombo().getText();
801  String repoName = cvSrcRepository.getCombo().getText();
802 
803  for (GitServerRepository repository : currRepos) {
804  if (repository.getProject().equals(projectname) && repository.getName().equals(repoName)) {
805  allBranches = repository.getBranches();
806  Collections.sort(allBranches);
807  String[] branches = allBranches.stream().toArray(String[]::new);
808  if (btnOnlyShowMaster.getSelection()) {
809  branches = filterMasterBranches(branches);
810  }
811  cvSrcBranch.setInput(branches);
812  foundIt = true;
813  break;
814  }
815  }
816 
817  if (!foundIt) {
818  setMessage("Repository not found!", IMessageProvider.WARNING);
819  setPageComplete(false);
820  btnFetchCommits.setEnabled(false);
821  cvSrcBranch.setInput(new String[0]);
822  return;
823  }
824 
825  cherryPickSettings.setSourceRepoString(projectname, repoName);
826 
827  }
828 
832  protected void proposeRepositories() {
833 
834  String projectname = cvSrcProject.getCombo().getText();
835  if (!projectRepositoryMap.containsKey(projectname)) {
836  setMessage("Project not found!", IMessageProvider.WARNING);
837  setPageComplete(false);
838  cvSrcRepository.setInput(new String[0]);
839  return;
840  }
841 
842  Set<String> repoSet = projectRepositoryMap.get(projectname);
843  String[] repos = new String[repoSet.size()];
844  repoSet.toArray(repos);
845  Arrays.sort(repos);
846  cvSrcRepository.setInput(repos);
847 
848  }
849 
856  private Map<String, Set<String>> fillProjectRepositoryMap() {
857 
858  projectRepositoryMap = new HashMap<String, Set<String>>();
859  for (GitServerRepository repository : currRepos) {
860 
861  Set<String> repositoriesForProject = projectRepositoryMap.get(repository.getProject());
862 
863  if (repositoriesForProject != null) {
864  repositoriesForProject.add(repository.getName());
865  } else {
866  String projectName = repository.getProject();
867  String repositoryName = repository.getName();
868  Set<String> repositories = new HashSet<String>();
869  repositories.add(repositoryName);
870  projectRepositoryMap.put(projectName, repositories);
871  }
872  }
873  return projectRepositoryMap;
874  }
875 
883  private String[] filterMasterBranches(String[] branches) {
884 
885  List<String> masterBranches = new ArrayList<String>();
886  for (String branch : branches) {
887  if (branch.contains("master")) {
888  masterBranches.add(branch);
889  }
890  }
891 
892  String[] masterArray = new String[masterBranches.size()];
893  masterBranches.toArray(masterArray);
894 
895  return masterArray;
896  }
897 
903  @Override
904  protected IDialogSettings getDialogSettings() {
905 
906  IDialogSettings dialogSettings = Activator.getDefault().getDialogSettings();
907  IDialogSettings section = dialogSettings.getSection(PAGE_NAME);
908  if (section == null)
909  section = dialogSettings.addNewSection(PAGE_NAME);
910  return section;
911 
912  }
913 
914 }
static Image getPluginImage(Object plugin, String name)