Esempio n. 1
0
        public static WikiV2 FindOrCreateProjectWiki(ClientSampleContext context)
        {
            VssConnection  connection = context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            Guid projectId = ClientSampleHelpers.FindAnyProject(context).Id;

            List <WikiV2> wikis        = wikiClient.GetAllWikisAsync(projectId).SyncResult();
            WikiV2        wikiToReturn = wikis != null && wikis.Count != 0
                ? wikis.Find(w => w.Type == WikiType.ProjectWiki)
                : null;

            if (wikiToReturn == null)
            {
                // No project wiki existing. Create one.
                var createParameters = new WikiCreateParametersV2()
                {
                    Name      = "sampleProjectWiki",
                    ProjectId = projectId,
                    Type      = WikiType.ProjectWiki
                };

                wikiToReturn = wikiClient.CreateWikiAsync(createParameters).SyncResult();
            }

            return(wikiToReturn);
        }
Esempio n. 2
0
        public static int GetAnyWikiPageId(ClientSampleContext context, WikiV2 wiki)
        {
            string         path       = GetAnyWikiPagePath(context, wiki);
            VssConnection  connection = context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiPage anyPage = wikiClient.GetPageAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                path: path,
                recursionLevel: VersionControlRecursionType.OneLevel).SyncResult().Page;

            if (!anyPage.Id.HasValue)
            {
                WikiPageCreateOrUpdateParameters parameters = new WikiPageCreateOrUpdateParameters()
                {
                    Content = "Wiki page content"
                };

                WikiPageResponse wikiPageResponse = wikiClient.CreateOrUpdatePageAsync(
                    parameters,
                    project: wiki.ProjectId,
                    wikiIdentifier: wiki.Id,
                    path: "SamplePage" + new Random().Next(1, 999),
                    Version: null).SyncResult();

                context.Log("Create page '{0}' in wiki '{1}'", wikiPageResponse.Page.Path, wiki.Name);

                anyPage = wikiPageResponse.Page;
            }

            return(anyPage.Id.Value);
        }
Esempio n. 3
0
        public static GitRepository FindAnyRepository(ClientSampleContext context, Guid projectId)
        {
            GitRepository repo;

            if (!FindAnyRepository(context, projectId, out repo))
            {
                throw new Exception("No repositories available. Create a repo in this project and run the sample again.");
            }

            return(repo);
        }
Esempio n. 4
0
        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);
        }
Esempio n. 5
0
        public void Initialize()
        {
            string connectionUrl = TestContext.Properties["connectionUrl"] as string;
            string userName      = TestContext.Properties["password"] as string;
            string password      = TestContext.Properties["password"] as string;

            ClientSampleContext context = new ClientSampleContext(new Uri(connectionUrl), new VssBasicCredential(userName, password));

            ClientSample         = new T();
            ClientSample.Context = context;
        }
Esempio n. 6
0
        public static string GetAnyWikiPagePath(ClientSampleContext context, WikiV2 wiki)
        {
            VssConnection  connection = context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiPage rootPage = wikiClient.GetPageAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                path: "/",
                recursionLevel: VersionControlRecursionType.OneLevel).SyncResult().Page;

            return(rootPage.SubPages[0].Path);
        }
Esempio n. 7
0
        public static GitPullRequestStatus CreatePullRequestStatus(ClientSampleContext context, Guid repositoryId, int pullRequestId, int?iterationId = null)
        {
            VssConnection connection = context.Connection;
            GitHttpClient gitClient  = connection.GetClient <GitHttpClient>();

            using (new ClientSampleHttpLoggerOutputSuppression())
            {
                GitPullRequestStatus status        = GenerateSampleStatus(iterationId);
                GitPullRequestStatus createdStatus = gitClient.CreatePullRequestStatusAsync(status, repositoryId, pullRequestId).Result;

                return(createdStatus);
            }
        }
Esempio n. 8
0
        public static WikiPageResponse CreatePage(ClientSampleContext context, WikiV2 wiki, string path)
        {
            VssConnection  connection = context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiPageCreateOrUpdateParameters parameters = new WikiPageCreateOrUpdateParameters()
            {
                Content = "Wiki page content"
            };

            WikiPageResponse wikiPageResponse = wikiClient.CreateOrUpdatePageAsync(
                parameters,
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                path: path,
                Version: null).SyncResult();

            return(wikiPageResponse);
        }
Esempio n. 9
0
        public static GitPullRequest AbandonPullRequest(ClientSampleContext context, GitRepository repo, int pullRequestId)
        {
            VssConnection connection = context.Connection;
            GitHttpClient gitClient  = connection.GetClient <GitHttpClient>();

            using (new ClientSampleHttpLoggerOutputSuppression())
            {
                // clean up after ourselves (and in case logging is on, don't log these calls)
                ClientSampleHttpLogger.SetSuppressOutput(context, true);

                // abandon the PR
                GitPullRequest updatedPr = new GitPullRequest()
                {
                    Status = PullRequestStatus.Abandoned,
                };

                var pullRequest = gitClient.UpdatePullRequestAsync(updatedPr, repo.Id, pullRequestId).Result;

                return(pullRequest);
            }
        }
Esempio n. 10
0
        public static GitPullRequest CreatePullRequest(ClientSampleContext context, GitRepository repo)
        {
            VssConnection connection = context.Connection;
            GitHttpClient gitClient  = connection.GetClient <GitHttpClient>();

            using (new ClientSampleHttpLoggerOutputSuppression())
            {
                GitPullRequest pullRequest = gitClient.GetPullRequestsAsync(
                    repo.Id,
                    new GitPullRequestSearchCriteria()
                {
                    Status = PullRequestStatus.Active
                }).Result.FirstOrDefault();

                if (pullRequest == null)
                {
                    pullRequest = CreatePullRequestInternal(context, repo, gitClient);
                }

                return(pullRequest);
            }
        }
Esempio n. 11
0
        public static WikiV2 FindOrCreateCodeWiki(ClientSampleContext context)
        {
            VssConnection  connection = context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            Guid projectId = ClientSampleHelpers.FindAnyProject(context).Id;

            List <WikiV2> wikis        = wikiClient.GetAllWikisAsync(projectId).SyncResult();
            WikiV2        wikiToReturn = wikis != null && wikis.Count != 0
                ? wikis.Find(w => w.Type == WikiType.CodeWiki)
                : null;

            if (wikiToReturn == null)
            {
                // No code wiki existing. Create one.
                GitHttpClient        gitClient    = connection.GetClient <GitHttpClient>();
                List <GitRepository> repositories = gitClient.GetRepositoriesAsync(projectId).Result;
                Guid repositoryId = repositories[0].Id;

                var createParameters = new WikiCreateParametersV2()
                {
                    Name         = "sampleCodeWiki",
                    ProjectId    = projectId,
                    RepositoryId = repositoryId,
                    Type         = WikiType.CodeWiki,
                    MappedPath   = "/",    // any folder path in the repository
                    Version      = new GitVersionDescriptor()
                    {
                        Version = "master"
                    }
                };

                wikiToReturn = wikiClient.CreateWikiAsync(createParameters).SyncResult();
            }

            return(wikiToReturn);
        }
Esempio n. 12
0
        private static bool FindAnyRepository(ClientSampleContext context, Guid projectId, out GitRepository repo)
        {
            // Check if we already have a repo loaded
            if (!context.TryGetValue <GitRepository>("$someRepo", out repo))
            {
                VssConnection connection = context.Connection;
                GitHttpClient gitClient  = connection.GetClient <GitHttpClient>();

                using (new ClientSampleHttpLoggerOutputSuppression())
                {
                    // Check if an ID was already set (this could have been provided by the caller)
                    Guid repoId;
                    if (!context.TryGetValue <Guid>("repositoryId", out repoId))
                    {
                        // Get the first repo
                        repo = gitClient.GetRepositoriesAsync(projectId).Result.FirstOrDefault();
                    }
                    else
                    {
                        // Get the details for this repo
                        repo = gitClient.GetRepositoryAsync(repoId.ToString()).Result;
                    }
                }

                if (repo != null)
                {
                    context.SetValue <GitRepository>("$someRepo", repo);
                }
                else
                {
                    // create a project here?
                    throw new Exception("No repos available for running the sample.");
                }
            }

            return(repo != null);
        }
Esempio n. 13
0
        public static GitRepository FindAnyRepositoryOnAnyProject(ClientSampleContext context)
        {
            Guid projectId = ClientSampleHelpers.FindAnyProject(context).Id;

            return(FindAnyRepository(context, projectId));
        }