@@ -2,14 +2,16 @@ import {existsSync} from 'node:fs';
2
2
import { green , Log } from '../../../utils/logging.js' ;
3
3
import { join } from 'node:path' ;
4
4
import { writeFile , readFile } from 'node:fs/promises' ;
5
+ import { targetLabels } from '../../../pr/common/labels/target.js' ;
5
6
6
7
/**
7
8
* Updates the `renovate.json` configuration file to include a new base branch.
9
+ * It also updates specific target labels within the package rules.
8
10
*
9
- * @param projectDir - The project directory path .
11
+ * @param projectDir - The path to the project directory.
10
12
* @param newBranchName - The name of the new branch to add to the base branches list.
11
- * @returns A promise that resolves to an string containing the path to the modified `renovate.json` file,
12
- * or null if config updating is disabled .
13
+ * @returns A promise that resolves to the path of the modified `renovate.json` file if updated ,
14
+ * or `null` if the file was not found or the `baseBranches` array has an unexpected format .
13
15
*/
14
16
export async function updateRenovateConfig (
15
17
projectDir : string ,
@@ -18,13 +20,13 @@ export async function updateRenovateConfig(
18
20
const renovateConfigPath = join ( projectDir , 'renovate.json' ) ;
19
21
if ( ! existsSync ( renovateConfigPath ) ) {
20
22
Log . warn ( ` ✘ Skipped updating Renovate config as it was not found.` ) ;
21
-
22
23
return null ;
23
24
}
24
25
25
26
const config = await readFile ( renovateConfigPath , 'utf-8' ) ;
26
27
const configJson = JSON . parse ( config ) as Record < string , unknown > ;
27
28
const baseBranches = configJson . baseBranches ;
29
+
28
30
if ( ! Array . isArray ( baseBranches ) || baseBranches . length !== 2 ) {
29
31
Log . warn (
30
32
` ✘ Skipped updating Renovate config: "baseBranches" must contain exactly 2 branches.` ,
@@ -34,8 +36,93 @@ export async function updateRenovateConfig(
34
36
}
35
37
36
38
configJson . baseBranches = [ 'main' , newBranchName ] ;
39
+
40
+ updateRenovateTargetLabel (
41
+ configJson ,
42
+ targetLabels [ 'TARGET_PATCH' ] . name ,
43
+ targetLabels [ 'TARGET_RC' ] . name ,
44
+ ) ;
37
45
await writeFile ( renovateConfigPath , JSON . stringify ( configJson , undefined , 2 ) ) ;
38
- Log . info ( green ( ` ✓ Updated Renovate config.` ) ) ;
39
46
47
+ Log . info ( green ( ` ✓ Updated Renovate config.` ) ) ;
40
48
return renovateConfigPath ;
41
49
}
50
+
51
+ /**
52
+ * Updates a specific target label in the `renovate.json` configuration file.
53
+ * This function specifically targets and replaces one label with another within the `packageRules`.
54
+ *
55
+ * @param projectDir - The path to the project directory.
56
+ * @param fromLabel - The label name to be replaced.
57
+ * @param toLabel - The new label name to replace `fromLabel` with.
58
+ * @returns A promise that resolves to the path of the modified `renovate.json` file if updated,
59
+ * or `null` if the file was not found or the `baseBranches` array has an unexpected format.
60
+ */
61
+ export async function updateRenovateConfigTargetLabels (
62
+ projectDir : string ,
63
+ fromLabel : string ,
64
+ toLabel : string ,
65
+ ) : Promise < string | null > {
66
+ const renovateConfigPath = join ( projectDir , 'renovate.json' ) ;
67
+ if ( ! existsSync ( renovateConfigPath ) ) {
68
+ Log . warn ( ` ✘ Skipped updating Renovate config as it was not found.` ) ;
69
+
70
+ return null ;
71
+ }
72
+
73
+ const config = await readFile ( renovateConfigPath , 'utf-8' ) ;
74
+ const configJson = JSON . parse ( config ) as Record < string , unknown > ;
75
+
76
+ // Check baseBranches just in case, though this function's primary focus is labels
77
+ const baseBranches = configJson . baseBranches ;
78
+ if ( ! Array . isArray ( baseBranches ) || baseBranches . length !== 2 ) {
79
+ Log . warn (
80
+ ` ✘ Skipped updating Renovate config: "baseBranches" must contain exactly 2 branches.` ,
81
+ ) ;
82
+
83
+ return null ;
84
+ }
85
+
86
+ if ( updateRenovateTargetLabel ( configJson , fromLabel , toLabel ) ) {
87
+ await writeFile ( renovateConfigPath , JSON . stringify ( configJson , undefined , 2 ) ) ;
88
+ Log . info ( green ( ` ✓ Updated target label in Renovate config.` ) ) ;
89
+
90
+ return renovateConfigPath ;
91
+ } else {
92
+ Log . info ( green ( ` ✓ No changes to target labels in Renovate config.` ) ) ;
93
+ return null ;
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Updates a specific target label within the `packageRules` of a Renovate configuration.
99
+ *
100
+ * @param configJson - The parsed JSON object of the Renovate configuration.
101
+ * @param fromLabel - The label name to be replaced.
102
+ * @param toLabel - The new label name to replace `fromLabel` with.
103
+ * @returns `true` is the label has been updated, otherwise `false`.
104
+ */
105
+ function updateRenovateTargetLabel (
106
+ configJson : Record < string , unknown > ,
107
+ fromLabel : string ,
108
+ toLabel : string ,
109
+ ) : boolean {
110
+ if ( ! Array . isArray ( configJson . packageRules ) ) {
111
+ return false ;
112
+ }
113
+
114
+ let updated = false ;
115
+ for ( const rule of configJson . packageRules ) {
116
+ if ( ! Array . isArray ( rule . addLabels ) ) {
117
+ continue ;
118
+ }
119
+
120
+ const idx = ( rule . addLabels as string [ ] ) . findIndex ( ( x ) => x === fromLabel ) ;
121
+ if ( idx >= 0 ) {
122
+ rule . addLabels [ idx ] = toLabel ;
123
+ updated = true ;
124
+ }
125
+ }
126
+
127
+ return updated ;
128
+ }
0 commit comments