Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 3 | import logging |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 4 | |
| 5 | from zuul.driver.github.githubconnection import GithubConnection |
| 6 | from zuul.driver.github import GithubDriver |
| 7 | from zuul.model import Change, Project |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 8 | |
| 9 | # This is a template with boilerplate code for debugging github issues |
| 10 | |
| 11 | # TODO: for real use override the following variables |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 12 | server = 'github.com' |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 13 | api_token = 'xxxx' |
Tobias Henkel | 89a9fca | 2018-02-05 10:09:00 +0100 | [diff] [blame] | 14 | appid = 2 |
| 15 | appkey = '/opt/project/appkey' |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 16 | |
| 17 | org = 'example' |
| 18 | repo = 'sandbox' |
| 19 | pull_nr = 8 |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 20 | |
| 21 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 22 | def configure_logging(context): |
| 23 | stream_handler = logging.StreamHandler() |
| 24 | logger = logging.getLogger(context) |
| 25 | logger.addHandler(stream_handler) |
| 26 | logger.setLevel(logging.DEBUG) |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 27 | |
| 28 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 29 | # uncomment for more logging |
| 30 | # configure_logging('urllib3') |
| 31 | # configure_logging('github3') |
| 32 | # configure_logging('cachecontrol') |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 33 | |
| 34 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 35 | # This is all that's needed for getting a usable github connection |
| 36 | def create_connection(server, api_token): |
| 37 | driver = GithubDriver() |
| 38 | connection_config = { |
| 39 | 'server': server, |
| 40 | 'api_token': api_token, |
| 41 | } |
| 42 | conn = GithubConnection(driver, 'github', connection_config) |
| 43 | conn._authenticateGithubAPI() |
| 44 | return conn |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 45 | |
| 46 | |
Tobias Henkel | 89a9fca | 2018-02-05 10:09:00 +0100 | [diff] [blame] | 47 | def create_connection_app(server, appid, appkey): |
| 48 | driver = GithubDriver() |
| 49 | connection_config = { |
| 50 | 'server': server, |
| 51 | 'app_id': appid, |
| 52 | 'app_key': appkey, |
| 53 | } |
| 54 | conn = GithubConnection(driver, 'github', connection_config) |
| 55 | conn._authenticateGithubAPI() |
| 56 | conn._prime_installation_map() |
| 57 | return conn |
| 58 | |
| 59 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 60 | def get_change(connection: GithubConnection, |
| 61 | org: str, |
| 62 | repo: str, |
| 63 | pull: int) -> Change: |
| 64 | p = Project("%s/%s" % (org, repo), connection.source) |
Tobias Henkel | 89a9fca | 2018-02-05 10:09:00 +0100 | [diff] [blame] | 65 | github = connection.getGithubClient(p.name) |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 66 | pr = github.pull_request(org, repo, pull) |
| 67 | sha = pr.head.sha |
| 68 | return conn._getChange(p, pull, sha, True) |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 69 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 70 | |
Tobias Henkel | 89a9fca | 2018-02-05 10:09:00 +0100 | [diff] [blame] | 71 | # create github connection with api token |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 72 | conn = create_connection(server, api_token) |
| 73 | |
Tobias Henkel | 89a9fca | 2018-02-05 10:09:00 +0100 | [diff] [blame] | 74 | # create github connection with app key |
| 75 | # conn = create_connection_app(server, appid, appkey) |
| 76 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 77 | |
| 78 | # Now we can do anything we want with the connection, e.g. check canMerge for |
| 79 | # a pull request. |
| 80 | change = get_change(conn, org, repo, pull_nr) |
| 81 | |
| 82 | print(conn.canMerge(change, {'cc/gate2'})) |
| 83 | |
| 84 | |
| 85 | # Or just use the github object. |
| 86 | # github = conn.getGithubClient() |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 87 | # |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 88 | # repository = github.repository(org, repo) |
| 89 | # print(repository.as_dict()) |