Skip to content

Commit 90dbde9

Browse files
fix: include the parenthesis only when a scope is supplied
Commit example: "style: fix eslint error" Closes #4
1 parent 1970e91 commit 90dbde9

File tree

5 files changed

+139
-75
lines changed

5 files changed

+139
-75
lines changed

src/com/leroymerlin/commit/CommitDialog.java

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
import javax.swing.*;
88

9-
import static org.apache.commons.lang.StringUtils.isBlank;
10-
119
/**
1210
* @author Damien Arrachequesne
1311
*/
@@ -29,56 +27,8 @@ protected JComponent createCenterPanel() {
2927
return panel.getMainPanel();
3028
}
3129

32-
String getCommitMessage() {
33-
return String.format("%s(%s): %s%n%n%s%s%s",
34-
panel.getChangeType(),
35-
panel.getChangeScope(),
36-
panel.getShortDescription(),
37-
getLongDescription(),
38-
getBreakingChanges(),
39-
getClosedIssues());
40-
}
41-
42-
private String getLongDescription() {
43-
return breakLines(panel.getLongDescription(), 100);
44-
}
45-
46-
private String getBreakingChanges() {
47-
if (isBlank(panel.getBreakingChanges())) {
48-
return "";
49-
}
50-
return String.format("%n%n%s", "BREAKING CHANGE: " + panel.getBreakingChanges());
30+
CommitMessage getCommitMessage() {
31+
return panel.getCommitMessage();
5132
}
5233

53-
private String getClosedIssues() {
54-
if (isBlank(panel.getClosedIssues())) {
55-
return "";
56-
}
57-
return String.format("%n%n%s", "Closes " + panel.getClosedIssues());
58-
}
59-
60-
private static String breakLines(String input, int maxLineLength) {
61-
String[] tokens = input.split("\\s+");
62-
StringBuilder output = new StringBuilder(input.length());
63-
int lineLength = 0;
64-
for (int i = 0; i < tokens.length; i++) {
65-
String word = tokens[i];
66-
67-
boolean shouldAddNewLine = lineLength + (" " + word).length() > maxLineLength;
68-
if (shouldAddNewLine) {
69-
if (i > 0) {
70-
output.append(System.lineSeparator());
71-
}
72-
lineLength = 0;
73-
}
74-
boolean shouldAddSpace = i < tokens.length - 1 &&
75-
(lineLength + (word + " ").length() + tokens[i + 1].length() <= maxLineLength);
76-
if (shouldAddSpace) {
77-
word += " ";
78-
}
79-
output.append(word);
80-
lineLength += word.length();
81-
}
82-
return output.toString();
83-
}
8434
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.leroymerlin.commit;
2+
3+
import static org.apache.commons.lang.StringUtils.isNotBlank;
4+
5+
/**
6+
* @author Damien Arrachequesne <damien.arrachequesne@gmail.com>
7+
*/
8+
class CommitMessage {
9+
private final String content;
10+
11+
CommitMessage(ChangeType changeType, String changeScope, String shortDescription, String longDescription, String closedIssues, String breakingChanges) {
12+
this.content = buildContent(changeType, changeScope, shortDescription, longDescription, closedIssues, breakingChanges);
13+
}
14+
15+
private String buildContent(ChangeType changeType, String changeScope, String shortDescription, String longDescription, String closedIssues, String breakingChanges) {
16+
StringBuilder builder = new StringBuilder();
17+
builder.append(changeType.label());
18+
if (isNotBlank(changeScope)) {
19+
builder
20+
.append('(')
21+
.append(changeScope)
22+
.append(')');
23+
}
24+
builder
25+
.append(": ")
26+
.append(shortDescription)
27+
.append(System.lineSeparator())
28+
.append(System.lineSeparator())
29+
.append(breakLines(longDescription, 100));
30+
31+
if (isNotBlank(breakingChanges)) {
32+
builder
33+
.append(System.lineSeparator())
34+
.append(System.lineSeparator())
35+
.append("BREAKING CHANGE: ")
36+
.append(breakingChanges);
37+
}
38+
39+
if (isNotBlank(closedIssues)) {
40+
builder.append(System.lineSeparator());
41+
for (String closedIssue : closedIssues.split(",")) {
42+
builder
43+
.append(System.lineSeparator())
44+
.append("Closes ")
45+
.append(closedIssue);
46+
}
47+
}
48+
49+
return builder.toString();
50+
}
51+
52+
private static String breakLines(String input, int maxLineLength) {
53+
String[] tokens = input.split("\\s+");
54+
StringBuilder output = new StringBuilder(input.length());
55+
int lineLength = 0;
56+
for (int i = 0; i < tokens.length; i++) {
57+
String word = tokens[i];
58+
59+
boolean shouldAddNewLine = lineLength + (" " + word).length() > maxLineLength;
60+
if (shouldAddNewLine) {
61+
if (i > 0) {
62+
output.append(System.lineSeparator());
63+
}
64+
lineLength = 0;
65+
}
66+
boolean shouldAddSpace = i < tokens.length - 1 &&
67+
(lineLength + (word + " ").length() + tokens[i + 1].length() <= maxLineLength);
68+
if (shouldAddSpace) {
69+
word += " ";
70+
}
71+
output.append(word);
72+
lineLength += word.length();
73+
}
74+
return output.toString();
75+
}
76+
77+
@Override
78+
public String toString() {
79+
return content;
80+
}
81+
}

src/com/leroymerlin/commit/CommitPanel.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,15 @@ JPanel getMainPanel() {
2626
return mainPanel;
2727
}
2828

29-
String getChangeType() {
30-
ChangeType type = (ChangeType) changeType.getSelectedItem();
31-
return type.label();
29+
CommitMessage getCommitMessage() {
30+
return new CommitMessage(
31+
(ChangeType) changeType.getSelectedItem(),
32+
changeScope.getText().trim(),
33+
shortDescription.getText().trim(),
34+
longDescription.getText().trim(),
35+
breakingChanges.getText().trim(),
36+
closedIssues.getText().trim()
37+
);
3238
}
3339

34-
String getChangeScope() {
35-
return changeScope.getText().trim();
36-
}
37-
38-
String getShortDescription() {
39-
return shortDescription.getText().trim();
40-
}
41-
42-
String getLongDescription() {
43-
return longDescription.getText().trim();
44-
}
45-
46-
String getBreakingChanges() {
47-
return breakingChanges.getText().trim();
48-
}
49-
50-
String getClosedIssues() {
51-
return closedIssues.getText().trim();
52-
}
5340
}

src/com/leroymerlin/commit/CreateCommitAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void actionPerformed(AnActionEvent actionEvent) {
2323
CommitDialog dialog = new CommitDialog(actionEvent.getProject());
2424
dialog.show();
2525
if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
26-
commitPanel.setCommitMessage(dialog.getCommitMessage());
26+
commitPanel.setCommitMessage(dialog.getCommitMessage().toString());
2727
}
2828
}
2929

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.leroymerlin.commit;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
public class CommitMessageTest {
8+
9+
@Test
10+
public void testFormatCommit() {
11+
CommitMessage commitMessage = new CommitMessage(ChangeType.FIX, "ngStyle",
12+
"skip setting empty value when new style has the property",
13+
"Previously, all the properties in oldStyles are set to empty value once. Using AngularJS with jQuery 3.3.1, this disables the CSS transition as reported in jquery/jquery#4185.",
14+
"#16709", "");
15+
String expected = "fix(ngStyle): skip setting empty value when new style has the property\n" +
16+
"\n" +
17+
"Previously, all the properties in oldStyles are set to empty value once. Using AngularJS with \n" +
18+
"jQuery 3.3.1, this disables the CSS transition as reported in jquery/jquery#4185.\n" +
19+
"\n" +
20+
"Closes #16709";
21+
assertEquals(commitMessage.toString(), expected);
22+
}
23+
24+
@Test
25+
public void testFormatCommit_withoutScope() {
26+
CommitMessage commitMessage = new CommitMessage(ChangeType.STYLE, "",
27+
"fix eslint error", "", "", "");
28+
String expected = "style: fix eslint error\n\n";
29+
assertEquals(commitMessage.toString(), expected);
30+
}
31+
32+
@Test
33+
public void testFormatCommit_withMultipleClosedIssues() {
34+
CommitMessage commitMessage = new CommitMessage(ChangeType.FEAT, "$route",
35+
"add support for the `reloadOnUrl` configuration option",
36+
"Enables users to specify that a particular route should not be reloaded after a URL change.",
37+
"#7925,#15002", "");
38+
String expected = "feat($route): add support for the `reloadOnUrl` configuration option\n" +
39+
"\n" +
40+
"Enables users to specify that a particular route should not be reloaded after a URL change.\n" +
41+
"\n" +
42+
"Closes #7925\n" +
43+
"Closes #15002";
44+
assertEquals(commitMessage.toString(), expected);
45+
}
46+
}

0 commit comments

Comments
 (0)