|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Mock GitHub API server for testing an opened PR |
| 3 | +# You can run this manually in `tasks/run-local.sh -i` with `podman cp` and running |
| 4 | +# cd bots |
| 5 | +# PYTHONPATH=. ./mock-github-pr cockpit-project/bots $(git rev-parse HEAD) & |
| 6 | +# export GITHUB_API=http://127.0.0.7:8443 |
| 7 | +# PYTHONPATH=. ./mock-github-pr --print-event cockpit-project/bots $(git rev-parse HEAD) | \ |
| 8 | +# ./publish-queue --amqp localhost:5671 --queue webhook |
| 9 | +# |
| 10 | +# and then two `./run-queue --amqp localhost:5671` |
| 11 | +# first to process webhook → tests-scan → public, second to actually run it |
| 12 | + |
| 13 | +import argparse |
| 14 | +import json |
| 15 | +import os |
| 16 | +import tempfile |
| 17 | + |
| 18 | +from task.test_mock_server import MockHandler, MockServer |
| 19 | + |
| 20 | +repo = None |
| 21 | +sha = None |
| 22 | + |
| 23 | + |
| 24 | +class Handler(MockHandler): |
| 25 | + def do_GET(self): |
| 26 | + if self.path in self.server.data: |
| 27 | + self.replyJson(self.server.data[self.path]) |
| 28 | + elif self.path.startswith(f'/repos/{repo}/pulls?'): |
| 29 | + self.replyJson([self.server.data[f'/repos/{repo}/pulls/1']]) |
| 30 | + elif self.path == f'/{repo}/{sha}/.cockpit-ci/container': |
| 31 | + self.replyData('quay.io/cockpit/tasks') |
| 32 | + else: |
| 33 | + self.send_error(404, 'Mock Not Found: ' + self.path) |
| 34 | + |
| 35 | + def do_POST(self): |
| 36 | + if self.path.startswith(f'/repos/{repo}/statuses/{sha}'): |
| 37 | + self.replyJson({}) |
| 38 | + else: |
| 39 | + self.send_error(405, 'Method not allowed: ' + self.path) |
| 40 | + |
| 41 | + |
| 42 | +argparser = argparse.ArgumentParser() |
| 43 | +argparser.add_argument('--port', type=int, default=8443, help="Port to listen on (default: %(default)s)") |
| 44 | +argparser.add_argument('--print-event', action='store_true', help="Print GitHub webhook pull_request event and exit") |
| 45 | +argparser.add_argument('repo', metavar='USER/PROJECT', help="GitHub user/org and project name") |
| 46 | +argparser.add_argument('sha', help="SHA to test in repo for the mock PR") |
| 47 | +args = argparser.parse_args() |
| 48 | +repo = args.repo |
| 49 | +sha = args.sha |
| 50 | + |
| 51 | +ADDRESS = ('127.0.0.7', args.port) |
| 52 | + |
| 53 | +GITHUB_DATA = { |
| 54 | + f'/repos/{repo}/pulls/1': { |
| 55 | + 'title': 'mock PR', |
| 56 | + 'number': 1, |
| 57 | + 'state': 'open', |
| 58 | + 'body': "This is the body", |
| 59 | + 'base': {'repo': {'full_name': repo}, 'ref': 'main'}, |
| 60 | + 'head': {'sha': args.sha, 'user': {'login': repo.split('/')[0]}}, |
| 61 | + 'labels': [], |
| 62 | + 'updated_at': 0, |
| 63 | + }, |
| 64 | + f'/repos/{repo}/commits/{args.sha}/status?page=1&per_page=100': { |
| 65 | + 'state': 'pending', |
| 66 | + 'statuses': [], |
| 67 | + 'sha': sha, |
| 68 | + }, |
| 69 | +} |
| 70 | + |
| 71 | +if args.print_event: |
| 72 | + print(json.dumps({ |
| 73 | + 'event': 'pull_request', |
| 74 | + 'request': { |
| 75 | + 'action': 'opened', |
| 76 | + 'pull_request': GITHUB_DATA[f'/repos/{repo}/pulls/1'] |
| 77 | + } |
| 78 | + }, indent=4)) |
| 79 | + exit(0) |
| 80 | + |
| 81 | +temp = tempfile.TemporaryDirectory() |
| 82 | +cache_dir = os.path.join(temp.name, 'cache') |
| 83 | +os.environ['XDG_CACHE_HOME'] = cache_dir |
| 84 | +server = MockServer(ADDRESS, Handler, GITHUB_DATA) |
| 85 | +server.start() |
| 86 | +print(f'export GITHUB_API=http://{ADDRESS[0]}:{ADDRESS[1]}') |
0 commit comments