generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from gentlementlegen/fix/gql
fix: pull-request fetch for private user profiles
- Loading branch information
Showing
8 changed files
with
297 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ junit.xml | |
cypress/screenshots | ||
script.ts | ||
.wrangler | ||
test-dashboard.md | ||
test-dashboard.md | ||
*.env.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { RestEndpointMethodTypes } from "@octokit/rest"; | ||
import type { Endpoints } from "@octokit/types"; | ||
import { Context } from "../types"; | ||
|
||
function isHttpError(error: unknown): error is { status: number; message: string } { | ||
return typeof error === "object" && error !== null && "status" in error && "message" in error; | ||
} | ||
|
||
/** | ||
* Fetches all open pull requests within a specified organization created by a particular user. | ||
* This method is slower than using a search query, but should work even if the user has his activity set to private. | ||
*/ | ||
export async function getAllPullRequestsFallback( | ||
context: Context, | ||
state: Endpoints["GET /repos/{owner}/{repo}/pulls"]["parameters"]["state"], | ||
username: string | ||
) { | ||
const { octokit, logger } = context; | ||
const organization = context.payload.repository.owner.login; | ||
|
||
try { | ||
const repositories = await octokit.paginate(octokit.rest.repos.listForOrg, { | ||
org: organization, | ||
per_page: 100, | ||
type: "all", | ||
}); | ||
|
||
const allPrs: RestEndpointMethodTypes["pulls"]["list"]["response"]["data"] = []; | ||
|
||
const tasks = repositories.map(async (repo) => { | ||
try { | ||
const prs = await octokit.paginate(octokit.rest.pulls.list, { | ||
owner: organization, | ||
repo: repo.name, | ||
state, | ||
per_page: 100, | ||
}); | ||
const userPrs = prs.filter((pr) => pr.user?.login === username); | ||
allPrs.push(...userPrs); | ||
} catch (error) { | ||
if (isHttpError(error) && (error.status === 404 || error.status === 403)) { | ||
logger.error(`Could not find pull requests for repository ${repo.url}, skipping: ${error}`); | ||
return; | ||
} | ||
logger.fatal("Failed to fetch pull requests for repository", { error: error as Error }); | ||
throw error; | ||
} | ||
}); | ||
|
||
await Promise.all(tasks); | ||
|
||
return allPrs; | ||
} catch (error) { | ||
logger.fatal("Failed to fetch pull requests for organization", { error: error as Error }); | ||
throw error; | ||
} | ||
} | ||
|
||
export async function getAssignedIssuesFallback(context: Context, username: string) { | ||
const org = context.payload.repository.owner.login; | ||
const assignedIssues = []; | ||
|
||
try { | ||
const repositories = await context.octokit.paginate(context.octokit.rest.repos.listForOrg, { | ||
org, | ||
type: "all", | ||
per_page: 100, | ||
}); | ||
|
||
for (const repo of repositories) { | ||
const issues = await context.octokit.paginate(context.octokit.rest.issues.listForRepo, { | ||
owner: org, | ||
repo: repo.name, | ||
assignee: username, | ||
state: "open", | ||
per_page: 100, | ||
}); | ||
|
||
assignedIssues.push( | ||
...issues.filter( | ||
(issue) => | ||
issue.pull_request === undefined && (issue.assignee?.login === username || issue.assignees?.some((assignee) => assignee.login === username)) | ||
) | ||
); | ||
} | ||
|
||
return assignedIssues; | ||
} catch (err: unknown) { | ||
throw new Error(context.logger.error("Fetching assigned issues failed!", { error: err as Error }).logMessage.raw); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { RestEndpointMethodTypes } from "@octokit/rest"; | ||
import { Logs } from "@ubiquity-os/ubiquity-os-logger"; | ||
import { Context } from "../src/types/context"; | ||
import { getAllPullRequestsWithRetry } from "../src/utils/issue"; | ||
|
||
const username = "private-user"; | ||
|
||
const mockPullRequestData = [ | ||
{ id: 1, number: 123, state: "open", user: { login: username } }, | ||
{ id: 2, number: 124, state: "open", user: { login: "public-user" } }, | ||
] as unknown as RestEndpointMethodTypes["pulls"]["list"]["response"]["data"]; | ||
|
||
const mockOctokit = { | ||
paginate: jest.fn().mockResolvedValue(mockPullRequestData), | ||
rest: { | ||
pulls: { | ||
list: jest.fn().mockResolvedValue(mockPullRequestData), | ||
}, | ||
repos: { | ||
listForOrg: jest.fn().mockResolvedValue(mockPullRequestData), | ||
}, | ||
}, | ||
}; | ||
|
||
const context: Context = { | ||
eventName: "pull_request", | ||
payload: { | ||
repository: { | ||
name: "test-repo", | ||
owner: { | ||
login: "test-owner", | ||
}, | ||
}, | ||
}, | ||
octokit: mockOctokit as unknown as Context["octokit"], | ||
logger: new Logs("debug"), | ||
adapters: {}, | ||
} as unknown as Context; | ||
|
||
describe("getAllPullRequestsWithRetry", () => { | ||
it("should return pull requests even if user information is private", async () => { | ||
const pullRequests = await getAllPullRequestsWithRetry(context, "all", username); | ||
expect(pullRequests).toHaveLength(2); | ||
expect(pullRequests[0].user?.login).toBe(username); | ||
expect(pullRequests[1].user?.login).toBe(username); | ||
console.log(pullRequests); | ||
}); | ||
}); |
Oops, something went wrong.