blob: 101fd11180acbe98aa2d52fa399ec2140cca4484 [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 Henkelac1763d2017-12-21 15:36:19 +010014
15org = 'example'
16repo = 'sandbox'
17pull_nr = 8
Tobias Henkel38f4ea32017-12-20 16:43:46 +010018
19
Tobias Henkelac1763d2017-12-21 15:36:19 +010020def configure_logging(context):
21 stream_handler = logging.StreamHandler()
22 logger = logging.getLogger(context)
23 logger.addHandler(stream_handler)
24 logger.setLevel(logging.DEBUG)
Tobias Henkel38f4ea32017-12-20 16:43:46 +010025
26
Tobias Henkelac1763d2017-12-21 15:36:19 +010027# uncomment for more logging
28# configure_logging('urllib3')
29# configure_logging('github3')
30# configure_logging('cachecontrol')
Tobias Henkel38f4ea32017-12-20 16:43:46 +010031
32
Tobias Henkelac1763d2017-12-21 15:36:19 +010033# This is all that's needed for getting a usable github connection
34def 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 Henkel38f4ea32017-12-20 16:43:46 +010043
44
Tobias Henkelac1763d2017-12-21 15:36:19 +010045def 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 Henkel38f4ea32017-12-20 16:43:46 +010054
Tobias Henkelac1763d2017-12-21 15:36:19 +010055
56# create github connection
57conn = 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.
62change = get_change(conn, org, repo, pull_nr)
63
64print(conn.canMerge(change, {'cc/gate2'}))
65
66
67# Or just use the github object.
68# github = conn.getGithubClient()
Tobias Henkel38f4ea32017-12-20 16:43:46 +010069#
Tobias Henkelac1763d2017-12-21 15:36:19 +010070# repository = github.repository(org, repo)
71# print(repository.as_dict())