Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update GitHubAPI.getPullRequestComments #1468

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/platforms/_tests/fixtures/bbc-dsl-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -2542,7 +2542,8 @@
"settings": {
"github": {
"accessToken": "12345",
"additionalHeaders": {}
"additionalHeaders": {},
"baseURL": "https://api.github.com"
},
"cliArgs": {}
}
Expand Down
3 changes: 2 additions & 1 deletion source/platforms/_tests/fixtures/bbs-dsl-input.json
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,8 @@
"settings": {
"github": {
"accessToken": "12345",
"additionalHeaders": {}
"additionalHeaders": {},
"baseURL": "https://api.github.com"
},
"cliArgs": {}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"id": 1,
"node_id": "MDEyOklzc3VlQ29tbWVudDE=",
"url": "https://api.github.com/repos/octocat/Hello-World/issues/comments/1",
"html_url": "https://github.com/octocat/Hello-World/issues/1347#issuecomment-1",
"body": "Me too",
"user": {
"login": "octocat",
"id": 1,
"node_id": "MDQ6VXNlcjE=",
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/users/octocat",
"html_url": "https://github.com/octocat",
"followers_url": "https://api.github.com/users/octocat/followers",
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
"organizations_url": "https://api.github.com/users/octocat/orgs",
"repos_url": "https://api.github.com/users/octocat/repos",
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/octocat/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2011-04-14T16:00:49Z",
"updated_at": "2011-04-14T16:00:49Z",
"issue_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
"author_association": "COLLABORATOR"
}
]
18 changes: 16 additions & 2 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,24 @@ export class GitHubAPI {
return response.json()
}

/**
* Fetches all comments from a Pull Request on GitHub.
*
* This function retrieves all the comments associated with a given pull request.
* It makes a request to the GitHub API endpoint that returns all comments for the
* specified pull request.
*
* https://docs.github.com/en/rest/issues/comments?apiVersion=2022-11-28#list-issue-comments
*
* @returns {Promise<GitHubIssueComment[]>} A promise that resolves to an array of GitHub issue comments.
*
*/
getPullRequestComments = async (): Promise<GitHubIssueComment[]> => {
const pr = await this.getPullRequestInfo()
const prNumber = pr.number
const repo = this.repoMetadata.repoSlug
const prID = this.repoMetadata.pullRequestID
return await this.getAllOfResource(`repos/${repo}/issues/${prID}/comments`)
const owner = pr.base.repo.owner.login
return await this.getAllOfResource(`repos/${owner}/${repo}/issues/${prNumber}/comments`)
}

getPullRequestInlineComments = async (
Expand Down
13 changes: 12 additions & 1 deletion source/platforms/github/_tests/_github_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,24 @@ new file mode 0

it("getDangerCommentIDs ignores comments not marked as generated", async () => {
api.getAllOfResource = await requestWithFixturedJSON("github_inline_comments_with_danger.json")
api.getUserID = () => new Promise<number>((r) => r(20229914))
api.fetch = jest.fn().mockReturnValue({ ok: true })
Copy link
Author

@kfbustam kfbustam Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was failing because it was making an API call (i.e., not mocked) which isn't allowed so I just included the fix in this PR 😅

api.getPullRequestInfo = await requestWithFixturedJSON("github_pr.json")

const commentIDs = await api.getDangerCommentIDs("default")

expect(commentIDs.length).toEqual(0)
})

it("getPullRequestComment gets only comments for given Pull Request", async () => {
api.getAllOfResource = await requestWithFixturedJSON("github_pr_comments_with_danger.json")
api.fetch = jest.fn().mockReturnValue({ ok: true })
api.getPullRequestInfo = await requestWithFixturedJSON("github_pr.json")

const comments = await api.getPullRequestComments()

expect(comments.length).toEqual(1)
})

it("getPullRequestInlineComment gets only comments for given dangerID", async () => {
api.getAllOfResource = await requestWithFixturedJSON("github_inline_comments_with_danger.json")

Expand Down