forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report-cmsdist-pull-request-results
executable file
·111 lines (94 loc) · 4.43 KB
/
report-cmsdist-pull-request-results
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
from optparse import OptionParser
from github import Github
from os.path import expanduser
import requests
import json
#
# Posts a message in the github issue that triggered the build
# The structure of the message depends on the option used
#
# -------------------------------------------------------------------------------
# Global Variables
# --------------------------------------------------------------------------------
GH_CMSSW_ORGANIZATION = 'cms-sw'
GH_CMSSW_REPO = 'cmssw'
GH_CMSDIST_REPO = 'cmsdist'
POST_TESTS_OK = 'TESTS_OK'
POST_TESTS_FAILED = 'TESTS_FAIL'
POST_TESTING = 'TESTING'
CMSDIST_TESTS_OK_MSG = '+1\nTested compilation until {package}.\nYou can see the log here: {tests_location}'
CMSDIST_TESTS_FAIL_MSG = '-1\nBuild failed ( compiled until {package} ).\n You can see the log here: {tests_location}'
CMSDIST_COMMIT_STATUS_BASE_URL = 'https://api.github.com/repos/cms-sw/cmsdist/statuses/%s'
COMMIT_STATES_DESCRIPTION = { POST_TESTS_OK : [ 'success' , 'Tests OK' ],
POST_TESTS_FAILED : [ 'failure', 'Tests Failed' ],
POST_TESTING : [ 'pending', 'cms-bot is testing this pull request' ] }
BASE_TESTS_URL='https://cmssdt.cern.ch/SDT/jenkins-artifacts/cms-externals-pr-integration/{jk_build_number}/results/build.log'
BASE_TESTING_URL='https://cmssdt.cern.ch/jenkins/job/test-externals-prs/{jk_build_number}/'
# -------------------------------------------------------------------------------
# Functions
# --------------------------------------------------------------------------------
#
# mars the commit with the result of the tests (success or failure)
#
def mark_commit( action, commit_hash, tests_url ):
if opts.dryRun:
print 'Not adding status to commit %s (dry-run):\n %s' % ( commit_hash, action )
return
url = CMSDIST_COMMIT_STATUS_BASE_URL % commit_hash
headers = {"Authorization" : "token " + TOKEN }
params = {}
params[ 'state' ] = COMMIT_STATES_DESCRIPTION[ action ][ 0 ]
params[ 'target_url' ] = tests_url
params[ 'description' ] = COMMIT_STATES_DESCRIPTION[ action ][ 1 ]
data = json.dumps(params)
print 'Setting status to %s ' % COMMIT_STATES_DESCRIPTION[ action ][ 0 ]
print url
r = requests.post(url, data=data, headers=headers)
print r.text
#
# posts a message to the issue in github
# if dry-run is selected it doesn't post the message and just prints it
#
def post_message( issue, msg ):
if opts.dryRun:
print 'Not posting message (dry-run):\n %s' % msg
else:
print 'Posting message:\n %s' % msg
issue.create_comment( msg )
# -------------------------------------------------------------------------------
# Start of execution
# --------------------------------------------------------------------------------
if __name__ == "__main__":
parser = OptionParser( usage="%prog <jenkins-build-number> <pr-id> <arch> <last_commit> <message-type> <package-name> [ options ] \n "
"message-type = TESTS_OK | TESTS_FAIL | TESTING " )
parser.add_option( "-n" , "--dry-run" , dest="dryRun" , action="store_true", help="Do not post on Github", default=False )
opts, args = parser.parse_args( )
if len( args ) != 6:
parser.error( "Not enough arguments" )
jenkins_build_number = int( args[ 0 ] )
issue_id = int( args[ 1 ] )
arch = args[ 2 ]
commit_hash = args[ 3 ]
action = args[ 4 ]
package_name = args[ 5 ]
TOKEN=open( expanduser( "~/.github-token" ) ).read( ).strip( )
GH = Github( login_or_token=TOKEN )
CMSDIST_REPO = GH.get_organization( GH_CMSSW_ORGANIZATION ).get_repo( GH_CMSDIST_REPO )
issue = CMSDIST_REPO.get_issue( issue_id )
if action == POST_TESTS_OK:
tests_url=BASE_TESTS_URL.format( jk_build_number=jenkins_build_number )
msg = CMSDIST_TESTS_OK_MSG.format( package=package_name, tests_location=tests_url )
post_message( issue , msg )
mark_commit( action, commit_hash, tests_url )
elif action == POST_TESTS_FAILED:
tests_url = BASE_TESTS_URL.format( jk_build_number=jenkins_build_number )
msg = CMSDIST_TESTS_FAIL_MSG.format( package=package_name, tests_location=tests_url )
post_message( issue , msg )
mark_commit( action, commit_hash, tests_url )
elif action == POST_TESTING:
# This action only marks the commit as testing, does not post any message
tests_url = BASE_TESTING_URL.format( jk_build_number=jenkins_build_number )
mark_commit( action, commit_hash, tests_url )
else:
parser.error( "Message type not recognized" )