Skip to content

Commit ef71310

Browse files
committedJun 11, 2018
Move hgmo changeset handling into new module
Move various functions for working with hg.mozilla.org data into their own module.
1 parent 082bb98 commit ef71310

File tree

4 files changed

+87
-72
lines changed

4 files changed

+87
-72
lines changed
 

‎committelemetry/hgmo.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
"""
5+
Functions for interacting with hg.mozilla.org APIs.
6+
"""
7+
import logging
8+
from typing import List
9+
10+
from committelemetry.http import requests_retry_session
11+
from committelemetry.sentry import client as sentry
12+
13+
log = logging.getLogger(__name__)
14+
15+
16+
def changesets_for_pushid(pushid: int, push_json_url: str) -> List[str]:
17+
"""Return a list of changeset IDs in a repository push.
18+
19+
Reads data published by the Mozilla hgweb pushlog extension.
20+
21+
Also see https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#writing-agents-that-consume-pushlog-data
22+
23+
Args:
24+
pushid: The integer pushlog pushid we want information about.
25+
push_json_url: The 'push_json_url' field from a hgpush message.
26+
See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/notifications.html#changegroup-1
27+
The pushid in the URL should match the pushid argument to this
28+
function.
29+
30+
Returns:
31+
A list of changeset ID strings (40 char hex strings).
32+
"""
33+
log.info(f'processing pushid {pushid}')
34+
sentry.extra_context({'pushid': pushid})
35+
response = requests_retry_session().get(push_json_url)
36+
response.raise_for_status()
37+
38+
# See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2
39+
changesets = response.json()['pushes'][str(pushid)]['changesets']
40+
log.info(f'got {len(changesets)} changesets for pushid {pushid}')
41+
return changesets
42+
43+
44+
def fetch_changeset(changeset_id, repo_url):
45+
# Example URL: https://hg.mozilla.org/mozilla-central/json-rev/deafa2891c61
46+
response = requests_retry_session(
47+
).get(f'{repo_url}/json-rev/{changesetid}')
48+
if response.status_code == 404:
49+
raise NoSuchChangeset(
50+
f'The changeset {changesetid} does not exist in repository {repo_url}' # yapf:disable
51+
)
52+
response.raise_for_status()
53+
return response.json()
54+
55+
56+
def utc_hgwebdate(hgweb_datejson):
57+
"""Turn a (unixtime, offset) tuple back into a UTC Unix timestamp.
58+
59+
Pushlog entries are not in UTC, but a tuple of (local-unixtime, utc-offset)
60+
created by
61+
https://www.mercurial-scm.org/repo/hg/file/8b86acc7aa64/mercurial/utils/dateutil.py#l63.
62+
This function reverses the operation that created the tuple.
63+
64+
Args:
65+
hgweb_datejson: A 2-element JSON list of ints.
66+
For example: https://hg.mozilla.org/mozilla-central/json-rev/deafa2891c61
67+
See https://www.mercurial-scm.org/repo/hg/file/8b86acc7aa64/mercurial/utils/dateutil.py#l63
68+
for how this value is created.
69+
70+
Returns:
71+
The UTC Unix time (seconds since the epoch), as an int.
72+
"""
73+
assert len(hgweb_datejson) == 2
74+
timestamp, offset = hgweb_datejson
75+
return timestamp + offset
76+
77+
78+
class Error(Exception):
79+
"""Generic error class for this module."""
80+
81+
82+
class NoSuchChangeset(Error):
83+
"""Raised if the given changeset ID does not exist in the target system."""

‎committelemetry/pulse.py

+1-30
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
import socket
1111
from contextlib import closing
1212
from functools import partial
13-
from typing import List
1413

1514
from kombu import Connection, Exchange, Queue
1615

1716
from committelemetry import config
18-
from committelemetry.http import requests_retry_session
17+
from committelemetry.hgmo import changesets_for_pushid
1918
from committelemetry.telemetry import payload_for_changeset, send_ping
2019
from committelemetry.sentry import client as sentry
2120

@@ -26,34 +25,6 @@ def noop(*args, **kwargs):
2625
return None
2726

2827

29-
def changesets_for_pushid(pushid: int, push_json_url: str) -> List[str]:
30-
"""Return a list of changeset IDs in a repository push.
31-
32-
Reads data published by the Mozilla hgweb pushlog extension.
33-
34-
Also see https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#writing-agents-that-consume-pushlog-data
35-
36-
Args:
37-
pushid: The integer pushlog pushid we want information about.
38-
push_json_url: The 'push_json_url' field from a hgpush message.
39-
See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/notifications.html#changegroup-1
40-
The pushid in the URL should match the pushid argument to this
41-
function.
42-
43-
Returns:
44-
A list of changeset ID strings (40 char hex strings).
45-
"""
46-
log.info(f'processing pushid {pushid}')
47-
sentry.extra_context({'pushid': pushid})
48-
response = requests_retry_session().get(push_json_url)
49-
response.raise_for_status()
50-
51-
# See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2
52-
changesets = response.json()['pushes'][str(pushid)]['changesets']
53-
log.info(f'got {len(changesets)} changesets for pushid {pushid}')
54-
return changesets
55-
56-
5728
def process_push_message(body, message, no_send=False):
5829
"""Process a hg push message from Mozilla Pulse.
5930

‎committelemetry/telemetry.py

+1-41
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from committelemetry import config
1414
from committelemetry.classifier import determine_review_system
15-
from committelemetry.http import requests_retry_session
15+
from committelemetry.hgmo import fetch_changeset, utc_hgwebdate
1616

1717
log = logging.getLogger(__name__)
1818

@@ -49,18 +49,6 @@ def payload_for_changeset(changesetid: str, repo_url: str) -> Dict:
4949
}
5050

5151

52-
def fetch_changeset(changeset_id, repo_url):
53-
# Example URL: https://hg.mozilla.org/mozilla-central/json-rev/deafa2891c61
54-
response = requests_retry_session(
55-
).get(f'{repo_url}/json-rev/{changesetid}')
56-
if response.status_code == 404:
57-
raise NoSuchChangeset(
58-
f'The changeset {changesetid} does not exist in repository {repo_url}'
59-
)
60-
response.raise_for_status()
61-
return response.json()
62-
63-
6452
def send_ping(ping_id, payload):
6553
"""Send an event ping to the Mozilla telemetry service.
6654
@@ -87,31 +75,3 @@ def send_ping(ping_id, payload):
8775
response.raise_for_status()
8876

8977

90-
def utc_hgwebdate(hgweb_datejson):
91-
"""Turn a (unixtime, offset) tuple back into a UTC Unix timestamp.
92-
93-
Pushlog entries are not in UTC, but a tuple of (local-unixtime, utc-offset)
94-
created by
95-
https://www.mercurial-scm.org/repo/hg/file/8b86acc7aa64/mercurial/utils/dateutil.py#l63.
96-
This function reverses the operation that created the tuple.
97-
98-
Args:
99-
hgweb_datejson: A 2-element JSON list of ints.
100-
For example: https://hg.mozilla.org/mozilla-central/json-rev/deafa2891c61
101-
See https://www.mercurial-scm.org/repo/hg/file/8b86acc7aa64/mercurial/utils/dateutil.py#l63
102-
for how this value is created.
103-
104-
Returns:
105-
The UTC Unix time (seconds since the epoch), as an int.
106-
"""
107-
assert len(hgweb_datejson) == 2
108-
timestamp, offset = hgweb_datejson
109-
return timestamp + offset
110-
111-
112-
class Error(Exception):
113-
"""Generic error class for this module."""
114-
115-
116-
class NoSuchChangeset(Error):
117-
"""Raised if the given changeset ID does not exist in the target system."""

‎committelemetry/tool.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
from committelemetry.pulse import run_pulse_listener
1111
from committelemetry.pushlog import send_pings_by_pushid
12-
from .telemetry import NoSuchChangeset, payload_for_changeset
12+
from .telemetry import payload_for_changeset
13+
from committelemetry.hgmo import NoSuchChangeset
1314

1415

1516
@click.command()

0 commit comments

Comments
 (0)
Failed to load comments.