private async Task <string> GetTargetPath(RepositoryContent sourceContent, PathGroupSpec pathSpec, RepositoryReference targetRepo) { var targetPath = pathSpec.DestinationPath; RepositoryContentEntry targetEntry; try { targetEntry = await GetRepositoryContentEntry(targetRepo, targetPath) .ConfigureAwait(continueOnCapturedContext: false); } catch (NotFoundException) { targetEntry = null; } switch (targetEntry) { case null when pathSpec.SourcePaths.Count > 1: case RepositoryContentEntry _ when !(targetEntry.Leaf is null) && targetEntry.Leaf.Type.TryParse(out var targetType) && targetType == ContentType.Dir: case RepositoryContentEntry _ when targetEntry.Leaf is null: return(pathSpec.DestinationPath.TrimEnd('/').TrimEnd() + "/" + sourceContent.Name); case null: return(targetPath); case RepositoryContentEntry _ when !(targetEntry.Leaf is null): return(targetEntry.Leaf.Path); } return(null); }
private async Task ExecuteCopyFileContent(string sourcePath, RepositoryContent sourceContent, PathGroupSpec copySpec, RepositoryReference sourceRepo, RepositoryReference targetRepo, CancellationToken cancelToken) { var targetPath = await GetTargetPath(sourceContent, copySpec, targetRepo) .ConfigureAwait(continueOnCapturedContext: false); if (cancelToken.IsCancellationRequested) { return; } RepositoryContentEntry targetEntry; try { targetEntry = await GetRepositoryContentEntry(targetRepo, targetPath) .ConfigureAwait(false); } catch (NotFoundException) { targetEntry = null; } var targetContent = targetEntry?.Leaf; bool copyCond = copySpec.Condition?.Evaluate( sourcePath, sourceRepo, sourceContent, targetPath, targetRepo, targetContent ) ?? true; if (!copyCond) { Logger.LogDebug($"Not copying path '{{{nameof(sourcePath)}}}' from {{{nameof(sourceRepo)}}} to '{{{nameof(targetPath)}}}' in {{{nameof(targetRepo)}}}. Condition of type {{conditionType}} evaluated {{conditionValue}}.", sourcePath, sourceRepo.ToLogString(), targetPath, targetRepo.ToLogString(), copySpec.Condition?.GetType(), copyCond); } }
private async Task ExecuteCopyFilePath(string sourcePath, string sourceContentPath, PathGroupSpec copySpec, RepositoryReference sourceRepo, RepositoryReference targetRepo, CancellationToken cancelToken) { RepositoryContentEntry sourceContentEntry; try { sourceContentEntry = await GetRepositoryContentEntry(sourceRepo, sourceContentPath) .ConfigureAwait(continueOnCapturedContext: false); } catch (NotFoundException notFoundExcept) { Logger.LogError(notFoundExcept, $"Unable to get repository contents from {{{nameof(sourceRepo)}}} at path: {{{nameof(sourcePath)}}}", sourceRepo.ToLogString(), sourceContentPath); return; } if (cancelToken.IsCancellationRequested) { return; } var sourceContents = sourceContentEntry.Contents; foreach (var sourceContent in sourceContents) { bool unsupportedContentType = !sourceContent.Type.TryParse(out var sourceContentType); if (!unsupportedContentType) { switch (sourceContentType) { case ContentType.File: await ExecuteCopyFileContent(sourcePath, sourceContent, copySpec, sourceRepo, targetRepo, cancelToken) .ConfigureAwait(continueOnCapturedContext: false); break; case ContentType.Dir: await ExecuteCopyFilePath(sourcePath, sourceContent.Path, copySpec, sourceRepo, targetRepo, cancelToken) .ConfigureAwait(continueOnCapturedContext: false); break; case ContentType.Symlink: await ExecuteCopyFilePath(sourcePath, sourceContent.Target, copySpec, sourceRepo, targetRepo, cancelToken) .ConfigureAwait(continueOnCapturedContext: false); break; default: unsupportedContentType = true; break; } } if (unsupportedContentType) { Logger.LogWarning($"Unsupported content type '{{{nameof(sourceContentType)}}}' in repository {{{nameof(sourceRepo)}}} at path: {{{nameof(sourcePath)}}}", sourceContent.Type, sourceRepo.ToLogString(), sourceContent.Path); } if (cancelToken.IsCancellationRequested) { return; } } }
private Task ExecuteCopyFilePath(string sourcePath, PathGroupSpec copySpec, RepositoryReference sourceRepo, RepositoryReference targetRepo, CancellationToken cancelToken) => ExecuteCopyFilePath(sourcePath, sourcePath, copySpec, sourceRepo, targetRepo, cancelToken);