Skip to content

Commit

Permalink
Merge pull request #24 from yodamad/dev
Browse files Browse the repository at this point in the history
1.4
  • Loading branch information
yodamad authored Nov 20, 2018
2 parents f3ca1a7 + 0b4002f commit 3828c62
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .jhipster/Migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
{
"fieldName": "status",
"fieldType": "StatusEnum",
"fieldValues": "WAITING,RUNNING,DONE,FAILED"
"fieldValues": "WAITING,RUNNING,DONE,FAILED,DONE_WITH_WARNINGS"
},
{
"fieldName": "maxFileSize",
Expand Down
2 changes: 1 addition & 1 deletion .jhipster/MigrationHistory.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
"fieldName": "status",
"fieldType": "StatusEnum",
"fieldValues": "WAITING,RUNNING,DONE,FAILED"
"fieldValues": "WAITING,RUNNING,DONE,FAILED,DONE_WITH_WARNINGS"
},
{
"fieldName": "date",
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>fr.yodamad.svn2git</groupId>
<artifactId>svn-2-git</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.4</version>
<packaging>war</packaging>
<name>Svn 2 GitLab</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
* The StatusEnum enumeration.
*/
public enum StatusEnum {
WAITING, RUNNING, DONE, FAILED, IGNORED
WAITING, RUNNING, DONE, FAILED, IGNORED, DONE_WITH_WARNINGS
}
104 changes: 72 additions & 32 deletions src/main/java/fr/yodamad/svn2git/service/MigrationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -85,6 +86,7 @@ public void startMigration(final long migrationId) {
MigrationHistory history = null;
String rootWorkingDir = workingDir(migration);
String gitWorkingDir = gitWorkingDir(migration);
AtomicBoolean withWarnings = new AtomicBoolean(false);

try {

Expand Down Expand Up @@ -178,6 +180,7 @@ public void startMigration(final long migrationId) {
endStep(innerHistory, StatusEnum.DONE, null);
} catch (Exception exc) {
endStep(innerHistory, StatusEnum.FAILED, exc.getMessage());
withWarnings.set(true);
}
});
clean = true;
Expand Down Expand Up @@ -226,7 +229,8 @@ public void startMigration(final long migrationId) {
execCommand(gitWorkingDir, gitCommand);

// 5. Apply mappings if some
applyMapping(gitWorkingDir, migration, MASTER);
boolean warning = applyMapping(gitWorkingDir, migration, MASTER);
withWarnings.set(withWarnings.get() || warning);

// 6. List branches & tags
List<String> remotes = svnList(gitWorkingDir);
Expand All @@ -248,8 +252,16 @@ public void startMigration(final long migrationId) {
.filter(b -> !b.contains("@"))
.collect(Collectors.toList());

gitBranches.forEach(b -> pushBranch(gitWorkingDir, migration, b));
gitTags.forEach(t -> pushTag(gitWorkingDir, migration, t));
gitBranches.forEach(b -> {
final boolean warn = pushBranch(gitWorkingDir, migration, b);
withWarnings.set(withWarnings.get() || warn);
}
);
gitTags.forEach(t -> {
final boolean warn = pushTag(gitWorkingDir, migration, t);
withWarnings.set(withWarnings.get() || warn);
}
);

// 7. Clean work directory
history = startStep(migration, StepEnum.CLEANING, format("Remove %s", workingDir(migration)));
Expand All @@ -258,9 +270,14 @@ public void startMigration(final long migrationId) {
endStep(history, StatusEnum.DONE, null);
} catch (Exception exc) {
endStep(history, StatusEnum.FAILED, exc.getMessage());
withWarnings.set(true);
}

migration.setStatus(StatusEnum.DONE);
if (withWarnings.get()) {
migration.setStatus(StatusEnum.DONE_WITH_WARNINGS);
} else{
migration.setStatus(StatusEnum.DONE);
}
migrationRepository.save(migration);
} catch (Exception exc) {
if (history != null) {
Expand Down Expand Up @@ -352,14 +369,15 @@ private static int execCommand(String directory, String command, String securedC
* @param migration Migration in progress
* @param branch Branch to process
*/
private void applyMapping(String gitWorkingDir, Migration migration, String branch) {
private boolean applyMapping(String gitWorkingDir, Migration migration, String branch) {
List<Mapping> mappings = mappingRepository.findAllByMigration(migration.getId());
boolean workDone = false;
List<StatusEnum> results = null;
if (!CollectionUtils.isEmpty(mappings)) {
List<Boolean> results = mappings.stream()
.map(mapping -> mvDirectory(gitWorkingDir, migration, mapping))
results = mappings.stream()
.map(mapping -> mvDirectory(gitWorkingDir, migration, mapping, branch))
.collect(Collectors.toList());
workDone = results.contains(true);
workDone = results.contains(StatusEnum.DONE);
}

if (workDone) {
Expand All @@ -376,37 +394,54 @@ private void applyMapping(String gitWorkingDir, Migration migration, String bran
endStep(history, StatusEnum.DONE, null);
} catch (IOException | InterruptedException iEx) {
endStep(history, StatusEnum.FAILED, iEx.getMessage());
return false;
}
}

// No mappings, OK
if (results == null) {
return true;
}
// Some errors, WARNING to be set
return results.contains(StatusEnum.DONE_WITH_WARNINGS);
}

/**
* Apply git mv
* @param gitWorkingDir Working directory
* @param migration Current migration
* @param mapping Mapping to apply
* @param branch Current branch
*/
private boolean mvDirectory(String gitWorkingDir, Migration migration, Mapping mapping) {
private StatusEnum mvDirectory(String gitWorkingDir, Migration migration, Mapping mapping, String branch) {
MigrationHistory history;
String msg = format("git mv %s %s on %s", mapping.getSvnDirectory(), mapping.getGitDirectory(), branch);
try {
boolean workDone;
if (mapping.getGitDirectory().equals("/") || mapping.getGitDirectory().equals(".")) {
// For root directory, we need to loop for subdirectory
List<Boolean> results = Files.list(Paths.get(gitWorkingDir, mapping.getSvnDirectory()))
.map(d -> mv(gitWorkingDir, migration, format("%s/%s", mapping.getSvnDirectory(), d.getFileName().toString()), d.getFileName().toString()))
List<StatusEnum> results = Files.list(Paths.get(gitWorkingDir, mapping.getSvnDirectory()))
.map(d -> mv(gitWorkingDir, migration, format("%s/%s", mapping.getSvnDirectory(), d.getFileName().toString()), d.getFileName().toString(), branch))
.collect(Collectors.toList());
workDone = results.contains(true);

if (results.isEmpty()) {
history = startStep(migration, StepEnum.GIT_MV, format("git mv %s %s", mapping.getSvnDirectory(), mapping.getGitDirectory()));
history = startStep(migration, StepEnum.GIT_MV, msg);
endStep(history, StatusEnum.IGNORED, null);
return StatusEnum.IGNORED;
}

if (results.contains(StatusEnum.DONE_WITH_WARNINGS)) {
return StatusEnum.DONE_WITH_WARNINGS;
}
return StatusEnum.DONE;

} else {
workDone = mv(gitWorkingDir, migration, mapping.getSvnDirectory(), mapping.getGitDirectory());
return mv(gitWorkingDir, migration, mapping.getSvnDirectory(), mapping.getGitDirectory(), branch);
}
return workDone;
} catch (IOException gitEx) {
LOG.error("Failed to mv directory", gitEx);
return false;
LOG.debug("Failed to mv directory", gitEx);
history = startStep(migration, StepEnum.GIT_MV, msg);
endStep(history, StatusEnum.IGNORED, null);
return StatusEnum.IGNORED;
}
}

Expand All @@ -416,26 +451,27 @@ private boolean mvDirectory(String gitWorkingDir, Migration migration, Mapping m
* @param migration Migration in progress
* @param svnDir Origin SVN element
* @param gitDir Target Git element
* @param branch Current branch
*/
private boolean mv(String gitWorkingDir, Migration migration, String svnDir, String gitDir) {
private StatusEnum mv(String gitWorkingDir, Migration migration, String svnDir, String gitDir, String branch) {
MigrationHistory history = null;
try {
String gitCommand = format("git mv %s %s", svnDir, gitDir);
String gitCommand = format("git mv %s %s on %s", svnDir, gitDir, branch);
history = startStep(migration, StepEnum.GIT_MV, gitCommand);
// git mv
int exitCode = execCommand(gitWorkingDir, gitCommand);

if (128 == exitCode) {
endStep(history, StatusEnum.IGNORED, null);
return false;
return StatusEnum.IGNORED;
} else {
endStep(history, StatusEnum.DONE, null);
return true;
return StatusEnum.DONE;
}
} catch (IOException | InterruptedException gitEx) {
LOG.error("Failed to mv directory", gitEx);
endStep(history, StatusEnum.FAILED, gitEx.getMessage());
return false;
return StatusEnum.DONE_WITH_WARNINGS;
}
}

Expand Down Expand Up @@ -474,22 +510,24 @@ private List<String> svnList(String directory) throws InterruptedException, IOEx
* @param migration Migration object
* @param branch Branch to migrate
*/
private void pushBranch(String gitWorkingDir, Migration migration, String branch) throws RuntimeException {
try {
String branchName = branch.replaceFirst("refs/remotes/origin/", "");
branchName = branchName.replaceFirst("origin/", "");
LOG.debug(format("Branch %s", branchName));
private boolean pushBranch(String gitWorkingDir, Migration migration, String branch) throws RuntimeException {
String branchName = branch.replaceFirst("refs/remotes/origin/", "");
branchName = branchName.replaceFirst("origin/", "");
LOG.debug(format("Branch %s", branchName));

MigrationHistory history = startStep(migration, StepEnum.GIT_PUSH, branchName);
MigrationHistory history = startStep(migration, StepEnum.GIT_PUSH, branchName);
try {
String gitCommand = format("git checkout -b %s %s", branchName, branch);
execCommand(gitWorkingDir, gitCommand);
execCommand(gitWorkingDir, GIT_PUSH);

endStep(history, StatusEnum.DONE, null);

applyMapping(gitWorkingDir, migration, branch);
return applyMapping(gitWorkingDir, migration, branch);
} catch (IOException | InterruptedException iEx) {
throw new RuntimeException();
LOG.error("Failed to push branch", iEx);
endStep(history, StatusEnum.FAILED, iEx.getMessage());
return false;
}
}

Expand All @@ -499,7 +537,7 @@ private void pushBranch(String gitWorkingDir, Migration migration, String branch
* @param migration Migration object
* @param tag Tag to migrate
*/
private void pushTag(String gitWorkingDir, Migration migration, String tag) {
private boolean pushTag(String gitWorkingDir, Migration migration, String tag) {
MigrationHistory history = startStep(migration, StepEnum.GIT_PUSH, tag);
try {
String tagName = tag.replaceFirst(ORIGIN_TAGS, "");
Expand All @@ -525,7 +563,9 @@ private void pushTag(String gitWorkingDir, Migration migration, String tag) {
} catch (IOException | InterruptedException gitEx) {
LOG.error("Failed to push branch", gitEx);
endStep(history, StatusEnum.FAILED, gitEx.getMessage());
return false;
}
return false;
}

// History management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@
border: 5px solid white;
padding: 10px;
}

.cell-warning {
background-color: orange;
color: white;
border: 5px solid white;
padding: 10px;
}
7 changes: 5 additions & 2 deletions src/main/webapp/app/entities/migration/migration.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { HttpErrorResponse, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Subscription } from 'rxjs';
import { JhiEventManager, JhiParseLinks, JhiAlertService } from 'ng-jhipster';
import { JhiAlertService, JhiEventManager, JhiParseLinks } from 'ng-jhipster';

import { IMigration, StatusEnum } from 'app/shared/model/migration.model';
import { Principal } from 'app/core';
Expand Down Expand Up @@ -112,6 +112,9 @@ export class MigrationComponent implements OnInit, OnDestroy {
if (status === StatusEnum.IGNORED) {
return 'cell-ignored';
}
if (status === StatusEnum.DONE_WITH_WARNINGS) {
return 'cell-warning';
}
}

private paginateMigrations(data: IMigration[], headers: HttpHeaders) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/webapp/app/shared/model/migration.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const enum StatusEnum {
RUNNING = 'RUNNING',
DONE = 'DONE',
FAILED = 'FAILED',
IGNORED = 'IGNORED'
IGNORED = 'IGNORED',
DONE_WITH_WARNINGS = 'DONE_WITH_WARNINGS'
}

export interface IMigration {
Expand Down
7 changes: 7 additions & 0 deletions src/main/webapp/app/shared/summary/summary-card.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@
border: 5px solid white;
padding: 10px;
}

.cell-warning {
background-color: orange;
color: white;
border: 5px solid white;
padding: 10px;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ export class DetailsCardComponent implements OnInit {
if (status === StatusEnum.IGNORED) {
return 'cell-ignored';
}
if (status === StatusEnum.DONE_WITH_WARNINGS) {
return 'cell-warning';
}
}
}
1 change: 1 addition & 0 deletions src/main/webapp/i18n/en/statusEnum.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"WAITING": "WAITING",
"RUNNING": "RUNNING",
"DONE": "DONE",
"DONE_WITH_WARNINGS": "DONE_WITH_WARNINGS",
"FAILED": "FAILED"
}
}
Expand Down

0 comments on commit 3828c62

Please sign in to comment.