public async Task UpdatePackageInProjects( IGitDriver git, PackageUpdateSet updateSet, RepositoryData repository) { try { _logger.Terse(UpdatesLogger.OldVersionsToBeUpdated(updateSet)); git.Checkout(repository.DefaultBranch); // branch var branchName = BranchNamer.MakeName(updateSet); _logger.Verbose($"Using branch name: '{branchName}'"); git.CheckoutNewBranch(branchName); await UpdateAllCurrentUsages(updateSet); var commitMessage = CommitWording.MakeCommitMessage(updateSet); git.Commit(commitMessage); git.Push("nukeeper_push", branchName); var prTitle = CommitWording.MakePullRequestTitle(updateSet); await MakeGitHubPullRequest(updateSet, repository, prTitle, branchName); git.Checkout(repository.DefaultBranch); } catch (Exception ex) { _logger.Error("Update failed", ex); } }
private async Task <int> MakeUpdatePullRequests( IGitDriver git, RepositoryData repository, NuGetSources sources, SettingsContainer settings, IReadOnlyCollection <PackageUpdateSet> updates) { _logger.Normal(UpdatesLogger.OldVersionsToBeUpdated(updates)); git.Checkout(repository.DefaultBranch); // branch var branchName = BranchNamer.MakeName(updates); _logger.Detailed($"Using branch name: '{branchName}'"); git.CheckoutNewBranch(branchName); foreach (var updateSet in updates) { await _updateRunner.Update(updateSet, sources); var commitMessage = CommitWording.MakeCommitMessage(updateSet); git.Commit(commitMessage); } git.Push("nukeeper_push", branchName); var title = CommitWording.MakePullRequestTitle(updates); var body = CommitWording.MakeCommitDetails(updates); await _gitHub.CreatePullRequest(repository, title, body, branchName, settings.SourceControlServerSettings.Labels); git.Checkout(repository.DefaultBranch); return(updates.Count); }
public void TestWithSinglePackageAndBranchNamePrefix() { var packages = PackageUpdates.MakeUpdateSet("SomePackage") .InList(); var branchName = BranchNamer.MakeName(packages, BranchNameTemplate); Assert.That(branchName, Is.EqualTo("nukeeper/nukeeper-update-SomePackage-to-1.2.3")); }
private ValidationResult PopulateBranchNameTemplate( SettingsContainer settings) { var settingsFromFile = FileSettingsCache.GetSettings(); var value = Concat.FirstValue(BranchNameTemplate, settingsFromFile.BranchNameTemplate); if (string.IsNullOrWhiteSpace(value)) { settings.BranchSettings.BranchNameTemplate = null; return(ValidationResult.Success); } // Validating git branch names: https://stackoverflow.com/a/12093994/1661209 // We validate the user defined branch name prefix in combination with a actual branch name that NuKeeper could create. // We want to validate the combination since the prefix doesn't need to fully comply with the rules (E.G. 'nukeeper/' is not allowed soley as a branch name). var tokenErrors = new StringBuilder(); var tokenSet = Regex.Matches(value, @"{(\w+)}").Select(match => match.Groups[1].Value); foreach (var token in tokenSet) { if (!BranchNamer.IsValidTemplateToken(token)) { tokenErrors.Append($",{token}"); } } // Check for valid placeholders if (tokenErrors.Length > 0) { return(ValidationResult.Failure( $"Provided branch template has unknown tokens: '{tokenErrors.ToString().Trim(',')}'.")); } // Test if the generated branchname would be ok. // We assume tokens will be generated in valid values, so we use dummy values here var tokenValues = new Dictionary <string, string>(); foreach (var token in BranchNamer.TemplateTokens) { tokenValues.Add(token, "dummy"); } var validationValue = BranchNamer.MakeName(tokenValues, value); if (!Regex.IsMatch(validationValue, @"^(?!@$|build-|/|.*([/.]\.|//|@\{|\\))[^\000-\037\177 ~^:?*[]+/[^\000-\037\177 ~^:?*[]+(?<!\.lock|[/.])$")) { return(ValidationResult.Failure( $"Provided branch template '{value}' does not comply with branch naming rules.")); } settings.BranchSettings.BranchNameTemplate = value; return(ValidationResult.Success); }
public void TestWithTwoPackagesAndBranchNamePrefix() { var packages = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage"), PackageUpdates.MakeUpdateSet("OtherPackage") }; var branchName = BranchNamer.MakeName(packages, BranchNameTemplate); Assert.That(branchName, Is.EqualTo("nukeeper/nukeeper-update-2-packages-AA9F9828431C8BFB7A18D3D8F0CF229D")); }
public void TestWithThreePackagesAndBranchNamePrefix() { var packages = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage"), PackageUpdates.MakeUpdateSet("OtherPackage"), PackageUpdates.MakeUpdateSet("SomethingElse"), }; var branchName = BranchNamer.MakeName(packages, BranchNameTemplate); Assert.That(branchName, Is.EqualTo("nukeeper/nukeeper-update-3-packages-BBBB3BF2315D6111CFCBF6A4A7A29DD8")); }
private async Task <int> MakeUpdatePullRequests( IGitDriver git, RepositoryData repository, NuGetSources sources, SettingsContainer settings, IReadOnlyCollection <PackageUpdateSet> updates) { _logger.Normal(UpdatesLogger.OldVersionsToBeUpdated(updates)); await git.Checkout(repository.DefaultBranch); // branch var branchWithChanges = BranchNamer.MakeName(updates, settings.BranchSettings.BranchNamePrefix); _logger.Detailed($"Using branch name: '{branchWithChanges}'"); await git.CheckoutNewBranch(branchWithChanges); foreach (var updateSet in updates) { await _updateRunner.Update(updateSet, sources); var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(updateSet); await git.Commit(commitMessage); } await git.Push(repository.Remote, branchWithChanges); var title = _collaborationFactory.CommitWorder.MakePullRequestTitle(updates); var body = _collaborationFactory.CommitWorder.MakeCommitDetails(updates); string qualifiedBranch; if (!repository.IsFork) //check if we are on a fork, if so qualify the branch name { qualifiedBranch = branchWithChanges; } else { qualifiedBranch = repository.Push.Owner + ":" + branchWithChanges; } var pullRequestRequest = new PullRequestRequest(qualifiedBranch, title, repository.DefaultBranch, settings.BranchSettings.DeleteBranchAfterMerge) { Body = body }; await _collaborationFactory.CollaborationPlatform.OpenPullRequest(repository.Pull, pullRequestRequest, settings.SourceControlServerSettings.Labels); await git.Checkout(repository.DefaultBranch); return(updates.Count); }
private async Task <bool> CanMakeBranchFor(ForkData pushFork, PackageUpdateSet packageUpdateSet) { try { var branchName = BranchNamer.MakeName(packageUpdateSet); var githubBranch = await _github.GetRepositoryBranch(pushFork.Owner, pushFork.Name, branchName); return(githubBranch == null); } catch (Exception ex) { _logger.Error($"Failed on existing branch check at {pushFork.Owner}/{pushFork.Name}", ex); return(false); } }
public void EquivalentInputs_HaveSameHash() { var packages1 = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage", "2.3.4"), PackageUpdates.MakeUpdateSet("OtherPackage", "2.3.4") }; var packages2 = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage", "2.3.4"), PackageUpdates.MakeUpdateSet("OtherPackage", "2.3.4") }; var branchName1 = BranchNamer.MakeName(packages1); var branchName2 = BranchNamer.MakeName(packages2); Assert.That(branchName1, Is.EqualTo(branchName2)); }
public void NameChangeWithBranchNamePrefix_ChangesHash() { var packages1 = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage", "2.3.4"), PackageUpdates.MakeUpdateSet("OtherPackage", "2.3.4") }; var packages2 = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("ZomePackage", "2.3.4"), PackageUpdates.MakeUpdateSet("OtherPackage", "2.3.4") }; var branchName1 = BranchNamer.MakeName(packages1, BranchNameTemplate); var branchName2 = BranchNamer.MakeName(packages2, BranchNameTemplate); Assert.That(branchName1, Is.Not.EqualTo(branchName2)); }
public void VersionChange_ChangesHash() { var packages1 = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage", "2.3.4"), PackageUpdates.MakeUpdateSet("OtherPackage", "2.3.4") }; var packages2 = new List <PackageUpdateSet> { PackageUpdates.MakeUpdateSet("SomePackage", "2.3.4"), PackageUpdates.MakeUpdateSet("OtherPackage", "2.3.5") }; var branchName1 = BranchNamer.MakeName(packages1); var branchName2 = BranchNamer.MakeName(packages2); Assert.That(branchName1, Is.Not.EqualTo(branchName2)); }
public async Task <bool> MakeUpdatePullRequest( IGitDriver git, RepositoryData repository, PackageUpdateSet updateSet, NuGetSources sources, SourceControlServerSettings serverSettings) { try { _logger.Minimal(UpdatesLogger.OldVersionsToBeUpdated(updateSet)); git.Checkout(repository.DefaultBranch); // branch var branchName = BranchNamer.MakeName(updateSet); _logger.Detailed($"Using branch name: '{branchName}'"); git.CheckoutNewBranch(branchName); await _updateRunner.Update(updateSet, sources); var commitMessage = CommitWording.MakeCommitMessage(updateSet); git.Commit(commitMessage); git.Push("nukeeper_push", branchName); var prTitle = CommitWording.MakePullRequestTitle(updateSet); await MakeGitHubPullRequest(updateSet, repository, prTitle, branchName, serverSettings.Labels); git.Checkout(repository.DefaultBranch); return(true); } catch (Exception ex) { _logger.Error("Update failed", ex); return(false); } }
private static bool HasExistingBranch(IGitDriver git, PackageUpdateSet packageUpdateSet) { var qualifiedBranchName = "origin/" + BranchNamer.MakeName(packageUpdateSet); return(git.BranchExists(qualifiedBranchName)); }
private async Task <int> MakeUpdatePullRequests( IGitDriver git, RepositoryData repository, NuGetSources sources, SettingsContainer settings, IReadOnlyCollection <PackageUpdateSet> updates) { _logger.Normal(UpdatesLogger.OldVersionsToBeUpdated(updates)); await git.Checkout(repository.DefaultBranch); // branch var branchWithChanges = BranchNamer.MakeName(updates, settings.BranchSettings.BranchNameTemplate); _logger.Detailed($"Using branch name: '{branchWithChanges}'"); var ditCheckOut = await git.CheckoutNewBranch(branchWithChanges); if (!ditCheckOut) { await git.CheckoutRemoteToLocal(branchWithChanges); } var filteredUpdates = await _existingCommitFilter.Filter(git, updates, repository.DefaultBranch, branchWithChanges); foreach (var filtered in updates.Where(u => !filteredUpdates.Contains(u))) { var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(filtered); _logger.Normal($"Commit '{commitMessage}' already in branch '{branchWithChanges}'"); } var haveUpdates = filteredUpdates.Any(); foreach (var updateSet in filteredUpdates) { var commitMessage = _collaborationFactory.CommitWorder.MakeCommitMessage(updateSet); await _updateRunner.Update(updateSet, sources); await git.Commit(commitMessage); } if (haveUpdates) { await git.Push(repository.Remote, branchWithChanges); string qualifiedBranch; if (!repository.IsFork) //check if we are on a fork, if so qualify the branch name { qualifiedBranch = branchWithChanges; } else { qualifiedBranch = repository.Push.Owner + ":" + branchWithChanges; } bool pullRequestExists = await _collaborationFactory.CollaborationPlatform.PullRequestExists(repository.Pull, qualifiedBranch, repository.DefaultBranch); if (!pullRequestExists) { var title = _collaborationFactory.CommitWorder.MakePullRequestTitle(updates); var body = _collaborationFactory.CommitWorder.MakeCommitDetails(updates); var pullRequestRequest = new PullRequestRequest(qualifiedBranch, title, repository.DefaultBranch, settings.BranchSettings.DeleteBranchAfterMerge) { Body = body }; await _collaborationFactory.CollaborationPlatform.OpenPullRequest(repository.Pull, pullRequestRequest, settings.SourceControlServerSettings.Labels); } else { _logger.Normal($"A pull request already exists for {repository.DefaultBranch} <= {qualifiedBranch}"); } } await git.Checkout(repository.DefaultBranch); return(filteredUpdates.Count); }