Exemple #1
0
        public void GetCommitInfoTest()
        {
            var filename         = Guid.NewGuid() + ".filelisttest";
            var expectedContents = "Hello, World!";

            // Create file
            _vstsHelper.SaveTextFile(_authentication, _projectName, _repoName, _branchName, filename, expectedContents,
                                     "Creating integration test file. ***NO_CI***", true);

            // Get list of files
            var fileList = _vstsHelper.GetFileList(_authentication, _projectName, _repoName, _branchName, "/", ".filelisttest", true);

            if (fileList != null && fileList.Count > 0)
            {
                // Get Commit Info
                var commitInfo = _vstsHelper.GetCommitInfo(_authentication, _projectName, _repoName, _branchName, fileList[0].CommitId);

                Assert.AreEqual(fileList[0].CommitId, commitInfo.commitId);
            }

            // Delete test file
            _vstsHelper.DeleteFile(_authentication, _projectName, _repoName, _branchName, filename, "Deleting integration test file. ***NO_CI***");
        }
Exemple #2
0
        public void PublishMarkdownFiles()
        {
            var publishOccurred = false;

            LoadMetadata();
            LoadMarkdownFileList();
            ClearMetadataReconcileFlag(); // So we can track "orphaned" entries (i.e. a .md file has been deleted)

            foreach (var markdownFile in markdownFileList)
            {
                // Check Commit ID to see if it's different from what was stored. If not, then nothing has
                // changed so no need to publish (again)
                var documentMetadata      = GetDocumentMetadata(markdownFile.Path);
                var commitId              = GetMarkdownFileCommitId(markdownFile.Path);
                var markdownFileSourceUrl = $"{vstsAuthentication.AccountUrl}/{project}/_git/{repoName}?path={markdownFile.Path}&version=GB{branch}&editMode=true";

                if (documentMetadata == null)
                {
                    Logger.LogMessage($"{markdownFile.Path} is new... creating initial metadata entry");

                    // There is no metedata defined for the markdown file being processed so
                    // let's create a default entry (that will be published as a Draft)
                    documentMetadata = new DocumentMetadata(markdownFile.Path);
                    metadata.Add(documentMetadata);
                }

                // Track as having a matching .md file
                documentMetadata.IsReconciled = true;

                // Is the file new or has it been updated and/or renamed?
                if ((documentMetadata.LastObjectId == null) ||
                    (!documentMetadata.LastObjectId.Equals(commitId, StringComparison.InvariantCultureIgnoreCase)) ||
                    (!documentMetadata.DocumentFilename.Equals(markdownFile.Path, StringComparison.InvariantCultureIgnoreCase)))
                {
                    Logger.LogMessage($"{documentMetadata.DocumentFilename} is new or has been updated so continue with publish");

                    var filenameHelper = new FilenameHelper();

                    // The Commit IDs are different so the file is either new or it has been updated so let's publish!
                    var fileContents = vstsHelper.GetFileContents(vstsAuthentication, project, repoName, branch, markdownFile.Path);
                    var commitInfo   = vstsHelper.GetCommitInfo(vstsAuthentication, project, repoName, branch, markdownFile.CommitId);

                    if (commitInfo != null)
                    {
                        // Track author information
                        documentMetadata.AuthorName  = commitInfo.author.name;
                        documentMetadata.AuthorEmail = commitInfo.author.email;
                        documentMetadata.AuthorDate  = commitInfo.author.date;
                    }

                    if (fileContents == null)
                    {
                        Logger.LogError($"##ERROR: The contents of the file '{documentMetadata.DocumentFilename}' could not be loaded.");

                        throw new InvalidOperationException($"##ERROR: The contents of the file '{documentMetadata.DocumentFilename}' could not be loaded.");
                    }

                    fileContents = ConvertMarkdownToHtml(fileContents);

                    // Add an "Edit this page" link if it is requested
                    if (addEditLink)
                    {
                        fileContents += $"<br/><br/><a style=\"{editLinkStyle}\" href=\"{markdownFileSourceUrl}\">{editLinkText}</a>";
                    }

                    // Convert image links to embedded images
                    fileContents = ConvertHtmlToEmbeddedImages(fileContents);

                    // Set the post title because it's possible the file has been renamed
                    documentMetadata.PostTitle = Path.GetFileNameWithoutExtension(filenameHelper.GetFilenameWithoutId(markdownFile.Path));

                    if (useFolderNameAsCategory)
                    {
                        var folderPath = Path.GetDirectoryName(documentMetadata.DocumentFilename);

                        // If the path is empty or contains only a leading backslash '\' then we'll skip this step (i.e. doesn't apply to root folders)
                        if (folderPath.Length > 1)
                        {
                            var folderName = folderPath.Substring(folderPath.LastIndexOf('\\') + 1);

                            if (documentMetadata.Categories.IndexOf(folderName) < 0)
                            {
                                Logger.LogMessage($"Adding Category '{folderName}' to {documentMetadata.DocumentFilename}");

                                documentMetadata.Categories.Add(folderName);
                            }
                        }
                    }

                    if (useFolderNameAsTag)
                    {
                        var folderPath = Path.GetDirectoryName(documentMetadata.DocumentFilename);

                        // If the path is empty or contains only a leading backslash '\' then we'll skip this step (i.e. doesn't apply to root folders)
                        if (folderPath.Length > 1)
                        {
                            var folderName = folderPath.Substring(folderPath.LastIndexOf('\\') + 1);

                            if (documentMetadata.Tags.IndexOf(folderName) < 0)
                            {
                                Logger.LogMessage($"Adding Tag '{folderName}' to {documentMetadata.DocumentFilename}");

                                documentMetadata.Tags.Add(folderName);
                            }
                        }
                    }

                    // Create the actual post/page
                    var postStatus = wpHelper.CreatePost(
                        documentMetadata.PostTitle,
                        fileContents,
                        documentMetadata.DocumentId,
                        documentMetadata.DocumentType,
                        documentMetadata.Categories,
                        documentMetadata.Tags,
                        !documentMetadata.IsPublic,
                        publishNewPostsAsDraft,
                        documentMetadata.AuthorName,
                        documentMetadata.AuthorEmail,
                        publishAsCommitter);

                    documentMetadata.DocumentId   = postStatus.Id;
                    documentMetadata.IsPublic     = !postStatus.IsDraft; // Track draft/public
                    documentMetadata.LastObjectId = commitId;

                    publishOccurred = true;

                    if (trackPostIdInFilename)
                    {
                        documentMetadata.DocumentFilename =
                            UpdateFilenameWithPostId(markdownFile.Path, documentMetadata.DocumentId);
                    }
                }
                else
                {
                    Logger.LogMessage($"The markdown file '{markdownFileSourceUrl}' has not been updated since it was last published. Skipping this file.");
                    //Logger.LogMessage($"The markdown file '{markdownFile.Path}' has not been updated since it was last published. Skipping this file.");
                }
            }

            // No point saving anything if nothing was changed/published
            if (publishOccurred || !metadata.TrueForAll(x => x.IsReconciled))
            {
                RemoveOrphanedMetadataEntries();
                SaveMetadata(metadataFilename);
            }
        }