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 | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 14 | |
| 15 | org = 'example' |
| 16 | repo = 'sandbox' |
| 17 | pull_nr = 8 |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 18 | |
| 19 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 20 | def configure_logging(context): |
| 21 | stream_handler = logging.StreamHandler() |
| 22 | logger = logging.getLogger(context) |
| 23 | logger.addHandler(stream_handler) |
| 24 | logger.setLevel(logging.DEBUG) |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 25 | |
| 26 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 27 | # uncomment for more logging |
| 28 | # configure_logging('urllib3') |
| 29 | # configure_logging('github3') |
| 30 | # configure_logging('cachecontrol') |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 31 | |
| 32 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 33 | # This is all that's needed for getting a usable github connection |
| 34 | def create_connection(server, api_token): |
| 35 | driver = GithubDriver() |
| 36 | connection_config = { |
| 37 | 'server': server, |
| 38 | 'api_token': api_token, |
| 39 | } |
| 40 | conn = GithubConnection(driver, 'github', connection_config) |
| 41 | conn._authenticateGithubAPI() |
| 42 | return conn |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 43 | |
| 44 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 45 | def get_change(connection: GithubConnection, |
| 46 | org: str, |
| 47 | repo: str, |
| 48 | pull: int) -> Change: |
| 49 | p = Project("%s/%s" % (org, repo), connection.source) |
| 50 | github = connection.getGithubClient(p) |
| 51 | pr = github.pull_request(org, repo, pull) |
| 52 | sha = pr.head.sha |
| 53 | return conn._getChange(p, pull, sha, True) |
Tobias Henkel | 38f4ea3 | 2017-12-20 16:43:46 +0100 | [diff] [blame] | 54 | |
Tobias Henkel | ac1763d | 2017-12-21 15:36:19 +0100 | [diff] [blame] | 55 | |
| 56 | # create github connection |
| 57 | conn = create_connection(server, api_token) |
| 58 | |
| 59 | |
| 60 | # Now we can do anything we want with the connection, e.g. check canMerge for |
| 61 | # a pull request. |
| 62 | change = get_change(conn, org, repo, pull_nr) |
| 63 | |
| 64 | print(conn.canMerge(change, {'cc/gate2'})) |
| 65 | |
| 66 | |
| 67 | # Or just use the github object. |
| 68 | # github = conn.getGithubClient() |
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 | # repository = github.repository(org, repo) |
| 71 | # print(repository.as_dict()) |