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

feat: update labels when config changes #25340

Merged
merged 71 commits into from Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
f20953b
implement update labels logic in github, gitlab, gitea
RahulGautamSingh Aug 17, 2023
1a65f9f
Merge branch 'main' into feat/update-labels
RahulGautamSingh Oct 3, 2023
37de4a4
Merge branch 'main' into feat/update-labels
RahulGautamSingh Oct 22, 2023
33a10d2
skip if pr-labels are modified
RahulGautamSingh Oct 22, 2023
472f4a3
fix tests
RahulGautamSingh Oct 22, 2023
18bca70
fix gitea label update logic
RahulGautamSingh Oct 22, 2023
3af757b
Apply suggestions from code review
RahulGautamSingh Oct 23, 2023
fa29bc8
add tests
RahulGautamSingh Oct 23, 2023
9b18b66
update snap[s
RahulGautamSingh Oct 23, 2023
3a7f427
fix labels order in tests
RahulGautamSingh Oct 24, 2023
ce6777f
revert ordering the labels
RahulGautamSingh Oct 24, 2023
d306847
change gitae logic
RahulGautamSingh Oct 24, 2023
d4bad42
update PrUpdateConfig interface
RahulGautamSingh Oct 24, 2023
068f70f
fix tests
RahulGautamSingh Oct 24, 2023
a84f3f8
Merge branch 'main' into feat/update-labels
RahulGautamSingh Oct 24, 2023
b221ab4
fix coverage
RahulGautamSingh Oct 24, 2023
d90a6ad
Merge branch 'feat/update-labels' of https://github.com/RahulGautamSi…
RahulGautamSingh Oct 24, 2023
3d6f734
refactor getChangedLabels fn
RahulGautamSingh Oct 24, 2023
65138bf
apply suggestions
RahulGautamSingh Oct 24, 2023
e73029f
add docs
RahulGautamSingh Oct 24, 2023
6a51748
Update docs/usage/configuration-options.md
RahulGautamSingh Nov 3, 2023
2365865
Update docs/usage/configuration-options.md
RahulGautamSingh Nov 3, 2023
b9f3cda
store labels in existing debugData
RahulGautamSingh Nov 6, 2023
277f2d6
fix build issues
RahulGautamSingh Nov 6, 2023
3192786
fix coverage
RahulGautamSingh Nov 6, 2023
61fe7d1
fix: logic updatePrdebugData
RahulGautamSingh Nov 7, 2023
b08bba8
Update lib/workers/repository/update/pr/body/index.ts
rarkins Nov 7, 2023
ad64b21
Merge branch 'main' into feat/update-labels
RahulGautamSingh Nov 8, 2023
407223e
fix formatting
RahulGautamSingh Nov 8, 2023
727e013
revert the labels struct changes
RahulGautamSingh Nov 8, 2023
39d8fe0
update snpas
RahulGautamSingh Nov 8, 2023
4c5e802
refactor params of the new fns
RahulGautamSingh Nov 8, 2023
3f09676
fix tests
RahulGautamSingh Nov 8, 2023
9de67bf
Merge branch 'main' into feat/update-labels
RahulGautamSingh Nov 8, 2023
1e5bee8
fix formatting
RahulGautamSingh Nov 8, 2023
d970ece
remove extra logs
RahulGautamSingh Nov 9, 2023
1c85e92
gitea: include labels in getPR result
RahulGautamSingh Nov 9, 2023
fee35a6
Merge branch 'main' into feat/update-labels
RahulGautamSingh Nov 9, 2023
fdeb79f
fix formatting
RahulGautamSingh Nov 9, 2023
71a632e
fix tests
RahulGautamSingh Nov 9, 2023
2a5aae6
refactor
RahulGautamSingh Nov 9, 2023
f198a68
add comments
RahulGautamSingh Nov 10, 2023
dacdb5d
Update docs/usage/configuration-options.md
RahulGautamSingh Nov 10, 2023
0dca273
Apply suggestions from code review
RahulGautamSingh Nov 11, 2023
15ec73a
fix lint issues
RahulGautamSingh Nov 11, 2023
c39e4e4
fix test
RahulGautamSingh Nov 11, 2023
bcc173b
Apply Suggestion
RahulGautamSingh Nov 13, 2023
c2156be
apply suggestions
RahulGautamSingh Nov 14, 2023
b7aebff
Update lib/workers/repository/update/pr/labels.ts
RahulGautamSingh Nov 15, 2023
6e4d3cf
Merge branch 'main' into feat/update-labels
RahulGautamSingh Nov 15, 2023
682469a
remove non-null assertion
RahulGautamSingh Nov 15, 2023
99ba709
merge
RahulGautamSingh Jan 23, 2024
1ec080f
fix gitea tests
RahulGautamSingh Jan 24, 2024
3198053
use pmap
RahulGautamSingh Jan 24, 2024
d8aa7ad
Update lib/modules/platform/gitea/index.ts
RahulGautamSingh Jan 30, 2024
ad91fbf
fix formatting
RahulGautamSingh Jan 30, 2024
ebb1575
Apply suggestions from code review
RahulGautamSingh Feb 3, 2024
4951c89
Update docs/usage/configuration-options.md
RahulGautamSingh Feb 3, 2024
97db32b
Update lib/workers/repository/update/pr/index.ts
RahulGautamSingh Feb 3, 2024
7491f6a
Apply suggestions from code review
RahulGautamSingh Feb 3, 2024
d68c1f5
remove unnecessary comments
RahulGautamSingh Feb 9, 2024
6f68ffa
refactor logic and add logs
RahulGautamSingh Feb 18, 2024
1a10a2c
Apply suggestions from code review
RahulGautamSingh Feb 18, 2024
f24c866
update docs
RahulGautamSingh Feb 18, 2024
670d907
sort in preparelabels
RahulGautamSingh Feb 19, 2024
69e976a
fix docs
RahulGautamSingh Feb 19, 2024
45818d7
fix tests
RahulGautamSingh Feb 19, 2024
921e9f2
Update docs/usage/configuration-options.md
RahulGautamSingh Feb 21, 2024
e0dc474
Merge branch 'main' into feat/update-labels
rarkins Feb 28, 2024
8b92b64
Merge branch 'main' into feat/update-labels
RahulGautamSingh Mar 18, 2024
70126e0
log waring for unavaiable labels
RahulGautamSingh Mar 18, 2024
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
3 changes: 2 additions & 1 deletion lib/modules/platform/azure/index.spec.ts
Expand Up @@ -1410,7 +1410,8 @@ describe('modules/platform/azure/index', () => {
it('returns updated pr body', () => {
const prBody =
'\n---\n\n - [ ] <!-- rebase-check --> rebase\n<!--renovate-config-hash:-->' +
'plus also [a link](https://github.com/foo/bar/issues/5)';
'plus also [a link](https://github.com/foo/bar/issues/5)' +
`<!--labels:aSmofwineo-->`;
expect(azure.massageMarkdown(prBody)).toBe(
'plus also [a link](https://github.com/foo/bar/issues/5)'
);
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/azure/index.ts
Expand Up @@ -812,7 +812,8 @@ export function massageMarkdown(input: string): string {
'renaming the PR to start with "rebase!"'
)
.replace(regEx(`\n---\n\n.*?<!-- rebase-check -->.*?\n`), '')
.replace(regEx(/<!--renovate-(?:debug|config-hash):.*?-->/g), '');
.replace(regEx(/<!--renovate-(?:debug|config-hash):.*?-->/g), '')
.replace(regEx(/<!--labels:.*?-->/g), '');
}

/* istanbul ignore next */
Expand Down
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
Expand Up @@ -198,6 +198,7 @@ exports[`modules/platform/bitbucket-server/index endpoint with no path massageMa

Empty comment.


RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
Followed by some information.
"
`;
Expand Down Expand Up @@ -402,6 +403,7 @@ exports[`modules/platform/bitbucket-server/index endpoint with path massageMarkd

Empty comment.


RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
Followed by some information.
"
`;
Expand Down
1 change: 1 addition & 0 deletions lib/modules/platform/bitbucket-server/index.spec.ts
Expand Up @@ -1743,6 +1743,7 @@ describe('modules/platform/bitbucket-server/index', () => {
<!---->
Empty comment.
<!-- This is another comment -->
<!--labels:aSwedlninfa-->
Followed by some information.
<!-- followed by some more comments -->`);
expect(prBody).toMatchSnapshot();
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/bitbucket-server/index.ts
Expand Up @@ -983,5 +983,6 @@ export function massageMarkdown(input: string): string {
.replace(regEx(/<\/?summary>/g), '**')
.replace(regEx(/<\/?details>/g), '')
.replace(regEx(`\n---\n\n.*?<!-- rebase-check -->.*?(\n|$)`), '')
.replace(regEx('<!--.*?-->', 'g'), '');
.replace(regEx('<!--.*?-->', 'g'), '')
.replace(regEx(/<!--labels:.*?-->/g), '');
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 2 additions & 2 deletions lib/modules/platform/bitbucket/index.spec.ts
Expand Up @@ -1370,8 +1370,8 @@ describe('modules/platform/bitbucket/index', () => {
const prBody =
'<details><summary>foo</summary>\n<blockquote>\n\n<details><summary>text</summary>' +
'\n---\n\n - [ ] <!-- rebase-check --> rebase\n<!--renovate-config-hash:-->' +
'\n\n</details>\n\n</blockquote>\n</details>';

'\n\n</details>\n\n</blockquote>\n</details>' +
`<!--labels:aSdwerfwefmk1-->`;
expect(bitbucket.massageMarkdown(prBody)).toMatchSnapshot();
});
});
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/bitbucket/index.ts
Expand Up @@ -564,7 +564,8 @@ export function massageMarkdown(input: string): string {
.replace(regEx(/<\/?(details|blockquote)>/g), '')
.replace(regEx(`\n---\n\n.*?<!-- rebase-check -->.*?\n`), '')
.replace(regEx(/\]\(\.\.\/pull\//g), '](../../pull-requests/')
.replace(regEx(/<!--renovate-(?:debug|config-hash):.*?-->/g), '');
.replace(regEx(/<!--renovate-(?:debug|config-hash):.*?-->/g), '')
.replace(regEx(/<!--labels:.*?-->/g), '');
}

export async function ensureIssue({
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/codecommit/index.spec.ts
Expand Up @@ -54,7 +54,8 @@ describe('modules/platform/codecommit/index', () => {

it('validates massageMarkdown functionality', () => {
const newStr = codeCommit.massageMarkdown(
'<details><summary>foo</summary>bar</details>text<details>\n<!--renovate-debug:hiddenmessage123-->'
'<details><summary>foo</summary>bar</details>text<details>\n<!--renovate-debug:hiddenmessage123-->' +
'<!--labels:aSdwerfwefmk1-->'
);
expect(newStr).toBe(
'**foo**bartext\n[//]: # (<!--renovate-debug:hiddenmessage123-->)'
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/platform/codecommit/index.ts
Expand Up @@ -320,7 +320,8 @@ export function massageMarkdown(input: string): string {
.replace(
regEx(/(?<hiddenComment><!--renovate-(?:debug|config-hash):.*?-->)/g),
'[//]: # ($<hiddenComment>)'
);
)
.replace(regEx(/<!--labels:.*?-->/g), '');
}

export async function getJsonFile(
Expand Down
17 changes: 17 additions & 0 deletions lib/modules/platform/gitea/gitea-helper.spec.ts
Expand Up @@ -2,6 +2,7 @@ import * as httpMock from '../../../../test/http-mock';
import { setBaseUrl } from '../../../util/http/gitea';
import { toBase64 } from '../../../util/string';
import {
assignLabel,
closeIssue,
closePR,
createComment,
Expand Down Expand Up @@ -602,6 +603,22 @@ describe('modules/platform/gitea/gitea-helper', () => {
});
});

describe('assignLabel', () => {
it('should call /api/v1/repos/[repo]/issues/[issue]/labels endpoint', async () => {
httpMock
.scope(baseUrl)
.patch(`/repos/${mockRepo.full_name}/issues/${mockIssue.number}/labels`)
.reply(200);

const res = await assignLabel(
mockRepo.full_name,
mockIssue.number,
mockLabel.id
);
expect(res).toBeUndefined();
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('createComment', () => {
it('should call /api/v1/repos/[repo]/issues/[issue]/comments endpoint', async () => {
httpMock
Expand Down
15 changes: 15 additions & 0 deletions lib/modules/platform/gitea/gitea-helper.ts
Expand Up @@ -306,6 +306,21 @@ export async function unassignLabel(
await giteaHttp.deleteJson(url, options);
}

export async function assignLabel(
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
repoPath: string,
issue: number,
label: number,
options?: GiteaHttpOptions
): Promise<void> {
const url = `${API_PATH}/repos/${repoPath}/issues/${issue}/labels`;
await giteaHttp.patchJson(url, {
...options,
body: {
labels: [label],
},
});
}

export async function createComment(
repoPath: string,
issue: number,
Expand Down
52 changes: 52 additions & 0 deletions lib/modules/platform/gitea/index.spec.ts
Expand Up @@ -1290,6 +1290,58 @@ describe('modules/platform/gitea/index', () => {
state: 'closed',
});
});

it('should add new labels', async () => {
helper.getRepoLabels.mockResolvedValueOnce([
{ id: 1, name: 'old_label', description: 'its a me', color: '#000000' },
{
id: 2,
name: 'new_label',
description: 'labelario',
color: '#ffffff',
},
]);
helper.getOrgLabels.mockResolvedValueOnce([]);
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
await gitea.updatePr({
number: 1,
prTitle: 'New Title',
prBody: 'New Body',
state: 'closed',
labels: ['new_label'],
});

expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, {
title: 'New Title',
body: 'New Body',
state: 'closed',
labels: [2],
});
});

it('should remove old labels', async () => {
helper.getRepoLabels.mockResolvedValueOnce([
{ id: 1, name: 'old_label', description: 'its a me', color: '#000000' },
]);
helper.getOrgLabels.mockResolvedValueOnce([]);
helper.searchPRs.mockResolvedValueOnce(mockPRs);
await initFakeRepo();
await gitea.updatePr({
number: 1,
prTitle: 'New Title',
prBody: 'New Body',
state: 'closed',
labels: [],
});

expect(helper.updatePR).toHaveBeenCalledWith(mockRepo.full_name, 1, {
title: 'New Title',
body: 'New Body',
state: 'closed',
labels: [],
});
});
});

describe('mergePr', () => {
Expand Down
11 changes: 10 additions & 1 deletion lib/modules/platform/gitea/index.ts
Expand Up @@ -622,11 +622,13 @@ const platform: Platform = {
number,
prTitle,
prBody: body,
labels,
state,
targetBranch,
}: UpdatePrConfig): Promise<void> {
let title = prTitle;
if ((await getPrList()).find((pr) => pr.number === number)?.isDraft) {
const pr = (await getPrList()).find((pr) => pr.number === number);
if (pr?.isDraft) {
title = DRAFT_PREFIX + title;
}

Expand All @@ -639,6 +641,13 @@ const platform: Platform = {
prUpdateParams.base = targetBranch;
}

// no need to do extra call for removing labels as gitea API replaces labels array
if (Array.isArray(labels)) {
prUpdateParams.labels = (
await Promise.all(labels.map(lookupLabelByName))
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved
).filter(is.number);
}

await helper.updatePR(config.repository, number, prUpdateParams);
},

Expand Down
38 changes: 38 additions & 0 deletions lib/modules/platform/github/index.spec.ts
Expand Up @@ -3051,6 +3051,44 @@ describe('modules/platform/github/index', () => {

await expect(github.updatePr(pr)).toResolve();
});

it('should add and remove labels', async () => {
const pr: UpdatePrConfig = {
number: 1234,
prTitle: 'The New Title',
prBody: 'Hello world again',
state: 'closed',
targetBranch: 'new_base',
addLabels: ['new_label'],
removeLabels: ['old_label'],
};
const scope = httpMock.scope(githubApiHost);
initRepoMock(scope, 'some/repo');
await github.initRepo({ repository: 'some/repo' });
scope
.patch('/repos/some/repo/pulls/1234')
.reply(200, {
number: 91,
base: { sha: '1234' },
head: { ref: 'somebranch', repo: { full_name: 'some/repo' } },
state: 'open',
title: 'old title',
updated_at: '01-09-2022',
body: '<!--labels:WyJvbGRfbGFiZWwiXQ==-->', // equivalent to labels array : ['old_label]
})
.post('/repos/some/repo/issues/1234/labels')
.reply(200, pr)
.delete('/repos/some/repo/issues/1234/labels/old_label')
.reply(200, pr);

await expect(github.updatePr(pr)).toResolve();
expect(logger.logger.debug).toHaveBeenCalledWith(
`Adding labels 'new_label' to #1234`
);
expect(logger.logger.debug).toHaveBeenCalledWith(
`Deleting label old_label from #1234`
);
});
});

describe('mergePr(prNo)', () => {
Expand Down
15 changes: 15 additions & 0 deletions lib/modules/platform/github/index.ts
Expand Up @@ -1673,6 +1673,8 @@ export async function updatePr({
number: prNo,
prTitle: title,
prBody: rawBody,
addLabels: labelsToAdd,
removeLabels,
state,
targetBranch,
}: UpdatePrConfig): Promise<void> {
Expand All @@ -1695,7 +1697,20 @@ export async function updatePr({
if (config.forkToken) {
options.token = config.forkToken;
}

try {
// add and remove labels before updating pr body because if by some mishap
// labels aren't changed then pr body will have wrong labelsHash
if (labelsToAdd) {
await addLabels(prNo, labelsToAdd);
}

if (removeLabels) {
for (const label of removeLabels) {
await deleteLabel(prNo, label);
}
}

const { body: ghPr } = await githubApi.patchJson<GhRestPr>(
`repos/${config.parentRepo ?? config.repository}/pulls/${prNo}`,
options
Expand Down
30 changes: 30 additions & 0 deletions lib/modules/platform/gitlab/index.spec.ts
Expand Up @@ -2539,6 +2539,36 @@ describe('modules/platform/gitlab/index', () => {
})
).toResolve();
});
RahulGautamSingh marked this conversation as resolved.
Show resolved Hide resolved

it('adds and removes labels', async () => {
await initPlatform('13.3.6-ee');
httpMock
.scope(gitlabApiHost)
.get(
'/api/v4/projects/undefined/merge_requests?per_page=100&scope=created_by_me'
)
.reply(200, [
{
iid: 1,
source_branch: 'branch-a',
title: 'branch a pr',
state: 'open',
description: `<!--labels:WyJvbGRfbGFiZWwiXQ==-->`, // equivallent to labels array : ['old_label']
},
])
.put('/api/v4/projects/undefined/merge_requests/1')
.reply(200);
await expect(
gitlab.updatePr({
number: 1,
prTitle: 'title',
prBody: 'body',
state: 'closed',
addLabels: ['new_label'],
removeLabels: ['old_label'],
})
).toResolve();
});
});

describe('mergePr(pr)', () => {
Expand Down
8 changes: 8 additions & 0 deletions lib/modules/platform/gitlab/index.ts
Expand Up @@ -750,6 +750,8 @@ export async function updatePr({
number: iid,
prTitle,
prBody: description,
addLabels,
removeLabels,
state,
platformOptions,
targetBranch,
Expand All @@ -772,6 +774,12 @@ export async function updatePr({
if (targetBranch) {
body.target_branch = targetBranch;
}
if (addLabels) {
body.add_labels = addLabels;
}
if (removeLabels) {
body.remove_labels = removeLabels;
}

await gitlabApi.putJson(
`projects/${config.repository}/merge_requests/${iid}`,
Expand Down