|
| 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 related to processing mercurial pushlog messsages. |
| 6 | +
|
| 7 | +See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#writing-agents-that-consume-pushlog-data |
| 8 | +""" |
| 9 | +import logging |
| 10 | + |
| 11 | +import requests |
| 12 | + |
| 13 | +from committelemetry.telemetry import payload_for_changeset, send_ping |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +def pushes_for_range(repo_url, starting_push_id, ending_push_id): |
| 19 | + """Fetch a dict of pushes by ID from a repo pushlog. |
| 20 | +
|
| 21 | + Args: |
| 22 | + repo_url: The full URL of the repo whose pushlog we want to process. |
| 23 | + starting_push_id: Integer. Process all pushes greater than this push id. |
| 24 | + ending_push_id: Integer. Process all pushes less than and including |
| 25 | + this push id. |
| 26 | +
|
| 27 | + Returns: |
| 28 | + A dict of {'pushid': {pushdata}}. See |
| 29 | + https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2. |
| 30 | + """ |
| 31 | + # See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2 |
| 32 | + params = dict(startID=starting_push_id, endID=ending_push_id, version=2) |
| 33 | + response = requests.get(f'{repo_url}/json-pushes/', params=params) |
| 34 | + response.raise_for_status() |
| 35 | + pushlog = response.json() |
| 36 | + return pushlog['pushes'] |
| 37 | + |
| 38 | + |
| 39 | +def send_pings_by_pushid(repo_url, starting_push_id, ending_push_id, no_send): |
| 40 | + """Fetch repo pushes by pushid and send pings for them. |
| 41 | +
|
| 42 | + Args: |
| 43 | + repo_url: The full URL of the repo whose pushlog we want to process. |
| 44 | + starting_push_id: Integer. Process all pushes greater than this push id. |
| 45 | + ending_push_id: Integer. Process all pushes less than and including |
| 46 | + this push id. |
| 47 | + no_send: Boolean: don't send any ping data, just print a message. |
| 48 | + """ |
| 49 | + if no_send: |
| 50 | + log.info('transmission of ping data has been disabled') |
| 51 | + |
| 52 | + for pushid, pushdata in pushes_for_range(repo_url, starting_push_id, ending_push_id).items(): |
| 53 | + log.info(f'processing pushid {pushid}') |
| 54 | + |
| 55 | + # See https://mozilla-version-control-tools.readthedocs.io/en/latest/hgmo/pushlog.html#version-2 |
| 56 | + changesets = pushdata['changesets'] |
| 57 | + log.info(f'got {len(changesets)} changesets for pushid {pushid}') |
| 58 | + |
| 59 | + for changeset in changesets: |
| 60 | + log.info(f'processing changeset {changeset}') |
| 61 | + ping = payload_for_changeset(changeset, repo_url) |
| 62 | + |
| 63 | + if no_send: |
| 64 | + log.info(f'ping data (not sent): {ping}') |
| 65 | + continue |
| 66 | + |
| 67 | + # Pings need a unique ID so they can be de-duplicated by the ingestion |
| 68 | + # service. We can use the changeset ID for the unique key. |
| 69 | + send_ping(changeset, ping) |
0 commit comments