summaryrefslogtreecommitdiff
path: root/plugins/github/src/org/jetbrains/plugins/github/ui/GithubCreatePullRequestDialog.java
blob: 482ef9492fdc522ed96d5272ae725de70ae4017e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.github.ui;

import com.intellij.CommonBundle;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.ThreeState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.plugins.github.GithubCreatePullRequestWorker;
import org.jetbrains.plugins.github.api.GithubFullPath;
import org.jetbrains.plugins.github.util.GithubNotifications;
import org.jetbrains.plugins.github.util.GithubProjectSettings;
import org.jetbrains.plugins.github.util.GithubSettings;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;

import static org.jetbrains.plugins.github.GithubCreatePullRequestWorker.BranchInfo;
import static org.jetbrains.plugins.github.GithubCreatePullRequestWorker.ForkInfo;

/**
 * @author Aleksey Pivovarov
 */
public class GithubCreatePullRequestDialog extends DialogWrapper {
  @NotNull private final GithubCreatePullRequestPanel myPanel;
  @NotNull private final GithubCreatePullRequestWorker myWorker;
  @NotNull private final GithubProjectSettings myProjectSettings;
  @NotNull private static final CreateRemoteDoNotAskOption ourDoNotAskOption = new CreateRemoteDoNotAskOption();

  public GithubCreatePullRequestDialog(@NotNull final Project project, @NotNull GithubCreatePullRequestWorker worker) {
    super(project, true);
    myWorker = worker;

    myProjectSettings = GithubProjectSettings.getInstance(project);

    myPanel = new GithubCreatePullRequestPanel();
    myPanel.getShowDiffButton().addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        myWorker.showDiffDialog(myPanel.getSelectedBranch());
      }
    });
    myPanel.getSelectForkButton().addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        ForkInfo fork = myWorker.showTargetDialog();
        if (fork != null) {
          myPanel.setForks(myWorker.getForks());
          myPanel.setSelectedFork(fork.getPath());
        }
      }
    });

    myPanel.getForkComboBox().addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.DESELECTED) {
          myPanel.setBranches(Collections.<BranchInfo>emptyList());
        }
        if (e.getStateChange() == ItemEvent.SELECTED) {
          final ForkInfo fork = (ForkInfo)e.getItem();
          if (fork == null) return;

          myPanel.setBranches(fork.getBranches());
          myPanel.setSelectedBranch(fork.getDefaultBranch());

          if (fork.getRemoteName() == null && !fork.isProposedToCreateRemote()) {
            fork.setProposedToCreateRemote(true);
            boolean createRemote = false;

            switch (GithubSettings.getInstance().getCreatePullRequestCreateRemote()) {
              case YES:
                createRemote = true;
                break;
              case NO:
                createRemote = false;
                break;
              case UNSURE:
                createRemote = GithubNotifications.showYesNoDialog(project,
                                                                   "Can't Find Remote",
                                                                   "Configure remote for '" + fork.getPath().getUser() + "'?",
                                                                   ourDoNotAskOption) == Messages.YES;
                break;
            }

            if (createRemote) {
              myWorker.configureRemote(fork);
            }
          }

          if (fork.getRemoteName() == null) {
            myPanel.setDiffEnabled(false);
          }
          else {
            myPanel.setDiffEnabled(true);
            myWorker.launchFetchRemote(fork);
          }
        }
      }
    });

    myPanel.getBranchComboBox().addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          BranchInfo branch = (BranchInfo)e.getItem();
          if (branch == null) return;

          if (branch.getForkInfo().getRemoteName() != null) {
            if (branch.getDiffInfoTask() != null && branch.getDiffInfoTask().isDone() && branch.getDiffInfoTask().safeGet() == null) {
              myPanel.setDiffEnabled(false);
            }
            else {
              myPanel.setDiffEnabled(true);
            }
          }

          if (myPanel.isTitleDescriptionEmptyOrNotModified()) {
            Pair<String, String> description = myWorker.getDefaultDescriptionMessage(branch);
            myPanel.setTitle(description.getFirst());
            myPanel.setDescription(description.getSecond());
          }

          myWorker.launchLoadDiffInfo(branch);
        }
      }
    });

    myPanel.setForks(myWorker.getForks());
    myPanel.setSelectedFork(myProjectSettings.getCreatePullRequestDefaultRepo());
    myPanel.setSelectedBranch(myProjectSettings.getCreatePullRequestDefaultBranch());

    setTitle("Create Pull Request - " + myWorker.getCurrentBranch());
    init();
  }

  @Override
  protected void doOKAction() {
    BranchInfo branch = myPanel.getSelectedBranch();
    if (myWorker.checkAction(branch)) {
      assert branch != null;
      myWorker.createPullRequest(branch, getRequestTitle(), getDescription());

      myProjectSettings.setCreatePullRequestDefaultBranch(branch.getRemoteName());
      myProjectSettings.setCreatePullRequestDefaultRepo(branch.getForkInfo().getPath());

      super.doOKAction();
    }
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    return myPanel.getPanel();
  }

  @Nullable
  @Override
  public JComponent getPreferredFocusedComponent() {
    return myPanel.getPreferredComponent();
  }

  @Override
  protected String getHelpId() {
    return "github.create.pull.request.dialog";
  }

  @Override
  protected String getDimensionServiceKey() {
    return "Github.CreatePullRequestDialog";
  }

  @NotNull
  private String getRequestTitle() {
    return myPanel.getTitle();
  }

  @NotNull
  private String getDescription() {
    return myPanel.getDescription();
  }

  @Nullable
  @Override
  protected ValidationInfo doValidate() {
    if (StringUtil.isEmptyOrSpaces(getRequestTitle())) {
      return new ValidationInfo("Title can't be empty'", myPanel.getTitleTextField());
    }
    return null;
  }

  @TestOnly
  public void testSetRequestTitle(String title) {
    myPanel.setTitle(title);
  }

  @TestOnly
  public void testSetBranch(String branch) {
    myPanel.setSelectedBranch(branch);
  }

  @TestOnly
  public void testCreatePullRequest() {
    myWorker.createPullRequest(myPanel.getSelectedBranch(), getRequestTitle(), getDescription());
  }

  @TestOnly
  public void testSetFork(@NotNull GithubFullPath forkPath) {
    myPanel.setSelectedFork(forkPath);
  }

  private static class CreateRemoteDoNotAskOption implements DoNotAskOption {
    @Override
    public boolean isToBeShown() {
      return true;
    }

    @Override
    public void setToBeShown(boolean value, int exitCode) {
      if (value) {
        GithubSettings.getInstance().setCreatePullRequestCreateRemote(ThreeState.UNSURE);
      }
      else if (exitCode == DialogWrapper.OK_EXIT_CODE) {
        GithubSettings.getInstance().setCreatePullRequestCreateRemote(ThreeState.YES);
      }
      else {
        GithubSettings.getInstance().setCreatePullRequestCreateRemote(ThreeState.NO);
      }
    }

    @Override
    public boolean canBeHidden() {
      return true;
    }

    @Override
    public boolean shouldSaveOptionsOnCancel() {
      return false;
    }

    @NotNull
    @Override
    public String getDoNotShowMessage() {
      return CommonBundle.message("dialog.options.do.not.ask");
    }
  }
}