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

fix(reconfigure/pr): find reconfigure pr separately #25954

Merged
merged 27 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d580e54
fix: find reconfigure pr
RahulGautamSingh Nov 24, 2023
cc7779d
Merge branch 'main' into fix-25754
RahulGautamSingh Nov 24, 2023
0da567f
fix formatting
RahulGautamSingh Nov 24, 2023
3e761c0
fix tests
RahulGautamSingh Nov 24, 2023
1016afa
support bitbucket & bitbucket-server
RahulGautamSingh Nov 24, 2023
c949d77
revert change for bitbucket-server
RahulGautamSingh Nov 25, 2023
e97da88
add logic for bitbutcket-server
RahulGautamSingh Nov 27, 2023
4be29f9
Apply Suggestions
RahulGautamSingh Nov 27, 2023
e4453f9
update snap
RahulGautamSingh Nov 30, 2023
a544ad8
update tests
RahulGautamSingh Nov 30, 2023
246b3e2
fix coverage
RahulGautamSingh Nov 30, 2023
78139b4
fix branch coverage
RahulGautamSingh Nov 30, 2023
d11ede5
apply suggestions
RahulGautamSingh Dec 3, 2023
6032954
refactor logs
RahulGautamSingh Dec 3, 2023
0f18e3b
fix tests
RahulGautamSingh Dec 3, 2023
008a9c1
apply changes as per suggestion
RahulGautamSingh Dec 5, 2023
e03a276
fix tests
RahulGautamSingh Dec 5, 2023
cdbc4a6
remove obsolete snap
RahulGautamSingh Dec 5, 2023
7101da5
remove extra open pr filters
RahulGautamSingh Dec 7, 2023
ee0ea50
gitea: remove isReconfigurePr() logic
RahulGautamSingh Dec 7, 2023
976f696
type: captailize Pr -> PR
RahulGautamSingh Dec 14, 2023
84448d4
apply suggestions
RahulGautamSingh Dec 14, 2023
f0eb04b
remove extra log statement
RahulGautamSingh Dec 14, 2023
98fca6b
Apply suggestions from code review
RahulGautamSingh Dec 16, 2023
af0e0f8
Merge branch 'main' into fix-25754
RahulGautamSingh Dec 16, 2023
1dbc888
Apply Suggestions
RahulGautamSingh Dec 27, 2023
a9ff017
inline regex matching
RahulGautamSingh Dec 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ exports[`modules/platform/bitbucket-server/index endpoint with no path addReview

exports[`modules/platform/bitbucket-server/index endpoint with no path deleteLAbel() does not throw 1`] = `undefined`;

exports[`modules/platform/bitbucket-server/index endpoint with no path findPr() finds pr from other authors 1`] = `
{
"bodyStruct": {
"hash": "7980dafc4eb6f0c79278fd929d3e8e5954b32b68ae118a22565c7c369fc2f591",
},
"createdAt": 1547853840016,
"number": 5,
"sourceBranch": "userName1/pullRequest5",
"state": "open",
"targetBranch": "master",
"title": "title",
"version": 1,
}
`;

exports[`modules/platform/bitbucket-server/index endpoint with no path findPr() has pr 1`] = `
{
"bodyStruct": {
Expand Down Expand Up @@ -214,6 +229,21 @@ exports[`modules/platform/bitbucket-server/index endpoint with path addReviewers

exports[`modules/platform/bitbucket-server/index endpoint with path deleteLAbel() does not throw 1`] = `undefined`;

exports[`modules/platform/bitbucket-server/index endpoint with path findPr() finds pr from other authors 1`] = `
{
"bodyStruct": {
"hash": "7980dafc4eb6f0c79278fd929d3e8e5954b32b68ae118a22565c7c369fc2f591",
},
"createdAt": 1547853840016,
"number": 5,
"sourceBranch": "userName1/pullRequest5",
"state": "open",
"targetBranch": "master",
"title": "title",
"version": 1,
}
`;

exports[`modules/platform/bitbucket-server/index endpoint with path findPr() has pr 1`] = `
{
"bodyStruct": {
Expand Down
38 changes: 38 additions & 0 deletions lib/modules/platform/bitbucket-server/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,44 @@ describe('modules/platform/bitbucket-server/index', () => {
}),
).toBeNull();
});

it('finds pr from other authors', async () => {
const scope = await initRepo();
scope
.get(
`${urlPath}/rest/api/1.0/projects/SOME/repos/repo/pull-requests?state=OPEN&direction=outgoing&at=refs/heads/branch&limit=1`,
)
.reply(200, {
isLastPage: true,
values: [prMock(url, 'SOME', 'repo')],
});
expect(
await bitbucket.findPr({
branchName: 'branch',
state: 'open',
includeOtherAuthors: true,
}),
).toMatchSnapshot();
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
});

it('returns null if no pr found - (includeOtherAuthors)', async () => {
const scope = await initRepo();
scope
.get(
`${urlPath}/rest/api/1.0/projects/SOME/repos/repo/pull-requests?state=OPEN&direction=outgoing&at=refs/heads/branch&limit=1`,
)
.reply(200, {
isLastPage: true,
values: [],
});

const pr = await bitbucket.findPr({
branchName: 'branch',
state: 'open',
includeOtherAuthors: true,
});
expect(pr).toBeNull();
});
});

describe('createPr()', () => {
Expand Down
32 changes: 32 additions & 0 deletions lib/modules/platform/bitbucket-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,40 @@ export async function findPr({
prTitle,
state = 'all',
refreshCache,
includeOtherAuthors,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, "${prTitle!}", "${state}")`);

if (includeOtherAuthors) {
// only fetch open prs from other authors
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const searchParams: Record<string, string> = {
state: 'OPEN',
};
searchParams['direction'] = 'outgoing';
searchParams['at'] = `refs/heads/${branchName}`;

const query = getQueryString(searchParams);
const prs = await utils.accumulateValues(
viceice marked this conversation as resolved.
Show resolved Hide resolved
`./rest/api/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}/pull-requests?${query}`,
'get',
{},
1, // only fetch the latest pr
);

if (!prs.length) {
logger.debug(`No reconfigure Pr found for branch ${branchName}`);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
return null;
}

// return the latest pr
const pr = utils.prInfo(prs[0]);
if (pr) {
logger.debug(`Found PR #${pr.number}`);
}

return pr;
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}

const prList = await getPrList(refreshCache);
const pr = prList.find(isRelevantPr(branchName, prTitle, state));
if (pr) {
Expand Down
14 changes: 14 additions & 0 deletions lib/modules/platform/bitbucket/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ exports[`modules/platform/bitbucket/index findPr() finds pr 1`] = `
}
`;

exports[`modules/platform/bitbucket/index findPr() finds pr from other authors 1`] = `
{
"bodyStruct": {
"hash": "761b7ad8ad439b2855fcbb611331c646ef0870b0631247bba3f3025cb6df5a53",
},
"createdAt": "2018-07-02T07:02:25.275030+00:00",
"number": 5,
"sourceBranch": "branch",
"state": "open",
"targetBranch": "master",
"title": "title",
}
`;

exports[`modules/platform/bitbucket/index getBranchPr() bitbucket finds PR for branch 1`] = `
{
"bodyStruct": {
Expand Down
34 changes: 34 additions & 0 deletions lib/modules/platform/bitbucket/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,40 @@ describe('modules/platform/bitbucket/index', () => {
});
expect(pr?.number).toBe(5);
});

it('finds pr from other authors', async () => {
const scope = await initRepoMock();
scope
.get(
'/2.0/repositories/some/repo/pullrequests?q=source.branch.name="branch"&state=open',
)
.reply(200, { values: [pr] });
expect(
await bitbucket.findPr({
branchName: 'branch',
state: 'open',
includeOtherAuthors: true,
}),
).toMatchSnapshot();
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
});

it('returns null if no open pr exists - (includeOtherAuthors)', async () => {
const scope = await initRepoMock();
scope
.get(
'/2.0/repositories/some/repo/pullrequests?q=source.branch.name="branch"&state=open',
)
.reply(200, {
values: [],
});

const pr = await bitbucket.findPr({
branchName: 'branch',
state: 'open',
includeOtherAuthors: true,
});
expect(pr).toBeNull();
});
});

describe('createPr()', () => {
Expand Down
23 changes: 23 additions & 0 deletions lib/modules/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,31 @@ export async function findPr({
branchName,
prTitle,
state = 'all',
includeOtherAuthors,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);

if (includeOtherAuthors) {
// only fetch open prs from other authors
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const prs = (
await bitbucketHttp.getJson<PagedResult<PrResponse>>(
`/2.0/repositories/${config.repository}/pullrequests?q=source.branch.name="${branchName}"&state=open`,
)
).body.values;

if (prs.length === 0) {
logger.debug(`No reconfigure Pr found for branch ${branchName}`);
return null;
}

// return the latest pr
const pr = utils.prInfo(prs[0]);
if (pr) {
logger.debug(`Found PR #${pr.number}`);
}
return pr;
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}

const prList = await getPrList();
const pr = prList.find(
(p) =>
Expand Down
18 changes: 13 additions & 5 deletions lib/modules/platform/gitea/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function toRenovateIssue(data: Issue): Issue {
}

// TODO #22198
function toRenovatePR(data: PR): Pr | null {
function toRenovatePR(data: PR, includeOtherAuthors = false): Pr | null {
if (!data) {
return null;
}
Expand All @@ -108,7 +108,12 @@ function toRenovatePR(data: PR): Pr | null {
}

const createdBy = data.user?.username;
if (createdBy && botUserName && createdBy !== botUserName) {
if (
!includeOtherAuthors &&
createdBy &&
botUserName &&
createdBy !== botUserName
) {
return null;
}

Expand Down Expand Up @@ -447,12 +452,14 @@ const platform: Platform = {
return 'yellow';
},

getPrList(): Promise<Pr[]> {
getPrList(includeOtherAuthors = false): Promise<Pr[]> {
if (config.prList === null) {
config.prList = helper
.searchPRs(config.repository, { state: 'all' }, { memCache: false })
.then((prs) => {
const prList = prs.map(toRenovatePR).filter(is.truthy);
const prList = prs
.map((pr) => toRenovatePR(pr, includeOtherAuthors))
.filter(is.truthy);
logger.debug(`Retrieved ${prList.length} Pull Requests`);
return prList;
});
Expand Down Expand Up @@ -491,9 +498,10 @@ const platform: Platform = {
branchName,
prTitle: title,
state = 'all',
includeOtherAuthors,
}: FindPRConfig): Promise<Pr | null> {
logger.debug(`findPr(${branchName}, ${title!}, ${state})`);
const prList = await platform.getPrList();
const prList = await platform.getPrList(includeOtherAuthors);
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const pr = prList.find(
(p) =>
p.sourceRepo === config.repository &&
Expand Down
15 changes: 15 additions & 0 deletions lib/modules/platform/github/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`modules/platform/github/index findPr(branchName, prTitle, state) finds pr from other authors 1`] = `
{
"bodyStruct": {
"hash": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
"node_id": undefined,
"number": 1,
"sourceBranch": "branch-a",
"sourceRepo": "some/repo",
"state": "open",
"title": "branch a pr",
"updated_at": undefined,
}
`;

exports[`modules/platform/github/index getRepoForceRebase should throw 401 1`] = `"Response code 401 (Unauthorized)"`;

exports[`modules/platform/github/index getRepos should return an array of repos 1`] = `
Expand Down
38 changes: 38 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2428,6 +2428,44 @@ describe('modules/platform/github/index', () => {
res = await github.findPr({ branchName: 'branch-b' });
expect(res).toBeNull();
});

it('finds pr from other authors', async () => {
const scope = httpMock.scope(githubApiHost);
initRepoMock(scope, 'some/repo');
scope
.get('/repos/some/repo/pulls?head=some/repo:branch&state=open')
.reply(200, [
{
number: 1,
head: { ref: 'branch-a', repo: { full_name: 'some/repo' } },
title: 'branch a pr',
state: 'open',
},
]);
await github.initRepo({ repository: 'some/repo' });
expect(
await github.findPr({
branchName: 'branch',
state: 'open',
includeOtherAuthors: true,
}),
).toMatchSnapshot();
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
});

it('returns null if no pr found - (includeOtherAuthors)', async () => {
const scope = httpMock.scope(githubApiHost);
initRepoMock(scope, 'some/repo');
scope
.get('/repos/some/repo/pulls?head=some/repo:branch&state=open')
.reply(200, []);
await github.initRepo({ repository: 'some/repo' });
const pr = await github.findPr({
branchName: 'branch',
state: 'open',
includeOtherAuthors: true,
});
expect(pr).toBeNull();
});
});

describe('createPr()', () => {
Expand Down
23 changes: 23 additions & 0 deletions lib/modules/platform/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,31 @@ export async function findPr({
branchName,
prTitle,
state = 'all',
includeOtherAuthors,
}: FindPRConfig): Promise<GhPr | null> {
logger.debug(`findPr(${branchName}, ${prTitle}, ${state})`);

if (includeOtherAuthors) {
const repo = config.parentRepo ?? config.repository;
// only fetch open prs from other authors
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
const response = await githubApi.getJson<GhRestPr[]>(
`repos/${repo}/pulls?head=${repo}:${branchName}&state=open`,
);

const { body: prList } = response;
if (!prList.length) {
logger.debug(`No reconfigure Pr found for branch ${branchName}`);
return null;
}

// return the latest pull request
const pr = coerceRestPr(prList[0]);
if (pr) {
logger.debug(`Found PR #${pr.number}`);
}
return pr;
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}

const prList = await getPrList();
const pr = prList.find((p) => {
if (p.sourceBranch !== branchName) {
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/platform/gitlab/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ exports[`modules/platform/gitlab/index createPr(branchName, title, body) uses de
}
`;

exports[`modules/platform/gitlab/index findPr(branchName, prTitle, state) finds reconfigure pr 1`] = `
{
"number": 1,
"sourceBranch": "branch",
"state": "open",
"title": "branch a pr",
}
`;

exports[`modules/platform/gitlab/index getBranchPr(branchName) should return the PR object 1`] = `
{
"bodyStruct": {
Expand Down