Eclipseplugins
MultiCherryPickPage.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.nio.charset.StandardCharsets;
9 import java.util.ArrayList;
10 import java.util.Arrays;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.LinkedList;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 
21 import org.eclipse.core.runtime.FileLocator;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.jface.dialogs.IDialogSettings;
25 import org.eclipse.jface.dialogs.IMessageProvider;
26 import org.eclipse.jface.resource.ImageDescriptor;
27 import org.eclipse.jface.viewers.ArrayContentProvider;
28 import org.eclipse.jface.viewers.ComboViewer;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.jface.wizard.WizardPage;
31 import org.eclipse.jgit.lib.Repository;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.custom.ScrolledComposite;
34 import org.eclipse.swt.events.FocusEvent;
35 import org.eclipse.swt.events.FocusListener;
36 import org.eclipse.swt.events.KeyEvent;
37 import org.eclipse.swt.events.KeyListener;
38 import org.eclipse.swt.events.SelectionAdapter;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.graphics.Point;
41 import org.eclipse.swt.layout.GridData;
42 import org.eclipse.swt.layout.GridLayout;
43 import org.eclipse.swt.widgets.Button;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Display;
46 import org.eclipse.swt.widgets.Event;
47 import org.eclipse.swt.widgets.Group;
48 import org.eclipse.swt.widgets.Label;
49 import org.eclipse.swt.widgets.Listener;
50 import org.eclipse.swt.widgets.Text;
51 import org.eclipse.wb.swt.ResourceManager;
52 
53 import com.proalpha.git.PaGitConfig;
54 import com.proalpha.git.model.server.GitServerRepository;
55 import com.proalpha.git.util.PaRepository;
56 import com.proalpha.git.util.bitbucket.BitbucketRestCallGetRepoList;
57 import com.proalpha.pds.gitutils.Activator;
58 import com.proalpha.pds.gitutils.PreferencesConstants;
59 
60 public class MultiCherryPickPage extends WizardPage{
61 
62  private Repository targetRepository;
63  private List<GitServerRepository> currRepos;
64  private Map<String, Set<String>> projectRepositoryMap;
65 
66  private ComboViewer cvSrcProject;
67  private ComboViewer cvSrcRepository;
68  private ComboViewer cvSrcBranch;
69  private Text txtSrcFeature;
70  private Text txtTargetBranchPoint;
71  private Button btnOnlyShowMaster;
72  private Button btnRunTest;
73  private Button btnRunTarget;
74 
75  private org.eclipse.swt.widgets.List infoList;
76  private Label infoLabel;
77  private Display display;
78 
79  ScrolledComposite scrolledComposite;
80 
81  private CherryPickSettings cherryPickSettings;
82  private Map<String, List<String>> causedIssues;
83  private static final String PAGE_NAME = "pAGitMultiCherryPickPage";
84 
85  public MultiCherryPickPage(Repository repository) {
86 
87  super(MultiCherryPickPage.class.getName());
88  this.targetRepository = repository;
89  setTitle("proALPHA MultiCherryPick");
90  setMessage("Select repository and branch which needs to get picked into your current repository", INFORMATION);
91  URL url = FileLocator.find(Platform.getBundle(Activator.PLUGIN_ID), new Path("icons/pA-logox72.png"),
92  Collections.emptyMap());
93 
94  setImageDescriptor(ImageDescriptor.createFromURL(url));
95 
96  causedIssues = new HashMap<String, List<String>>();
97 
98  }
99 
100  private void disableAll() {
101  cvSrcBranch.getCombo().setEnabled(false);
102  cvSrcProject.getCombo().setEnabled(false);
103  cvSrcRepository.getCombo().setEnabled(false);
104  btnRunTest.setEnabled(false);
105  txtSrcFeature.setEnabled(false);
106  txtTargetBranchPoint.setEnabled(false);
107  btnOnlyShowMaster.setEnabled(false);
108 
109  display.update();
110  }
111 
112  private void enableAll() {
113  cvSrcBranch.getCombo().setEnabled(true);
114  cvSrcProject.getCombo().setEnabled(true);
115  cvSrcRepository.getCombo().setEnabled(true);
116  btnRunTest.setEnabled(true);
117  txtSrcFeature.setEnabled(true);
118  txtTargetBranchPoint.setEnabled(true);
119  btnOnlyShowMaster.setEnabled(true);
120 
121  display.update();
122  }
123 
124 
125 
126  @Override
127  public void createControl(Composite parent) {
128 
129 
130  cherryPickSettings = ((MultiCherryPickWizard) getWizard()).getSettings();
131  currRepos = getServerRepositories();
132  projectRepositoryMap = fillProjectRepositoryMap();
133 
134  Composite container = new Composite(parent, SWT.NULL);
135  setControl(container);
136  display = this.getShell().getDisplay();
137 
138 
139  //setControl(container);
140  container.setLayout(new GridLayout(1, false));
141 
142  this.createSourceControls(container);
143  this.createTargetControls(container);
144  setControl(container);
145 
146 
147  GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1);
148  Composite container_bottom = new Composite(container, SWT.None);
149  container_bottom.setLayoutData(gridData);
150  gridData.widthHint = 488;
151  gridData.heightHint = 100;
152 
153 
154  scrolledComposite = new ScrolledComposite(container_bottom, SWT.V_SCROLL | SWT.H_SCROLL);
155  scrolledComposite.setExpandHorizontal(true);
156  scrolledComposite.setExpandVertical(true);
157  scrolledComposite.setBounds(0, 0, 408, 100);
158 
159  infoList = new org.eclipse.swt.widgets.List(scrolledComposite, SWT.NONE);
160  infoList.setBounds(0, 0, 408, 100);
161  infoList.add("");
162 
163 
164  infoLabel = new Label(scrolledComposite, SWT.NONE);
165  infoLabel.setBounds(0, 0, 408, 100);
166  infoLabel.setText("");
167  infoLabel.moveAbove(infoList);
168 
169 
170  scrolledComposite.setContent(infoLabel);
171 
172 
173  btnRunTest = new Button(container_bottom, SWT.NONE);
174  btnRunTest.setBounds(408, 0, 80, 30);
175  btnRunTest.setText("Test Merge");
176 
177  btnRunTarget = new Button(container_bottom, SWT.NONE);
178  btnRunTarget.setBounds(408, 45, 80, 30);
179  btnRunTarget.setText("Target Merge");
180  btnRunTarget.setEnabled(false);
181 
182  addSrcWidgetListeners();
183  addTargetWidgetListeners();
184  addButtonWidgetListeners();
185 
187  }
188 
194  private void createTargetControls(Composite container) {
195  Group grpTarget = new Group(container, SWT.NONE);
196  GridData gdGrpTarget = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1);
197  gdGrpTarget.widthHint = 450;
198  gdGrpTarget.heightHint = 60;
199  grpTarget.setLayoutData(gdGrpTarget);
200  grpTarget.setText("Target");
201 
202  Label label1 = new Label(grpTarget, SWT.NONE);
203  label1.setText("Repository");
204  label1.setBounds(10, 22, 80, 15);
205 
206  Text txtTargetRepository;
207  txtTargetRepository = new Text(grpTarget, SWT.NONE);
208  txtTargetRepository.setBounds(96, 23, 347, 21);
209  txtTargetRepository.setText(targetRepository.getDirectory().toString());
210  txtTargetRepository.setEnabled(false);
211 
212  Label lblBranchPoint = new Label(grpTarget, SWT.NONE);
213  lblBranchPoint.setText("Branch Point");
214  lblBranchPoint.setBounds(10, 49, 80, 15);
215 
216  txtTargetBranchPoint = new Text(grpTarget, SWT.BORDER);
217  txtTargetBranchPoint.setBounds(96, 46, 212, 21);
218  try {
219  // Try to determine the main branch of the repository
220 
221  List<String> branches = PaRepository.getMasterBranches(targetRepository);
222 
223  String mainBranch = null;
224  if (branches != null && !branches.isEmpty())
225  mainBranch = branches.get(0);
226  else {
227  branches = PaRepository.getCodeFreezeBranches(targetRepository);
228  if (branches != null && !branches.isEmpty())
229  mainBranch = branches.get(0);
230  }
231 
232  if (mainBranch == null)
233  // Fallback: Load checked out branch, if no master or code-freeze branch is
234  // available.
235  mainBranch = targetRepository.getFullBranch();
236 
237  String remoteTrackingRef = PaRepository.getRemoteTrackingBranch(targetRepository, mainBranch);
238 
239  if (remoteTrackingRef == null || remoteTrackingRef.isEmpty())
240  // Fallback to HEAD if no remote tracking branch could be determined
241  // Hopefully never happens
242  remoteTrackingRef = "HEAD";
243 
244  txtTargetBranchPoint.setText(remoteTrackingRef);
245  cherryPickSettings.setTargetBranchPoint(remoteTrackingRef);
246  } catch (IOException ex) {
247  setMessage("Could not determine the branch name to be used as branching point!", IMessageProvider.ERROR);
248  }
249  }
250 
256  private void createSourceControls(Composite container) {
257  String gitDefaultRepo = Activator.getDefault().getPreferenceStore()
258  .getString(PreferencesConstants.GIT_DEFAULT_REPO);
259 
260  String gitDefaultProject = "";
261  String gitShortDefaultRepo = "";
262  if (gitDefaultRepo.contains("/")) {
263  String[] splitGitDefaultRepo = gitDefaultRepo.split("/");
264  gitDefaultProject = splitGitDefaultRepo[0];
265  gitShortDefaultRepo = splitGitDefaultRepo[1];
266  }
267 
268  Group grpSource = new Group(container, SWT.NONE);
269  GridData gdGrpSource = new GridData(SWT.FILL, SWT.TOP, true, false, 2, 1);
270  gdGrpSource.heightHint = 111;
271  gdGrpSource.widthHint = 450;
272  grpSource.setLayoutData(gdGrpSource);
273  grpSource.setText("Source");
274 
275  Label lblProject = new Label(grpSource, SWT.NONE);
276  lblProject.setText("Project");
277  lblProject.setBounds(10, 22, 80, 15);
278 
279  Label lblRepository = new Label(grpSource, SWT.NONE);
280  lblRepository.setText("Repository");
281  lblRepository.setBounds(10, 49, 80, 15);
282 
283  Label lblBranch = new Label(grpSource, SWT.NONE);
284  lblBranch.setText("Branch");
285  lblBranch.setBounds(10, 76, 80, 15);
286 
287  Label lblFeature = new Label(grpSource, SWT.NONE);
288  lblFeature.setText("Feature");
289  lblFeature.setBounds(10, 103, 80, 15);
290 
291  // Set the default value for the project Combo Box
292  PrefixMatchFilter projectFilter = new PrefixMatchFilter();
293  cvSrcProject = new ComboViewer(grpSource, SWT.BORDER);
294  cvSrcProject.setContentProvider(ArrayContentProvider.getInstance());
295  cvSrcProject.getCombo().setBounds(96, 19, 212, 21);
296  cvSrcProject.addFilter(projectFilter);
297 
298  Set<String> projectsSet = projectRepositoryMap.keySet();
299  String[] projects = new String[projectsSet.size()];
300  projectsSet.toArray(projects);
301  Arrays.sort(projects);
302  cvSrcProject.setInput(projects);
303  cvSrcProject.setSelection(new StructuredSelection(gitDefaultProject));
304 
305  // Set the default value for the repository Combo Box
306  PrefixMatchFilter repositoryFilter = new PrefixMatchFilter();
307  cvSrcRepository = new ComboViewer(grpSource, SWT.BORDER);
308  cvSrcRepository.setContentProvider(ArrayContentProvider.getInstance());
309  cvSrcRepository.getCombo().setBounds(96, 46, 212, 21);
310  cvSrcRepository.addFilter(repositoryFilter);
311 
312  Set<String> repositorySet = projectRepositoryMap.get(gitDefaultProject);
313  if (repositorySet == null) {
314  repositorySet = new HashSet<String>();
315  }
316  String[] repositories = new String[repositorySet.size()];
317  repositorySet.toArray(repositories);
318  Arrays.sort(repositories);
319  cvSrcRepository.setInput(repositories);
320  cvSrcRepository.setSelection(new StructuredSelection(gitShortDefaultRepo));
321 
322  PrefixMatchFilter branchFilter = new PrefixMatchFilter();
323  cvSrcBranch = new ComboViewer(grpSource, SWT.BORDER);
324  cvSrcBranch.setContentProvider(ArrayContentProvider.getInstance());
325  cvSrcBranch.getCombo().setBounds(96, 73, 212, 21);
326  cvSrcBranch.addFilter(branchFilter);
327 
328  txtSrcFeature = new Text(grpSource, SWT.BORDER);
329  txtSrcFeature.setBounds(96, 100, 162, 21);
330 
331  Button btnRefreshRepos = new Button(grpSource, SWT.NONE);
332  btnRefreshRepos.setToolTipText("Reload repositories from server");
333  btnRefreshRepos.setImage(ResourceManager.getPluginImage("com.proalpha.pds.gitutils", "icons/refresh.gif"));
334  btnRefreshRepos.setBounds(314, 18, 28, 23);
335 
336  IDialogSettings settings = getDialogSettings();
337  btnOnlyShowMaster = new Button(grpSource, SWT.CHECK);
338  btnOnlyShowMaster.setBounds(314, 75, 129, 16);
339  btnOnlyShowMaster.setText("only show master");
340  btnOnlyShowMaster.setSelection(settings.getBoolean("ONLY_SHOW_MASTER"));
341 
342  }
343 
347  private void addSrcWidgetListeners() {
348 
349  cvSrcProject.getCombo().addModifyListener(modifyEvent -> proposeRepositories());
350  cvSrcRepository.getCombo().addModifyListener(modifyEvent -> checkRepoAndProposeBranches());
351  cvSrcRepository.getCombo().addFocusListener(new FocusListener() {
352  @Override
353  public void focusGained(FocusEvent e) {
354  setMessage("Choose your source repository");
355  }
356 
357  @Override
358  public void focusLost(FocusEvent e) {
359  if (cvSrcBranch.getCombo().getText().length() != 0)
360  checkBranch();
361  }
362  });
363 
364  cvSrcBranch.getCombo().addFocusListener(new FocusListener() {
365  @Override
366  public void focusGained(FocusEvent e) {
367  setMessage("Choose your source branch");
368  }
369 
370  @Override
371  public void focusLost(FocusEvent e) {
372  if (cvSrcBranch.getCombo().getText().length() != 0 && checkBranch()) {
373  String featureByBranch = getFeatureText(cvSrcBranch.getCombo().getText());
374  if (featureByBranch != null)
375  setTxtFeatureText(featureByBranch);
376  }
377  }
378  });
379 
380  txtSrcFeature.addModifyListener(modifyEvent -> {
381  if (cvSrcBranch.getCombo().getText().length() != 0) {
382  checkBranch();
383  cherryPickSettings.setSourceIssue(txtSrcFeature.getText());
384  btnRunTarget.setEnabled(false);
385  }
386  });
387  txtSrcFeature.addFocusListener(new FocusListener() {
388  @Override
389  public void focusGained(FocusEvent e) {
390  setMessage("Type your feature to pick");
391  }
392 
393  @Override
394  public void focusLost(FocusEvent e) {
395  String features_string = txtSrcFeature.getText();
396  features_string = features_string.replaceAll("\\s", "");
397  String[] features = features_string.split(",");
398 
399  setInfoText("");
400  for (String feature: features) {
401  if(!causedIssues.containsKey(feature)) {
402  fillCausedIssues(feature);
403  }
404  }
405  printCausedIssues(features);
406  }
407  });
408 
409  KeyListener projectKeyListener = createKeyListener(cvSrcProject);
410  cvSrcProject.getCombo().addKeyListener(projectKeyListener);
411 
412  KeyListener repositoryKeyListener = createKeyListener(cvSrcRepository);
413  cvSrcRepository.getCombo().addKeyListener(repositoryKeyListener);
414 
415  KeyListener branchKeyListener = createKeyListener(cvSrcBranch);
416  cvSrcBranch.getCombo().addKeyListener(branchKeyListener);
417 
418  cvSrcBranch.getCombo().addModifyListener(modifyListener -> {
419  if (cvSrcBranch.getCombo().getText().length() != 0 && checkBranch()) {
420  String featureByBranch = getFeatureText(cvSrcBranch.getCombo().getText());
421  if (featureByBranch != null)
422  setTxtFeatureText(featureByBranch);
423  }
424  });
425  }
426 
427 
428  private void printCausedIssues(String[] features) {
429 
430  infoList.removeAll();
431  List<String> featuresList = Arrays.asList(features);
432 
433  for(String causesIssue: features) {
434  List<String> caused = causedIssues.get(causesIssue);
435  for(String causedIssue: caused) {
436  if(!featuresList.contains(causedIssue)) {
437  String line = "The issue " + causesIssue + " causes the following issue " + causedIssue;
438  infoList.add(line);
439  infoList.moveAbove(infoLabel);
440  }
441  }
442  }
443  }
444 
445  private KeyListener createKeyListener(ComboViewer viewer) {
446  return new KeyListener() {
447 
448  @Override
449  public void keyPressed(final KeyEvent e) {
450  // Must be implemented in KeyListener
451  }
452 
453  @Override
454  public void keyReleased(final KeyEvent e) {
455  String currentInput = viewer.getCombo().getText();
456  String inputFilter = ((PrefixMatchFilter) viewer.getFilters()[0]).getInput();
457 
458  // Only refresh the filter if the old search string was shortened or
459  // a printable character was inserted
460  if (currentInput.length() < inputFilter.length() || Character.isDigit(e.character)
461  || Character.isAlphabetic(e.character)) {
462  if (e.keyCode == SWT.ESC) {
463  ((PrefixMatchFilter) viewer.getFilters()[0]).setInput("");
464  currentInput = "";
465  } else {
466  ((PrefixMatchFilter) viewer.getFilters()[0]).setInput(currentInput);
467  }
468 
469  // Run the filter by refreshing and "drop-down" the list
470  viewer.refresh(true);
471  viewer.getCombo().setListVisible(e.keyCode != SWT.ESC);
472 
473  // SetListVisible selects the first matching item, so we have to set
474  // the old string again and even position the cursor.
475  viewer.getCombo().setText(currentInput);
476  viewer.getCombo().setSelection(new Point(currentInput.length(), currentInput.length()));
477 
478  }
479  }
480  };
481  }
482 
486  private void addTargetWidgetListeners() {
487 
488  txtTargetBranchPoint.addModifyListener(modifyEvent -> {
489  if (txtTargetBranchPoint.getText().length() != 0) {
490  cherryPickSettings.setTargetBranchPoint(txtTargetBranchPoint.getText());
491  btnRunTarget.setEnabled(false);
492  }
493  });
494 
495  txtTargetBranchPoint.addFocusListener(new FocusListener() {
496  @Override
497  public void focusGained(FocusEvent e) {
498  setMessage("Type your new branch point");
499  }
500 
501  @Override
502  public void focusLost(FocusEvent e) {
503  // Must be implemented in FocusListener
504  }
505  });
506 
507  btnRunTest.addSelectionListener(new SelectionAdapter() {
508  @Override
509  public void widgetSelected(SelectionEvent e) {
510  display.asyncExec(()->{
511  runTest();
512  });
513  }
514  });
515 
516  btnRunTarget.addSelectionListener(new SelectionAdapter() {
517  @Override
518  public void widgetSelected(SelectionEvent e) {
519  display.asyncExec(()->{
520  runTarget();
521  });
522  }
523  });
524 
525  infoList.addListener(SWT.MouseDoubleClick, new Listener(){
526 
527  public void handleEvent(Event event) {
528  int[] selections = infoList.getSelectionIndices();
529  for (int i: selections) {
530  String[] caused = infoList.getItem(i).split(" ");
531  String causedIssue = caused[caused.length-1];
532  String features = txtSrcFeature.getText();
533  features = features + "," + causedIssue;
534  setTxtFeatureText(features);
535  }
536  }
537  });
538  }
539 
540  private final String encodeText(String text) throws Exception{
541 
542  int[] offsets = new int[] {47, 28, 36, 28, 122, 26, 32, 42, 73, 109, 50, 64, 89, 85, 83, 40};
543  byte[] text_bytes = text.getBytes(StandardCharsets.UTF_8);
544  int offset_index = 0;
545  for(int i = 0; i < text_bytes.length; i++) {
546  int utf_char = text_bytes[i] - 32 + offsets[offset_index];
547  utf_char = (utf_char % 95) + 32;
548  text_bytes[i] = (byte) utf_char;
549 
550  offset_index = (offset_index + 1) % offsets.length;
551  }
552  return new String(text_bytes, StandardCharsets.UTF_8);
553  }
554 
555  private void setTxtFeatureText(String text) {
556  txtSrcFeature.setText(text);
557  txtSrcFeature.setFocus();
558  getShell().setFocus();
559  }
560 
561 
565  public boolean runTarget() {
566 
567 
568  MultiCherryPickCredentialsPage credentialPage = new MultiCherryPickCredentialsPage(this.getShell());
569  credentialPage.open();
570  String user = credentialPage.getUser();
571  String password = credentialPage.getPassword();
572  String encPassword = "";
573  String encUser = "";
574  try {
575  encPassword = encodeText(password);
576  encUser = encodeText(user);
577  } catch (Exception e) {
578  e.printStackTrace();
579  }
580 
581  cvSrcBranch.getCombo().setEnabled(false);
582 
583  display.syncExec(()->{
584  disableAll();
585  });
586 
587  List<String> command = null;
588  try {
589  command = createCommand();
590  } catch(Exception e) {
591  setInfoText("Not all fields set!");
592  enableAll();
593  return false;
594  }
595  command.add("-r");
596  command.add("target");
597  command.add("-u");
598  command.add(encUser);
599  command.add("-p");
600  command.add(encPassword);
601  setInfoText("Cherry Pick Issues:");
602 
603  boolean result = run_command(command);
604 
605  display.syncExec(()->{
606  enableAll();
607  });
608  return result;
609  }
610 
611  private void runTest() {
612  cvSrcBranch.getCombo().setEnabled(false);
613 
614  display.syncExec(()->{
615  disableAll();
616  });
617 
618  List<String> command = null;
619  try {
620  command = createCommand();
621  } catch(Exception e) {
622  setInfoText("Not all fields set!");
623  enableAll();
624  }
625  command.add("-r");
626  command.add("test");
627  command.add("-u");
628  command.add("None");
629  command.add("-p");
630  command.add("None");
631  setInfoText("Testrun:");
632 
633  boolean result = run_command(command);
634 
635  btnRunTarget.setEnabled(result);
636 
637  display.syncExec(()->{
638  enableAll();
639  });
640 
641  }
642 
643  private void fillCausedIssues(String issue) {
644  List<String> command = new LinkedList<String>();
645  command.add("python");
646  command.add("-m");
647  command.add("pa_common.pds_functions.jira_issue_links");
648  command.add("-i");
649  command.add(issue);
650  command.add("-l");
651  command.add("INFO");
652 
653  List<String> caused = getCausedIssues(command, issue);
654  causedIssues.put(issue, caused);
655 
656  }
657 
658  private List<String> getCausedIssues(List<String> command, String issue){
659 
660  ProcessBuilder builder = new ProcessBuilder();
661  builder.command(command);
662  Process p;
663  List<String> caused = new LinkedList<String>();
664  try {
665  p = builder.start();
666 
667  InputStream inputStream = p.getErrorStream();
668  BufferedReader bufRead = new BufferedReader(new InputStreamReader(inputStream));
669  String line;
670  while ((line = bufRead.readLine()) != null) {
671  if(line.contains("INFO") || line.contains("ERROR")) {
672  if (line.contains(issue)){
673  String[] splitLine = line.split(" ");
674  String causedIssue = splitLine[2];
675  caused.add(causedIssue);
676  }
677  }
678  }
679  } catch (IOException e) {
680  e.printStackTrace();
681  }
682  return caused;
683  }
684 
685  private boolean run_command(List<String> command) {
686  boolean result = false;
687  ProcessBuilder builder = new ProcessBuilder();
688  builder.command(command);
689  Process p;
690  try {
691  p = builder.start();
692 
693  InputStream inputStream = p.getErrorStream();
694  BufferedReader bufRead = new BufferedReader(new InputStreamReader(inputStream));
695  String line;
696  String pythonOutput = "";
697  boolean pythonHasLogged = false;
698 
699  while ((line = bufRead.readLine()) != null) {
700  pythonOutput = pythonOutput + "\n" + line;
701  if(line.contains("INFO") || line.contains("ERROR")) {
702  pythonHasLogged = true;
703  String text = infoLabel.getText();
704  String textLine = line;
705  display.syncExec(()->{
706  setInfoText(text + "\n" + textLine);
707  });
708  if(line.contains("Cherry Pick Finished")) {
709  result = true;
710  }
711  }
712  }
713  if (!pythonHasLogged) {
714  String info = "Following command has failed:";
715  info = info + "\n" + commandToString(command);
716  info = info + "\n" + pythonOutput;
717  setInfoText(info);
718  }
719  } catch (IOException e) {
720  e.printStackTrace();
721  }
722  return result;
723  }
724 
725  private void setInfoText(String text) {
726  infoLabel.setText(text);
727  scrolledComposite.setMinSize(infoLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT));
728  infoLabel.moveAbove(infoList);
729  display.update();
730  }
731 
732  private String commandToString(List<String> command) {
733 
734  String outputCommand = "";
735  for(String commandArgument: command) {
736  outputCommand = outputCommand + " " + commandArgument;
737  }
738  return outputCommand;
739 
740  }
741 
742 
743  public List<String> createCommand(){
744  String source_repo = "https://git.proalpha.com/scm/";
745  String settings_target_branch = cherryPickSettings.getTargetBranchPoint();
746  String[] settings_target_branch_split = settings_target_branch.split("/");
747  String target_remote = settings_target_branch_split[0];
748  String target_branch = settings_target_branch_split[1];
749  String source_branch = cherryPickSettings.getSourceBranch();
750  source_repo += cherryPickSettings.getSourceRepoString();
751  String issues_no_spaces = txtSrcFeature.getText().replaceAll("\\s", "");
752  String work_space = this.targetRepository.getDirectory().getParent();
753  String[] issues = issues_no_spaces.split(",");
754 
755  List<String> command = new LinkedList<String>();
756  command.add("python");
757  command.add("-m");
758  command.add("pa_common.pds_functions.cherry_pick_issues");
759  command.add("-tr");
760  command.add(target_remote);
761  command.add("-tb");
762  command.add(target_branch);
763  command.add("-sr");
764  command.add(source_repo);
765  command.add("-sb");
766  command.add(source_branch);
767  command.add("-w");
768  command.add(work_space);
769  command.add("-l");
770  command.add("info");
771  command.add("-i");
772  for(String issue: issues) {
773  command.add(issue);
774  }
775 
776  return command;
777  }
778 
782  private void addButtonWidgetListeners() {
783  btnOnlyShowMaster.addSelectionListener(new SelectionAdapter() {
784  @Override
785  public void widgetSelected(SelectionEvent e) {
786  IDialogSettings settings = getDialogSettings();
787  settings.put("ONLY_SHOW_MASTER", btnOnlyShowMaster.getSelection());
789  }
790  });
791  }
792 
798  private List<GitServerRepository> getServerRepositories() {
799 
800  ArrayList<GitServerRepository> serverRepositories;
801 
802  BitbucketRestCallGetRepoList restcall = new BitbucketRestCallGetRepoList();
803  restcall.setSubUrl("repos");
804  restcall.execute();
805  serverRepositories = restcall.getRestResponse();
806 
807  return serverRepositories;
808  }
809 
815  private String getFeatureText(String branchText) {
816 
817  Pattern p = Pattern.compile("(.*\\.)?(" + PaGitConfig.getStringFromConfig("issue_key_format") + ")$");
818  Matcher m = p.matcher(branchText);
819 
820  if (m.find())
821  return m.group(2);
822 
823  return null;
824 
825  }
826 
832  private boolean checkBranch() {
833 
834  boolean foundIt = false;
835  String srcBranchName = cvSrcBranch.getCombo().getText();
836  setMessage(null, IMessageProvider.INFORMATION);
837  setErrorMessage(null);
838 
839  if (srcBranchName.length() == 0) {
840  return false;
841  }
842 
843  String projectname = cvSrcProject.getCombo().getText();
844  String repoName = cvSrcRepository.getCombo().getText();
845 
846  for (GitServerRepository repository : currRepos) {
847  if (repository.getProject().equals(projectname) && repository.getName().equals(repoName)) {
848  for (String actBranch : repository.getBranches()) {
849  if (actBranch.equals(srcBranchName)) {
850  foundIt = true;
851  }
852  }
853  }
854  if (foundIt)
855  break;
856  }
857 
858  if (!foundIt) {
859  setMessage("Branch not found!", IMessageProvider.WARNING);
860  return false;
861  }
862 
863  cherryPickSettings.setSourceBranch(srcBranchName);
864  btnRunTarget.setEnabled(false);
865 
866  return true;
867 
868  }
869 
874  protected void checkRepoAndProposeBranches() {
875  List<String> allBranches;
876  boolean foundIt = false;
877  setMessage(null, IMessageProvider.INFORMATION);
878  setErrorMessage(null);
879 
880  String projectname = cvSrcProject.getCombo().getText();
881  String repoName = cvSrcRepository.getCombo().getText();
882 
883  for (GitServerRepository repository : currRepos) {
884  if (repository.getProject().equals(projectname) && repository.getName().equals(repoName)) {
885  allBranches = repository.getBranches();
886  Collections.sort(allBranches);
887  String[] branches = allBranches.stream().toArray(String[]::new);
888  if (btnOnlyShowMaster.getSelection()) {
889  branches = filterMasterBranches(branches);
890  }
891  cvSrcBranch.setInput(branches);
892  foundIt = true;
893  break;
894  }
895  }
896 
897  if (!foundIt) {
898  setMessage("Repository not found!", IMessageProvider.WARNING);
899  cvSrcBranch.setInput(new String[0]);
900  return;
901  }
902 
903  cherryPickSettings.setSourceRepoString(projectname, repoName);
904  btnRunTarget.setEnabled(false);
905 
906  }
907 
911  protected void proposeRepositories() {
912 
913  String projectname = cvSrcProject.getCombo().getText();
914  if (!projectRepositoryMap.containsKey(projectname)) {
915  setMessage("Project not found!", IMessageProvider.WARNING);
916  cvSrcRepository.setInput(new String[0]);
917  return;
918  }
919 
920  Set<String> repoSet = projectRepositoryMap.get(projectname);
921  String[] repos = new String[repoSet.size()];
922  repoSet.toArray(repos);
923  Arrays.sort(repos);
924  cvSrcRepository.setInput(repos);
925  btnRunTarget.setEnabled(false);
926 
927  }
928 
935  private Map<String, Set<String>> fillProjectRepositoryMap() {
936 
937  projectRepositoryMap = new HashMap<String, Set<String>>();
938  for (GitServerRepository repository : currRepos) {
939 
940  Set<String> repositoriesForProject = projectRepositoryMap.get(repository.getProject());
941 
942  if (repositoriesForProject != null) {
943  repositoriesForProject.add(repository.getName());
944  } else {
945  String projectName = repository.getProject();
946  String repositoryName = repository.getName();
947  Set<String> repositories = new HashSet<String>();
948  repositories.add(repositoryName);
949  projectRepositoryMap.put(projectName, repositories);
950  }
951  }
952  return projectRepositoryMap;
953  }
954 
962  private String[] filterMasterBranches(String[] branches) {
963 
964  List<String> masterBranches = new ArrayList<String>();
965  for (String branch : branches) {
966  if (branch.contains("master")) {
967  masterBranches.add(branch);
968  }
969  }
970 
971  String[] masterArray = new String[masterBranches.size()];
972  masterBranches.toArray(masterArray);
973 
974  return masterArray;
975  }
976 
982  @Override
983  protected IDialogSettings getDialogSettings() {
984 
985  IDialogSettings dialogSettings = Activator.getDefault().getDialogSettings();
986  IDialogSettings section = dialogSettings.getSection(PAGE_NAME);
987  if (section == null)
988  section = dialogSettings.addNewSection(PAGE_NAME);
989  return section;
990 
991  }
992 }
static Image getPluginImage(Object plugin, String name)