Esempio n. 1
0
        public static WikiV2 FindOrCreateCodeWiki(VssConnection connection, Guid projectId)
        {
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            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 = "main"
                    }
                };

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

            return(wikiToReturn);
        }
Esempio n. 2
0
        public WikiPageResponse GetWikiPageByIdAndSubPages()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki       = Helpers.FindOrCreateProjectWiki(this.Context);
            int    somePageId = Helpers.GetAnyWikiPageId(this.Context, wiki);

            WikiPageResponse rootPageResponse = wikiClient.GetPageByIdAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                id: somePageId,
                recursionLevel: VersionControlRecursionType.OneLevel).SyncResult();

            if (rootPageResponse.Page.SubPages.Count() == 0)
            {
                WikiPageResponse wikiPageResponse1 = Helpers.CreatePage(this.Context, wiki, rootPageResponse.Page.Path + "/SubPage" + new Random().Next(1, 999));
                WikiPageResponse wikiPageResponse2 = Helpers.CreatePage(this.Context, wiki, rootPageResponse.Page.Path + "/SubPage" + new Random().Next(1, 999));

                rootPageResponse = wikiClient.GetPageByIdAsync(
                    project: wiki.ProjectId,
                    wikiIdentifier: wiki.Id,
                    id: somePageId,
                    recursionLevel: VersionControlRecursionType.OneLevel).SyncResult();
            }

            Context.Log("Retrieved the following subpages for the root page:");
            foreach (WikiPage subPage in rootPageResponse.Page.SubPages)
            {
                Context.Log("Sub-page : '{0}'", subPage.Path);
            }

            return(rootPageResponse);
        }
Esempio n. 3
0
        public WikiV2 CreateCodeWiki()
        {
            VssConnection  connection = this.Context.Connection;
            GitHttpClient  gitClient  = connection.GetClient <GitHttpClient>();
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            Guid projectId = ClientSampleHelpers.FindAnyProject(this.Context).Id;
            List <GitRepository> repositories = gitClient.GetRepositoriesAsync(projectId).Result;

            WikiV2 createdWiki  = null;
            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"
                }
            };

            createdWiki = wikiClient.CreateWikiAsync(createParameters).SyncResult();

            Context.Log("Created wiki with name '{0}' in project '{1}'", createdWiki.Name, createdWiki.ProjectId);

            // Cleanup
            ClientSampleHttpLogger.SetSuppressOutput(this.Context, true);
            wikiClient.DeleteWikiAsync(createdWiki.Id).SyncResult();

            return(createdWiki);
        }
Esempio n. 4
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. 5
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. 6
0
        public WikiV2 UpdateWiki()
        {
            VssConnection  connection = this.Context.Connection;
            GitHttpClient  gitClient  = connection.GetClient <GitHttpClient>();
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 codeWiki = Helpers.FindOrCreateCodeWiki(this.Context);

            // Get the versions in that wiki
            List <GitVersionDescriptor> versions = codeWiki.Versions.ToList();

            // Append the new version
            List <GitBranchStats> branches = gitClient.GetBranchesAsync(codeWiki.ProjectId, codeWiki.RepositoryId).SyncResult();

            foreach (var branch in branches)
            {
                versions.Add(new GitVersionDescriptor()
                {
                    Version = branch.Name
                });
            }

            WikiUpdateParameters updateParams = new WikiUpdateParameters()
            {
                Versions = versions
            };

            WikiV2 updatedCodeWiki = wikiClient.UpdateWikiAsync(updateParams, codeWiki.ProjectId, codeWiki.Name).SyncResult();

            Context.Log("Updated wiki with name '{0}' in project '{1}'", updatedCodeWiki.Name, updatedCodeWiki.ProjectId);
            Context.Log("Updated versions are : {0}", string.Join(",", updatedCodeWiki.Versions.Select(v => v.Version)));

            return(updatedCodeWiki);
        }
        public WikiPageMoveResponse ReorderWikiPage()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki = Helpers.FindOrCreateProjectWiki(this.Context);

            var pageCreateParameters = new WikiPageCreateOrUpdateParameters()
            {
                Content = "Wiki page content",
            };

            var randomNumber = new Random().Next(1, 999);
            // First page
            string           firstPagePath     = "SamplePage" + randomNumber;
            WikiPageResponse firstPageResponse = wikiClient.CreateOrUpdatePageAsync(
                pageCreateParameters,
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name,
                path: firstPagePath,
                Version: null).SyncResult();

            Context.Log("Created page '{0}' in wiki '{1}'", firstPageResponse.Page.Path, wiki.Name);

            // Second page
            string           secondPagePath     = "SamplePage" + (randomNumber + 1);
            WikiPageResponse secondPageResponse = wikiClient.CreateOrUpdatePageAsync(
                pageCreateParameters,
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name,
                path: secondPagePath,
                Version: null).SyncResult();

            Context.Log("Created page '{0}' in wiki '{1}'", secondPageResponse.Page.Path, wiki.Name);

            var pageMoveParameters = new WikiPageMoveParameters()
            {
                Path     = firstPagePath,
                NewPath  = firstPagePath,
                NewOrder = 0
            };

            WikiPageMoveResponse pageMoveResponse = wikiClient.CreatePageMoveAsync(
                pageMoveParameters: pageMoveParameters,
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name).SyncResult();

            Context.Log("Page '{0}' moved to order '{1}'", pageMoveResponse.PageMove.Path, pageMoveResponse.PageMove.NewOrder);

            // Cleanup
            ClientSampleHttpLogger.SetSuppressOutput(this.Context, true);
            wikiClient.DeletePageAsync(wiki.ProjectId, wiki.Id, firstPagePath).SyncResult();
            wikiClient.DeletePageAsync(wiki.ProjectId, wiki.Id, secondPagePath).SyncResult();

            return(pageMoveResponse);
        }
Esempio n. 8
0
        public WikiV2 GetWikiByName()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 existingWiki = Helpers.FindOrCreateCodeWiki(this.Context);

            WikiV2 wiki = wikiClient.GetWikiAsync(existingWiki.ProjectId, existingWiki.Name).SyncResult();

            Context.Log("Retrieved wiki with name '{0}' in project '{1}'", wiki.Name, wiki.ProjectId);

            return(wiki);
        }
Esempio n. 9
0
        public WikiV2 DeleteCodeWiki()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 codeWiki = Helpers.FindOrCreateCodeWiki(this.Context);

            WikiV2 deletedWiki = wikiClient.DeleteWikiAsync(codeWiki.ProjectId, codeWiki.Name).SyncResult();

            Context.Log("Deleted wiki with name '{0}' in project '{1}'", deletedWiki.Name, deletedWiki.ProjectId);

            return(deletedWiki);
        }
Esempio n. 10
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. 11
0
        public IEnumerable <WikiV2> GetAllWikisInACollection()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            List <WikiV2> wikis = wikiClient.GetAllWikisAsync().SyncResult();

            foreach (WikiV2 wiki in wikis)
            {
                Context.Log("Retrieved wiki with name '{0}' in project '{1}'", wiki.Name, wiki.ProjectId);
            }

            return(wikis);
        }
Esempio n. 12
0
        public IEnumerable <WikiV2> GetAllWikisInAProject()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            Guid projectId = ClientSampleHelpers.FindAnyProject(this.Context).Id;

            List <WikiV2> wikis = wikiClient.GetAllWikisAsync(projectId).SyncResult();

            foreach (WikiV2 wiki in wikis)
            {
                Context.Log("Retrieved wiki with name '{0}' in project '{1}'", wiki.Name, wiki.ProjectId);
            }

            return(wikis);
        }
Esempio n. 13
0
        public WikiPageResponse GetWikiPageMetadata()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki         = Helpers.FindOrCreateProjectWiki(this.Context);
            string somePagePath = Helpers.GetAnyWikiPagePath(this.Context, wiki);

            WikiPageResponse somePageResponse = wikiClient.GetPageAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name,
                path: somePagePath).SyncResult();

            Context.Log("Retrieved page '{0}' metadata in wiki '{1}'", somePagePath, wiki.Name);

            return(somePageResponse);
        }
Esempio n. 14
0
        public WikiPageResponse DeleteWikiPageById()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki       = Helpers.FindOrCreateProjectWiki(this.Context);
            int    somePageId = Helpers.GetAnyWikiPageId(this.Context, wiki);

            WikiPageResponse somePageResponse = wikiClient.DeletePageByIdAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                id: somePageId).SyncResult();

            Context.Log("Deleted page with Id : '{0}' from wiki '{1}'", somePageId, wiki.Name);

            return(somePageResponse);
        }
Esempio n. 15
0
        public WikiPageResponse GetWikiPageByIdMetadataWithContent()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki       = Helpers.FindOrCreateProjectWiki(this.Context);
            int    somePageId = Helpers.GetAnyWikiPageId(this.Context, wiki);

            WikiPageResponse WikiPageResponse = wikiClient.GetPageByIdAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                id: somePageId,
                includeContent: true).SyncResult();

            Context.Log("Retrieved page with id : '{0}' metadata in wiki '{1}' with content '{2}'", WikiPageResponse.Page.Path, wiki.Name, WikiPageResponse.Page.Content);

            return(WikiPageResponse);
        }
Esempio n. 16
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. 17
0
        public string GetWikiPageText()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki         = Helpers.FindOrCreateProjectWiki(this.Context);
            string somePagePath = Helpers.GetAnyWikiPagePath(this.Context, wiki);

            using (var reader = new StreamReader(wikiClient.GetPageTextAsync(
                                                     project: wiki.ProjectId,
                                                     wikiIdentifier: wiki.Id,
                                                     path: somePagePath).SyncResult()))
            {
                string pageContent = reader.ReadToEnd();
                Context.Log("Retrieved page '{0}' in wiki '{1}' with content '{2}'", somePagePath, wiki.Name, pageContent);

                return(pageContent);
            }
        }
Esempio n. 18
0
        public WikiPageResponse GetWikiPageAndSubPages()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2           wiki             = Helpers.FindOrCreateProjectWiki(this.Context);
            WikiPageResponse rootPageResponse = wikiClient.GetPageAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name,
                path: "/",
                recursionLevel: VersionControlRecursionType.OneLevel).SyncResult();

            Context.Log("Retrieved the following subpages for the root page:");
            foreach (WikiPage subPage in rootPageResponse.Page.SubPages)
            {
                Context.Log("Sub-page : '{0}'", subPage.Path);
            }

            return(rootPageResponse);
        }
Esempio n. 19
0
        public WikiPageResponse EditWikiPage()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki         = Helpers.FindOrCreateProjectWiki(this.Context);
            string somePagePath = Helpers.GetAnyWikiPagePath(this.Context, wiki);

            WikiPageResponse pageResponse = wikiClient.GetPageAsync(
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name,
                path: somePagePath,
                includeContent: true).SyncResult();

            WikiPage somePage = pageResponse.Page;

            Context.Log("Retrieved page '{0}' as JSON in wiki '{1}' with content '{2}'", somePage.Path, wiki.Name, somePage.Content);

            var originalContent = somePage.Content;
            var originalVersion = pageResponse.ETag.ToList()[0];

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

            WikiPageResponse editedPageResponse = wikiClient.CreateOrUpdatePageAsync(
                parameters: parameters,
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Name,
                path: somePagePath,
                Version: originalVersion).SyncResult();

            var updatedContent = editedPageResponse.Page.Content;
            var updatedVersion = editedPageResponse.ETag.ToList()[0];

            Context.Log("Before editing --> Page path: {0}, version: {1}, content: {2}", somePage.Path, originalVersion, originalContent);
            Context.Log("After editing --> Page path: {0}, version: {1}, content: {2}", somePage.Path, updatedVersion, updatedContent);

            return(editedPageResponse);
        }
        public WikiAttachmentResponse AddAttachment()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki             = Helpers.FindOrCreateProjectWiki(this.Context);
            Stream attachmentStream = File.OpenRead(
                Path.Combine(
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                    @"Content\Logo.png"));

            WikiAttachmentResponse attachmentResponse = wikiClient.CreateAttachmentAsync(
                uploadStream: attachmentStream.ConvertToBase64(),
                project: wiki.ProjectId,
                wikiIdentifier: wiki.Id,
                name: "Attachment" + new Random().Next(0, 999) + ".png").SyncResult();

            Context.Log("Attachment '{0}' added to wiki '{1}'", attachmentResponse.Attachment.Name, wiki.Name);

            return(attachmentResponse);
        }
Esempio n. 21
0
        public WikiPageResponse CreateWikiPage()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            WikiV2 wiki = Helpers.FindOrCreateProjectWiki(this.Context);
            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);

            return(wikiPageResponse);
        }
Esempio n. 22
0
        public async Task Run(string input, ILogger log)
#endif
        {
            string sb = await GenerateReleaseNotesMarkdown();

            string iterationName = await GetCurrentIterationName();

            using (WikiHttpClient wikiHttpClient = new WikiHttpClient(_uri, credentials))
            {
                try
                {
                    WikiPageResponse wikiPage = await wikiHttpClient.GetPageAsync(_project, _wikiName, $"/{iterationName}");

                    await wikiHttpClient.CreateOrUpdatePageAsync(new WikiPageCreateOrUpdateParameters()
                    {
                        Content = sb.ToString()
                    }, _project, _wikiName, $"/{iterationName}", wikiPage.ETag.First().ToString(), $"Updated on release notes on {DateTime.UtcNow}(UTC)");
                }
                catch (VssServiceException ex)
                {
                    /*
                     * What an ugly pattern to code with!
                     * Had to do this since the GetPageAsync call throws exception if page is not found.
                     * Hopefully, I fix this in the future.
                     */
                    log.LogInformation("Service exception raised. Assumed that the wiki page does not exist. Trying to create one.");
                    await wikiHttpClient.CreateOrUpdatePageAsync(new WikiPageCreateOrUpdateParameters()
                    {
                        Content = sb.ToString()
                    }, _project, _wikiName, $"/{iterationName}", null, $"Added release notes for {iterationName}");
                }
                catch
                {
                    throw;
                }
            }
            log.LogInformation("Completed execution. Please check your wiki for your release notes.");
        }
Esempio n. 23
0
        public WikiV2 CreateProjectWikiIfNotExisting()
        {
            VssConnection  connection = this.Context.Connection;
            WikiHttpClient wikiClient = connection.GetClient <WikiHttpClient>();

            Guid          projectId = ClientSampleHelpers.FindAnyProject(this.Context).Id;
            List <WikiV2> wikis     = wikiClient.GetAllWikisAsync(projectId).SyncResult();

            WikiV2 createdWiki           = null;
            var    isProjectWikiExisting = false;

            if (wikis != null && wikis.Count > 0)
            {
                isProjectWikiExisting = wikis.Where(wiki => wiki.Type.Equals(WikiType.ProjectWiki)).Any();
            }

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

                createdWiki = wikiClient.CreateWikiAsync(createParameters).SyncResult();

                Context.Log("Created wiki with name '{0}' in project '{1}'", createdWiki.Name, createdWiki.ProjectId);
            }
            else
            {
                Context.Log("Project wiki already exists for this project.");
            }

            return(createdWiki);
        }