Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Test CI

on:
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Echo hello
run: echo "Hello World"
17 changes: 17 additions & 0 deletions .github/workflows/w_run.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: After Test CI

on:
workflow_run:
workflows: ["Test CI"]
types:
- completed

jobs:
post-build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Echo workflow_run triggered
run: echo "Workflow_run triggered!"
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ node_modules/
*.json
!package.json
__pycache__/
*.yml
*.yaml

79 changes: 64 additions & 15 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import subprocess
import threading

import collections
from dotenv import load_dotenv
import uvicorn
import yaml
Expand All @@ -14,6 +14,7 @@
import requests
import time
from metrics import MetricsHandler
pending_commits = collections.defaultdict(set)


from prometheus_client import generate_latest
Expand Down Expand Up @@ -49,6 +50,7 @@ class RepoToWatch:
name: str
branch: str
path: str
actions_need_to_pass: bool


@dataclasses.dataclass
Expand Down Expand Up @@ -174,42 +176,89 @@ def update_repo(repo_config: RepoToWatch) -> RepoUpdateResult:
logger.exception("update_repo had a bad time")


@app.post("/webhook")
async def github_webhook(request: Request):
MetricsHandler.last_smee_request_timestamp.set(time.time())
payload_body = await request.body()
payload = json.loads(payload_body)
def handle_workflow_run(payload, repo_name):
workflow_run = payload.get("workflow_run", {})
status = workflow_run.get("status")
conclusion = workflow_run.get("conclusion")
head_commit = workflow_run.get("head_commit", {}).get("id")
branch = workflow_run.get("head_branch")

event_header = request.headers.get("X-GitHub-Event")
# check if this is a push event
if event_header != "push":
if head_commit not in pending_commits.get(repo_name, set()):
return {
"status": f"X-GitHub-Event header was not set to push, got value {event_header}"
"status": f"Not in pending_commits"
}

if status != "completed" or conclusion != "success":
return {
"status": f"Committed changes did not pass requirements. Status: {status}"
}


actions_need_to_pass = True
commits = pending_commits.get(repo_name)
if commits:
pending_commits[repo_name].discard(head_commit)

# check for any empty repos
if not commits:
pending_commits.pop(repo_name)

ref = payload.get("ref", "")
branch = ref.split("/")[-1]
repo_name = payload.get("repository", {}).get("name")

key = (repo_name, branch)

if args.development and key not in config:
# if we are in development mode, pretend that
# we wanted to watch this repo no matter what
config[key] = RepoToWatch(name=repo_name, branch=branch, path="/dev/null")
config[key] = RepoToWatch(name=repo_name, branch=branch, path="/dev/null", actions_need_to_pass=actions_need_to_pass)

if key not in config:
logging.warning(f"not acting on repo and branch name of {key}")
return {"status": f"not acting on repo and branch name of {key}"}

logger.info(f"Push to {branch} detected for {repo_name}")
# update the repo
thread = threading.Thread(target=update_repo, args=(config[key],))
thread.start()
logger.info(f" Workflow_run: Stored commit {head_commit} for repo {repo_name}") # <-- log here

return {"status": "webhook received"}



@app.post("/webhook")
async def github_webhook(request: Request):
MetricsHandler.last_smee_request_timestamp.set(time.time())
payload_body = await request.body()
payload = json.loads(payload_body)
event_header = request.headers.get("X-GitHub-Event")
repo_name = payload.get("repository", {}).get("name")
actions_need_to_pass = False

if (not repo_name):
return {"status": "missing repo name"}

if event_header == "push":
head_commit = payload.get("head_commit", {}).get("id")
if not head_commit:
return {"status": "missing head_commit"}
pending_commits[repo_name].add(head_commit)
logger.info(f"Push: Stored {head_commit} for {repo_name}")

return {
"status": f"commit recorded"
}

elif event_header == "workflow_run":
logger.info(f"Event header is workflow_run for repo {repo_name}")

handle_workflow_run(payload, repo_name)

else:
return {
"status": f"X-GitHub-Event header was not set to a valid event, got value {event_header}"
}


@app.get("/metrics")
def get_metrics():
return Response(
Expand Down