Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions fork-ci/actions/pr-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@ or `lens –`, quoted or not), or a `read at` with a hex sha outside a `>` quote
(a fenced or indented block is no quote). To name a sha a lens read in such a
comment, quote that line with `>`.

A quote does not help a lens field: a line like `the security lens — fixed in
<sha>` blocks for good, quoted or not, and `not a lens:` cannot clear it. In a
fix-round or note comment, put each sha on a line of its own, with no `lens`
and `:`, `=`, `—` or `–` before it on that line:

```
not a lens: fix round 2 for the security and state lenses
- security, major: fixed, with a test that fails on the old head.
- state, minor: left as is, because ...
Fixed in:
<sha>
```

A comment holds one lens: below a valid header, a line that starts with
`lens:`, `lens —`, `lens –` or `lens=` and names a sha is a second header, and
the comment reads nothing. (`LENS = '...'`, with a blank before the `=`, is no
second header: a report's test code assigns sample headers that way.) Lines in
code blocks count too, so a report quotes sample headers with placeholders
(`<sha>`), never a real-looking sha (seven or more hex digits).

Fenced blocks are read as GitHub renders them: only a line of the same
character, at least as long and with nothing after it, closes a fence
(` ```js ` inside an open fence is code), and a backtick line with
another backtick after it is inline code, not a fence.

A comment that looks like a report but is not one valid read is a lens of its
own, keyed by that comment. Posting the report again the right way adds a new
read and leaves the old comment blocking, and hiding it is not enough either.
Expand Down
76 changes: 51 additions & 25 deletions fork-ci/actions/pr-queue/lens.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
# that is indented 4+ spaces, quoted or a fence is never the header (lens_header), but its header line still counts as
# one (lens_problem: fail closed).
LENS_RE = re.compile(r'lens\s*:\s*(.*)', re.I)
# A line that starts with a lens field in a form loose_header reads (`lens:`, `lens —`, `lens –`, and `lens=` with no
# blank before the `=`): below the first line, one with a sha is a second lens's header (lens_problem; issue #303).
# `LENS = '...'` is no field here: a report's test code assigns sample headers that way.
FIELD_START_RE = re.compile(r'lens(?:\s*[:—–]|=)\s*(.*)', re.I)
# The name ends at the first field: ` · reviewer:` or ` · read at:` (also `read at <hex>`; the separator may also be
# `,` `—` or none). A bare `reviewer` or `read at` stays in the name (`data-integrity reviewer 1` and `... 2` are two
# lenses).
Expand All @@ -38,7 +42,8 @@
# leaves the old one; hiding the old one is not enough either.
# The queue posts this text, so it never holds the lens marker with its colon (no_lens_text).
NO_HEADER_WAY_OUT = ('delete that comment (hiding it is not enough), or edit it to start with `not a lens` and a colon '
'and hold no lens header line, with any line that names the sha a lens read quoted with `>`, or')
'and hold no lens header line and no sha on a line with a lens field, with any line that names '
'the sha a lens read quoted with `>`, or')
EDITED = 'edited after posting: post a new comment instead'
# A review's edit and hidden flags, by review node id, as the caller read them with a query of its own (pr_queue
# review_flags). gh pr view never fetches them for reviews but prints `includesCreatedEdit: false` anyway, so a review
Expand Down Expand Up @@ -205,18 +210,31 @@ def lens_header(body, narrow=False):
return name, (at.group(1).lower() if at else None)


# A fence as GitHub (GFM) reads one: an opening line is 3+ backticks or tildes after at most 3 spaces, and a backtick
# fence's info string holds no backtick (```` ```x``` ```` is inline code, no fence). Only a line of the same character,
# at least as long, with nothing but blanks after it closes the fence (```` ```js ```` inside an open fence is code). An
# unclosed fence runs to the end of the comment. Issue #303: a line with an info string closed the fence, and a
# backtick line with a backtick after it opened one, so lines GitHub shows as code were read as quotes or plain text,
# and plain lines as code.
FENCE_OPEN_RE = re.compile(r' {0,3}(?:(`{3,})[^`]*|(~{3,}).*)')
FENCE_CLOSE_RE = re.compile(r' {0,3}(`{3,}|~{3,})[ \t]*')


def fence_step(fence, line):
"""(the fence open after this line or None, whether the line belongs to a fence), given the fence open before it."""
if fence:
m = FENCE_CLOSE_RE.fullmatch(line)
return (None if m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence) else fence), True
m = FENCE_OPEN_RE.fullmatch(line)
return (m.group(1) or m.group(2), True) if m else (None, False)


def plain_lines(body):
"""The comment's lines outside fenced blocks, 4-space/tab indented blocks and quotes."""
"""The comment's lines outside fenced blocks (fence_step), 4-space/tab indented blocks and quotes."""
out, fence = [], None
for line in (body or '').splitlines():
m = re.match(r' {0,3}(`{3,}|~{3,})', line)
if fence:
if m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence):
fence = None
continue
if m:
fence = m.group(1)
elif line.strip() and not re.match(r' {4}|\t|[ ]{0,3}>', line):
fence, fenced = fence_step(fence, line)
if not fenced and line.strip() and not re.match(r' {4}|\t|[ ]{0,3}>', line):
out.append(line)
return out

Expand All @@ -226,13 +244,8 @@ def unquoted_lines(body):
indented block is code, and the line stays."""
out, fence = [], None
for line in (body or '').splitlines():
m = re.match(r' {0,3}(`{3,}|~{3,})', line)
if fence:
if m and m.group(1)[0] == fence[0] and len(m.group(1)) >= len(fence):
fence = None
elif m:
fence = m.group(1)
elif re.match(r' {0,3}>', line):
fence, fenced = fence_step(fence, line)
if not fenced and re.match(r' {0,3}>', line):
continue
out.append(line)
return out
Expand All @@ -256,13 +269,24 @@ def no_lens_text(text, keep_markdown=False):
"""Text that can never count as a lens read, name a lens or look like a lens report (lens_problem), for anything
the queue quotes into a comment (gh errors): no `read at`, no lens field (`lens:`, `lens=`, `lens —`, `lens –`),
no `safe to merge`. keep_markdown: the
queue's own advice (a command in backticks, a `<sha>` placeholder) stays as written when, read without its markdown,
it holds none of these; otherwise it is cleaned like any other text."""
queue's own advice (a command in backticks, a `<sha>` placeholder) stays as written when, read without its markdown
either way (clean_inline narrow or not), it holds none of these; otherwise it is cleaned like any other text."""
plain = clean_inline(text)
out = re.sub(r'\bread([\s-]*)at\b', r'read\1@', plain, flags=re.I)
if keep_markdown and _no_lens_forms(plain) == plain: # the reader cleans the text as written once, either way
narrow = clean_inline(text, narrow=True)
if _no_lens_forms(narrow) == narrow:
return text
# Cleaned text is cleaned AGAIN by the reader, and nested markup (`lens<<a>b>=`, `lens[[](z)](w)=`) left by one
# pass would turn into a field on the second (issue #303). So the `<` `>` `[` `]` one pass left become blanks: with
# none of them, another pass (either way) changes nothing.
return _no_lens_forms(re.sub(r'[<>\[\]]', ' ', plain))


def _no_lens_forms(text):
"""The text with every lens form no_lens_text names rewritten."""
out = re.sub(r'\bread([\s-]*)at\b', r'read\1@', text, flags=re.I)
out = re.sub(r'\bsafe(\s+)to(\s+)merge\b', r'safe\1to\2land', out, flags=re.I)
out = re.sub(r'\blens(\s*)[:=—–]', r'lens\1-', out, flags=re.I) # every field form loose_header reads
return text if keep_markdown and out == plain else out
return re.sub(r'\blens(\s*)[:=—–]', r'lens\1-', out, flags=re.I) # every field form loose_header reads


def check_state(c):
Expand Down Expand Up @@ -355,9 +379,11 @@ def norm(line):
first = next((line for line in lines if line.strip()), '')
normed, start = [norm(line) for line in lines], f'starts with {first.strip()[:60]!r}'
at = next((i for i, line in enumerate(lines) if line.strip()), 0)
# a header line, or (after the first line) a line starting `lens:` with a sha in any form (a second lens's
# loosely written read behind a valid header). Only at the line start: `lens:` in mid-line is often a mention.
headers = sum(header_line(line) or (i > at and bool((m := LENS_RE.match(line)) and HEX_RE.search(m.group(1))))
# a header line, or (after the first line) a line starting with a lens field in a form FIELD_START_RE reads and
# a sha in any form (a second lens's loosely written read behind a valid header). Only at the line start:
# `lens:` in mid-line is often a mention.
headers = sum(header_line(line)
or (i > at and bool((m := FIELD_START_RE.match(line)) and HEX_RE.search(m.group(1))))
for i, line in enumerate(normed))
if headers > 1:
return f'{EMBEDDED} {headers} times (post one comment per lens), {start}'
Expand Down
91 changes: 90 additions & 1 deletion fork-ci/actions/pr-queue/lens.test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
committed, with the source's override prefix set to `proj ready --head`. Where the port differs from the source on
purpose, the replay says so (Corpus).
"""
import copy, json, os, re, sys, unittest
import copy, json, os, random, re, sys, unittest

sys.dont_write_bytecode = True # no __pycache__ next to the scripts

Expand Down Expand Up @@ -98,6 +98,28 @@ def test_a_hex_word_without_a_digit_is_no_sha(self):
self.assertIsNone(SOURCE.lens_problem(post('Notes\n```\nlens: effaced\n```')))
self.assertIn(lens.EMBEDDED, SOURCE.lens_problem(post('Notes\n```\nlens: effaced1\n```')))

def test_a_second_header_in_any_field_form_below_a_valid_one_counts(self): # issue #303 point 3
first, hex = f'lens: tests · reviewer: x · read at: {SHA_B}\n\n', NotALens.HEX
for second in (f'Lens — security · read at: {hex}', f'lens= security {hex[:10]}',
f'**Lens** – security ({hex[:7]})', f'- lens=security read at {hex}',
f'lens: security reviewed at {hex[:10]}'):
with self.subTest(second=second):
self.assertIn('2 times', SOURCE.lens_problem(post(first + second)))
self.assertEqual(SOURCE.reviewed_head(pr(post(first + second)))[:2], (True, None))
for note in ('Lens — security: nothing found', 'the security lens — see above', f'see lens= {hex} later',
f" LENS = 'lens: correctness · reviewer: a · read at: {hex[:7]}'"): # test code in a report
with self.subTest(note=note): # no sha, or the field is not at the line start: still one valid read
self.assertIsNone(SOURCE.lens_problem(post(first + note)))

def test_plain_lines_follow_githubs_fences(self): # issue #303 point 3: the same fence rule as unquoted_lines
self.assertEqual(lens.plain_lines('a\n```\nx\n```js\nb\n```\nc'), ['a', 'c']) # ```js is code, not a close
self.assertEqual(lens.plain_lines('a\n```x```\nb'), ['a', '```x```', 'b']) # inline code, no fence
self.assertEqual(lens.plain_lines('a\n~~~ x ~~~\nb\n~~~\nc'), ['a', 'c']) # a tilde info string may hold one
self.assertEqual(lens.plain_lines('a\n````\n```\nb\n````\nc'), ['a', 'c'])
self.assertEqual(lens.plain_lines('a\n```\nb'), ['a']) # unclosed: to the end
# the rendered `safe to merge` line below the inner fence is plain text, and blocks
self.assertIsNotNone(SOURCE.lens_problem(post('Notes\n```\nx\n```py\ny\n```\nsafe to merge: no\n```')))


class NotALens(unittest.TestCase):
"""Issue #77: below a `not a lens:` first line, a lens read written another way (no colon after `lens`, another
Expand Down Expand Up @@ -154,6 +176,65 @@ def test_the_queues_quote_of_any_lens_field_never_reads_as_a_lens_line(self):
self.assertFalse(lens.loose_header(lens.norm_line(quoted)), quoted)
self.assertIsNone(self.problem(f'Auto-merge OFF: {quoted}'), quoted)

def test_the_queues_quote_of_nested_markup_never_reads_as_a_lens_line(self): # issue #303 point 1
for field in (':', '=', '—', '–'):
for line in (f'lens<<a>b>{field} sec {self.HEX[:10]} NOT SAFE', f'lens[[](z)](w){field} sec {self.HEX}',
f'lens<<<a>b>c>{field} sec {self.HEX[:10]}', f'lens[[[](z)](y)](w){field} {self.HEX}',
f'read<<a>b> at {self.HEX}', f'safe<<a>b> to merge: no ({self.HEX[:10]})'):
with self.subTest(line=line):
quoted = lens.no_lens_text(line)
for narrow in (False, True):
self.assertFalse(lens.loose_header(lens.norm_line(quoted, narrow)), quoted)
self.assertIsNone(self.problem(f'pr_queue turned auto-merge OFF: {quoted}'), quoted)

def test_the_queues_quote_of_any_text_reads_the_same_when_cleaned_again(self): # issue #303 point 1
rnd = random.Random(303)
parts = ['lens', 'read', ' at', 'safe', ' to ', 'merge', ':', '=', '—', '–', '<', '>', '<a>', '<b ', '[', ']',
'](x)', '(', ')', '!', '*', '`', '_', '|', ' ', '\u00a0', 'x', self.HEX[:10], '#']
for _ in range(20000):
text = ''.join(rnd.choice(parts) for _ in range(rnd.randrange(1, 16)))
quoted = lens.no_lens_text(text)
with self.subTest(text=text):
for narrow in (False, True):
self.assertEqual(lens.clean_inline(quoted, narrow), quoted)
self.assertIsNone(self.problem(f'pr_queue turned auto-merge OFF: {quoted}'))

def test_a_note_names_a_sha_on_its_own_line_not_after_a_lens_field(self): # issue #303 point 2: the README rule
self.assertIn(lens.EMBEDDED, self.problem(f'not a lens: round 2\nthe security lens — fixed in {self.HEX[:8]}'))
quoted = f'not a lens: round 2\n> the security lens — fixed in {self.HEX[:8]}' # a quote does not help
self.assertIn(lens.EMBEDDED, self.problem(quoted))
self.assertIsNone(self.problem(f'not a lens: round 2\nthe security lens — fixed in:\n{self.HEX[:8]}'))
self.assertIsNone(self.problem(f'not a lens: round 2\nthe security lens: fixed.\n> read at {self.HEX}'))

def test_a_quote_marker_after_a_tab_is_code_not_a_quote(self): # #283 bypass M9: `\s*>` would clear it
self.assertIsNotNone(self.problem(f'not a lens: notes\n\n\t> read at {self.HEX}'))

def test_only_a_fence_of_the_same_kind_and_length_closes_one(self): # #283 bypass M10: any fence line closed
for body in (f'not a lens: notes\n~~~\n```\n> read at {self.HEX}\n~~~',
f'not a lens: notes\n````\n```\n> read at {self.HEX}\n````',
f'not a lens: notes\n```\n~~~\n> read at {self.HEX}\n```'):
with self.subTest(body=body):
self.assertIn(lens.OPT_OUT_READ, self.problem(body))

def test_a_fence_line_with_an_info_string_does_not_close_a_fence(self): # issue #303 point 3, GFM
for close in ('``` y', '```js', '~~~ y'):
body = (f'not a lens: r2\n{close[:3]}\nx\n{close}\n> correctness lens NOT SAFE read at {self.HEX}\n'
f'{close[:3]}')
with self.subTest(close=close):
self.assertIn(lens.OPT_OUT_READ, self.problem(body))
self.assertIsNone(self.problem(f'not a lens: r2\n```\nx\n``` \n> read at {self.HEX}')) # blanks only: closes

def test_a_longer_closing_fence_closes_one_and_only_blanks_may_follow_it(self): # GFM; #591 lens mutants
self.assertIsNone(self.problem(f'not a lens: r2\n```\nx\n````\n> read at {self.HEX}'))
# a no-break space is no space or tab: the fence stays open, the quote is code
self.assertIn(lens.OPT_OUT_READ, self.problem(f'not a lens: r2\n```\nx\n```\u00a0\n> read at {self.HEX}'))

def test_a_fence_opens_and_closes_only_after_at_most_3_spaces(self): # GFM; #591 lens mutants
self.assertIn(lens.OPT_OUT_READ, self.problem(f'not a lens: r2\n ```\n> read at {self.HEX}'))
self.assertIsNone(self.problem(f'not a lens: r2\n ```\n\n> read at {self.HEX}')) # indented code, no fence
self.assertIn(lens.OPT_OUT_READ,
self.problem(f'not a lens: r2\n```\nx\n ```\n> read at {self.HEX}\n```'))

def test_the_way_out_names_the_quote_and_never_reads_as_a_report(self):
self.assertIn('`>`', lens.NO_HEADER_WAY_OUT)
self.assertEqual(lens.no_lens_text(lens.NO_HEADER_WAY_OUT, keep_markdown=True), lens.NO_HEADER_WAY_OUT)
Expand Down Expand Up @@ -222,6 +303,14 @@ def test_advice_that_holds_a_lens_word_is_cleaned_like_any_text(self): # also
for text, out in (('`lens: x`', 'lens- x'), ('re`ad at`: x', 'read @: x'), ('**safe to merge**', 'safe to land')):
self.assertEqual(lens.no_lens_text(text, keep_markdown=True), out)

def test_advice_whose_narrow_reading_holds_a_lens_word_is_cleaned(self): # issue #303: the reader reads both ways
text = '<lens= x 0123abcd99 <q></b>' # one tag read the wide way; `<lens= x 0123abcd99` read the narrow way
self.assertEqual(lens.clean_inline(text), '')
out = lens.no_lens_text(text, keep_markdown=True)
self.assertNotEqual(out, text)
self.assertIsNone(SOURCE.lens_problem(post(f'pr_queue turned auto-merge OFF: {out}')))
self.assertIsNotNone(SOURCE.lens_problem(post(f'pr_queue turned auto-merge OFF: {text}')))


class ProjectRules(unittest.TestCase):
"""What the config changes: the override comment and the bot logins."""
Expand Down
4 changes: 3 additions & 1 deletion fork-ci/actions/pr-queue/pr_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,9 @@ def leave_off(self, n, why, advice=''):
text) is cleaned of them and of markdown; `advice`, the queue's own text from the config (a command in
backticks, a `<sha>` placeholder), keeps its markdown unless it holds one (lens.no_lens_text). 0, or SILENT when
the comment failed (the caller keeps STATE, so the next run says it)."""
why = lens.no_lens_text(why) + (' ' + lens.no_lens_text(advice, keep_markdown=True) if advice else '')
# One line: a multi-line gh error could start a line with `covered:`, `checks:` or a heading naming a lens.
why = lens.no_lens_text(' '.join(why.split()))
why += ' ' + lens.no_lens_text(advice, keep_markdown=True) if advice else ''
print(f'#{n}: auto-merge left OFF: {why}')
r = self.gh_write('pr', 'comment', str(n), '--repo', self.repo, '--body',
f'pr_queue turned auto-merge OFF: {why}\nNeeds a lens read of any new commits, then '
Expand Down
9 changes: 9 additions & 0 deletions fork-ci/actions/pr-queue/pr_queue.test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Tests of pr_queue.py (run: python3 fork-ci/actions/pr-queue/pr_queue.test.py).

The queue turns auto-merge OFF before it updates a PR from its base and turns it back on, for the new head only, when
Expand Down Expand Up @@ -610,6 +610,15 @@
self.assertIn('back on', f.comments[0])
self.assertNotRegex(f.comments[0], r'(?i)read at\s*:', 'the quoted gh error had one')

def test_a_multi_line_gh_error_is_quoted_on_one_line(self): # no `covered:`/`checks:` line or lens heading
f = self.fake()
self.q.gh = self.q.gh_write = f
self.assertEqual(run_quiet(self.q.leave_off, 7, 'gh failed\ncovered: x\nchecks: y\n# security lens')[0], 0)
why, needs = f.comments[0].split('\n')
self.assertTrue(why.startswith('pr_queue turned auto-merge OFF: gh failed covered'), why)
self.assertIn('checks', why)
self.assertTrue(needs.startswith('Needs a lens read'), needs)

def test_a_failed_comment_keeps_the_state(self): # still OFF and silent: the run fails, the next run says it
f = self.fake('resolved', fail=('comment',))
self.assertEqual(self.update(f)[0], Q.SILENT)
Expand Down
Loading