blob: bb7d61afc3ee38af601e6baa63c7cbeebe593af1 [file] [log] [blame]
[email protected]5f0788b2015-06-09 00:04:511#!/usr/bin/env python
2# Copyright (c) 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Tool for interacting with Buildbucket.
7
8Usage:
9 $ depot-tools-auth login https://cr-buildbucket.appspot.com
10 $ buildbucket.py \
11 put \
12 --bucket master.tryserver.chromium.linux \
13 --builder my-builder \
14
15 Puts a build into buildbucket for my-builder on tryserver.chromium.linux.
16"""
17
18import argparse
19import json
20import urlparse
21import os
22import sys
23
24from third_party import httplib2
25
26import auth
27
28
29BUILDBUCKET_URL = 'https://cr-buildbucket.appspot.com'
[email protected]8e6e1e62015-08-07 21:52:1830BUILDBUCKET_API_URL = urlparse.urljoin(
[email protected]5f0788b2015-06-09 00:04:5131 BUILDBUCKET_URL,
32 '_ah/api/buildbucket/v1/builds',
33)
34
35
36def main(argv):
37 parser = argparse.ArgumentParser()
38 parser.add_argument(
39 '-v',
40 '--verbose',
41 action='store_true',
42 )
43 subparsers = parser.add_subparsers(dest='command')
[email protected]8e6e1e62015-08-07 21:52:1844 get_parser = subparsers.add_parser('get')
45 get_parser.add_argument(
46 '--id',
47 help='The ID of the build to get the status of.',
48 required=True,
49 )
[email protected]5f0788b2015-06-09 00:04:5150 put_parser = subparsers.add_parser('put')
51 put_parser.add_argument(
[email protected]1da6b032015-06-09 00:16:0352 '-b',
[email protected]5f0788b2015-06-09 00:04:5153 '--bucket',
54 help=(
55 'The bucket to schedule the build on. Typically the master name, e.g.'
56 ' master.tryserver.chromium.linux.'
57 ),
58 required=True,
59 )
60 put_parser.add_argument(
[email protected]c1ae89e2015-06-22 23:06:4461 '-c',
62 '--changes',
63 help='A flie to load a JSON list of changes dicts from.',
64 )
65 put_parser.add_argument(
[email protected]5f0788b2015-06-09 00:04:5166 '-n',
67 '--builder-name',
68 help='The builder to schedule the build on.',
69 required=True,
70 )
71 put_parser.add_argument(
72 '-p',
73 '--properties',
74 help='A file to load a JSON dict of properties from.',
75 )
76 args = parser.parse_args()
[email protected]5f0788b2015-06-09 00:04:5177
[email protected]8e6e1e62015-08-07 21:52:1878 body = None
[email protected]c1ae89e2015-06-22 23:06:4479
[email protected]8e6e1e62015-08-07 21:52:1880 if args.command == 'get':
81 method = 'GET'
82 url = '%s/%s' % (BUILDBUCKET_API_URL, args.id)
83 elif args.command == 'put':
84 changes = []
85 if args.changes:
86 try:
87 with open(args.changes) as fp:
88 changes.extend(json.load(fp))
89 except (TypeError, ValueError):
90 sys.stderr.write('%s contained invalid JSON list.\n' % args.changes)
91 raise
92
93 properties = {}
94 if args.properties:
95 try:
96 with open(args.properties) as fp:
97 properties.update(json.load(fp))
98 except (TypeError, ValueError):
99 sys.stderr.write('%s contained invalid JSON dict.\n' % args.properties)
100 raise
101
102 body = json.dumps({
103 'bucket': args.bucket,
104 'parameters_json': json.dumps({
105 'builder_name': args.builder_name,
106 'changes': changes,
107 'properties': properties,
108 }),
109 })
110 method = 'PUT'
111 url = BUILDBUCKET_API_URL
[email protected]5f0788b2015-06-09 00:04:51112
113 authenticator = auth.get_authenticator_for_host(
114 BUILDBUCKET_URL,
115 auth.make_auth_config(use_oauth2=True),
116 )
117 http = authenticator.authorize(httplib2.Http())
118 http.force_exception_to_status_code = True
119 response, content = http.request(
[email protected]8e6e1e62015-08-07 21:52:18120 url,
121 method,
122 body=body,
[email protected]5f0788b2015-06-09 00:04:51123 headers={'Content-Type': 'application/json'},
124 )
125
126 if args.verbose:
127 print content
128
129 return response.status != 200
130
131
132if __name__ == '__main__':
133 sys.exit(main(sys.argv))