Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix scm accept parsing in version 3.1.0. #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.deluan.jenkins.plugins.rtc.JazzConfiguration;
import com.deluan.jenkins.plugins.rtc.changelog.JazzChangeSet;
import com.deluan.jenkins.plugins.rtc.commands.accept.AcceptNewOutputParser;
import com.deluan.jenkins.plugins.rtc.commands.accept.AcceptOldOutputParser;
import com.deluan.jenkins.plugins.rtc.commands.accept.BaseAcceptOutputParser;
import com.deluan.jenkins.plugins.rtc.commands.accept.*;
import hudson.util.ArgumentListBuilder;

import java.io.BufferedReader;
Expand All @@ -19,16 +17,24 @@
*/
public class AcceptCommand extends AbstractCommand implements ParseableCommand<Map<String, JazzChangeSet>> {

public static final String NEW_FORMAT_VERSION = "2.1.0";
public static final String FORMAT_VERSION_2_1_0 = "2.1.0";
public static final String FORMAT_VERSION_3_1_0 = "3.1.0";
private Collection<String> changeSets;
private boolean useJson;
private BaseAcceptOutputParser parser;
protected boolean oldFormat = false;

public AcceptCommand(JazzConfiguration configurationProvider, Collection<String> changeSets, String version) {
super(configurationProvider);
this.changeSets = new LinkedHashSet<String>(changeSets);
this.oldFormat = (version.compareTo(NEW_FORMAT_VERSION) < 0);
parser = (oldFormat) ? new AcceptOldOutputParser() : new AcceptNewOutputParser();
this.useJson = version.equals("3.1.0-json"); // TODO: Obviously only for testing.

if (version.compareTo(FORMAT_VERSION_3_1_0) >= 0) {
parser = useJson ? new JsonAcceptOutputParser() : new AcceptOutputParser_3_1_0();
} else {
this.oldFormat = (version.compareTo(FORMAT_VERSION_2_1_0) < 0);
parser = (oldFormat) ? new AcceptOldOutputParser() : new AcceptNewOutputParser();
}
}

public ArgumentListBuilder getArguments() {
Expand All @@ -39,6 +45,8 @@ public ArgumentListBuilder getArguments() {
addLocalWorkspaceArgument(args);
addSourceStream(args);
args.add("--flow-components", "-o", "-v");
if (useJson) args.add("--json");

if (hasAnyChangeSets()) {
addChangeSets(args);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.deluan.jenkins.plugins.rtc.commands.accept;

/**
* @author: Chris Cosby <[email protected]>
*/

public class AcceptOutputParser_3_1_0 extends BaseAcceptOutputParser {

public AcceptOutputParser_3_1_0() {
super("^\\s{6}[^\\d\\s]+(\\d+)[^\\d]+\\s(.*)$",
"^\\s{10}(.{5})\\s+(.*)$",
"^\\s{10}[^\\d\\s]+(\\d+)[^\\d]+(.*)$");
}

@Override
protected String parseWorkItem(String string) {
return string;
}

@Override
protected String parseEditFlag(String string) {
return string.substring(2, 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.deluan.jenkins.plugins.rtc.commands.accept;

import com.deluan.jenkins.plugins.rtc.changelog.JazzChangeSet;
import hudson.scm.EditType;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* @author Chris Cosby <[email protected]>
*/

public class JsonAcceptOutputParser extends BaseAcceptOutputParser {

public JsonAcceptOutputParser() {
super("", "", "");
System.err.println("JSON Parser is still very experimental");
}

public Map<String, JazzChangeSet> parse(BufferedReader reader) throws ParseException, IOException {
Map<String, JazzChangeSet> result = new HashMap<String, JazzChangeSet>();

String line;
StringBuilder json = new StringBuilder();
JazzChangeSet changeSet;

try {
while ((line = reader.readLine()) != null) {
json.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}

// TODO: Are there multiple items in these arrays? I'm only grabbing #0
JSONObject jazzAcceptObj = JSONObject.fromObject(json.toString());
JSONArray jazzChanges = jazzAcceptObj.getJSONArray("repos").getJSONObject(0).getJSONArray("workspaces").getJSONObject(0).getJSONArray("components").getJSONObject(0).getJSONArray("changes");
for (Iterator<JSONObject> jazzChangesIter = jazzChanges.iterator(); jazzChangesIter.hasNext(); ) {
JSONObject jazzChange = jazzChangesIter.next();

//String author = jazzChange.getString("author");
String rev = jazzChange.getString("uuid");

changeSet = new JazzChangeSet();
changeSet.setRev(rev);

// Iterate through the changes under the changes (stupid stupid JSON) and add to the changeSet
JSONArray jazzChangeChanges = jazzChange.getJSONArray("changes");
for (Iterator<JSONObject> jazzChangeChangesIter = jazzChangeChanges.iterator(); jazzChangeChangesIter.hasNext(); ) {
JSONObject jazzChangeChange = jazzChangeChangesIter.next();

String path = parsePath(jazzChangeChange.getString("path"));
String action = parseAction(jazzChangeChange.getJSONObject("state"));
changeSet.addItem(path, action);

}

// Iterate through the workitems under the changes and add to the changeSet
JSONArray jazzChangeWorkitems = jazzChange.getJSONArray("workitems");
for (Iterator<JSONObject> jazzChangeWorkitemsIter = jazzChangeWorkitems.iterator(); jazzChangeWorkitemsIter.hasNext(); ) {
JSONObject jazzChangeWorkItem = jazzChangeWorkitemsIter.next();

String id = jazzChangeWorkItem.getString("id");
String workitemLabel = jazzChangeWorkItem.getString("workitem-label");
String workitem = parseWorkItem(id, workitemLabel);

changeSet.addWorkItem(workitem);
}

result.put(rev, changeSet);
}

return result;
}

protected String parseWorkItem(String id, String workitemLabel) {
return id + " \"" + workitemLabel + "\"";
}

@Override
protected String parseWorkItem(String string) {
return string;
}

@Override
protected String parseEditFlag(String string) {
return string;
}

@Override
protected String parsePath(String string) {
String path = string.replaceAll("\\\\", "/").trim();
if (path.startsWith("/")) {
path = path.substring(1);
}
return path;
}

protected String parseAction(JSONObject state) {
String action = "";
if (state.getBoolean("content_change")) {
action = EditType.EDIT.getName();
} else if (state.getBoolean("add")) {
action = EditType.ADD.getName();
} else if (state.getBoolean("delete")) {
action = EditType.DELETE.getName();
}
return action;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.core.Is.is;
Expand All @@ -15,37 +16,94 @@ public class AcceptCommandTest extends BaseCommandTest {

@Test
public void acceptCommandArguments() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "2.1.0");
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "2.1.0");

assertEquals("accept -u user -P password -d c:\\test -s \"My Stream\" --flow-components -o -v -c 1714 1657 1652 1651 1650 1648 1645 1640 1625", cmd.getArguments().toStringWithQuote());
}

@Test
public void createCommandForVersion_2_0_2() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "2.0.2");
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "2.0.2");
assertThat(cmd.oldFormat, is(true));
}

@Test
public void createCommandForVersion_2_1_0() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "2.1.0");
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "2.1.0");
assertThat(cmd.oldFormat, is(false));
}

@Test
public void createCommandForVersion_3_0_0() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "3.0.0");
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "3.0.0");
assertThat(cmd.oldFormat, is(false));
}

@Test
public void acceptCommandParseJson_3_1_0() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_3_1_0), "3.1.0-json");
Map<String, JazzChangeSet> result = callParser(cmd, "scm-accept-json-3.1.0.txt", TEST_REVISIONS_3_1_0_JSON);

HashMap hashMap = new HashMap();
hashMap.put("1008", "_d2oMcUU4EeKJGJt6NdfIqg");
hashMap.put("1010", "_etjOekOlEeKJEZt6NdfIqg");
hashMap.put("1009", "_K4DX40SbEeKJFZt6NdfIqg");

JazzChangeSet changeSet = result.get(hashMap.get("1008"));
assertEquals("The number of files in the changesets was incorrect", 6, changeSet.getAffectedPaths().size());
assertEquals("The number of work items in the changesets was incorrect", 2, changeSet.getWorkItems().size());

JazzChangeSet.Item item = changeSet.getItems().get(3);
assertTrue("The file is not the expected one", item.getPath().endsWith(".jazzignore"));
assertEquals("The edit type is not the expected one", EditType.ADD, item.getEditType());


changeSet = result.get(hashMap.get("1010"));
item = changeSet.getItems().get(0);
assertTrue("The file is not the expected one", item.getPath().endsWith("MyThingAPI.C"));
assertEquals("The edit type is not the expected one", EditType.EDIT, item.getEditType());


changeSet = result.get(hashMap.get("1009"));
item = changeSet.getItems().get(0);
assertTrue("The file is not the expected one", item.getPath().endsWith(".jazzignore"));
assertEquals("The edit type is not the expected one", EditType.DELETE, item.getEditType());
}

@Test
public void acceptCommandParse_3_1_0() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_3_1_0), "3.1.0");
Map<String, JazzChangeSet> result = callParser(cmd, "scm-accept-3.1.0.txt", TEST_REVISIONS_3_1_0);

JazzChangeSet changeSet = result.get("1008");
assertEquals("The number of files in the changesets was incorrect", 6, changeSet.getAffectedPaths().size());
assertEquals("The number of work items in the changesets was incorrect", 2, changeSet.getWorkItems().size());

JazzChangeSet.Item item = changeSet.getItems().get(3);
assertTrue("The file is not the expected one", item.getPath().endsWith(".jazzignore"));
assertEquals("The edit type is not the expected one", EditType.ADD, item.getEditType());


changeSet = result.get("1010");
item = changeSet.getItems().get(0);
assertTrue("The file is not the expected one", item.getPath().endsWith("MyThingAPI.C"));
assertEquals("The edit type is not the expected one", EditType.EDIT, item.getEditType());


changeSet = result.get("1009");
item = changeSet.getItems().get(0);
assertTrue("The file is not the expected one", item.getPath().endsWith(".jazzignore"));
assertEquals("The edit type is not the expected one", EditType.DELETE, item.getEditType());
}

@Test
public void acceptCommandParse_2_1_0() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "2.1.0");
Map<String, JazzChangeSet> result = callParser(cmd, "scm-accept-2.1.0.txt", TEST_REVISIONS);
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "2.1.0");
Map<String, JazzChangeSet> result = callParser(cmd, "scm-accept-2.1.0.txt", TEST_REVISIONS_2_1_0);

JazzChangeSet changeSet = result.get("1714");
assertEquals("The number of files in the changesets was incorrect", 8, changeSet.getAffectedPaths().size());
assertEquals("The number of work itens in the changesets was incorrect", 2, changeSet.getWorkItems().size());
assertEquals("The number of work items in the changesets was incorrect", 2, changeSet.getWorkItems().size());

JazzChangeSet.Item item = changeSet.getItems().get(0);
assertTrue("The file is not the expected one", item.getPath().endsWith("GerenteOferta.java"));
Expand All @@ -61,12 +119,12 @@ public void acceptCommandParse_2_1_0() throws Exception {

@Test
public void acceptCommandParse_Chinese() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "2.0.2");
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "2.0.2");
Map<String, JazzChangeSet> result = callParser(cmd, "scm-accept-chinese.txt", "1019", "1021", "1020");

JazzChangeSet changeSet = result.get("1020");
assertEquals("The number of files in the changesets was incorrect", 2, changeSet.getAffectedPaths().size());
assertEquals("The number of work itens in the changesets was incorrect", 1, changeSet.getWorkItems().size());
assertEquals("The number of work items in the changesets was incorrect", 1, changeSet.getWorkItems().size());

JazzChangeSet.Item item = changeSet.getItems().get(0);
assertTrue("The file is not the expected one", item.getPath().endsWith("com_tps_eppic_ConfigValues_core_properties"));
Expand All @@ -78,12 +136,12 @@ public void acceptCommandParse_Chinese() throws Exception {

@Test
public void acceptCommandParse_2_0_2() throws Exception {
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS), "2.0.2");
AcceptCommand cmd = new AcceptCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0), "2.0.2");
Map<String, JazzChangeSet> result = callParser(cmd, "scm-accept-2.0.2.txt", "1002", "1001", "1008", "1009");

JazzChangeSet changeSet = result.get("1002");
assertEquals("The number of files in the changesets was incorrect", 5, changeSet.getAffectedPaths().size());
assertEquals("The number of work itens in the changesets was incorrect", 1, changeSet.getWorkItems().size());
assertEquals("The number of work items in the changesets was incorrect", 1, changeSet.getWorkItems().size());

JazzChangeSet.Item item = changeSet.getItems().get(3);
assertTrue("The file is not the expected one", item.getPath().endsWith("FabricaEJBBean.java"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
*/
abstract public class BaseCommandTest {

protected static final String[] TEST_REVISIONS = new String[]{"1714", "1657", "1652", "1651", "1650", "1648", "1645", "1640", "1625"};
protected static final String[] TEST_REVISIONS_2_1_0 = new String[]{"1714", "1657", "1652", "1651", "1650", "1648", "1645", "1640", "1625"};
protected static final String[] TEST_REVISIONS_3_1_0 = new String[]{"1010", "1009", "1007", "1008", "1006", "1004"};
protected static final String[] TEST_REVISIONS_3_1_0_JSON = new String[]{"_etjOekOlEeKJEZt6NdfIqg", "_K4DX40SbEeKJFZt6NdfIqg", "_r_6ZYUY3EeKJIJt6NdfIqg", "_d2oMcUU4EeKJGJt6NdfIqg", "_F9ue60Y7EeKJIJt6NdfIqg", "_RBKcUUiaEeKJLpt6NdfIqg"};

protected JazzConfiguration config;

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ public void stopDaemonCommandArguments() throws Exception {

@Test
public void listCommandArguments() throws Exception {
ListCommand cmd = new ListCommand(config, Arrays.asList(TEST_REVISIONS));
ListCommand cmd = new ListCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0));

assertEquals("list changesets -u user -P password -d c:\\test 1714 1657 1652 1651 1650 1648 1645 1640 1625", cmd.getArguments().toStringWithQuote());
}

@Test
public void listCommandParse() throws Exception {
ListCommand cmd = new ListCommand(config, Arrays.asList(TEST_REVISIONS));
Map<String, JazzChangeSet> result = callParser(cmd, "scm-list.txt", TEST_REVISIONS);
ListCommand cmd = new ListCommand(config, Arrays.asList(TEST_REVISIONS_2_1_0));
Map<String, JazzChangeSet> result = callParser(cmd, "scm-list.txt", TEST_REVISIONS_2_1_0);

JazzChangeSet changeSet = result.get("1714");
assertEquals("The number of files in the changesets was incorrect", 8, changeSet.getAffectedPaths().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void compareCommandArguments() throws Exception {

@Test
public void compareCommandParse() throws Exception {
Map<String, JazzChangeSet> result = callParser(new CompareCommand(config), "scm-compare.txt", TEST_REVISIONS);
Map<String, JazzChangeSet> result = callParser(new CompareCommand(config), "scm-compare.txt", TEST_REVISIONS_2_1_0);

JazzChangeSet changeSet = result.get("1657");
assertEquals("Roberto", changeSet.getUser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ public void versionCommandArguments() throws Exception {
assertThat(cmd.getArguments().toStringWithQuote(), is("version"));
}

@Test
public void versionCommandParse_3_1_0() throws Exception {
BufferedReader reader = getReader("scm-version-3.1.0.txt");
String result = cmd.parse(reader);
assertThat(result, is("3.1.0"));
}

@Test
public void versionCommandParse_2_1_0() throws Exception {
BufferedReader reader = getReader("scm-version-2.1.0.txt");
Expand All @@ -40,6 +47,4 @@ public void parse() throws Exception {

assertThat(result, is("3.1.100"));
}


}
Loading