summaryrefslogtreecommitdiff
path: root/chromium/infra/config/generators/cq-builders-md.star
blob: 6cc304c8006a91fae22357ff2695623fbf42e319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright 2020 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

load("//lib/try.star", "DEFAULT_EXCLUDE_REGEXPS")
load("//outages/config.star", outages_config = "config")

_MD_HEADER = """\
<!-- Auto-generated by lucicfg (via cq-builders-md.star). -->
<!-- Do not modify manually. -->

# List of CQ builders

[TOC]

Changes that only modify files in //infra/config, which do not change any build
outputs, only require the chromium_presubmit builder to pass before landing.

Each builder name links to that builder on Milo. The "matching builders" links
point to the file used to determine which configurations a builder should copy
when running. These links might 404 or error; they are hard-coded right now,
using common assumptions about how builders are configured.
"""

_REQUIRED_HEADER = """\
These builders must pass before a CL may land that affects files outside of
//docs and //infra/config.
"""

_OPTIONAL_HEADER = """\
These builders optionally run, depending on the files in a CL. For example, a CL
which touches `//gpu/BUILD.gn` would trigger the builder
`android_optional_gpu_tests_rel`, due to the `location_regexp` values for that
builder.
"""

_EXPERIMENTAL_HEADER = """\
These builders are run on some percentage of builds. Their results are ignored
by CQ. These are often used to test new configurations before they are added
as required builders.
""" + ("""\

These builders are currently disabled due to the cq_disable_experiments outages
setting. See //infra/config/outages/README.md for more information.
""" if outages_config.disable_cq_experiments else "")

_TRY_BUILDER_VIEW_URL = "https://ci.chromium.org/p/chromium/builders/try"

_REGEX_PREFIX = ".+/[+]/"

def _get_main_config_group_builders(ctx):
    cq_cfg = ctx.output["luci/commit-queue.cfg"]

    for c in cq_cfg.config_groups:
        if len(c.gerrit) != 1:
            continue

        gerrit = c.gerrit[0]
        if len(gerrit.projects) != 1:
            continue

        project = gerrit.projects[0]
        if len(project.ref_regexp) != 1:
            continue

        if (project.name == "chromium/src" and
            # Repeated proto fields have an internal type that won't compare equal
            # to a list, so convert it
            list(project.ref_regexp) == ["refs/heads/.+"]):
            return c.verifiers.tryjob.builders

    fail("Could not find the main CQ group")

def _normalize_builder(builder):
    location_regexp_exclude = [
        r
        for r in builder.location_regexp_exclude
        if r not in DEFAULT_EXCLUDE_REGEXPS
    ]
    location_regexp = builder.location_regexp
    if list(location_regexp) == [".*"] and not location_regexp_exclude:
        location_regexp = None
    return struct(
        name = builder.name,
        experiment_percentage = builder.experiment_percentage,
        includable_only = builder.includable_only,
        location_regexp = location_regexp,
        location_regexp_exclude = location_regexp_exclude,
    )

def _group_builders_by_section(builders):
    required = []
    experimental = []
    optional = []

    for builder in builders:
        builder = _normalize_builder(builder)
        if builder.experiment_percentage:
            experimental.append(builder)
        elif builder.location_regexp or builder.location_regexp_exclude:
            optional.append(builder)
        elif builder.includable_only:
            continue
        else:
            required.append(builder)

    return struct(
        required = required,
        experimental = experimental,
        optional = optional,
    )

def _codesearch_query(*atoms):
    query = ["https://cs.chromium.org/search?q="]
    for atom in atoms:
        query.append("+")
        query.append(atom)
    return "".join(query)

def _get_regex_line_details(regex):
    if regex.startswith(_REGEX_PREFIX):
        regex = regex[len(_REGEX_PREFIX):]
    title = "//" + regex.lstrip("/")
    if regex.endswith(".+"):
        regex = regex[:-len(".+")]

    url = _codesearch_query("file:" + regex)

    # If the regex doesn't have any interesting characters that might be part of a
    # regex, assume the regex is targeting a single path and direct link to it
    # Equals sign and dashes used by layout tests
    if all([c.isalnum() or c in "/-_=" for c in regex.codepoints()]):
        url = "https://cs.chromium.org/chromium/src/" + regex

    return struct(
        title = title,
        url = url,
    )

def _generate_cq_builders_md(ctx):
    builders = _get_main_config_group_builders(ctx)

    builders_by_section = _group_builders_by_section(builders)

    lines = [_MD_HEADER]

    for title, header, section in (
        ("Required builders", _REQUIRED_HEADER, "required"),
        ("Optional builders", _OPTIONAL_HEADER, "optional"),
        ("Experimental builders", _EXPERIMENTAL_HEADER, "experimental"),
    ):
        builders = getattr(builders_by_section, section)
        if not builders:
            continue

        lines.append("## %s" % title)
        lines.append(header)

        for b in builders:
            name = b.name.rsplit("/", 1)[-1]

            # Some builders share a common prefix (android-marshmallow-x86-rel
            # and android-marshmallow-x86-rel-non-cq for example). The quotes
            # below ensure codesearch links find the exact builder name, rather
            # than everything with a common prefix. Two sets of quotes are
            # needed because the first set is interpreted by codesearch.
            quoted_name = "\"\"{name}\"\"".format(name = name)
            lines.append((
                "* [{name}]({try_builder_view}/{name}) " +
                "([definition]({definition_query}+{quoted_name})) " +
                "([matching builders]({trybot_query}+{quoted_name}))"
            ).format(
                name = name,
                quoted_name = quoted_name,
                try_builder_view = _TRY_BUILDER_VIEW_URL,
                definition_query = _codesearch_query(
                    "file:/try/.*\\.star$",
                ),
                trybot_query = _codesearch_query("file:trybots.py"),
            ))

            if b.experiment_percentage:
                lines.append("  * Experiment percentage: {percentage}".format(
                    percentage = b.experiment_percentage,
                ))

            for attr, regexp_header in (
                ("location_regexp", "Path regular expressions:"),
                ("location_regexp_exclude", "Path exclude regular expressions:"),
            ):
                regexes = getattr(b, attr)
                if not regexes:
                    continue
                lines.append("")
                lines.append("  " + regexp_header)
                for regex in regexes:
                    regex_line_details = _get_regex_line_details(regex)
                    lines.append("  * [`{title}`]({url})".format(
                        title = regex_line_details.title,
                        url = regex_line_details.url,
                    ))

            lines.append("")

        lines.append("")

    ctx.output["cq-builders.md"] = "\n".join(lines)

lucicfg.generator(_generate_cq_builders_md)