From 5d148cf125187929516f02bb352cb9e025704070 Mon Sep 17 00:00:00 2001 From: "Randal S. Harisch" Date: Sun, 24 Sep 2023 17:57:04 -0600 Subject: [PATCH] chore: added gitea-set-status task --- tasks/gitea-set-status.yaml | 131 ++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 tasks/gitea-set-status.yaml diff --git a/tasks/gitea-set-status.yaml b/tasks/gitea-set-status.yaml new file mode 100644 index 0000000..776800e --- /dev/null +++ b/tasks/gitea-set-status.yaml @@ -0,0 +1,131 @@ +apiVersion: tekton.dev/v1 +kind: Task +metadata: + name: gitea-set-status + namespace: goghvideo-cicd-pipeline +spec: + description: |- + This task will set the status of the CI job to the specified value along with a link to the specified target URL where developers can follow the progress of the CI job. + The `gitea-set-status` task allows external services to mark Gitea commits with an `error`, `failure`, `pending`, or `success` state, which is then reflected in pull requests involving those commits. Statuses include as well a `description` and a `target_url`, to give the user informations about the CI statuses or a direct link to the full log. + params: + - description: | + The Gitea host, e.g: git.yourcompany.com. Can include port. + name: GITEA_HOST_URL + type: string + - default: https + description: | + If we should connect with HTTP or HTTPS. Use "http" or "https" here. + name: GITEA_HTTPS_OR_HTTP + type: string + - default: /api/v1 + description: | + The API path prefix of Gitea, default: /api/v1 + name: API_PATH_PREFIX + type: string + - description: | + The Gitea repository full name, e.g.: tektoncd/catalog + name: REPO_FULL_NAME + type: string + - default: gitea + description: | + The name of the kubernetes secret that contains the Gitea token, default: gitea + name: GITEA_TOKEN_SECRET_NAME + type: string + - default: token + description: | + The key within the kubernetes secret that contains the Gitea token, default: token + name: GITEA_TOKEN_SECRET_KEY + type: string + - description: | + Commit SHA to set the status for. + name: SHA + type: string + - description: | + The target URL to associate with this status. This URL will be linked + from the Gitea UI to allow users to easily see the source of the + status. + name: TARGET_URL + type: string + - description: | + A short description of the status. + name: DESCRIPTION + type: string + - default: continuous-integration/tekton + description: | + The Gitea context, A string label to differentiate this status from + the status of other systems. ie: "continuous-integration/tekton" + name: CONTEXT + type: string + - description: | + The state of the status. Can be one of the following `error`, + `failure`, `pending`, `warning` or `success`. + name: STATE + type: string + - default: python:3.10.1-alpine3.15@sha256:affe0faa14e7553fc570beec3864e74b5e36f8c19b2bb49ae8ba79c0e9e7236e + description: | + Image providing the python binary which this task uses. + name: IMAGE + type: string + - default: /usr/bin/env python + description: | + Python path. Depends on the image. + name: SHEBANG + type: string + steps: + - image: $(params.IMAGE) + name: set-status + script: | + #!$(params.SHEBANG) + + """This script will set the CI status on a Gitea commit""" + + import json + import sys + import http.client + + gitea_token = open("/etc/gitea-set-status/$(params.GITEA_TOKEN_SECRET_KEY)", "r").read() + + status_url = "$(params.API_PATH_PREFIX)" + "/repos/$(params.REPO_FULL_NAME)/" + \ + "statuses/$(params.SHA)" + + data = { + "state": "$(params.STATE)", + "target_url": "$(params.TARGET_URL)", + "description": "$(params.DESCRIPTION)", + "context": "$(params.CONTEXT)" + } + print("Sending this data to Gitea: ") + print(data) + + authHeader = "token " + gitea_token + + if "$(params.GITEA_HTTPS_OR_HTTP)" == "https": + conn = http.client.HTTPSConnection("$(params.GITEA_HOST_URL)") + else: + conn = http.client.HTTPConnection("$(params.GITEA_HOST_URL)") + + conn.request( + "POST", + status_url, + body=json.dumps(data), + headers={ + "User-Agent": "TektonCD, the peaceful cat", + "Authorization": authHeader, + "Accept": "application/json", + "Content-Type": "application/json", + }) + resp = conn.getresponse() + if not str(resp.status).startswith("2"): + print("Error: %d" % (resp.status)) + print(resp.read()) + sys.exit(1) + else: + print("Gitea status '$(params.STATE)' has been set on " + "$(params.REPO_FULL_NAME)#$(params.SHA) ") + volumeMounts: + - mountPath: /etc/gitea-set-status + name: giteatoken + volumes: + - name: giteatoken + secret: + secretName: $(params.GITEA_TOKEN_SECRET_NAME)