forked from davegill/jenkins-auto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.py
164 lines (156 loc) · 5.45 KB
/
mail.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import boto3
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import sys
#Variables
SENDER = '[email protected]'
SENDERNAME = 'no-reply-jenkins'
FILE_NAME = sys.argv[1]
BUILD_STATUS=sys.argv[2]
JOB_NAME=sys.argv[3]
BUILD_NUMBER=sys.argv[4]
RECIPIENT=sys.argv[5]
COMMIT_ID=sys.argv[6]
REQUESTOR=sys.argv[7]
PULLNUMBER=sys.argv[8]
FILE=sys.argv[9]
PRCOMMENT=sys.argv[10]
E=sys.argv[11]
F=sys.argv[12]
G=sys.argv[13]
H=sys.argv[14]
I=sys.argv[15]
J=sys.argv[16]
K=sys.argv[17]
L=sys.argv[18]
M=sys.argv[19]
N=sys.argv[20]
O=sys.argv[21]
P=sys.argv[22]
AWS_REGION = "us-east-1"
WEI_WANG="[email protected]"
VIKRAM="[email protected]"
SCALA="[email protected]"
f = open(FILE,"r")
contents=f.read()
print(contents)
#If either I or J is non-zero, then the title of the email message uses the word FAIL.
#If both I and J are zero, then the title of the email message uses the word PASS.
if (I=="0") and (J=="0"):
SUBJECT =("WRF Jenkins-PASS {} {} {}").format(REQUESTOR,PULLNUMBER,PRCOMMENT)
#Pass HTML Body
HTML_BODY_PASS="""
<html>
<head></head>
<body>
<p>Please find result of the WRF regression test cases in the attachment. This build is for Commit ID: {0}, requested by: {1} for PR: https://github.com/wrf-model/WRF/pull/{2}.
For any query please send e-mail to <a href="mailto:[email protected]">Wei Wang. </a></p>
<pre>
Test Type | Expected | Received | Failed
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
Number of Tests : {11} {3}
Number of Builds : {12} {4}
Number of Simulations : {13} {5} {7}
Number of Comparisons : {14} {6} {8}
Failed Simulations are:
{9}
Which comparisons are not bit-for-bit:
{10}
</pre>
</body>
</html>""".format(COMMIT_ID,REQUESTOR,PULLNUMBER,E,F,G,H,I,J,K,L,M,N,O,P)
else :
SUBJECT =("WRF Jenkins-FAIL {} {} {}").format(REQUESTOR,PULLNUMBER,PRCOMMENT)
#Pass HTML Body
HTML_BODY_PASS="""
<html>
<head></head>
<body>
<p>Please find result of the WRF regression test cases in the attachment. This build is for Commit ID: {0}, requested by: {1} for PR: https://github.com/wrf-model/WRF/pull/{2}.
For any query please send e-mail to <a href="mailto:[email protected]">Wei Wang. </a></p>
<pre>
Test Type | Expected | Received | Failed
= = = = = = = = = = = = = = = = = = = = = = = = = = = =
Number of Tests : {11} {3}
Number of Builds : {12} {4}
Number of Simulations : {13} {5} {7}
Number of Comparisons : {14} {6} {8}
Failed Simulations are:
{9}
Which comparisons are not bit-for-bit:
{10}
</pre>
</body>
</html>""".format(COMMIT_ID,REQUESTOR,PULLNUMBER,E,F,G,H,I,J,K,L,M,N,O,P)
#Fail/Aborted HTML Body
HTML_BODY_FAIL="""
<html>
<head></head>
<body>
<h1>{}: {}-BUILD-{}</h1>
<p>This WRF-Model build has {}. This build is for Commit ID: {}, requested by: {} for PR: https://github.com/wrf-model/WRF/pull/{}.
For any query please send e-mail to <a href="mailto:[email protected]">Wei Wang</a></p>
</body>
</html>""".format(BUILD_STATUS,JOB_NAME,BUILD_NUMBER,BUILD_STATUS,COMMIT_ID,REQUESTOR,PULLNUMBER)
# The full path to the file that will be attached to the email.
ATTACHMENT = FILE_NAME
CHARSET = "utf-8"
client = boto3.client('ses',region_name=AWS_REGION)
msg = MIMEMultipart('mixed')
# Add subject, from and to lines.
msg['Subject'] = SUBJECT
msg['From'] = SENDER
msg['To'] = RECIPIENT
if (BUILD_STATUS=="SUCCESS"):
msg_body = MIMEMultipart('alternative')
htmlpart = MIMEText(HTML_BODY_PASS.encode(CHARSET), 'html', CHARSET)
msg_body.attach(htmlpart)
# Define the attachment part and encode it using MIMEApplication.
att = MIMEApplication(open(ATTACHMENT, 'rb').read())
att.add_header('Content-Disposition','attachment',filename=os.path.basename(ATTACHMENT))
msg.attach(msg_body)
# Add the attachment to the parent container.
msg.attach(att)
try:
#Provide the contents of the email.
response = client.send_raw_email(
Source=SENDER,
Destinations=[
RECIPIENT,WEI_WANG,VIKRAM,SCALA
],
RawMessage={
'Data':msg.as_string(),
},
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
if (BUILD_STATUS=="FAILURE"):
msg_body = MIMEMultipart('alternative')
htmlpart = MIMEText(HTML_BODY_FAIL.encode(CHARSET), 'html', CHARSET)
msg_body.attach(htmlpart)
# Define the attachment part and encode it using MIMEApplication.
msg.attach(msg_body)
try:
#Provide the contents of the email.
response = client.send_raw_email(
Source=SENDER,
Destinations=[
RECIPIENT,WEI_WANG,VIKRAM,SCALA
],
RawMessage={
'Data':msg.as_string(),
},
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])