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