1#!/usr/bin/env python2.7
2#
3# Copyright 2018 The gRPC Authors
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17import argparse
18import json
19import urllib2
20
21
22def run():
23  argp = argparse.ArgumentParser(description='Set status on pull request')
24
25  argp.add_argument(
26      '--sha1', type=str, help='SHA1 of the commit', required=True)
27  argp.add_argument(
28      '--state',
29      type=str,
30      choices=('error', 'failure', 'pending', 'success'),
31      help='State to set',
32      required=True)
33  argp.add_argument(
34      '--description', type=str, help='Status description', required=True)
35  argp.add_argument('--context', type=str, help='Status context', required=True)
36  argp.add_argument(
37      '--oauth_file', type=str, help='File with OAuth token', required=True)
38
39  args = argp.parse_args()
40  sha1 = args.sha1
41  state = args.state
42  description = args.description
43  context = args.context
44  oauth_file = args.oauth_file
45
46  with open(oauth_file, 'r') as oauth_file_reader:
47    oauth_token = oauth_file_reader.read().replace('\n', '')
48
49  req = urllib2.Request(
50      url='https://api.github.com/repos/grpc/grpc-java/statuses/%s' % sha1,
51      data=json.dumps({
52          'state': state,
53          'description': description,
54          'context': context,
55      }),
56      headers={
57          'Authorization': 'token %s' % oauth_token,
58          'Content-Type': 'application/json',
59      })
60  print urllib2.urlopen(req).read()
61
62
63if __name__ == '__main__':
64  run()
65