generated from ubiquity/ts-template
-
Notifications
You must be signed in to change notification settings - Fork 29
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 #98 from gentlementlegen/feat/graphql-pr-fetch
feat: graphql pr fetch
- Loading branch information
Showing
20 changed files
with
1,394 additions
and
761 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
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 |
---|---|---|
|
@@ -10,3 +10,4 @@ static/dist | |
.env | ||
junit.xml | ||
coverage | ||
test-dashboard.md |
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,6 @@ | ||
schema: | ||
- https://api.github.com/graphql: | ||
headers: | ||
Authorization: Bearer ${GITHUB_TOKEN} | ||
documents: src/* | ||
projects: {} |
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 |
---|---|---|
@@ -1,77 +1,34 @@ | ||
import { GitHubLinkEvent, isGitHubLinkEvent } from "../github-types"; | ||
import { IssueParams, getAllTimelineEvents, parseGitHubUrl } from "../start"; | ||
import { PullRequest, Repository, User } from "@octokit/graphql-schema"; | ||
import { getOctokitInstance } from "../octokit"; | ||
import { IssueParams } from "../start"; | ||
import { LINKED_PULL_REQUESTS } from "../types/requests"; | ||
|
||
export async function collectLinkedMergedPulls(issue: IssueParams) { | ||
// normally we should only use this one to calculate incentives, because this specifies that the pull requests are merged (accepted) | ||
// and that are also related to the current issue, no just mentioned by | ||
const onlyPullRequests = await collectLinkedPulls(issue); | ||
return onlyPullRequests.filter((event) => { | ||
if (!event.source.issue.body) { | ||
return false; | ||
} | ||
// Matches all keywords according to the docs: | ||
// https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword | ||
// Works on multiple linked issues, and matches #<number> or URL patterns | ||
const linkedIssueRegex = | ||
/\b(?:Close(?:s|d)?|Fix(?:es|ed)?|Resolve(?:s|d)?):?\s+(?:#(\d+)|https?:\/\/(?:www\.)?github\.com\/(?:[^/\s]+\/[^/\s]+\/(?:issues|pull)\/(\d+)))\b/gi; | ||
// We remove the comments as they should not be part of the linked pull requests | ||
const linkedPrUrls = event.source.issue.body.replace(/<!--[\s\S]+-->/, "").match(linkedIssueRegex); | ||
if (!linkedPrUrls) { | ||
return false; | ||
} | ||
let isClosingPr = false; | ||
for (const linkedPrUrl of linkedPrUrls) { | ||
const idx = linkedPrUrl.indexOf("#"); | ||
if (idx !== -1) { | ||
isClosingPr = Number(linkedPrUrl.slice(idx + 1)) === issue.issue_number; | ||
} else { | ||
const url = linkedPrUrl.match(/https.+/)?.[0]; | ||
if (url) { | ||
const linkedRepo = parseGitHubUrl(url); | ||
isClosingPr = | ||
linkedRepo.issue_number === issue.issue_number && | ||
linkedRepo.repo === issue.repo && | ||
linkedRepo.owner === issue.owner; | ||
} | ||
} | ||
if (isClosingPr) break; | ||
} | ||
return isGitHubLinkEvent(event) && event.source.issue.pull_request?.merged_at && isClosingPr; | ||
}); | ||
} | ||
export async function collectLinkedPulls(issue: IssueParams) { | ||
// this one was created to help with tests, but probably should not be used in the main code | ||
const issueLinkEvents = await getLinkedEvents(issue); | ||
const onlyConnected = eliminateDisconnects(issueLinkEvents); | ||
return onlyConnected.filter((event) => isGitHubLinkEvent(event) && event.source.issue.pull_request); | ||
} | ||
type ClosedByPullRequestsReferences = { | ||
node: Pick<PullRequest, "url" | "title" | "number" | "state" | "body"> & { | ||
author: Pick<User, "login" | "id">; | ||
repository: Pick<Repository, "owner" | "name">; | ||
}; | ||
}; | ||
|
||
function eliminateDisconnects(issueLinkEvents: GitHubLinkEvent[]) { | ||
// Track connections and disconnections | ||
const connections = new Map<number, GitHubLinkEvent>(); // Use issue/pr number as key for easy access | ||
const disconnections = new Map<number, GitHubLinkEvent>(); // Track disconnections | ||
type IssueWithClosedByPRs = { | ||
repository: { | ||
issue: { | ||
closedByPullRequestsReferences: { | ||
edges: ClosedByPullRequestsReferences[]; | ||
}; | ||
}; | ||
}; | ||
}; | ||
|
||
issueLinkEvents.forEach((issueEvent: GitHubLinkEvent) => { | ||
const issueNumber = issueEvent.source.issue.number as number; | ||
export async function collectLinkedMergedPull(issue: IssueParams) { | ||
const octokit = getOctokitInstance(); | ||
const { owner, repo, issue_number } = issue; | ||
|
||
if (issueEvent.event === "connected" || issueEvent.event === "cross-referenced") { | ||
// Only add to connections if there is no corresponding disconnected event | ||
if (!disconnections.has(issueNumber)) { | ||
connections.set(issueNumber, issueEvent); | ||
} | ||
} else if (issueEvent.event === "disconnected") { | ||
disconnections.set(issueNumber, issueEvent); | ||
// If a disconnected event is found, remove the corresponding connected event | ||
if (connections.has(issueNumber)) { | ||
connections.delete(issueNumber); | ||
} | ||
} | ||
const result = await octokit.graphql.paginate<IssueWithClosedByPRs>(LINKED_PULL_REQUESTS, { | ||
owner, | ||
repo, | ||
issue_number, | ||
}); | ||
|
||
return Array.from(connections.values()); | ||
} | ||
|
||
async function getLinkedEvents(params: IssueParams): Promise<GitHubLinkEvent[]> { | ||
const issueEvents = await getAllTimelineEvents(params); | ||
return issueEvents.filter(isGitHubLinkEvent); | ||
return result.repository.issue.closedByPullRequestsReferences.edges.map((edge) => edge.node).slice(-1); | ||
} |
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
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,35 @@ | ||
export const LINKED_PULL_REQUESTS = /* GraphQL */ ` | ||
query collectLinkedPullRequests($owner: String!, $repo: String!, $issue_number: Int!, $cursor: String) { | ||
repository(owner: $owner, name: $repo) { | ||
issue(number: $issue_number) { | ||
id | ||
closedByPullRequestsReferences(first: 10, includeClosedPrs: false, after: $cursor) { | ||
edges { | ||
node { | ||
id | ||
title | ||
number | ||
url | ||
author { | ||
login | ||
... on User { | ||
id: databaseId | ||
} | ||
} | ||
repository { | ||
owner { | ||
login | ||
} | ||
name | ||
} | ||
} | ||
} | ||
pageInfo { | ||
hasNextPage | ||
endCursor | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`; |
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,19 @@ | ||
module.exports = { | ||
paginateGraphQL() { | ||
return { | ||
graphql: { | ||
paginate(query, args) { | ||
return { | ||
repository: { | ||
issue: { | ||
closedByPullRequestsReferences: { | ||
edges: [], | ||
}, | ||
}, | ||
}, | ||
}; | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; |
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
Oops, something went wrong.