Skip to content

Commit b58e6fb

Browse files
committed
Support for new Pull Request
1 parent cb766fa commit b58e6fb

6 files changed

Lines changed: 237 additions & 28 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: 61 additions & 10 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,34 @@ 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+
newPullRequest.setTitle(issueParams.getTitle())
345+
.setBody(issueParams.getBody());
346+
try {
347+
PullRequest createdPullRequest = issue.createPullRequest(newPullRequest);
348+
if (createdPullRequest != null) {
349+
issue.editIssue(issueParams);
350+
p.update();
351+
} else {
352+
// show dialog
353+
UiUtils.showErrorDialog(Bundle.SubmitIssueAction_message_pull_request_added_fail());
354+
}
355+
} catch (IOException ex) {
356+
UiUtils.showErrorDialog(ex.getMessage());
357+
}
341358
} else {
342-
// show dialog
343-
UiUtils.showErrorDialog(Bundle.SubmitIssueAction_message_issue_added_fail());
359+
// add issue
360+
Issue newIssue = issue.submitNewIssue(issueParams);
361+
if (newIssue != null) {
362+
p.update();
363+
} else {
364+
// show dialog
365+
UiUtils.showErrorDialog(Bundle.SubmitIssueAction_message_issue_added_fail());
366+
}
344367
}
345368
} else {
346369
// edit issue
@@ -482,15 +505,29 @@ private boolean closeReopen() {
482505
@NbBundle.Messages({
483506
"CreatePullRequestAction.confirmation.message=Do you want to change this issue to Pull Request?",
484507
"CreatePullRequestAction.error.message.same.branch=Another branch must be set.",
485-
"CreatePullRequestAction.descriptor.title=Change to Pull Request"
508+
"CreatePullRequestAction.descriptor.title=Pull Request"
486509
})
487510
public class CreatePullRequestAction implements ActionListener {
488511

489512
@Override
490513
public void actionPerformed(ActionEvent e) {
514+
String actionCommand = e.getActionCommand();
515+
final boolean isNewPullRequest = actionCommand.equals("New PR"); // NOI18N
491516
GitHubIssuePanel p = getPanel();
492517
p.setCreatePullRequestButtonEnabled(false);
493518
final GitHubIssue issue = p.getIssue();
519+
520+
if (isNewPullRequest && !p.isNewPullRequestSelected()) {
521+
// remove the new pull request from the panle
522+
SwingUtilities.invokeLater(new Runnable() {
523+
@Override
524+
public void run() {
525+
getPanel().setNewPullRequest(null);
526+
}
527+
});
528+
return;
529+
}
530+
494531
RequestProcessor rp = GitHubIssues.getInstance().getRequestProcessor();
495532
rp.post(new Runnable() {
496533
@Override
@@ -543,13 +580,27 @@ public void stateChanged(ChangeEvent e) {
543580
String headBranch = mySelf.getLogin() + ":" + selectedHeadBranch.getName(); // NOI18N
544581
PullRequest pullRequest;
545582
try {
546-
pullRequest = issue.createPullRequest(headBranch, baseBranch);
547-
if (pullRequest != null) {
548-
getPanel().refresh();
583+
if (isNewPullRequest) {
584+
// set new pull request to panel
585+
PullRequestMarker baseMarker = new PullRequestMarker();
586+
baseMarker.setLabel(baseBranch);
587+
PullRequestMarker headMarker = new PullRequestMarker();
588+
headMarker.setLabel(headBranch);
589+
PullRequest newPullRequest = new PullRequest()
590+
.setBase(baseMarker)
591+
.setHead(headMarker);
592+
getPanel().setNewPullRequest(newPullRequest);
593+
} else {
594+
pullRequest = issue.createPullRequest(headBranch, baseBranch);
595+
if (pullRequest != null) {
596+
getPanel().refresh();
597+
}
549598
}
550599
} catch (IOException ex) {
551600
UiUtils.showErrorDialog("Can't create a pull request:" + ex.getMessage()); // NOI18N
552601
}
602+
} else if (isNewPullRequest) {
603+
getPanel().setNewPullRequestSelected(false);
553604
}
554605

555606
// 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)