Skip to content

Commit

Permalink
hooks: add .arcconfig change blocking hook for conduit-testing re…
Browse files Browse the repository at this point in the history
…pos (Bug 1789686) r=grammar,zeid

Adds a hook to check that changes to `.arcconfig` files in
`conduit-testing` repos do not revert the file from pointing
to the dev Phabricator server.

Differential Revision: https://phabricator.services.mozilla.com/D157342

--HG--
extra : moz-landing-system : lando
  • Loading branch information
cgsheeh committed Sep 14, 2022
1 parent 380f4e2 commit ab11204
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 0 deletions.
65 changes: 65 additions & 0 deletions hghooks/mozhghooks/check/prevent_conduit_arcconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from __future__ import absolute_import

import json

from mercurial import error

from ..checks import (
PreTxnChangegroupCheck,
print_banner,
)


REJECT_MESSAGE = b"""
Push contains unwanted changes to `.arcconfig` files.
Please ensure `.arcconfig` points to the Phab dev server for
`conduit-testing` repos.
"""

ARCCONFIG_PATH = b".arcconfig"

DEV_PHAB_URL = "https://phabricator-dev.allizom.org/"


class PreventConduitArcconfig(PreTxnChangegroupCheck):
"""Prevent `.arcconfig` updates for `conduit-testing` repos."""

@property
def name(self):
return b"prevent_conduit_arcconfig"

def relevant(self) -> bool:
return b"conduit-testing" in self.repo.root.split(b"/")

def pre(self, _node):
self.latest_arcconfig_contents = ""

def check(self, ctx) -> bool:
if ARCCONFIG_PATH in ctx.files():
fctx = ctx[ARCCONFIG_PATH]
self.latest_arcconfig_contents = fctx.data().decode("utf-8")

return True

def post_check(self) -> bool:
if not self.latest_arcconfig_contents:
# No updates to `.arcconfig`.
return True

try:
arcconfig = json.loads(self.latest_arcconfig_contents)
except json.JSONDecodeError as exc:
raise error.Abort(b"Could not decode `.arcconfig` to JSON.") from exc

if arcconfig["phabricator.uri"].startswith(DEV_PHAB_URL):
# Latest state of `.arcconfig` maintains dev URL.
return True

# `.arcconfig` has been updated to point to the wrong server (prod).
print_banner(self.ui, b"error", REJECT_MESSAGE)
return False

2 changes: 2 additions & 0 deletions hghooks/mozhghooks/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def get_check_classes(hook):
merge_day,
prevent_cross_channel_messages,
check_bug_references,
prevent_conduit_arcconfig,
prevent_subrepos,
prevent_symlinks,
prevent_sync_ipc_changes,
Expand All @@ -67,6 +68,7 @@ def get_check_classes(hook):
if hook == b'pretxnchangegroup':
return (
merge_day.MergeDayCheck,
prevent_conduit_arcconfig.PreventConduitArcconfig,
prevent_cross_channel_messages.XChannelMessageCheck,
prevent_subrepos.PreventSubReposCheck,
prevent_symlinks.PreventSymlinksCheck,
Expand Down
70 changes: 70 additions & 0 deletions hghooks/tests/test-prevent-arcconfig.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
$ . $TESTDIR/hghooks/tests/common.sh

`conduit-testing` repos should only allow the dev phab server in `.arcconfig`.

$ hg init conduit-testing
$ configurehooks conduit-testing
$ hg -q clone conduit-testing client
$ cd client
$ echo '{"phabricator.uri":"https://phabricator-dev.allizom.org/"}' > .arcconfig
$ hg ci -q -A -m "add .arcconfig"
$ hg push
pushing to $TESTTMP/conduit-testing
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files

$ echo '{"phabricator.uri":"https://phabricator.services.mozilla.com/"}' > .arcconfig
$ hg ci -q -A -m "update .arcconfig to bad value"
$ hg push
pushing to $TESTTMP/conduit-testing
searching for changes
adding changesets
adding manifests
adding file changes

************************** ERROR ***************************
Push contains unwanted changes to `.arcconfig` files.

Please ensure `.arcconfig` points to the Phab dev server for
`conduit-testing` repos.
************************************************************

transaction abort!
rollback completed
abort: pretxnchangegroup.mozhooks hook failed
[255]

Pushes that update `.arcconfig` and then revert it should be allowed.

$ echo '{"phabricator.uri":"https://phabricator-dev.allizom.org/"}' > .arcconfig
$ hg ci -q -A -m "revert .arcconfig to correct value"
$ hg log -G
@ changeset: 2:743d41d3d1de
| tag: tip
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: revert .arcconfig to correct value
|
o changeset: 1:dcd947bcda7b
| user: test
| date: Thu Jan 01 00:00:00 1970 +0000
| summary: update .arcconfig to bad value
|
o changeset: 0:58f2675adc14
user: test
date: Thu Jan 01 00:00:00 1970 +0000
summary: add .arcconfig

$ hg push
pushing to $TESTTMP/conduit-testing
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 2 changes to 1 files

$ cd ..

0 comments on commit ab11204

Please sign in to comment.