Skip to content

Commit

Permalink
Create GH issues from comments (#1023)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored and benbp committed Dec 1, 2020
1 parent 4b100e7 commit 3792186
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/dotnet/APIView/APIViewWeb/Client/css/site.scss
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ form.comment {
float: right;
margin-left: 10px;
margin-right: -5px;
max-height: 50px;
}

.comment-row {
Expand Down
48 changes: 45 additions & 3 deletions src/dotnet/APIView/APIViewWeb/Client/src/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,44 @@
e.preventDefault();
});

$(document).on("click", ".js-github", e => {
let target = $(e.target);
let repo = target.attr("data-repo");
let language = getLanguage(e.target);

// Fall back to the repo-language if we don't know the review language,
// e.g.Go doesn't specify Language yet.
if (!language) {
language = target.attr("data-repo-language");
}
let commentElement = getCommentElement(getCommentId(e.target)!);
let comment = commentElement.find(".js-comment-raw").html();
let codeLine = commentElement.closest(".comment-row").prev(".code-line").find(".code");
let apiViewUrl = "";

// if creating issue from the convos tab, the link to the code element is in the DOM already.
let commentUrlElem = codeLine.find(".comment-url");
if (commentUrlElem.length) {
apiViewUrl = location.protocol + '//' + location.host + escape(commentUrlElem.attr("href")!);
}
else {
// otherwise we construct the link from the current URL and the element ID
// Double escape the element - this is used as the URL back to API View and GitHub will render one layer of the encoding.
apiViewUrl = window.location.href.split("#")[0] + "%23" + escape(escape(getElementId(commentElement[0])!));
}

let issueBody = escape("```" + language + "\n" + codeLine.text().trim() + "\n```\n#\n" + comment);
// TODO uncomment below once the feature to support public ApiView Reviews is enabled.
//+ "\n#\n")
//+ "[Created from ApiView comment](" + apiViewUrl + ")";

window.open(
"https://github.com/Azure/" + repo + "/issues/new?" +
"&body=" + issueBody,
'_blank');
e.preventDefault();
});

$(document).on("keydown", ".new-thread-comment-text", e => {
if (e.ctrlKey && (e.keyCode === 10 || e.keyCode === 13)) {
const form = $(e.target).closest("form");
Expand All @@ -101,26 +139,30 @@
});

addEventListener("hashchange", e => {
highlightCurrentRow();
highlightCurrentRow();
});

addEventListener("load", e => {
highlightCurrentRow();
highlightCurrentRow();
});

function highlightCurrentRow() {
if (location.hash.length < 1) return;
var row = getCodeRow(location.hash.substring(1));
row.addClass("active");
row.on("animationend", () => {
row.removeClass("active");
row.removeClass("active");
});
}

function getReviewId(element: HTMLElement) {
return getParentData(element, "data-review-id");
}

function getLanguage(element: HTMLElement) {
return getParentData(element, "data-language");
}

function getRevisionId(element: HTMLElement) {
return getParentData(element, "data-revision-id");
}
Expand Down
3 changes: 3 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Models/ReviewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ public string DisplayName

[JsonIgnore]
public DateTime LastUpdated => Revisions.LastOrDefault()?.CreationDate ?? CreationDate;

[JsonIgnore]
public string Language => Revisions.FirstOrDefault()?.Files.FirstOrDefault()?.Language;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@foreach (var thread in revision.Value)
{
var elementId = thread.LineId;
<tr class="code-line"><td class="code p-2"><a asp-page="Review"
<tr class="code-line"><td class="code p-2"><a class="comment-url" asp-page="Review"
asp-route-id=@Model.Review.ReviewId
asp-route-revisionId=@revision.Key.RevisionId
asp-fragment=@Uri.EscapeUriString(elementId)>@elementId</a></td></tr>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
</div>
</div>
</div>
<div class="row" data-review-id="@Model.Review.ReviewId" data-revision-id="@Model.Revision.RevisionId">
<div class="row" data-review-id="@Model.Review.ReviewId" data-revision-id="@Model.Revision.RevisionId" data-language="@Model.Review.Language">
<div id="review-left" class="col-lg-2 col">
<div class="namespace-view">
@if (Model.CodeFile != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,54 @@
</button>
</form>

@if (comment.Username == User.GetGitHubLogin())
{
<div class="dropdown show">
<a class="btn btn-sm" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
...
</a>
<div class="dropdown show">
<a class="btn btn-sm" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
...
</a>

<div class="dropdown-menu">
<div class="dropdown-menu">
@if (comment.Username == User.GetGitHubLogin())
{
<form data-post-update="comments" asp-controller="Comments" asp-action="Delete" method="post" asp-route-reviewId="@Model.ReviewId">
<input type="hidden" name="commentId" value="@comment.CommentId"/>
<input type="hidden" name="commentId" value="@comment.CommentId" />
<button id="@comment.CommentId" type="submit" class="text-danger dropdown-item">Delete</button>
</form>
<a class="dropdown-item js-edit-comment">Edit</a>
<a href="#" class="dropdown-item js-edit-comment">Edit</a>
}
<button type="button" class="btn dropdown-toggle dropdown-item" data-toggle="dropdown" aria-haspopup="true"
aria-expanded="false">
Create GitHub issue
</button>
<div class="dropdown-menu">
<table>
<tr>
<td>
<a href="#" class="btn btn-link js-github" data-repo="azure-sdk-for-net" data-repo-language="c#">.NET</a>
</td>
<td>
<a href="#" class="btn btn-link js-github" data-repo="azure-sdk-for-java" data-repo-language="java">Java</a>
</td>
<td>
<a href="#" class="btn btn-link js-github" data-repo="azure-sdk-for-python" data-repo-language="python">Python</a>
</td>
</tr>
<tr>
<td>
<a href="#" class="btn btn-link js-github" data-repo="azure-sdk-for-c" data-repo-language="c">C</a>
</td>
<td>
<a href="#" class="btn btn-link js-github" data-repo="azure-sdk-for-js" data-repo-language="javascript">JS</a>
</td>
<td>
<a href="#" class="btn btn-link js-github" data-repo="azure-sdk-for-go" data-repo-language="go">Go</a>
</td>
</tr>
</table>
</div>
</div>
}
</div>
</div>

<div class="media">
<a username="@comment.Username">
<img username="@comment.Username" class="comment-icon align-self-start mr-3" height="28" width="28" />
Expand Down Expand Up @@ -81,7 +112,7 @@
}
else
{
<form data-post-update="comments" method="post" asp-controller="Comments" asp-action="Resolve" asp-route-reviewId="@Model.ReviewId">
<form data-post-update="comments" method="post" asp-controller="Comments" asp-action="Resolve" asp-route-reviewId="@Model.ReviewId">
<button type="submit" name="submit" value="Submit" class="btn btn-outline-secondary">Resolve</button>
</form>
}
Expand Down

0 comments on commit 3792186

Please sign in to comment.