Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
minwoox committed Jan 17, 2025
1 parent a8188b2 commit d0182e5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public String direction() {
return direction;
}

// TODO(minwoox): Remove this property after migration is done.
@JsonProperty("localRepo")
public String localRepo() {
return localRepo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ public CompletableFuture<MirrorDto> getMirror(@Param String projectName,
@StatusCode(201)
@RequiresRepositoryRole(RepositoryRole.ADMIN)
public CompletableFuture<PushResultDto> createMirror(@Param String projectName,
Repository ignored,
Repository repository,
MirrorRequest newMirror,
Author author) {
return createOrUpdate(projectName, newMirror, author, false);
return createOrUpdate(projectName, repository.name(), newMirror, author, false);
}

/**
Expand All @@ -182,11 +182,11 @@ public CompletableFuture<PushResultDto> createMirror(@Param String projectName,
@Put("/projects/{projectName}/repos/{repoName}/mirrors/{id}")
@RequiresRepositoryRole(RepositoryRole.ADMIN)
public CompletableFuture<PushResultDto> updateMirror(@Param String projectName,
Repository ignored,
Repository repository,
MirrorRequest mirror,
@Param String id, Author author) {
checkArgument(id.equals(mirror.id()), "The mirror ID (%s) can't be updated", id);
return createOrUpdate(projectName, mirror, author, true);
return createOrUpdate(projectName, repository.name(), mirror, author, true);
}

/**
Expand All @@ -211,23 +211,25 @@ public CompletableFuture<Void> deleteMirror(@Param String projectName,
});
}

private CompletableFuture<PushResultDto> createOrUpdate(String projectName, MirrorRequest newMirror,
Author author, boolean update) {
private CompletableFuture<PushResultDto> createOrUpdate(
String projectName, String repoName, MirrorRequest newMirror,
Author author, boolean update) {
final MetaRepository metaRepo = metaRepo(projectName);
return metaRepo.createMirrorPushCommand(newMirror, author, zoneConfig, update).thenCompose(command -> {
return executor().execute(command).thenApply(result -> {
metaRepo.mirror(newMirror.localRepo(), newMirror.id(), result.revision())
.handle((mirror, cause) -> {
if (cause != null) {
// This should not happen in normal cases.
logger.warn("Failed to get the mirror: {}", newMirror.id(), cause);
return null;
}
return notifyMirrorEvent(mirror, update);
});
return new PushResultDto(result.revision(), command.timestamp());
});
});
return metaRepo.createMirrorPushCommand(repoName, newMirror, author, zoneConfig, update).thenCompose(
command -> {
return executor().execute(command).thenApply(result -> {
metaRepo.mirror(repoName, newMirror.id(), result.revision())
.handle((mirror, cause) -> {
if (cause != null) {
// This should not happen in normal cases.
logger.warn("Failed to get the mirror: {}", newMirror.id(), cause);
return null;
}
return notifyMirrorEvent(mirror, update);
});
return new PushResultDto(result.revision(), command.timestamp());
});
});
}

private Void notifyMirrorEvent(Mirror mirror, boolean update) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,13 +365,13 @@ private CompletableFuture<Map<String, Entry<?>>> find(String filePattern) {

@Override
public CompletableFuture<Command<CommitResult>> createMirrorPushCommand(
MirrorRequest mirrorRequest, Author author, @Nullable ZoneConfig zoneConfig, boolean update) {
final String repoName = mirrorRequest.localRepo();
String repoName, MirrorRequest mirrorRequest, Author author,
@Nullable ZoneConfig zoneConfig, boolean update) {
validateMirror(mirrorRequest, zoneConfig);
if (update) {
final String summary = "Update the mirror '" + mirrorRequest.id() + "' in " + repoName;
return mirror(repoName, mirrorRequest.id()).thenApply(mirror -> {
return newMirrorCommand(mirrorRequest, author, summary);
return newMirrorCommand(repoName, mirrorRequest, author, summary);
});
} else {
String summary = "Create a new mirror from " + mirrorRequest.remoteUrl() +
Expand All @@ -382,7 +382,8 @@ public CompletableFuture<Command<CommitResult>> createMirrorPushCommand(
} else {
summary = "[Local-to-remote] " + summary;
}
return UnmodifiableFuture.completedFuture(newMirrorCommand(mirrorRequest, author, summary));
return UnmodifiableFuture.completedFuture(
newMirrorCommand(repoName, mirrorRequest, author, summary));
}
}

Expand Down Expand Up @@ -429,11 +430,12 @@ private Command<CommitResult> newCredentialCommand(String credentialFile, Creden
change);
}

private Command<CommitResult> newMirrorCommand(MirrorRequest mirrorRequest, Author author, String summary) {
private Command<CommitResult> newMirrorCommand(String repoName, MirrorRequest mirrorRequest,
Author author, String summary) {
final MirrorConfig mirrorConfig = converterToMirrorConfig(mirrorRequest);
final JsonNode jsonNode = Jackson.valueToTree(mirrorConfig);
final Change<JsonNode> change =
Change.ofJsonUpsert(mirrorFile(mirrorRequest.localRepo(), mirrorConfig.id()), jsonNode);
Change.ofJsonUpsert(mirrorFile(repoName, mirrorConfig.id()), jsonNode);
return Command.push(author, parent().name(), name(), Revision.HEAD, summary, "", Markup.PLAINTEXT,
change);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ default CompletableFuture<Mirror> mirror(String repoName, String id) {
/**
* Create a push {@link Command} for the {@link MirrorDto}.
*/
CompletableFuture<Command<CommitResult>> createMirrorPushCommand(MirrorRequest mirrorRequest, Author author,
@Nullable ZoneConfig zoneConfig,
boolean update);
CompletableFuture<Command<CommitResult>> createMirrorPushCommand(
String repoName, MirrorRequest mirrorRequest, Author author,
@Nullable ZoneConfig zoneConfig, boolean update);

/**
* Create a push {@link Command} for the {@link Credential}.
Expand Down

0 comments on commit d0182e5

Please sign in to comment.