-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option for adding the reviewers of the original PRs (#452)
* add option for adding the reviewers of the original PRs * Fixup PR * Fix spinner * Add test * Fix test --------- Co-authored-by: Søren Louv-Jansen <sorenlouv@gmail.com>
- Loading branch information
Showing
10 changed files
with
140 additions
and
1 deletion.
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
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,61 @@ | ||
import { Octokit } from '@octokit/rest'; | ||
import { flatten, uniq } from 'lodash'; | ||
import { filterNil } from '../../../utils/filterEmpty'; | ||
import { logger } from '../../logger'; | ||
import { ora } from '../../ora'; | ||
|
||
export async function getReviewersFromPullRequests({ | ||
options, | ||
pullNumbers, | ||
}: { | ||
options: { | ||
githubApiBaseUrlV3?: string; | ||
repoName: string; | ||
repoOwner: string; | ||
accessToken: string; | ||
interactive: boolean; | ||
authenticatedUsername: string; | ||
}; | ||
pullNumbers: number[]; | ||
}) { | ||
const { | ||
githubApiBaseUrlV3, | ||
repoName, | ||
repoOwner, | ||
accessToken, | ||
interactive, | ||
authenticatedUsername, | ||
} = options; | ||
|
||
const text = `Retrieving original reviewers`; | ||
const spinner = ora(interactive, text).start(); | ||
|
||
const octokit = new Octokit({ | ||
auth: accessToken, | ||
baseUrl: githubApiBaseUrlV3, | ||
log: logger, | ||
}); | ||
|
||
try { | ||
const promises = pullNumbers.map(async (pullNumber) => { | ||
const reviews = await octokit.pulls.listReviews({ | ||
owner: repoOwner, | ||
repo: repoName, | ||
pull_number: pullNumber, | ||
}); | ||
|
||
return reviews.data | ||
.map((review) => review.user?.login) | ||
.filter((username) => username !== authenticatedUsername) | ||
.filter(filterNil); | ||
}); | ||
|
||
const reviewers = uniq(flatten(await Promise.all(promises))); | ||
spinner.stop(); | ||
return reviewers; | ||
} catch (e) { | ||
// eslint-disable-next-line no-console | ||
console.log(e); | ||
spinner.fail(`Retrieving reviewers failed`); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/lib/github/v3/getReviewersFromPullRequests.ts.private.test.ts
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,36 @@ | ||
import { getDevAccessToken } from '../../../test/private/getDevAccessToken'; | ||
import { getReviewersFromPullRequests } from './getReviewersFromPullRequests'; | ||
|
||
const accessToken = getDevAccessToken(); | ||
|
||
describe('getReviewersFromPullRequests', () => { | ||
it('returns reviewers', async () => { | ||
const reviewers = await getReviewersFromPullRequests({ | ||
options: { | ||
repoOwner: 'backport-org', | ||
repoName: 'commit-author', | ||
accessToken, | ||
authenticatedUsername: 'foobar', | ||
interactive: true, | ||
}, | ||
pullNumbers: [2], | ||
}); | ||
|
||
expect(reviewers).toEqual(['sqren', 'backport-demo-user']); | ||
}); | ||
|
||
it('excludes current user', async () => { | ||
const reviewers = await getReviewersFromPullRequests({ | ||
options: { | ||
repoOwner: 'backport-org', | ||
repoName: 'commit-author', | ||
accessToken, | ||
authenticatedUsername: 'sqren', | ||
interactive: false, | ||
}, | ||
pullNumbers: [2], | ||
}); | ||
|
||
expect(reviewers).toEqual(['backport-demo-user']); | ||
}); | ||
}); |
23 changes: 23 additions & 0 deletions
23
src/lib/github/v3/syncSourcePullRequestReviewersToTargetPullRequest.ts
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,23 @@ | ||
import { ValidConfigOptions } from '../../../options/options'; | ||
import { filterNil } from '../../../utils/filterEmpty'; | ||
import { Commit } from '../../sourceCommit/parseSourceCommit'; | ||
import { addReviewersToPullRequest } from './addReviewersToPullRequest'; | ||
import { getReviewersFromPullRequests } from './getReviewersFromPullRequests'; | ||
|
||
export async function syncSourcePullRequestReviewersToTargetPullRequest( | ||
options: ValidConfigOptions, | ||
commits: Commit[], | ||
pullNumber: number, | ||
) { | ||
const pullNumbers = commits | ||
.map((commit) => commit.sourcePullRequest?.number) | ||
.filter(filterNil); | ||
|
||
const reviewers = await getReviewersFromPullRequests({ | ||
options, | ||
pullNumbers, | ||
}); | ||
if (reviewers) { | ||
await addReviewersToPullRequest(options, pullNumber, reviewers); | ||
} | ||
} |
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
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