blob: da6fd0c6641a3ddf9d83b3cb3a857b1f343c5880 [file] [log] [blame]
Tobias Henkelac1763d2017-12-21 15:36:19 +01001#!/usr/bin/env python3
2
Tobias Henkel38f4ea32017-12-20 16:43:46 +01003import logging
Tobias Henkelac1763d2017-12-21 15:36:19 +01004
5from zuul.driver.github.githubconnection import GithubConnection
6from zuul.driver.github import GithubDriver
7from zuul.model import Change, Project
Tobias Henkel38f4ea32017-12-20 16:43:46 +01008
9# This is a template with boilerplate code for debugging github issues
10
11# TODO: for real use override the following variables
Tobias Henkelac1763d2017-12-21 15:36:19 +010012server = 'github.com'
Tobias Henkel38f4ea32017-12-20 16:43:46 +010013api_token = 'xxxx'
Tobias Henkel89a9fca2018-02-05 10:09:00 +010014appid = 2
15appkey = '/opt/project/appkey'
Tobias Henkelac1763d2017-12-21 15:36:19 +010016
17org = 'example'
18repo = 'sandbox'
19pull_nr = 8
Tobias Henkel38f4ea32017-12-20 16:43:46 +010020
21
Tobias Henkelac1763d2017-12-21 15:36:19 +010022def configure_logging(context):
23 stream_handler = logging.StreamHandler()
24 logger = logging.getLogger(context)
25 logger.addHandler(stream_handler)
26 logger.setLevel(logging.DEBUG)
Tobias Henkel38f4ea32017-12-20 16:43:46 +010027
28
Tobias Henkelac1763d2017-12-21 15:36:19 +010029# uncomment for more logging
30# configure_logging('urllib3')
31# configure_logging('github3')
32# configure_logging('cachecontrol')
Tobias Henkel38f4ea32017-12-20 16:43:46 +010033
34
Tobias Henkelac1763d2017-12-21 15:36:19 +010035# This is all that's needed for getting a usable github connection
36def 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 Henkel38f4ea32017-12-20 16:43:46 +010045
46
Tobias Henkel89a9fca2018-02-05 10:09:00 +010047def 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 Henkelac1763d2017-12-21 15:36:19 +010060def get_change(connection: GithubConnection,
61 org: str,
62 repo: str,
63 pull: int) -> Change:
64 p = Project("%s/%s" % (org, repo), connection.source)
Tobias Henkel89a9fca2018-02-05 10:09:00 +010065 github = connection.getGithubClient(p.name)
Tobias Henkelac1763d2017-12-21 15:36:19 +010066 pr = github.pull_request(org, repo, pull)
67 sha = pr.head.sha
68 return conn._getChange(p, pull, sha, True)
Tobias Henkel38f4ea32017-12-20 16:43:46 +010069
Tobias Henkelac1763d2017-12-21 15:36:19 +010070
Tobias Henkel89a9fca2018-02-05 10:09:00 +010071# create github connection with api token
Tobias Henkelac1763d2017-12-21 15:36:19 +010072conn = create_connection(server, api_token)
73
Tobias Henkel89a9fca2018-02-05 10:09:00 +010074# create github connection with app key
75# conn = create_connection_app(server, appid, appkey)
76
Tobias Henkelac1763d2017-12-21 15:36:19 +010077
78# Now we can do anything we want with the connection, e.g. check canMerge for
79# a pull request.
80change = get_change(conn, org, repo, pull_nr)
81
82print(conn.canMerge(change, {'cc/gate2'}))
83
84
85# Or just use the github object.
86# github = conn.getGithubClient()
Tobias Henkel38f4ea32017-12-20 16:43:46 +010087#
Tobias Henkelac1763d2017-12-21 15:36:19 +010088# repository = github.repository(org, repo)
89# print(repository.as_dict())