1
0
mirror of https://github.com/tektoncd/catalog.git synced 2024-12-02 06:52:15 +00:00
catalog/github/add_comment.yaml
akihikokuroda 61bd4641ab Github Enterprise support for github tasks
(cherry picked from commit 5e71cc4e71)
2020-04-10 07:51:02 +01:00

83 lines
2.3 KiB
YAML

---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: github-add-comment
description: |
This Task will add a comment to a pull request or an issue.
spec:
params:
- name: GITHUB_HOST_URL
description: |
The GitHub host, adjust this if you run a GitHub enteprise.
default: "api.github.com"
type: string
- name: API_PATH_PREFIX
description: |
The API path prefix, GitHub Enterprise has a prefix e.g. /api/v3
default: ""
type: string
- name: REQUEST_URL
description: |
The GitHub issue or pull request URL where we want to add a new
comment.
type: string
- name: COMMENT
description: |
The actual comment to add.
type: string
steps:
- name: add-comment
env:
- name: GITHUBTOKEN
valueFrom:
secretKeyRef:
name: github
key: token
image: registry.access.redhat.com/ubi8/ubi:latest
script: |
#!/usr/libexec/platform-python
import json
import os
import http.client
import urllib.parse
split_url = urllib.parse.urlparse(
"$(params.REQUEST_URL)").path.split("/")
# This will convert https://github.com/foo/bar/pull/202 to
# api url path /repos/foo/issues/202
api_url = "$(params.API_PATH_PREFIX)" + "/repos/" + "/".join(split_url[1:3]) + \
"/issues/" + split_url[-1]
# Trying to avoid quotes issue as much as possible by using triple
# quotes
comment = """$(params.COMMENT)"""
data = {
"body": comment,
}
print("Sending this data to GitHub: ")
print(data)
conn = http.client.HTTPSConnection("$(params.GITHUB_HOST_URL)")
r = conn.request(
"POST",
api_url + "/comments",
body=json.dumps(data),
headers={
"User-Agent": "TektonCD, the peaceful cat",
"Authorization": "Bearer " + os.environ["GITHUBTOKEN"],
})
resp = conn.getresponse()
if not str(resp.status).startswith("2"):
print("Error: %d" % (resp.status))
print(resp.read())
else:
print("a GitHub comment has been added to "
"$(params.REQUEST_URL)")