/// <summary> /// Create new Pull Request /// </summary> /// <param name="TeamProjectName"></param> /// <param name="RepoName"></param> /// <param name="SourceRef"></param> /// <param name="TargetRef"></param> /// <param name="WorkItems"></param> /// <returns></returns> static int CreatePullRequest(string TeamProjectName, string RepoName, string SourceRef, string TargetRef, int [] WorkItems) { GitPullRequest pr = new GitPullRequest(); pr.Title = pr.Description = String.Format("PR from {0} into {1} ", SourceRef, TargetRef); pr.SourceRefName = SourceRef; pr.TargetRefName = TargetRef; if (WorkItems != null && WorkItems.Length > 0) { List <ResourceRef> wiRefs = new List <ResourceRef>(); foreach (int wiId in WorkItems) { WorkItem workItem = WitClient.GetWorkItemAsync(wiId).Result; wiRefs.Add(new ResourceRef { Id = workItem.Id.ToString(), Url = workItem.Url }); } pr.WorkItemRefs = wiRefs.ToArray(); } var newPr = GitClient.CreatePullRequestAsync(pr, TeamProjectName, RepoName).Result; Console.WriteLine("PR was created: " + newPr.PullRequestId); return(newPr.PullRequestId); }
/// <summary> /// Creates a pull request, and then adds a reviewer to it. /// </summary> /// <param name="gitHttpClient"> GitHttpClient that is created for accessing vsts</param> /// <param name="gitPullRequest"> the pull request to be created</param> /// <param name="repositoryId"> the unique identifier of the repository</param> /// <param name="reviewerAlias"> reviewer's alias in vsts</param> /// <param name="vstsAccountUrl">vsts account's url</param> /// <param name="personalToken"> personal access token to access the vsts account. </param> public static async void CreatePullRequestAndAddReviewer( GitHttpClient gitHttpClient, GitPullRequest gitPullRequest, string repositoryId, string reviewerAlias, Uri vstsAccountUrl, string personalToken) { // 1- Create the pull request. GitPullRequest pullRequest = gitHttpClient.CreatePullRequestAsync(gitPullRequest, repositoryId, cancellationToken: CancellationToken.None).Result; // 2- Create an Identity Client to get the reviewer's vsts id IdentityHttpClient identityHttpClient = CreateIdentityClient(vstsAccountUrl, personalToken); // 3- Find the reviewer's vsts identity. Identity identity = SearchForReviewerVstsIdentity(identityHttpClient, reviewerAlias).Result; // 4- Create a IdentityRefWithVote for the reviewer IdentityRefWithVote identityRefWithVote = new IdentityRefWithVote { Id = identity.Id.ToString(), IsRequired = true // false otherwise. }; // 5- Finally add the reviewer to the pull request. await AddReviewerToPullRequest(gitHttpClient, pullRequest, identityRefWithVote); }
private async Task <bool> CreateNewPullRequest(string dummyBranchName, CancellationToken token) { token.ThrowIfCancellationRequested(); var pullRequest = new GitPullRequest() { Title = $"AutoMerge PR from {SourceName} to {DestName}", SourceRefName = SourceBranch, TargetRefName = dummyBranchName, }; try { var response = await GitHttpClient.CreatePullRequestAsync(pullRequest, RepositoryId); Logger.Info($"Pull Request ID: {response.PullRequestId}, URL: {response.Url}"); AutoPullRequestId = response.PullRequestId; return(true); } catch (OperationCanceledException) { Logger.Error($"Time out occurs when try to create PR from {SourceBranch} to {dummyBranchName}"); throw; } catch (Exception e) { Logger.Error($"Exception occurs when try to create PR from {SourceBranch} to {dummyBranchName}, message: {e.Message}"); throw; } }
private async Task <int?> CreatePullRequestIfChanged(Guid repoId, List <GitChange> changes, string lastCommit, string baseBranchName, string targetBranchName, ITemplateReplacer replacer) { if (changes.Count > 0) { var title = GetTitle(replacer); var description = GetDescription(replacer); var commitMessage = GetCommitMessage(replacer); var push = CreatePush(lastCommit, changes, targetBranchName, commitMessage); await _gitClient.CreatePushAsync(push, repoId).ConfigureAwait(false); Log($"Push for {repoId} at {targetBranchName} created."); if (!string.Equals(baseBranchName, targetBranchName, StringComparison.InvariantCultureIgnoreCase)) { var pr = CreatePullRequest(baseBranchName, targetBranchName, title, description); var result = await _gitClient.CreatePullRequestAsync(pr, repoId).ConfigureAwait(false); Log($"Pull request for {repoId} to {baseBranchName} created."); return(result.PullRequestId); } Log($"Skipping pull request since {baseBranchName} was the target."); } return(null); }
public static void CreatePullRequests(GitHttpClient client) { var criteria = new GitQueryCommitsCriteria { }; var commits = client.GetCommitsAsync(Config.PrepopulatedRepositoryProject, Config.PrepopulatedGitRepository, criteria, 0, 10000).Result; Console.WriteLine($"Retreived {commits.Count} commits"); var someCommits = commits.Skip(1).Take(1000); var branchNumber = 0; var branches = client.UpdateRefsAsync(someCommits.Select(c => new GitRefUpdate { Name = $"refs/heads/branch{branchNumber++}", RepositoryId = Config.PrepopulatedGitRepositoryGuid, OldObjectId = "0000000000000000000000000000000000000000", NewObjectId = c.CommitId }), Config.PrepopulatedGitRepositoryGuid).Result; Console.WriteLine($"Created {branches.Count} branches"); var firstBranch = branches.First(); var otherBranches = branches.Skip(1); foreach (var branch in otherBranches) { var pullRequest = client.CreatePullRequestAsync(new GitPullRequest { SourceRefName = branch.Name, TargetRefName = firstBranch.Name, Title = $"Merge {branch.Name} to {firstBranch.Name}" }, Config.PrepopulatedGitRepositoryGuid).Result; Console.WriteLine($"Created pullrequest {pullRequest.Title}"); } }
private void CreatePullRequest(GitHttpClient client, Guid repoId, string sourceBranch, string targetBranch, VersionInfo versionInfo) { client.CreatePullRequestAsync(new GitPullRequest() { SourceRefName = AzureGitHelper.WithRefsAndHeadsPrefix(sourceBranch), TargetRefName = AzureGitHelper.WithRefsAndHeadsPrefix(targetBranch), Title = $"Inicialização da release {versionInfo}" }, repoId).Wait(); }
private static GitPullRequest CreatePullRequestInternal(ClientSampleContext context, GitRepository repo, GitHttpClient gitClient) { // we need a new branch with changes in order to create a PR // first, find the default branch string defaultBranchName = WithoutRefsPrefix(repo.DefaultBranch); GitRef defaultBranch = gitClient.GetRefsAsync(repo.Id, filter: defaultBranchName).Result.First(); // next, craft the branch and commit that we'll push GitRefUpdate newBranch = new GitRefUpdate() { Name = $"refs/heads/vsts-api-sample/{ChooseRefsafeName()}", OldObjectId = defaultBranch.ObjectId, }; string newFileName = $"{ChooseItemsafeName()}.md"; GitCommitRef newCommit = new GitCommitRef() { Comment = "Add a sample file", Changes = new GitChange[] { new GitChange() { ChangeType = VersionControlChangeType.Add, Item = new GitItem() { Path = $"/vsts-api-sample/{newFileName}" }, NewContent = new ItemContent() { Content = "# Thank you for using VSTS!", ContentType = ItemContentType.RawText, }, } }, }; // create the push with the new branch and commit GitPush push = gitClient.CreatePushAsync(new GitPush() { RefUpdates = new GitRefUpdate[] { newBranch }, Commits = new GitCommitRef[] { newCommit }, }, repo.Id).Result; // finally, create a PR var pr = gitClient.CreatePullRequestAsync(new GitPullRequest() { SourceRefName = newBranch.Name, TargetRefName = repo.DefaultBranch, Title = $"Add {newFileName} (from VSTS REST samples)", Description = "Adding this file from the pull request samples", }, repo.Id).Result; return(pr); }
/// <summary> /// Creates a pull request, and then sets it to auto complete. /// </summary> /// <param name="gitHttpClient">GitHttpClient that is created for accessing vsts repo, and codebase.</param> /// <param name="repositoryId">The unique identifier of the repository</param> /// <param name="pullRequest">The pull request to be created, and then set autocomplete.</param> /// <param name="mergeCommitMessage">Provides text to post, when the pull request is completed and merged.</param> public static GitPullRequest CreatePullRequestAndSetAutoComplete(GitHttpClient gitHttpClient, string repositoryId, GitPullRequest pullRequest, string mergeCommitMessage) { // 1- Create the pull request. pullRequest = gitHttpClient.CreatePullRequestAsync( pullRequest, repositoryId, cancellationToken: CancellationToken.None).Result; //2- Set autocomplete. pullRequest = EnableAutoCompleteOnAnExistingPullRequest(gitHttpClient, pullRequest, mergeCommitMessage); return(pullRequest); }
private CommandResult CreatePullRequest(GitHttpClient client, Guid repoId, GitPullRequest pullRequestBase, string topicBranchName, string targetBranch, string pullRequestTargetBranch) { var pullRequest = client.CreatePullRequestAsync(new GitPullRequest() { SourceRefName = AzureGitHelper.WithRefsAndHeadsPrefix(topicBranchName), TargetRefName = AzureGitHelper.WithRefsAndHeadsPrefix(targetBranch), Title = $"Merge {pullRequestTargetBranch} to {Options.TargetBranch}", Description = $"Originated by Pull Request {pullRequestBase.PullRequestId} - {pullRequestBase.Title}" }, repoId).Result; var url = AzureGitHelper.BuildUrl(Context, "/pullrequest/" + pullRequest.PullRequestId); BrowserHelper.OpenUrl(url); return(Ok()); }
private async Task <bool> CreateNewPullRequest(string dummyBranchName, CancellationToken token) { token.ThrowIfCancellationRequested(); var pullRequest = new GitPullRequest() { Title = $"AutoMerge PR from {SourceName} to {DestName}", SourceRefName = dummyBranchName, TargetRefName = DestBranch, }; Console.WriteLine($"Creating a new Pull Request: {pullRequest.Title} on \"{dummyBranchName}\""); var response = await gitHttpClient.CreatePullRequestAsync(pullRequest, RepositoryId); Console.WriteLine($"Pull Request ID: {response.PullRequestId}, URL: {response.Url}"); AutoPullRequestId = response.PullRequestId; return(true); }
public async Task <string> CreatePullRequestAsync(string repoUri, PullRequest pullRequest) { VssConnection connection = CreateConnection(repoUri); GitHttpClient client = await connection.GetClientAsync <GitHttpClient>(); (string team, string repo) = ParseRepoUri(repoUri); GitPullRequest createdPr = await client.CreatePullRequestAsync( new GitPullRequest { Title = pullRequest.Title, Description = pullRequest.Description, SourceRefName = "refs/heads/" + pullRequest.HeadBranch, TargetRefName = "refs/heads/" + pullRequest.BaseBranch }, team, repo); return(createdPr.Url); }
/// <summary> /// Create and complete pull request /// </summary> /// <param name="TeamProjectName"></param> /// <param name="RepoName"></param> /// <param name="SourceRef"></param> /// <param name="TargetRef"></param> static void CreateCompletePullRequest(string TeamProjectName, string RepoName, string SourceRef, string TargetRef) { GitPullRequest pr = new GitPullRequest(); pr.Title = pr.Description = String.Format("PR from {0} into {1} ", SourceRef, TargetRef); pr.SourceRefName = SourceRef; pr.TargetRefName = TargetRef; var newPr = GitClient.CreatePullRequestAsync(pr, TeamProjectName, RepoName).Result; Console.WriteLine("PR was created: " + newPr.PullRequestId); Thread.Sleep(5000); GitPullRequest prUdated = new GitPullRequest(); prUdated.Status = PullRequestStatus.Completed; prUdated.LastMergeSourceCommit = newPr.LastMergeSourceCommit; prUdated = GitClient.UpdatePullRequestAsync(prUdated, TeamProjectName, RepoName, newPr.PullRequestId).Result; }
/// <summary> /// Create new Pull Request /// </summary> /// <param name="TeamProjectName"></param> /// <param name="RepoName"></param> /// <param name="SourceRef"></param> /// <param name="TargetRef"></param> /// <param name="WorkItems"></param> /// <returns></returns> static int CreatePullRequest(string TeamProjectName, string RepoName, string SourceRef, string TargetRef, int [] WorkItems, string ReviewerId) { GitPullRequest pr = new GitPullRequest(); pr.Title = pr.Description = String.Format("PR from {0} into {1} ", SourceRef, TargetRef); pr.SourceRefName = SourceRef; pr.TargetRefName = TargetRef; if (ReviewerId != "") { IdentityRefWithVote[] identityRefWithVotes = { new IdentityRefWithVote { Id = ReviewerId } }; pr.Reviewers = identityRefWithVotes; } if (WorkItems != null && WorkItems.Length > 0) { List <ResourceRef> wiRefs = new List <ResourceRef>(); foreach (int wiId in WorkItems) { WorkItem workItem = WitClient.GetWorkItemAsync(wiId).Result; wiRefs.Add(new ResourceRef { Id = workItem.Id.ToString(), Url = workItem.Url }); } pr.WorkItemRefs = wiRefs.ToArray(); } var newPr = GitClient.CreatePullRequestAsync(pr, TeamProjectName, RepoName).Result; CreateNewCommentThread(TeamProjectName, RepoName, newPr.PullRequestId, "Fix this code!!!!"); Console.WriteLine("PR was created: " + newPr.PullRequestId); return(newPr.PullRequestId); }
public GitPullRequest CreatePullRequestInner(bool cleanUp) { VssConnection connection = this.Context.Connection; GitHttpClient gitClient = connection.GetClient <GitHttpClient>(); TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context); GitRepository repo = GitSampleHelpers.FindAnyRepository(this.Context, project.Id); // we need a new branch with changes in order to create a PR // first, find the default branch string defaultBranchName = GitSampleHelpers.WithoutRefsPrefix(repo.DefaultBranch); GitRef defaultBranch = gitClient.GetRefsAsync(repo.Id, filter: defaultBranchName).Result.First(); // next, craft the branch and commit that we'll push GitRefUpdate newBranch = new GitRefUpdate() { Name = $"refs/heads/vsts-api-sample/{GitSampleHelpers.ChooseRefsafeName()}", OldObjectId = defaultBranch.ObjectId, }; string newFileName = $"{GitSampleHelpers.ChooseItemsafeName()}.md"; GitCommitRef newCommit = new GitCommitRef() { Comment = "Add a sample file", Changes = new GitChange[] { new GitChange() { ChangeType = VersionControlChangeType.Add, Item = new GitItem() { Path = $"/vsts-api-sample/{newFileName}" }, NewContent = new ItemContent() { Content = "# Thank you for using VSTS!", ContentType = ItemContentType.RawText, }, } }, }; // create the push with the new branch and commit GitPush push = gitClient.CreatePushAsync(new GitPush() { RefUpdates = new GitRefUpdate[] { newBranch }, Commits = new GitCommitRef[] { newCommit }, }, repo.Id).Result; // finally, create a PR var pr = gitClient.CreatePullRequestAsync(new GitPullRequest() { SourceRefName = newBranch.Name, TargetRefName = repo.DefaultBranch, Title = $"Add {newFileName} (from VSTS REST samples)", Description = "Adding this file from the pull request samples", }, repo.Id).Result; Console.WriteLine("project {0}, repo {1}", project.Name, repo.Name); Console.WriteLine("{0} (#{1}) {2} -> {3}", pr.Title.Substring(0, Math.Min(40, pr.Title.Length)), pr.PullRequestId, pr.SourceRefName, pr.TargetRefName); if (cleanUp) { // clean up after ourselves (and in case logging is on, don't log these calls) ClientSampleHttpLogger.SetSuppressOutput(this.Context, true); // abandon the PR GitPullRequest updatedPr = new GitPullRequest() { Status = PullRequestStatus.Abandoned, }; pr = gitClient.UpdatePullRequestAsync(updatedPr, repo.Id, pr.PullRequestId).Result; // delete the branch GitRefUpdateResult refDeleteResult = gitClient.UpdateRefsAsync( new GitRefUpdate[] { new GitRefUpdate() { OldObjectId = push.RefUpdates.First().NewObjectId, NewObjectId = new string('0', 40), Name = push.RefUpdates.First().Name, } }, repositoryId: repo.Id).Result.First(); } return(pr); }