diff --git a/pj-create/src/main/java/com/g2forge/project/plan/create/Create.java b/pj-create/src/main/java/com/g2forge/project/plan/create/Create.java index ae8b427..6494a1a 100644 --- a/pj-create/src/main/java/com/g2forge/project/plan/create/Create.java +++ b/pj-create/src/main/java/com/g2forge/project/plan/create/Create.java @@ -215,10 +215,10 @@ protected static void verifyChanges(final Changes changes) { protected final Map> projectComponentsCache = new LinkedHashMap<>(); - public List createIssues(IDataSource serverDataSource, IDataSource configDataSource) throws JsonParseException, JsonMappingException, IOException, URISyntaxException, InterruptedException, ExecutionException { + public Map createIssues(IDataSource serverDataSource, IDataSource configDataSource) throws JsonParseException, JsonMappingException, IOException, URISyntaxException, InterruptedException, ExecutionException { // Load the config, but if it's empty, don't bother final CreateConfig config = HConfig.load(configDataSource, CreateConfig.class); - if ((config.getIssues() == null) || config.getIssues().isEmpty()) return Collections.emptyList(); + if ((config.getIssues() == null) || config.getIssues().isEmpty()) return Collections.emptyMap(); // Load the server if one is specified; final Server server = (serverDataSource != null) ? HConfig.load(serverDataSource, Server.class) : null; @@ -245,7 +245,8 @@ protected Map getProjectComponents(final ExtendedJiraRes }); } - protected List implementChanges(Server server, Changes changes) throws IOException, URISyntaxException, InterruptedException, ExecutionException { + protected Map implementChanges(Server server, Changes changes) throws IOException, URISyntaxException, InterruptedException, ExecutionException { + final boolean dryrun = ProjectCreateFlag.DRYRUN.getAccessor().get(); HLog.getLogControl().setLogLevel(Level.INFO); try (final ExtendedJiraRestClient client = JiraAPI.createFromPropertyInput(server == null ? null : server.getApi(), null).connect(true)) { final Map linkTypes = new HashMap<>(); @@ -283,43 +284,46 @@ protected List implementChanges(Server server, Changes changes) throws I } if ((issue.getLabels() != null) && !issue.getLabels().isEmpty()) builder.setFieldInput(new FieldInput(IssueFieldId.LABELS_FIELD, issue.getLabels())); - BasicIssue created = null; - { - final List throwables = new ArrayList<>(); - for (int i = 0; i < 5; i++) { - final Promise promise = issueClient.createIssue(builder.build()); - try { - created = promise.get(); - } catch (ExecutionException e) { - throwables.add(e); - continue; + if (dryrun) issues.put(issue.getSummary(), "DRYRUN"); + else { + BasicIssue created = null; + { + final List throwables = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + final Promise promise = issueClient.createIssue(builder.build()); + try { + created = promise.get(); + } catch (ExecutionException e) { + throwables.add(e); + continue; + } + issues.put(issue.getSummary(), created.getKey()); + throwables.clear(); + break; } - issues.put(issue.getSummary(), created.getKey()); - throwables.clear(); - break; + if (!throwables.isEmpty()) HError.withSuppressed(new RuntimeException(String.format("Failed to create issue: %1$s", issue.getSummary())), throwables).printStackTrace(System.err); } - if (!throwables.isEmpty()) HError.withSuppressed(new RuntimeException(String.format("Failed to create issue: %1$s", issue.getSummary())), throwables).printStackTrace(System.err); - } - final String transitionName = issue.getTransition(); - if (transitionName != null) { - final List throwables = new ArrayList<>(); - for (int i = 0; i < 5; i++) { - try { - final Issue actualIssue = issueClient.getIssue(created.getKey()).get(); - final Iterable transitions = issueClient.getTransitions(actualIssue).get(); - final Transition transition = StreamSupport.stream(transitions.spliterator(), false).filter(t -> Objects.equal(t.getName(), transitionName)).findFirst().orElse(null); - issueClient.transition(actualIssue, new TransitionInput(transition.getId())).get(); - } catch (ExecutionException e) { - throwables.add(e); - continue; + final String transitionName = issue.getTransition(); + if (transitionName != null) { + final List throwables = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + try { + final Issue actualIssue = issueClient.getIssue(created.getKey()).get(); + final Iterable transitions = issueClient.getTransitions(actualIssue).get(); + final Transition transition = StreamSupport.stream(transitions.spliterator(), false).filter(t -> Objects.equal(t.getName(), transitionName)).findFirst().orElse(null); + issueClient.transition(actualIssue, new TransitionInput(transition.getId())).get(); + } catch (ExecutionException e) { + throwables.add(e); + continue; + } } + if (!throwables.isEmpty()) HError.withSuppressed(new RuntimeException(String.format("Failed to transition issue: %1$s %2$s", created.getKey(), issue.getSummary())), throwables).printStackTrace(System.err); } - if (!throwables.isEmpty()) HError.withSuppressed(new RuntimeException(String.format("Failed to transition issue: %1$s %2$s", created.getKey(), issue.getSummary())), throwables).printStackTrace(System.err); } } - for (LinkIssuesInput link : changes.getLinks()) { + if (!dryrun) for (LinkIssuesInput link : changes.getLinks()) { final LinkType linkType = linkTypes.get(link.getLinkType()); final String from = issues.get(link.getFromIssueKey()); final String to = issues.getOrDefault(link.getToIssueKey(), link.getToIssueKey()); @@ -327,7 +331,7 @@ protected List implementChanges(Server server, Changes changes) throws I issueClient.linkIssue(new LinkIssuesInput(linkType.isReverse() ? to : from, linkType.isReverse() ? from : to, linkType.getName(), link.getComment())).get(); } - return new ArrayList<>(issues.values()); + return issues; } } @@ -337,8 +341,7 @@ public IExit invoke(CommandInvocation invocation) thro final boolean hasServer = invocation.getArguments().size() > 1; final IDataSource server = hasServer ? new PathDataSource(Paths.get(invocation.getArguments().get(0))) : null; final IDataSource config = new PathDataSource(Paths.get(invocation.getArguments().get(hasServer ? 1 : 0))); - createIssues(server, config).forEach(System.out::println); + createIssues(server, config).entrySet().forEach(entry -> System.out.println(String.format("%1$s %2$s", entry.getValue(), entry.getKey()))); return IStandardCommand.SUCCESS; } - } diff --git a/pj-create/src/main/java/com/g2forge/project/plan/create/ProjectCreateFlag.java b/pj-create/src/main/java/com/g2forge/project/plan/create/ProjectCreateFlag.java new file mode 100644 index 0000000..2dff3ea --- /dev/null +++ b/pj-create/src/main/java/com/g2forge/project/plan/create/ProjectCreateFlag.java @@ -0,0 +1,12 @@ +package com.g2forge.project.plan.create; + +import com.g2forge.alexandria.java.core.properties.IKnownPropertyBoolean; + +public enum ProjectCreateFlag implements IKnownPropertyBoolean { + DRYRUN { + @Override + public Boolean getDefault() { + return false; + } + }; +} \ No newline at end of file