-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: --find-matching-prs for commits without PR-URL (#130)
- Loading branch information
Showing
7 changed files
with
102 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { promisify } from 'util' | ||
import ghauth from 'ghauth' | ||
|
||
const authOptions = { | ||
configName: 'changelog-maker', | ||
scopes: ['repo'], | ||
noDeviceFlow: true | ||
} | ||
|
||
export async function auth () { | ||
return await promisify(ghauth)(authOptions) | ||
} |
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,72 @@ | ||
'use strict' | ||
|
||
import { auth } from './auth.js' | ||
import { graphql } from '@octokit/graphql' | ||
import async from 'async' | ||
|
||
// Query to find the first 4 pull requests that include the commit that we're | ||
// concerned about. We'll filter them and take the first one that was MERGED | ||
// as our prUrl. | ||
const query = ` | ||
query ($owner: String!, $name: String!, $commit: GitObjectID!) { | ||
repository(owner: $owner, name: $name) { | ||
object(oid: $commit) { | ||
... on Commit { | ||
associatedPullRequests(first: 4) { | ||
... on PullRequestConnection { | ||
edges { | ||
node { | ||
... on PullRequest { | ||
number | ||
url | ||
title | ||
state | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
export async function findMatchingPrs (ghId, list) { | ||
// only look up commits that don't have a prUrl from metadata | ||
const sublist = list.filter((commit) => typeof commit.prUrl !== 'string') | ||
if (!sublist.length) { | ||
return | ||
} | ||
|
||
const authData = await auth() | ||
const headers = { authorization: `token ${authData.token}` } | ||
const cache = {} | ||
|
||
const q = async.queue(async (commit, next) => { | ||
if (commit.ghUser === 'iojs') { | ||
commit.ghUser = 'nodejs' // forcibly rewrite as the GH API doesn't do it for us | ||
} | ||
|
||
// cache on commit, so we don't run the same commit twice (is this possible?) | ||
cache[commit.sha] = cache[commit.sha] || (async () => { | ||
try { | ||
const res = await graphql(query, { owner: ghId.user, name: ghId.repo, commit: commit.sha, headers }) | ||
if (res.repository?.object?.associatedPullRequests?.edges?.length) { | ||
const pr = res.repository.object.associatedPullRequests.edges.filter((e) => e.node?.state === 'MERGED')[0] | ||
if (pr) { | ||
commit.ghIssue = pr.node.number | ||
commit.prUrl = pr.node.url | ||
} | ||
} | ||
} catch (err) { | ||
console.error(`Error querying GitHub to find pull request for commit: ${err}`) | ||
} | ||
})() | ||
await cache[commit.sha] | ||
next() | ||
}, 15) | ||
|
||
q.push(sublist) | ||
await q.drain() | ||
} |
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