Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow_dispatch github event handling and upgrade rpc #81

Closed
15 changes: 15 additions & 0 deletions .github/workflows/workflow_dispatch_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Workflow Dispatch Tests

on:
# Manually dispatched workflow action
workflow_dispatch:
inputs:
runner:
description: 'Self hosted gh runner'
required: true
jobs:
workflow-dispatch-tests:
runs-on: ubuntu-latest
steps:
- name: Run a one-line script
run: echo Hello, world of workflow dispatches!
15 changes: 10 additions & 5 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,15 @@ def _install_deps(self) -> None:
"""Install dependencies."""
logger.info("Installing charm dependencies.")

# Snap and Apt will use any proxies configured in the Juju model.
# Binding for snap, apt, and lxd init commands are not available so subprocess.run used.
execute_command(["/usr/bin/apt-get", "update"])
# install dependencies used by repo-policy-compliance and the firewall
execute_command(
["/usr/bin/apt-get", "install", "-qy", "gunicorn", "python3-pip", "nftables"]
)

# Prepare environment for pip subprocess
env = {}
if "http" in self.proxies:
env["HTTP_PROXY"] = self.proxies["http"]
Expand All @@ -588,15 +596,12 @@ def _install_deps(self) -> None:
env["NO_PROXY"] = self.proxies["no_proxy"]
env["no_proxy"] = self.proxies["no_proxy"]

execute_command(["/usr/bin/apt-get", "update"])
# install dependencies used by repo-policy-compliance and the firewall
execute_command(
["/usr/bin/apt-get", "install", "-qy", "gunicorn", "python3-pip", "nftables"]
)
# Install repo-policy-compliance package
execute_command(
[
"/usr/bin/pip",
"install",
"--upgrade",
"git+https://github.com/canonical/repo-policy-compliance@main",
],
env=env,
Expand Down
52 changes: 43 additions & 9 deletions templates/pre-job.j2
jdkandersson marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
#!/usr/bin/env bash

GITHUB_SOURCE_REPOSITORY=$(cat "${GITHUB_EVENT_PATH}" | jq -r '.pull_request.head.repo.full_name')

# Request repo-policy-compliance service check.
curl --noproxy '*' \
--fail-with-body \
-H 'Authorization: Bearer {{one_time_token}}' \
-H 'Content-Type: application/json' \
-d "{\"repository_name\": \"${GITHUB_REPOSITORY}\", \"source_repository_name\": \"${GITHUB_SOURCE_REPOSITORY}\", \"target_branch_name\": \"${GITHUB_BASE_REF}\", \"source_branch_name\": \"${GITHUB_HEAD_REF}\", \"commit_sha\": \"${GITHUB_SHA}\"}" \
http://{{host_ip}}:8080/check-run
# Log common env variables.
logger -s "GITHUB_EVENT_NAME: ${GITHUB_EVENT_NAME}, \
GITHUB_REPOSITORY: ${GITHUB_REPOSITORY}, \
GITHUB_SHA: ${GITHUB_SHA}"

# Prepare curl arguments
CURL_ARGS="--max-time 60 \
--noproxy '*' \
--fail-with-body \
-H 'Authorization: Bearer {{one_time_token}}' \
-H 'Content-Type: application/json'"

# Workflow dispatch - Request repo-policy-compliance service check:
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then

logger -s "GITHUB_REF_NAME: ${GITHUB_REF_NAME}"

curl "${CURL_ARGS}" \
-d "{\"repository_name\": \"${GITHUB_REPOSITORY}\", \"branch_name\": \"${GITHUB_REF_NAME}\", \"commit_sha\": \"${GITHUB_SHA}\"}" \
http://{{host_ip}}:8080/workflow_dispatch/check-run

# Pull request - Request repo-policy-compliance service check:
elif [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then

GITHUB_SOURCE_REPOSITORY=$(cat "${GITHUB_EVENT_PATH}" | jq -r '.pull_request.head.repo.full_name')

logger -s " \
GITHUB_SOURCE_REPOSITORY: ${GITHUB_SOURCE_REPOSITORY}, \
GITHUB_SOURCE_REPOSITORY: ${GITHUB_SOURCE_REPOSITORY} \
GITHUB_BASE_REF: ${GITHUB_BASE_REF}, \
GITHUB_HEAD_REF: ${GITHUB_HEAD_REF}"

curl "${CURL_ARGS}" \
-d "{\"repository_name\": \"${GITHUB_REPOSITORY}\", \"source_repository_name\": \"${GITHUB_SOURCE_REPOSITORY}\", \"target_branch_name\": \"${GITHUB_BASE_REF}\", \"source_branch_name\": \"${GITHUB_HEAD_REF}\", \"commit_sha\": \"${GITHUB_SHA}\"}" \
http://{{host_ip}}:8080/pull_request/check-run

else

logger -p user.error -s "${GITHUB_EVENT_NAME} is not supported yet. Please request it to be added on https://github.com/canonical/github-runner-operator/issues/new/choose"

return 1

fi