public async Task <bool> HasOpenPullRequestForThisBranchAsync(IFolder repositoryFolder, IErrorsAndInfos errorsAndInfos) { var pullRequests = await GetPullRequestsAsync(repositoryFolder, "open", errorsAndInfos); var checkedOutBranch = vGitUtilities.CheckedOutBranch(repositoryFolder); return(pullRequests.Any(p => p.Branch == checkedOutBranch)); }
public void CanIdentifyCheckedOutBranch() { Assert.AreEqual("development", vSut.CheckedOutBranch(DevelopmentFolder)); var developmentSubFolder = DevelopmentFolder.SubFolder("src").SubFolder("Test"); Assert.AreEqual("development", vSut.CheckedOutBranch(developmentSubFolder)); Assert.AreEqual("master", vSut.CheckedOutBranch(MasterFolder)); Assert.AreEqual("", vSut.CheckedOutBranch(NoGitFolder)); }
private async Task AutoCommitAndPushAsync(string nugetFeedId, IFolder repositoryFolder, List <string> files, bool onlyIfNecessary, string commitMessage, bool noRebuildRequired, IErrorsAndInfos errorsAndInfos) { var branchName = GitUtilities.CheckedOutBranch(repositoryFolder); if (branchName != "master") { errorsAndInfos.Errors.Add(Properties.Resources.CheckedOutBranchIsNotMaster); return; } var headTipShaBeforePush = GitUtilities.HeadTipIdSha(repositoryFolder); GitUtilities.IdentifyOwnerAndName(repositoryFolder, out var owner, out _, errorsAndInfos); if (errorsAndInfos.AnyErrors()) { errorsAndInfos.Errors.Add(Properties.Resources.OwnerAndNameNotFound); return; } var personalAccessTokensSecret = new PersonalAccessTokensSecret(); var personalAccessTokens = await SecretRepository.GetAsync(personalAccessTokensSecret, errorsAndInfos); var personalAccessToken = personalAccessTokens.FirstOrDefault(t => t.Owner == owner && t.Purpose == "AutoCommitPush"); if (personalAccessToken == null) { errorsAndInfos.Errors.Add(Properties.Resources.AutoCommitPushAccessTokenNotFound); return; } using var repo = new Repository(repositoryFolder.FullName); var remotes = repo.Network.Remotes.ToList(); if (remotes.Count != 1) { errorsAndInfos.Errors.Add(Properties.Resources.RemoteNotFoundOrNotUnique); return; } var remote = remotes[0]; files.ForEach(f => { // ReSharper disable once AccessToDisposedClosure Commands.Stage(repo, f); }); var checkFiles = GitUtilities.FilesWithUncommittedChanges(repositoryFolder); if (onlyIfNecessary && !checkFiles.Any()) { return; } if (checkFiles.Count != files.Count) { errorsAndInfos.Errors.Add(string.Format(Properties.Resources.NumberOfFilesWithUncommittedChangesHasChanged, string.Join(", ", files), string.Join(", ", checkFiles))); return; } var author = new Signature(personalAccessToken.TokenName, personalAccessToken.Email, DateTime.Now); var committer = author; repo.Commit(commitMessage, author, committer); var options = new PushOptions { CredentialsProvider = (_, _, _) => new UsernamePasswordCredentials { Username = owner, Password = personalAccessToken.Token } }; repo.Network.Push(remote, @"refs/heads/" + branchName, options); if (!noRebuildRequired) { return; } var pushedHeadTipShaRepository = PushedHeadTipShaRepository; if (!(await pushedHeadTipShaRepository.GetAsync(nugetFeedId, errorsAndInfos)).Contains(headTipShaBeforePush)) { return; } var headTipSha = GitUtilities.HeadTipIdSha(repositoryFolder); await pushedHeadTipShaRepository.AddAsync(nugetFeedId, headTipSha, errorsAndInfos); }