Exemple #1
0
        public void ImageInfoHelper_MergeRepos_EmptyTarget()
        {
            RepoData[] repoDataSet = new RepoData[]
            {
                new RepoData
                {
                    Repo = "repo1",
                },
                new RepoData
                {
                    Repo   = "repo2",
                    Images = new SortedDictionary <string, ImageData>
                    {
                        {
                            "image1",
                            new ImageData()
                        }
                    }
                }
            };

            List <RepoData> targetRepos = new List <RepoData>();

            ImageInfoHelper.MergeRepos(repoDataSet, targetRepos);

            CompareRepos(repoDataSet, targetRepos);
        }
Exemple #2
0
        public override Task ExecuteAsync()
        {
            var imageInfoFiles = Directory.EnumerateFiles(
                Options.SourceImageInfoFolderPath,
                "*.json",
                SearchOption.AllDirectories);

            List <RepoData[]> srcReposList = imageInfoFiles
                                             .OrderBy(file => file) // Ensure the files are ordered for testing consistency between OS's.
                                             .Select(imageDataPath => JsonConvert.DeserializeObject <RepoData[]>(File.ReadAllText(imageDataPath)))
                                             .ToList();

            if (!srcReposList.Any())
            {
                throw new InvalidOperationException(
                          $"No JSON files found in source folder '{Options.SourceImageInfoFolderPath}'");
            }

            List <RepoData> combinedRepos = new List <RepoData>();

            foreach (RepoData[] repos in srcReposList)
            {
                ImageInfoHelper.MergeRepos(repos, combinedRepos);
            }

            RepoData[] reposArray = combinedRepos
                                    .OrderBy(r => r.Repo)
                                    .ToArray();

            string destinationContents = JsonHelper.SerializeObject(reposArray) + Environment.NewLine;

            File.WriteAllText(Options.DestinationImageInfoPath, destinationContents);

            return(Task.CompletedTask);
        }
Exemple #3
0
        public void ImageInfoHelper_MergeRepos_ExistingTarget()
        {
            ImageData repo2Image1;
            ImageData repo2Image2;
            ImageData repo2Image3;
            ImageData repo3Image1;

            RepoData[] repoDataSet = new RepoData[]
            {
                new RepoData
                {
                    Repo   = "repo2",
                    Images = new SortedDictionary <string, ImageData>
                    {
                        {
                            "image1",
                            repo2Image1 = new ImageData
                            {
                                BaseImages = new SortedDictionary <string, string>
                                {
                                    { "base1", "base1digest-NEW" }
                                }
                            }
                        }
                        ,
                        {
                            "image3",
                            repo2Image3 = new ImageData()
                        }
                    }
                },
                new RepoData
                {
                    Repo   = "repo3",
                    Images = new SortedDictionary <string, ImageData>
                    {
                        {
                            "image1",
                            repo3Image1 = new ImageData()
                        }
                    }
                },
                new RepoData
                {
                    Repo = "repo4",
                }
            };

            List <RepoData> targetRepos = new List <RepoData>
            {
                new RepoData
                {
                    Repo = "repo1"
                },
                new RepoData
                {
                    Repo   = "repo2",
                    Images = new SortedDictionary <string, ImageData>
                    {
                        {
                            "image1",
                            new ImageData
                            {
                                BaseImages = new SortedDictionary <string, string>
                                {
                                    { "base1", "base1digest" }
                                }
                            }
                        },
                        {
                            "image2",
                            repo2Image2 = new ImageData
                            {
                                BaseImages = new SortedDictionary <string, string>
                                {
                                    { "base2", "base2digest" }
                                }
                            }
                        }
                    }
                },
                new RepoData
                {
                    Repo = "repo3"
                }
            };

            ImageInfoHelper.MergeRepos(repoDataSet, targetRepos);

            List <RepoData> expected = new List <RepoData>
            {
                new RepoData
                {
                    Repo = "repo1"
                },
                new RepoData
                {
                    Repo   = "repo2",
                    Images = new SortedDictionary <string, ImageData>
                    {
                        {
                            "image1",
                            repo2Image1
                        },
                        {
                            "image2",
                            repo2Image2
                        },
                        {
                            "image3",
                            repo2Image3
                        }
                    }
                },
                new RepoData
                {
                    Repo   = "repo3",
                    Images = new SortedDictionary <string, ImageData>
                    {
                        {
                            "image1",
                            repo3Image1
                        }
                    }
                },
                new RepoData
                {
                    Repo = "repo4",
                }
            };

            CompareRepos(expected, targetRepos);
        }
Exemple #4
0
        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 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.");
                    }
                });
            }
        }