private async Task <GitObject> GetUpdatedImageInfoGitObjectAsync() { ImageArtifactDetails srcImageArtifactDetails = ImageInfoHelper.LoadFromFile(Options.ImageInfoPath, Manifest); string repoPath = await GitHelper.DownloadAndExtractGitRepoArchiveAsync(httpClient, Options.GitOptions); try { string repoImageInfoPath = Path.Combine(repoPath, Options.GitOptions.Path); string originalTargetImageInfoContents = File.ReadAllText(repoImageInfoPath); ImageArtifactDetails newImageArtifactDetails; if (originalTargetImageInfoContents != null) { ImageArtifactDetails targetImageArtifactDetails = ImageInfoHelper.LoadFromContent( originalTargetImageInfoContents, Manifest, skipManifestValidation: true); RemoveOutOfDateContent(targetImageArtifactDetails); ImageInfoMergeOptions options = new ImageInfoMergeOptions { ReplaceTags = true }; ImageInfoHelper.MergeImageArtifactDetails(srcImageArtifactDetails, targetImageArtifactDetails, options); newImageArtifactDetails = targetImageArtifactDetails; } else { // If there is no existing file to update, there's nothing to merge with so the source data // becomes the target data. newImageArtifactDetails = srcImageArtifactDetails; } string newTargetImageInfoContents = JsonHelper.SerializeObject(newImageArtifactDetails) + Environment.NewLine; if (originalTargetImageInfoContents != newTargetImageInfoContents) { return(new GitObject { Path = Options.GitOptions.Path, Type = GitObject.TypeBlob, Mode = GitObject.ModeFile, Content = newTargetImageInfoContents }); } else { return(null); } } finally { Directory.Delete(repoPath, recursive: true); } }
private string?GetUpdatedImageInfo(string repoPath) { ImageArtifactDetails srcImageArtifactDetails = ImageInfoHelper.LoadFromFile(Options.ImageInfoPath, Manifest); string repoImageInfoPath = Path.Combine(repoPath, Options.GitOptions.Path); string originalTargetImageInfoContents = File.ReadAllText(repoImageInfoPath); ImageArtifactDetails newImageArtifactDetails; if (originalTargetImageInfoContents != null) { ImageArtifactDetails targetImageArtifactDetails = ImageInfoHelper.LoadFromContent( originalTargetImageInfoContents, Manifest, skipManifestValidation: true); RemoveOutOfDateContent(targetImageArtifactDetails); ImageInfoMergeOptions options = new ImageInfoMergeOptions { IsPublish = true }; ImageInfoHelper.MergeImageArtifactDetails(srcImageArtifactDetails, targetImageArtifactDetails, options); newImageArtifactDetails = targetImageArtifactDetails; } else { // If there is no existing file to update, there's nothing to merge with so the source data // becomes the target data. newImageArtifactDetails = srcImageArtifactDetails; } string newTargetImageInfoContents = JsonHelper.SerializeObject(newImageArtifactDetails) + Environment.NewLine; if (originalTargetImageInfoContents != newTargetImageInfoContents) { return(newTargetImageInfoContents); } else { return(null); } }
public void ImageInfoHelper_MergeRepos_RemoveTag() { ImageData srcImage1; ImageData targetImage2; RepoData[] repoDataSet = new RepoData[] { new RepoData { Repo = "repo1", Images = new SortedDictionary <string, ImageData> { { "image1", srcImage1 = new ImageData { SimpleTags = { "tag1", "tag3" } } } } } }; List <RepoData> targetRepos = new List <RepoData> { new RepoData { Repo = "repo1", Images = new SortedDictionary <string, ImageData> { { "image1", new ImageData { SimpleTags = { "tag1", "tag2", "tag4" } } }, { "image2", targetImage2 = new ImageData { SimpleTags = { "a" } } } } } }; ImageInfoMergeOptions options = new ImageInfoMergeOptions { ReplaceTags = true }; ImageInfoHelper.MergeRepos(repoDataSet, targetRepos, options); List <RepoData> expected = new List <RepoData> { new RepoData { Repo = "repo1", Images = new SortedDictionary <string, ImageData> { { "image1", srcImage1 }, { "image2", targetImage2 } } } }; CompareRepos(expected, targetRepos); }
public void ImageInfoHelper_MergeRepos_RemoveTag() { PlatformData srcPlatform1; PlatformData targetPlatform2; ImageInfo imageInfo1 = CreateImageInfo(); ImageArtifactDetails imageArtifactDetails = new ImageArtifactDetails { Repos = { new RepoData { Repo = "repo1", Images = { new ImageData { ManifestImage = imageInfo1, Platforms = { { srcPlatform1 = new PlatformData { Dockerfile = "image1", SimpleTags = { "tag1", "tag3" } } } }, Manifest = new ManifestData { SharedTags = { "sharedtag1", } } } } } } }; ImageArtifactDetails targetImageArtifactDetails = new ImageArtifactDetails { Repos = { new RepoData { Repo = "repo1", Images = { new ImageData { ManifestImage = imageInfo1, Platforms = { { new PlatformData { Dockerfile = "image1", SimpleTags = { "tag1", "tag2", "tag4" } } }, { targetPlatform2 = new PlatformData { Dockerfile = "image2", SimpleTags = { "a" } } } }, Manifest = new ManifestData { SharedTags = { "sharedtag2", } } } } } } }; ImageInfoMergeOptions options = new ImageInfoMergeOptions { ReplaceTags = true }; ImageInfoHelper.MergeImageArtifactDetails(imageArtifactDetails, targetImageArtifactDetails, options); ImageArtifactDetails expected = new ImageArtifactDetails { Repos = { new RepoData { Repo = "repo1", Images = { new ImageData { Platforms = { srcPlatform1, targetPlatform2 }, Manifest = new ManifestData { SharedTags = { "sharedtag1", } } } } } } }; CompareImageArtifactDetails(expected, targetImageArtifactDetails); }
public override async Task ExecuteAsync() { RepoData[] srcRepos = JsonConvert.DeserializeObject <RepoData[]>(File.ReadAllText(Options.ImageInfoPath)); using (IGitHubClient gitHubClient = this.gitHubClientFactory.GetClient(Options.GitOptions.ToGitHubAuth(), Options.IsDryRun)) { await GitHelper.ExecuteGitOperationsWithRetryAsync(async() => { bool hasChanges = false; GitReference gitRef = await GitHelper.PushChangesAsync(gitHubClient, Options, "Merging image info updates from build.", async branch => { string originalTargetImageInfoContents = await gitHubClient.GetGitHubFileContentsAsync(Options.GitOptions.Path, branch); IEnumerable <RepoData> newImageInfo; if (originalTargetImageInfoContents != null) { List <RepoData> targetRepos = JsonConvert.DeserializeObject <RepoData[]>(originalTargetImageInfoContents).ToList(); ImageInfoMergeOptions options = new ImageInfoMergeOptions { ReplaceTags = true }; ImageInfoHelper.MergeRepos(srcRepos, targetRepos, options); newImageInfo = targetRepos; } else { // If there is no existing file to update, there's nothing to merge with so the source data // becomes the target data. newImageInfo = srcRepos; } string newTargetImageInfoContents = JsonHelper.SerializeObject(newImageInfo.OrderBy(r => r.Repo).ToArray()) + Environment.NewLine; if (originalTargetImageInfoContents != newTargetImageInfoContents) { GitObject imageInfoGitObject = new GitObject { Path = Options.GitOptions.Path, Type = GitObject.TypeBlob, Mode = GitObject.ModeFile, Content = newTargetImageInfoContents }; hasChanges = true; return(new GitObject[] { imageInfoGitObject }); } else { return(Enumerable.Empty <GitObject>()); } }); Uri imageInfoPathIdentifier = GitHelper.GetBlobUrl(Options.GitOptions); if (hasChanges) { if (!Options.IsDryRun) { Uri commitUrl = GitHelper.GetCommitUrl(Options.GitOptions, gitRef.Object.Sha); Logger.WriteMessage($"The '{imageInfoPathIdentifier}' file was updated ({commitUrl})."); } else { Logger.WriteMessage($"The '{imageInfoPathIdentifier}' file would have been updated."); } } else { Logger.WriteMessage($"No changes to the '{imageInfoPathIdentifier}' file were needed."); } }); } }