Skip to content

Commit ecdcabb

Browse files
committed
Merge pull request #37 from junichi11/new-pull-request
Support for a new pull request
2 parents cb766fa + 6ded960 commit ecdcabb

6 files changed

Lines changed: 240 additions & 29 deletions

File tree

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/GitHubIssue.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,16 @@ public GitHubIssueController getController() {
295295

296296
public Issue submitNewIssue(CreateIssueParams params) {
297297
Issue newIssue = repository.submitNewIssue(params);
298+
setNewIssue(newIssue);
299+
return newIssue;
300+
}
301+
302+
/**
303+
* Set a new issue. Add GitHubIssue to the issue cache.
304+
*
305+
* @param newIssue a new Issue
306+
*/
307+
private void setNewIssue(Issue newIssue) {
298308
if (newIssue != null) {
299309
setIssue(newIssue);
300310
// add to cache
@@ -305,7 +315,6 @@ public Issue submitNewIssue(CreateIssueParams params) {
305315
fireScheduleChange();
306316
setIssueStatus(Status.SEEN);
307317
}
308-
return newIssue;
309318
}
310319

311320
public Issue editIssue(CreateIssueParams params) {
@@ -360,6 +369,25 @@ public PullRequest createPullRequest(String head, String base) throws IOExceptio
360369
return pullRequest;
361370
}
362371

372+
/**
373+
* Create a new pull request. Title, body, base and head can be set.
374+
*
375+
* @param pullRequest PullRequest
376+
* @return PullRequest if new pull request has been created, otherwise
377+
* {@code null}
378+
* @throws IOException
379+
*/
380+
@CheckForNull
381+
public PullRequest createPullRequest(PullRequest pullRequest) throws IOException {
382+
GitHubRepository repo = getRepository();
383+
PullRequest newPullRequest = repo.createPullRequest(pullRequest);
384+
if (newPullRequest != null) {
385+
Issue newIssue = repo.getIssue(newPullRequest.getNumber());
386+
setNewIssue(newIssue);
387+
}
388+
return newPullRequest;
389+
}
390+
363391
public boolean isCreatedUser() {
364392
if (issue == null) {
365393
return false;

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/GitHubIssueController.java

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.eclipse.egit.github.core.Issue;
7070
import org.eclipse.egit.github.core.Milestone;
7171
import org.eclipse.egit.github.core.PullRequest;
72+
import org.eclipse.egit.github.core.PullRequestMarker;
7273
import org.eclipse.egit.github.core.Repository;
7374
import org.eclipse.egit.github.core.RepositoryBranch;
7475
import org.eclipse.egit.github.core.RepositoryCommitCompare;
@@ -316,6 +317,7 @@ private SubmitIssueAction() {
316317
}
317318

318319
@NbBundle.Messages({
320+
"SubmitIssueAction.message.pull.request.added.fail=The pull request has not been added.",
319321
"SubmitIssueAction.message.issue.added.fail=The issue has not been added.",
320322
"SubmitIssueAction.message.issue.updated.fail=The issue has not been updated."
321323
})
@@ -334,13 +336,36 @@ public void run() {
334336
GitHubIssue issue = p.getIssue();
335337
CreateIssueParams issueParams = getCreateIssueParams(issue.isNew(), p);
336338
if (issue.isNew()) {
337-
// add issue
338-
Issue newIssue = issue.submitNewIssue(issueParams);
339-
if (newIssue != null) {
340-
p.update();
339+
if (p.isNewPullRequestSelected()) {
340+
// add pull request
341+
// can be added only title and body to a pull request
342+
// add other than those after the pull request was created
343+
PullRequest newPullRequest = p.getNewPullRequest();
344+
if (newPullRequest != null) {
345+
newPullRequest.setTitle(issueParams.getTitle())
346+
.setBody(issueParams.getBody());
347+
try {
348+
PullRequest createdPullRequest = issue.createPullRequest(newPullRequest);
349+
if (createdPullRequest != null) {
350+
issue.editIssue(issueParams);
351+
p.update();
352+
} else {
353+
// show dialog
354+
UiUtils.showErrorDialog(Bundle.SubmitIssueAction_message_pull_request_added_fail());
355+
}
356+
} catch (IOException ex) {
357+
UiUtils.showErrorDialog(ex.getMessage());
358+
}
359+
}
341360
} else {
342-
// show dialog
343-
UiUtils.showErrorDialog(Bundle.SubmitIssueAction_message_issue_added_fail());
361+
// add issue
362+
Issue newIssue = issue.submitNewIssue(issueParams);
363+
if (newIssue != null) {
364+
p.update();
365+
} else {
366+
// show dialog
367+
UiUtils.showErrorDialog(Bundle.SubmitIssueAction_message_issue_added_fail());
368+
}
344369
}
345370
} else {
346371
// edit issue
@@ -482,15 +507,29 @@ private boolean closeReopen() {
482507
@NbBundle.Messages({
483508
"CreatePullRequestAction.confirmation.message=Do you want to change this issue to Pull Request?",
484509
"CreatePullRequestAction.error.message.same.branch=Another branch must be set.",
485-
"CreatePullRequestAction.descriptor.title=Change to Pull Request"
510+
"CreatePullRequestAction.descriptor.title=Pull Request"
486511
})
487512
public class CreatePullRequestAction implements ActionListener {
488513

489514
@Override
490515
public void actionPerformed(ActionEvent e) {
516+
String actionCommand = e.getActionCommand();
517+
final boolean isNewPullRequest = actionCommand.equals("New PR"); // NOI18N
491518
GitHubIssuePanel p = getPanel();
492519
p.setCreatePullRequestButtonEnabled(false);
493520
final GitHubIssue issue = p.getIssue();
521+
522+
if (isNewPullRequest && !p.isNewPullRequestSelected()) {
523+
// remove the new pull request from the panle
524+
SwingUtilities.invokeLater(new Runnable() {
525+
@Override
526+
public void run() {
527+
getPanel().setNewPullRequest(null);
528+
}
529+
});
530+
return;
531+
}
532+
494533
RequestProcessor rp = GitHubIssues.getInstance().getRequestProcessor();
495534
rp.post(new Runnable() {
496535
@Override
@@ -533,7 +572,7 @@ public void stateChanged(ChangeEvent e) {
533572
createPullRequestPanel.addChangeListener(changeListener);
534573
ComparePullRequestPropertyChangeListener propertyChangeListener = new ComparePullRequestPropertyChangeListener(repository, createPullRequestPanel, descriptor);
535574
createPullRequestPanel.addPropertyChangeListener(propertyChangeListener);
536-
propertyChangeListener.propertyChange(null);
575+
changeListener.stateChanged(null);
537576

538577
// show dialog
539578
if (DialogDisplayer.getDefault().notify(descriptor) == NotifyDescriptor.OK_OPTION) {
@@ -543,13 +582,27 @@ public void stateChanged(ChangeEvent e) {
543582
String headBranch = mySelf.getLogin() + ":" + selectedHeadBranch.getName(); // NOI18N
544583
PullRequest pullRequest;
545584
try {
546-
pullRequest = issue.createPullRequest(headBranch, baseBranch);
547-
if (pullRequest != null) {
548-
getPanel().refresh();
585+
if (isNewPullRequest) {
586+
// set new pull request to panel
587+
PullRequestMarker baseMarker = new PullRequestMarker();
588+
baseMarker.setLabel(baseBranch);
589+
PullRequestMarker headMarker = new PullRequestMarker();
590+
headMarker.setLabel(headBranch);
591+
PullRequest newPullRequest = new PullRequest()
592+
.setBase(baseMarker)
593+
.setHead(headMarker);
594+
getPanel().setNewPullRequest(newPullRequest);
595+
} else {
596+
pullRequest = issue.createPullRequest(headBranch, baseBranch);
597+
if (pullRequest != null) {
598+
getPanel().refresh();
599+
}
549600
}
550601
} catch (IOException ex) {
551602
UiUtils.showErrorDialog("Can't create a pull request:" + ex.getMessage()); // NOI18N
552603
}
604+
} else if (isNewPullRequest) {
605+
getPanel().setNewPullRequestSelected(false);
553606
}
554607

555608
// remove listeners

src/main/java/com/junichi11/netbeans/modules/github/issues/issue/ui/GitHubIssuePanel.form

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
<Group type="102" alignment="1" attributes="0">
125125
<Component id="headerErrorLabel" min="-2" max="-2" attributes="0"/>
126126
<EmptySpace max="32767" attributes="0"/>
127+
<Component id="newPullRequestToggleButton" min="-2" max="-2" attributes="0"/>
128+
<EmptySpace max="-2" attributes="0"/>
127129
<Component id="changeToPullRequestButton" min="-2" max="-2" attributes="0"/>
128130
<EmptySpace max="-2" attributes="0"/>
129131
<Component id="newMilestoneButton" min="-2" max="-2" attributes="0"/>
@@ -198,6 +200,7 @@
198200
<Component id="newLabelButton" alignment="3" min="-2" max="-2" attributes="0"/>
199201
<Component id="newMilestoneButton" alignment="3" min="-2" max="-2" attributes="0"/>
200202
<Component id="changeToPullRequestButton" alignment="3" min="-2" max="-2" attributes="0"/>
203+
<Component id="newPullRequestToggleButton" alignment="3" min="-2" max="-2" attributes="0"/>
201204
</Group>
202205
</Group>
203206
</Group>
@@ -333,6 +336,13 @@
333336
</Property>
334337
</Properties>
335338
</Component>
339+
<Component class="javax.swing.JToggleButton" name="newPullRequestToggleButton">
340+
<Properties>
341+
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
342+
<ResourceString bundle="com/junichi11/netbeans/modules/github/issues/issue/ui/Bundle.properties" key="GitHubIssuePanel.newPullRequestToggleButton.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
343+
</Property>
344+
</Properties>
345+
</Component>
336346
</SubComponents>
337347
</Container>
338348
<Container class="javax.swing.JScrollPane" name="mainScrollPane">

0 commit comments

Comments
 (0)