private GitHubCommentModel CreateCommentModel(int version, GitHubComment comment) { // create URL for the commit from the comment URL string commentUrl = comment.html_url.AbsoluteUri; int hashIndex = commentUrl.IndexOf('#'); string commitUrl = hashIndex == -1 ? commentUrl : commentUrl.Substring(0, hashIndex); // create basic model GitHubCommentModel model = new GitHubCommentModel { CommentUrl = commentUrl, CommitUrl = commitUrl, CommentBody = new HtmlString(comment.body_html), CommitId = comment.commit_id.Substring(0, 8), FilePath = comment.path, LineNumber = comment.line.GetValueOrDefault() == 0 ? null : comment.line, Commenter = comment.user.login, }; // add extra details if present if (version == 2) { GitHubCommit commit = m_commits[comment.commit_id]; string author = commit.author != null ? commit.author.login : "******"; model.Author = author; model.CommitMessage = commit.commit.message; model.CommitFiles = commit.files.Select(f => f.filename).OrderBy(f => f, StringComparer.OrdinalIgnoreCase).ToList(); } return(model); }
// Creates the HTML for a feed item for an individual comment. private string CreateCommentHtml(int version, GitHubCommentModel model) { using (StringWriter writer = new StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, version == 2 ? "Commit" : "Simple"); ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, new ViewDataDictionary(model), new TempDataDictionary(), writer); viewResult.View.Render(viewContext, writer); return(writer.ToString()); } }
private static string RenderCommitForSubject(GitHubCommentModel model) { string message = Regex.Replace(model.CommitMessage, @"\s+", " ").Trim(); const int maxLength = 100; if (message.Length > maxLength) { message = message.Substring(0, maxLength) + "\u2026"; } return(message); }
private SyndicationItem CreateCommentItem(ListParameters p, GitHubComment comment) { GitHubCommentModel model = CreateCommentModel(p.Version, comment); string title = p.Version == 1 ? "{0} commented on {1}/{2}".FormatWith(model.Commenter, p.User, p.Repo) : "{0}’s commit: {1}".FormatWith(model.Author, RenderCommitForSubject(model)); return(new SyndicationItem { Authors = { new SyndicationPerson(null, comment.user.login, null) }, Content = new TextSyndicationContent(CreateCommentHtml(p.Version, model), TextSyndicationContentKind.Html), Id = comment.url.AbsoluteUri, LastUpdatedTime = comment.updated_at, Links = { SyndicationLink.CreateAlternateLink(comment.html_url), new SyndicationLink(new Uri(model.CommitUrl)) { RelationshipType = "related", Title= "CommitUrl" } }, PublishDate = comment.created_at, Title = new TextSyndicationContent(HttpUtility.HtmlEncode(title), TextSyndicationContentKind.Html), }); }