-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
59 lines (48 loc) · 2.16 KB
/
main.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
import os
import requests
from datetime import datetime , timedelta
# Using Github API reference for executing the call
# https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests
# PR state can be one of: open, closed, all
# Default PR state: open
# We need only open , closed
owner = str(os.environ.get('OWNER'))
repository = str(os.environ.get('REPOSITORY'))
def printPRs(state):
body = ""
prsCount = 0
# API call to collect a list of all PRs
queryParams = {'state':state}
response = requests.get("https://api.github.com/repos/" + owner + "/" + repository + "/pulls", params=queryParams)
# Get the valid time
stateTime = "closed_at"
if(state=="open"):
stateTime = "created_at"
# Traverse each PR and print the details of those within past 7 days
for pr in response.json():
prCreatedTime = datetime.strptime(pr[stateTime], "%Y-%m-%dT%H:%M:%SZ")
isLastSevenDaysPR = datetime.now() - prCreatedTime < timedelta(days=7)
if(isLastSevenDaysPR):
prsCount += 1
body += "============PR #" + str(pr["number"]) + "============\n"
body += "ID: " + str(pr["id"]) + "\n"
body += "URL: " + str(pr["url"]) + "\n"
body += "Author: " + str(pr["user"]["login"]) + "\n"
body += "From branch: " + str(pr["head"]["ref"]) + "\n"
body += "Into branch: " + str(pr["base"]["ref"]) + "\n"
body += "Date: " + str(pr[stateTime]) + "\n"
body += "Title: " + str(pr["title"]) + "\n"
body += "Description: " + str(pr["body"]) + "\n"
body += "\n\n\n"
# Check if PRs exist
if prsCount == 0:
body += "\nNo " + state + " PRs for the past 7 days\n"
if prsCount > 0:
body = "\nYou have " + str(prsCount) + " " + state + " PRs for the past 7 days\n\n" + body
return body
mailFrom = "[email protected]"
mailTo = "[email protected]"
mailSubject = "Past 7 days PRs - SP Repos"
mailBody = printPRs('open')
mailBody += printPRs('closed')
print("\nSending Mail...\nFrom: " + mailFrom + "\nTo: " + mailTo + "\nMail Subject: " + mailSubject + "\nMail Body: " + mailBody)