Skip to content

Instantly share code, notes, and snippets.

@Ardagan
Last active April 14, 2020 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ardagan/13e6031e8d1c9ebbd3029bf365c1a517 to your computer and use it in GitHub Desktop.
Save Ardagan/13e6031e8d1c9ebbd3029bf365c1a517 to your computer and use it in GitHub Desktop.
Send multiple comments to github PR.
#
# Set GH_ACCESS_TOKEN
# Update PullRequest(number:
# Update subjectId
#
import itertools
import os
import socket
import sys
import time
import traceback
import re
from datetime import datetime
import requests
import psycopg2
# Replace GH_ACCESS_TOKEN and PR number and subject ID below.
# To get subject id
# https://developer.github.com/v4/explorer/
# {
# repository(owner: "apache", name:"beam"){
# url
# pullRequest(number: 9884){
# number
# url
# author {
# avatarUrl
# login
# resourcePath
# url
# }
# id
# }
# }
# }
GET_PR_ID_REQUEST='''
query FindPullRequestID {
repository(owner:"apache", name:"beam") {
pullRequest(number:9884) {
id
}
}
}
'''
ADD_PR_COMMENT_REQUEST='''
mutation AddPullRequestComment {
addComment(input:{subjectId:"MDExOlB1bGxSZXF1ZXN0MzMyNjE4ODIw",body: "<CommentBodyString>"}) {
commentEdge {
node {
createdAt
body
}
}
subject {
id
}
}
}
'''
GH_ACCESS_TOKEN='...'
def executeGHGraphqlQuery(query):
'''Runs graphql query on GitHub.'''
url = 'https://api.github.com/graphql'
headers = {'Authorization': f'Bearer {GH_ACCESS_TOKEN}'}
r = requests.post(url=url, json={'query': query}, headers=headers)
return r.json()
def fetchGHData(commentBody):
'''Fetches GitHub data required for reporting Beam metrics'''
query = ADD_PR_COMMENT_REQUEST.replace('<CommentBodyString>', commentBody)
return executeGHGraphqlQuery(query)
COMMENTS_TO_ADD=[
"Run Go PostCommit",
"Run Java PostCommit",
"Run Java PortabilityApi PostCommit",
"Run Java Flink PortableValidatesRunner Batch",
"Run Java Flink PortableValidatesRunner Streaming",
"Run Apex ValidatesRunner",
"Run Dataflow ValidatesRunner",
"Run Flink ValidatesRunner",
"Run Gearpump ValidatesRunner",
"Run Dataflow PortabilityApi ValidatesRunner",
"Run Samza ValidatesRunner",
"Run Spark ValidatesRunner",
"Run Python Dataflow ValidatesContainer",
"Run Python Dataflow ValidatesRunner",
"Run Python Flink ValidatesRunner",
"Run Python PostCommit",
"Run SQL PostCommit",
"Run Go PreCommit",
"Run Java PreCommit",
"Run Java_Examples_Dataflow PreCommit",
"Run JavaPortabilityApi PreCommit",
"Run Portable_Python PreCommit",
"Run PythonLint PreCommit",
"Run Python PreCommit"
]
def postComments():
'''
Main workhorse method. Fetches data from GitHub and puts it in metrics table.
'''
for commentBody in COMMENTS_TO_ADD:
jsonData = fetchGHData(commentBody)
print(jsonData)
def probeGitHubIsUp():
'''
Returns True if GitHub responds to simple queries. Else returns False.
'''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('github.com', 443))
return True if result == 0 else False
################################################################################
if __name__ == '__main__':
'''
This script is supposed to be invoked directly.
However for testing purposes and to allow importing,
wrap work code in module check.
'''
print("Started.")
if not probeGitHubIsUp():
print("GitHub is unavailable, skipping fetching data.")
exit();
print("GitHub is available start fetching data.")
postComments()
print("Fetched data.")
print('Done.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment