2019-08-23 21:16:30 +07:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-09-18 20:19:13 +07:00
|
|
|
import contextlib
|
2019-08-23 21:16:30 +07:00
|
|
|
import itertools
|
2019-09-11 20:04:19 +07:00
|
|
|
import re
|
2024-01-16 21:03:45 +07:00
|
|
|
import time
|
2024-10-07 13:06:03 +07:00
|
|
|
import typing
|
2019-08-23 21:16:30 +07:00
|
|
|
|
2021-07-23 20:45:23 +07:00
|
|
|
from lxml import html
|
|
|
|
|
2019-08-23 21:16:30 +07:00
|
|
|
MESSAGE_TEMPLATE = """{message}
|
|
|
|
|
|
|
|
closes {repo}#{number}
|
|
|
|
|
2024-01-16 16:51:37 +07:00
|
|
|
{headers}Signed-off-by: {name} <{email}>"""
|
2025-01-30 15:08:35 +07:00
|
|
|
# target branch '-' source branch '-' batch id '-fw'
|
|
|
|
REF_PATTERN = r'{target}-{source}-\d+-fw'
|
2019-08-23 21:16:30 +07:00
|
|
|
|
|
|
|
class Commit:
|
2019-09-11 20:04:19 +07:00
|
|
|
def __init__(self, message, *, author=None, committer=None, tree, reset=False):
|
2019-08-23 21:16:30 +07:00
|
|
|
self.id = None
|
|
|
|
self.message = message
|
|
|
|
self.author = author
|
|
|
|
self.committer = committer
|
|
|
|
self.tree = tree
|
2019-09-11 20:04:19 +07:00
|
|
|
self.reset = reset
|
2019-08-23 21:16:30 +07:00
|
|
|
|
2025-02-06 20:33:40 +07:00
|
|
|
def validate_all(repos, refs, contexts=('default',)):
|
2019-08-23 21:16:30 +07:00
|
|
|
""" Post a "success" status for each context on each ref of each repo
|
|
|
|
"""
|
|
|
|
for repo, branch, context in itertools.product(repos, refs, contexts):
|
|
|
|
repo.post_status(branch, 'success', context)
|
2019-09-11 20:04:19 +07:00
|
|
|
|
2020-11-17 21:21:21 +07:00
|
|
|
def get_partner(env, gh_login):
|
|
|
|
return env['res.partner'].search([('github_login', '=', gh_login)])
|
|
|
|
|
|
|
|
def _simple_init(repo):
|
|
|
|
""" Creates a very simple initialisation: a master branch with a commit,
|
|
|
|
and a PR by 'user' with two commits, targeted to the master branch
|
|
|
|
"""
|
|
|
|
m = repo.make_commit(None, 'initial', None, tree={'m': 'm'})
|
|
|
|
repo.make_ref('heads/master', m)
|
|
|
|
c1 = repo.make_commit(m, 'first', None, tree={'m': 'c1'})
|
|
|
|
c2 = repo.make_commit(c1, 'second', None, tree={'m': 'c2'})
|
|
|
|
prx = repo.make_pr(title='title', body='body', target='master', head=c2)
|
|
|
|
return prx
|
|
|
|
|
2024-06-04 17:15:29 +07:00
|
|
|
class matches(str):
|
|
|
|
# necessary so str.__new__ does not freak out on `flags`
|
|
|
|
def __new__(cls, pattern, flags=0):
|
|
|
|
return super().__new__(cls, pattern)
|
|
|
|
|
2019-09-11 20:04:19 +07:00
|
|
|
def __init__(self, pattern, flags=0):
|
2024-06-04 17:15:29 +07:00
|
|
|
p, n = re.subn(
|
|
|
|
# `re.escape` will escape the `$`, so we need to handle that...
|
|
|
|
# maybe it should not be $?
|
|
|
|
r'\\\$(\w*?)\\\$',
|
|
|
|
lambda m: f'(?P<{m[1]}>.*?)' if m[1] else '(.*?)',
|
|
|
|
re.escape(self),
|
|
|
|
)
|
|
|
|
assert n, f"matches' pattern should have at least one placeholder, found none in\n{pattern}"
|
|
|
|
self._r = re.compile(p, flags | re.DOTALL)
|
2019-09-11 20:04:19 +07:00
|
|
|
|
|
|
|
def __eq__(self, text):
|
2024-06-04 17:15:29 +07:00
|
|
|
if not isinstance(text, str):
|
|
|
|
return NotImplemented
|
|
|
|
return self._r.search(text)
|
2019-09-30 20:18:44 +07:00
|
|
|
|
2020-11-17 21:21:21 +07:00
|
|
|
def seen(env, pr, users):
|
2024-03-05 18:59:58 +07:00
|
|
|
url = to_pr(env, pr).url
|
|
|
|
return users['user'], f'[]({url})'
|
2019-09-30 20:18:44 +07:00
|
|
|
|
2024-02-26 15:51:22 +07:00
|
|
|
def make_basic(
|
|
|
|
env,
|
|
|
|
config,
|
|
|
|
make_repo,
|
|
|
|
*,
|
|
|
|
project_name='myproject',
|
|
|
|
reponame='proj',
|
2025-02-06 20:33:40 +07:00
|
|
|
statuses,
|
2024-02-26 15:51:22 +07:00
|
|
|
fp_token=True,
|
|
|
|
fp_remote=True,
|
|
|
|
):
|
|
|
|
""" Creates a project ``project_name`` **if none exists**, otherwise
|
|
|
|
retrieves the existing one and adds a new repository and its fork.
|
|
|
|
|
|
|
|
Repositories are setup with three forking branches:
|
|
|
|
|
|
|
|
::
|
|
|
|
|
|
|
|
f = 0 -- 1 -- 2 -- 3 -- 4 : a
|
|
|
|
|
|
|
|
|
g = `-- 11 -- 22 : b
|
|
|
|
|
|
|
|
|
h = `-- 111 : c
|
|
|
|
|
2019-09-30 20:18:44 +07:00
|
|
|
each branch just adds and modifies a file (resp. f, g and h) through the
|
|
|
|
contents sequence a b c d e
|
2024-02-26 15:51:22 +07:00
|
|
|
|
|
|
|
:param env: Environment, for odoo model interactions
|
|
|
|
:param config: pytest project config thingie
|
|
|
|
:param make_repo: repo maker function, normally the fixture, should be a
|
|
|
|
``Callable[[str], Repo]``
|
|
|
|
:param project_name: internal project name, can be used to recover the
|
|
|
|
project object afterward, matches exactly since it's
|
|
|
|
unique per odoo db (and thus test)
|
|
|
|
:param reponame: the base name of the repository, for identification, for
|
|
|
|
concurrency reasons the actual repository name *will* be
|
|
|
|
different
|
|
|
|
:param statuses: required statuses for the repository, stupidly default to
|
|
|
|
the old Odoo statuses, should be moved to ``default`` over
|
|
|
|
time for simplicity (unless the test specifically calls for
|
|
|
|
multiple statuses)
|
|
|
|
:param fp_token: whether to set the ``fp_github_token`` on the project if
|
|
|
|
/ when creating it
|
|
|
|
:param fp_remote: whether to create a fork repo and set it as the
|
|
|
|
repository's ``fp_remote_target``
|
2019-09-30 20:18:44 +07:00
|
|
|
"""
|
|
|
|
Projects = env['runbot_merge.project']
|
|
|
|
project = Projects.search([('name', '=', project_name)])
|
|
|
|
if not project:
|
|
|
|
project = env['runbot_merge.project'].create({
|
|
|
|
'name': project_name,
|
|
|
|
'github_token': config['github']['token'],
|
|
|
|
'github_prefix': 'hansen',
|
2024-11-29 15:04:06 +07:00
|
|
|
'github_name': config['github']['name'],
|
|
|
|
'github_email': "foo@example.org",
|
2024-02-26 15:51:22 +07:00
|
|
|
'fp_github_token': fp_token and config['github']['token'],
|
2023-06-12 19:41:42 +07:00
|
|
|
'fp_github_name': 'herbert',
|
2019-09-30 20:18:44 +07:00
|
|
|
'branch_ids': [
|
2023-06-08 13:19:43 +07:00
|
|
|
(0, 0, {'name': 'a', 'sequence': 100}),
|
|
|
|
(0, 0, {'name': 'b', 'sequence': 80}),
|
|
|
|
(0, 0, {'name': 'c', 'sequence': 60}),
|
2019-09-30 20:18:44 +07:00
|
|
|
],
|
|
|
|
})
|
|
|
|
|
|
|
|
prod = make_repo(reponame)
|
[ADD] *: per-repository webhook secret
Currently webhook secrets are configured per *project* which is an
issue both because different repositories may have different
administrators and thus creates safety concerns, and because multiple
repositories can feed into different projects (e.g. on mergebot,
odoo-dev/odoo is both an ancillary repository to the main RD project,
and the main repository to the minor / legacy master-wowl
project). This means it can be necessary to have multiple projects
share the same secret as well, this then mandates the secret for more
repositories per (1).
This is a pain in the ass, so just detach secrets from projects and
link them *only* to repositories, it's cleaner and easier to manage
and set up progressively.
This requires a lot of changes to the tests, as they all need to
correctly configure the signaling.
For `runbot_merge` there was *some* setup sharing already via the
module-level `repo` fixtures`, those were merged into a conftest-level
fixture which could handle the signaling setup. A few tests which
unnecessarily set up repositories ad-hoc were also moved to the
fixture. But for most of the ad-hoc setup in `runbot_merge`, as well
as `forwardport` where it's all ad-hoc, events sources setup was just
appended as is. This should probably be cleaned up at one point, with
the various requirements collected and organised into a small set of
fixtures doing the job more uniformly.
Fixes #887
2024-06-06 16:07:57 +07:00
|
|
|
env['runbot_merge.events_sources'].create({'repository': prod.name})
|
2019-09-30 20:18:44 +07:00
|
|
|
with prod:
|
2024-10-22 15:51:30 +07:00
|
|
|
_0, _1, a_2, _3, _4, = prod.make_commits(
|
2019-09-30 20:18:44 +07:00
|
|
|
None,
|
|
|
|
Commit("0", tree={'f': 'a'}),
|
|
|
|
Commit("1", tree={'f': 'b'}),
|
|
|
|
Commit("2", tree={'f': 'c'}),
|
|
|
|
Commit("3", tree={'f': 'd'}),
|
|
|
|
Commit("4", tree={'f': 'e'}),
|
|
|
|
ref='heads/a',
|
|
|
|
)
|
2024-10-22 15:51:30 +07:00
|
|
|
b_1, _2 = prod.make_commits(
|
2019-09-30 20:18:44 +07:00
|
|
|
a_2,
|
|
|
|
Commit('11', tree={'g': 'a'}),
|
|
|
|
Commit('22', tree={'g': 'b'}),
|
|
|
|
ref='heads/b',
|
|
|
|
)
|
|
|
|
prod.make_commits(
|
|
|
|
b_1,
|
|
|
|
Commit('111', tree={'h': 'a'}),
|
|
|
|
ref='heads/c',
|
|
|
|
)
|
2024-02-26 15:51:22 +07:00
|
|
|
other = prod.fork() if fp_remote else None
|
2020-02-10 21:05:08 +07:00
|
|
|
repo = env['runbot_merge.repository'].create({
|
|
|
|
'project_id': project.id,
|
|
|
|
'name': prod.name,
|
2024-02-26 15:51:22 +07:00
|
|
|
'required_statuses': statuses,
|
|
|
|
'fp_remote_target': other.name if other else False,
|
2024-06-12 16:16:14 +07:00
|
|
|
'group_id': False,
|
2020-02-10 21:05:08 +07:00
|
|
|
})
|
|
|
|
env['res.partner'].search([
|
|
|
|
('github_login', '=', config['role_reviewer']['user'])
|
|
|
|
]).write({
|
|
|
|
'review_rights': [(0, 0, {'repository_id': repo.id, 'review': True})]
|
|
|
|
})
|
|
|
|
env['res.partner'].search([
|
|
|
|
('github_login', '=', config['role_self_reviewer']['user'])
|
|
|
|
]).write({
|
|
|
|
'review_rights': [(0, 0, {'repository_id': repo.id, 'self_review': True})]
|
2019-09-30 20:18:44 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
return prod, other
|
2021-07-23 20:45:23 +07:00
|
|
|
|
|
|
|
def pr_page(page, pr):
|
|
|
|
return html.fromstring(page(f'/{pr.repo.name}/pull/{pr.number}'))
|
|
|
|
|
2024-12-02 20:17:28 +07:00
|
|
|
def to_pr(env, pr, *, attempts=5):
|
|
|
|
for _ in range(attempts):
|
2024-01-16 21:03:45 +07:00
|
|
|
pr_id = env['runbot_merge.pull_requests'].search([
|
|
|
|
('repository.name', '=', pr.repo.name),
|
|
|
|
('number', '=', pr.number),
|
|
|
|
])
|
|
|
|
if pr_id:
|
|
|
|
assert len(pr_id) == 1, f"Expected to find {pr.repo.name}#{pr.number}, got {pr_id}."
|
|
|
|
return pr_id
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
raise TimeoutError(f"Unable to find {pr.repo.name}#{pr.number}")
|
2021-08-09 12:55:38 +07:00
|
|
|
|
|
|
|
def part_of(label, pr_id, *, separator='\n\n'):
|
2024-01-16 16:51:37 +07:00
|
|
|
""" Adds the "part-of" pseudo-header in the footer.
|
|
|
|
"""
|
2024-05-30 15:55:40 +07:00
|
|
|
return f"""\
|
|
|
|
{label}{separator}\
|
|
|
|
Part-of: {pr_id.display_name}
|
|
|
|
Signed-off-by: {pr_id.reviewed_by.formatted_email}"""
|
[CHG] *: rewrite commands set, rework status management
This commit revisits the commands set in order to make it more
regular, and limit inconsistent command-sets, although it includes
pseudo-command aliases for common tasks now removed from the core set.
Hard Errors
===========
The previous iteration of the commands set would ignore any
non-command term in a command line. This has been changed to hard
error (and ignoring the entire thing) if any command is unknown or
invalid.
This fixes inconsistent / unexpected interpretations where a user
sends a command, then writes a novel on the same line some words of
which happen to *also* be commands, leading to merge states they did
not expect. They should now be told to fuck off.
Priority Restructuring
----------------------
The numerical priority system was pretty messy in that it confused
"staging priority" (in ways which were not entirely straightforward)
with overrides to other concerns.
This has now being split along all the axis, with separate command
subsets for:
- staging prioritisation, now separated between `default`, `priority`,
and `alone`,
- `default` means PRs are picked by an unspecified order when
creating a staging, if nothing better is available
- `priority` means PRs are picked first when staging, however if
`priority` PRs don't fill the staging the rest will be filled with
`default`, this mode did not previously exist
- `alone` means the PRs are picked first, before splits, and only
`alone` PRs can be part of the staging (which usually matches the
modename)
- `skipchecks` overrides both statuses and approval checks, for the
batch, something previously implied in `p=0`, but now
independent. Setting `skipchecks` basically makes the entire batch
`ready`.
For consistency this also sets the reviewer implicitly: since
skipchecks overrides both statuses *and approval*, whoever enables
this mode is essentially the reviewer.
- `cancel` cancels any ongoing staging when the marked PR becomes
ready again, previously this was also implied (in a more restricted
form) by setting `p=0`
FWBot removal
=============
While the "forwardport bot" still exists as an API level (to segregate
access rights between tokens) it has been removed as an interaction
point, as part of the modules merge plan. As a result,
fwbot stops responding
----------------------
Feedback messages are now always sent by the mergebot, the
forward-porting bot should not send any message or notification
anymore.
commands moved to the merge bot
-------------------------------
- `ignore`/`up to` simply changes bot
- `close` as well
- `skipci` is now a choice / flag of an `fw` command, which denotes
the forward-port policy,
- `fw=default` is the old `ci` and resets the policy to default,
that is wait for the PR to be merged to create forward ports, and
for the required statuses on each forward port to be received
before creating the next
- `fw=skipci` is the old `skipci`, it waits for the merge of the
base PR but then creates all the forward ports immediately (unless
it gets a conflict)
- `fw=skipmerge` immediately creates all the forward ports, without
even waiting for the PR to be merged
This is a completely new mode, and may be rather broken as until
now the 'bot has always assumed the source PR had been merged.
approval rework
---------------
Because of the previous section, there is no distinguishing feature
between `mergebot r+` = "merge this PR" and `forwardbot r+` = "merge
this PR and all its parent with different access rights".
As a result, the two have been merged under a single `mergebot r+`
with heuristics attempting to provide the best experience:
- if approving a non-forward port, the behavior does not change
- else, with review rights on the source, all ancestors are approved
- else, as author of the original, approves all ancestors which descend
from a merged PR
- else, approves all ancestors up to and including the oldest ancestor
to which we have review rights
Most notably, the source's author is not delegated on the source or
any of its descendants anymore. This might need to be revisited if it
provides too restrictive.
For the very specialized need of approving a forward-port *and none of
its ancestors*, `review=` can now take a comma (`,`) separated list of
pull request numbers (github numbers, not mergebot ids).
Computed State
==============
The `state` field of pull requests is now computed. Hopefully this
makes the status more consistent and predictable in the long run, and
importantly makes status management more reliable (because reference
datum get updated naturally flowing to the state).
For now however it makes things more complicated as some of the states
have to be separately signaled or updated:
- `closed` and `error` are now separate flags
- `merge_date` is pulled down from forwardport and becomes the
transition signal for ready -> merged
- `reviewed_by` becomes the transition signal for approval (might be a
good idea to rename it...)
- `status` is computed from the head's statuses and overrides, and
*that* becomes the validation state
Ideally, batch-level flags like `skipchecks` should be on, well, the
batch, and `state` should have a dependency on the batch. However
currently the batch is not a durable / permanent member of the system,
so it's a PR-level flag and a messy pile.
On notable change is that *forcing* the state to `ready` now does that
but also sets the reviewer, `skipchecks`, and overrides to ensure the
API-mediated readying does not get rolled back by e.g. the runbot
sending a status.
This is useful for a few types of automated / programmatic PRs
e.g. translation exports, where we set the state programmatically to
limit noise.
recursive dependency hack
-------------------------
Given a sequence of PRs with an override of the source, if one of the
PRs is updated its descendants should not have the override
anymore. However if the updated PR gets overridden, its descendants
should have *that* override.
This requires some unholy manipulations via an override of `modified`,
as the ORM supports recursive fields but not recursive
dependencies (on a different field).
unconditional followup scheduling
---------------------------------
Previously scheduling forward-port followup was contigent on the FW
policy, but it's not actually correct if the new PR is *immediately*
validated (which can happen now that the field is computed, if there
are no required statuses *or* all of the required statuses are
overridden by an ancestor) as nothing will trigger the state change
and thus scheduling of the fp followup.
The followup function checks all the properties of the batch to port,
so this should not result on incorrect ports. Although it's a bit more
expensive, and will lead to more spam.
Previously this would not happen because on creation of a PR the
validation task (commit -> PR) would still have to execute.
Misc Changes
============
- If a PR is marked as overriding / canceling stagings, it now does
so on retry not just when setting initially.
This was not handled at all previously, so a PR in P0 going into
error due to e.g. a non-deterministic bug would be retried and still
p=0, but a current staging would not get cancelled. Same when a PR
in p=0 goes into error because something was failed, then is updated
with a fix.
- Add tracking to a bunch of relevant PR fields.
Post-mortem analysis currently generally requires going through the
text logs to see what happened, which is annoying.
There is a nondeterminism / inconsistency in the tracking which
sometimes leads the admin user to trigger tracking before the bot
does, leading to the staging tracking being attributed to them
during tests, shove under the carpet by ignoring the user to whom
that tracking is attributed.
When multiple users update tracked fields in the same transaction
all the changes are attributed to the first one having triggered
tracking (?), I couldn't find why the admin sometimes takes over.
- added and leveraged support for enum-backed selection fields
- moved variuous fields from forwardport to runbot_merge
- fix a migration which had never worked and which never run (because
I forgot to bump the version on the module)
- remove some unnecessary intermediate de/serialisation
fixes #673, fixes #309, fixes #792, fixes #846 (probably)
2023-10-31 13:42:07 +07:00
|
|
|
|
|
|
|
def ensure_one(records):
|
|
|
|
assert len(records) == 1
|
|
|
|
return records
|
2024-09-18 20:19:13 +07:00
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def prevent_unstaging(st) -> None:
|
|
|
|
# hack: `Stagings.cancel` only cancels *active* stagings,
|
|
|
|
# so if we disable the staging, do a thing, then re-enable
|
|
|
|
# then things should work out
|
|
|
|
assert st and st.active, "preventing unstaging of not-a-staging is not useful"
|
|
|
|
st.active = False
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
st.active = True
|
2024-10-07 13:06:03 +07:00
|
|
|
|
|
|
|
|
|
|
|
TYPE_MAPPING = {
|
|
|
|
'boolean': 'integer',
|
|
|
|
'date': 'datetime',
|
|
|
|
'monetary': 'float',
|
|
|
|
'selection': 'char',
|
|
|
|
'many2one': 'char',
|
|
|
|
}
|
|
|
|
def read_tracking_value(tv) -> tuple[str, typing.Any, typing.Any]:
|
|
|
|
field_id = tv.field_id if 'field_id' in tv else tv.field
|
|
|
|
field_type = field_id.field_type if 'field_type' in field_id else field_id.ttype
|
|
|
|
t = TYPE_MAPPING.get(field_type) or field_type
|
|
|
|
return field_id.name, tv[f"old_value_{t}"], tv[f"new_value_{t}"]
|