11#!/usr/bin/env -S scala-cli shebang
22// SPDX-License-Identifier: Apache-2.0
33//> using scala " 2.13"
4- //> using dep " io.circe::circe-yaml:0.15.1 "
5- //> using dep " com.lihaoyi::os-lib:0.9.1 "
4+ //> using dep " io.circe::circe-yaml:0.16.0 "
5+ //> using dep " com.lihaoyi::os-lib:0.11.3 "
66
77/* Generates a Mergify config YAML (to STDOUT) based on input config
88 *
99 * There are built-in conditions, but different CI requires different conditions
1010 * Listed branches should be stable branches that we want to backport to, in ascending order
11- * {{{
11+ *
1212 * conditions:
13- - status-success=Travis CI - Pull Request
13+ - status-success=all tests passed
1414 branches:
15- - 1.2 .x
16- - 1.3 .x
17- - 1.4 .x
18- * }}}
15+ - 3.6 .x
16+ - 5 .x
17+ - 6 .x
18+ *
1919 */
2020
2121import io .circe ._
2222import io .circe .syntax ._ // for .asJson
2323import io .circe .yaml .parser
2424import io .circe .yaml .syntax ._ // for .asYaml
2525
26- def mergeQueue (conditions : List [String ]) = Json .obj(
27- " name" -> " default" .asJson,
28- " conditions" -> conditions.asJson,
29- " draft_bot_account" -> " chiselbot" .asJson,
30- " update_bot_account" -> " chiselbot" .asJson,
31- " merge_bot_account" -> " chiselbot" .asJson
32- )
33-
34- val queueAction = Json .obj(
35- " queue" -> Json .obj(
36- " name" -> " default" .asJson,
37- " method" -> " squash" .asJson,
38- " update_method" -> " merge" .asJson,
39- " update_bot_account" -> " chiselbot" .asJson,
40- " merge_bot_account" -> " chiselbot" .asJson
41- )
42- )
43-
44- def mergeToMain (conditions : List [String ]) = Json .obj(
45- " name" -> " automatic squash-and-merge on CI success and review" .asJson,
46- " conditions" -> (conditions ++ List (
47- " #approved-reviews-by>=1" ,
26+ def mergeQueue (branch : String , conditions : List [String ]) = Json .obj(
27+ " name" -> s " $branch merge queue " .asJson,
28+ " queue_conditions" -> (conditions ++ List (
29+ s " base= $branch" ,
4830 " #changes-requested-reviews-by=0" ,
49- " base=main" ,
50- " label=\" Please Merge\" " ,
31+ " label=\" Backport\" " ,
5132 " label!=\" DO NOT MERGE\" " ,
5233 " label!=\" bp-conflict\" "
5334 )).asJson,
54- " actions" -> queueAction
35+ " merge_conditions" -> conditions.asJson,
36+ " draft_bot_account" -> " chiselbot" .asJson,
37+ " update_bot_account" -> " chiselbot" .asJson,
38+ " merge_bot_account" -> " chiselbot" .asJson
5539)
5640
5741def makeBackportRule (branches : List [String ]): Json = {
5842 Json .obj(
5943 " name" -> s """ backport to ${branches.mkString(" , " )}""" .asJson,
60- " conditions" -> List (" merged" , " base=main " , s " milestone= ${branches.head}" ).asJson,
44+ " conditions" -> List (" merged" , s " milestone= ${branches.head}" ).asJson,
6145 " actions" -> Json .obj(
6246 " backport" -> Json .obj(
6347 " branches" -> branches.asJson,
6448 " labels" -> List (" Backport" ).asJson,
6549 " ignore_conflicts" -> true .asJson,
66- " label_conflicts" -> " bp-conflict" .asJson
50+ " merge_conflict_style" -> " diff3" .asJson,
51+ " label_conflicts" -> " bp-conflict" .asJson,
52+ " bot_account" -> " chiselbot" .asJson,
53+ " report_mode" -> List (" comment" ).asJson
6754 ),
6855 " label" -> Json .obj(
6956 " add" -> List (" Backported" ).asJson
@@ -73,34 +60,40 @@ def makeBackportRule(branches: List[String]): Json = {
7360}
7461
7562def backportMergeRule (conditions : List [String ])(branch : String ): Json = Json .obj(
76- " name" -> s " automatic squash-and-mege of $branch backport PRs " .asJson,
63+ " name" -> s " queue $branch backport PRs " .asJson,
7764 " conditions" -> (conditions ++ List (
7865 " #changes-requested-reviews-by=0" ,
7966 s " base= $branch" ,
8067 " label=\" Backport\" " ,
8168 " label!=\" DO NOT MERGE\" " ,
8269 " label!=\" bp-conflict\" "
8370 )).asJson,
84- " actions" -> queueAction
71+ " actions" -> Json .obj(
72+ " queue" -> Json .obj(
73+ " name" -> s " $branch merge queue " .asJson
74+ )
75+ )
8576)
8677
87-
8878def error (msg : String ) = throw new Exception (msg) with scala.util.control.NoStackTrace
8979
9080def processTemplate (path : os.Path ): (List [String ], List [String ]) = {
9181 val contents = os.read(path)
92- val parsed = parser.parse(contents)
93- .getOrElse(error(s " Invalid YAML $path" ))
82+ val parsed = parser
83+ .parse(contents)
84+ .getOrElse(error(s " Invalid YAML $path" ))
9485
9586 val cursor : HCursor = parsed.hcursor
9687
97- val conditions = cursor.downField(" conditions" )
98- .as[List [String ]]
99- .getOrElse(error(s " Invalid template, expected field 'conditions': List[String] " ))
88+ val conditions = cursor
89+ .downField(" conditions" )
90+ .as[List [String ]]
91+ .getOrElse(error(s " Invalid template, expected field 'conditions': List[String] " ))
10092
101- val branches = cursor.downField(" branches" )
102- .as[List [String ]]
103- .getOrElse(error(s " Invalid template, expected field 'branches': List[String] " ))
93+ val branches = cursor
94+ .downField(" branches" )
95+ .as[List [String ]]
96+ .getOrElse(error(s " Invalid template, expected field 'branches': List[String] " ))
10497 (conditions, branches)
10598}
10699
@@ -112,15 +105,13 @@ val (conditions, branches) = processTemplate(template)
112105val branchSets = branches.scanRight(List .empty[String ])(_ :: _).init.reverse
113106
114107val config = Json .obj(
115- " queue_rules" -> Json .fromValues(
116- mergeQueue(conditions) ::
117- Nil
118- ),
119- " pull_request_rules" -> Json .fromValues(
120- mergeToMain(conditions) ::
121- branchSets.map(makeBackportRule) :::
122- branches.map(backportMergeRule(conditions))
123- )
108+ " queue_rules" -> Json .fromValues(
109+ branches.map(mergeQueue(_, conditions))
110+ ),
111+ " pull_request_rules" -> Json .fromValues(
112+ branchSets.map(makeBackportRule) :::
113+ branches.map(backportMergeRule(conditions))
114+ )
124115)
125116
126117println(s " # Generated by $scriptPath" )
0 commit comments