Created
April 5, 2022 00:31
-
-
Save fukasawah/08d351219799f8102b1108fcd3603e56 to your computer and use it in GitHub Desktop.
Azure PipelinesでPullRequestにコメントをつけるだけのスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# env: | |
# SYSTEM_ACCESSTOKEN: $(System.AccessToken) | |
# usage) | |
# echo hello | python pull-request-comment.py | |
# test) | |
# # https://gist.github.com/fukasawah/d8ecaea8ac2b73f440b7193cdb295e92 | |
# python echo-http-server.py & | |
# echo hello | SYSTEM_TEAMFOUNDATIONCOLLECTIONURI=http://localhost:8080/ SYSTEM_TEAMPROJECTID=FOO SYSTEM_TEAMPROJECTID=BAR BUILD_REPOSITORY_NAME=BAZ SYSTEM_PULLREQUEST_PULLREQUESTID=123 python pull-request-comment.py | |
# # => POST /BAR/_apis/git/repositories/BAZ/pullRequests/123/threads?api-version=6.0 | |
import json | |
import sys | |
import urllib.request | |
import urllib.error | |
import os | |
import argparse | |
def post_http_json(url, headers, json_object): | |
''' | |
HTTP POSTでJSONを送るヘルパ | |
''' | |
data = json.dumps(json_object).encode() | |
headers['Content-Type'] = 'application/json' | |
request = urllib.request.Request(url, data=data, method='POST', headers=headers) | |
try: | |
with urllib.request.urlopen(request) as response: | |
# response ref: https://docs.python.org/ja/3.7/library/http.client.html#httpresponse-objects | |
response_body = response.read().decode("utf-8") | |
status = response.status | |
return (status, response_body) | |
except urllib.error.HTTPError as ex: | |
response_body = ex.fp.read().decode("utf-8") | |
status = ex.code | |
except ConnectionRefusedError as ex: | |
# TODO: 面倒みきれない | |
raise ex | |
return (status, response_body) | |
def main(): | |
# 明示的に指定が必要な環境変数を利用 | |
SYSTEM_ACCESSTOKEN = os.environ.get('SYSTEM_ACCESSTOKEN') | |
# Azure Pipeline agentがPullRequest時に渡してくる環境変数から利用 | |
SYSTEM_TEAMFOUNDATIONCOLLECTIONURI = os.environ.get('SYSTEM_TEAMFOUNDATIONCOLLECTIONURI') | |
SYSTEM_TEAMPROJECTID = os.environ.get('SYSTEM_TEAMPROJECTID') | |
BUILD_REPOSITORY_NAME = os.environ.get('BUILD_REPOSITORY_NAME') | |
SYSTEM_PULLREQUEST_PULLREQUESTID = os.environ.get('SYSTEM_PULLREQUEST_PULLREQUESTID') | |
# 引数解析 | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--title', nargs='?', help='content header', default="Stdin") | |
parser.add_argument('--message', nargs='?', help='content overview', default="Result") | |
args = parser.parse_args() | |
# PRへ送るコメントを構築(Markdown) | |
content_markdown = f''' | |
# {args.title} | |
{args.message} | |
``` | |
{sys.stdin.read()} | |
``` | |
''' | |
# Rest API実行 | |
# ref: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pull-request-threads/create?view=azure-devops-rest-6.0 | |
(status, body) = post_http_json( | |
f"{SYSTEM_TEAMFOUNDATIONCOLLECTIONURI}{SYSTEM_TEAMPROJECTID}/_apis/git/repositories/{BUILD_REPOSITORY_NAME}/pullRequests/{SYSTEM_PULLREQUEST_PULLREQUESTID}/threads?api-version=6.0" | |
, { | |
'Authorization': f'Bearer {SYSTEM_ACCESSTOKEN}' | |
}, { | |
"comments": [ | |
{ | |
"parentCommentId": 0, | |
"content": content_markdown, | |
"commentType": 1 | |
} | |
], | |
"status": 1, # Active | |
}) | |
# 必要に応じてタスクの結果として出力 | |
print(f"{status}, {body}") | |
if status == 200: | |
return 0 | |
else: | |
return 1 | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment