Skip to content

Commit

Permalink
Add: github workflow to track issues, commits and PRs along with basi…
Browse files Browse the repository at this point in the history
…c go module
  • Loading branch information
maths-lover committed Nov 30, 2024
0 parents commit 7f49f63
Show file tree
Hide file tree
Showing 4 changed files with 756 additions and 0 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/generate_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import json
import sys


def generate_message(context):
event_name = context["event_name"]
repo_name = context["repository"]

messages = {
"push": lambda: generate_push_message(context, repo_name),
"issues": lambda: generate_issue_message(context, repo_name),
"pull_request": lambda: generate_pr_message(context, repo_name),
}

# Fallback message for unknown events
default_message = f"💡 Unknown event: {event_name} in {repo_name}."
return messages.get(event_name, lambda: default_message)()


def generate_push_message(context, repo_name):
event = context.get("event", {})
commits = event.get("commits", [])
branch = context["ref"].split("/")[
-1
] # Extract branch name from 'refs/heads/branch-name'
commit_links = [f"- [{commit['message']}]({commit['url']})" for commit in commits]
commit_messages = (
"\n".join(commit_links) if commit_links else "No commits available."
)
return f"🚀 Push to branch `{branch}` in *{repo_name}* with {len(commits)} commits:\n{commit_messages}"


def generate_issue_message(context, repo_name):
event = context.get("event", {})
issue = event.get("issue", {})
title = issue.get("title", "")
url = issue.get("html_url", "")
action = event.get("action", "")
return f"🐛 Issue *{action}* in *{repo_name}*: [{title}]({url})"


def generate_pr_message(context, repo_name):
event = context.get("event", {})
pr = event.get("pull_request", {})
title = pr.get("title", "")
url = pr.get("html_url", "")
action = event.get("action", "")
return f"📬 Pull Request *{action}* in *{repo_name}*: [{title}]({url})"


if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: generate_message.py <github_context_json>")
sys.exit(1)

with open(sys.argv[1], "r") as f:
# Load GitHub context JSON
github_context = json.loads(f.read())

# Generate and print the message
print(generate_message(github_context))
31 changes: 31 additions & 0 deletions .github/workflows/telegram-notify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Notify Telegram with Custom Output

on:
push: # Trigger for all branches
branches:
- '**'
issues: # Trigger for issue events
pull_request: # Trigger for pull request events

jobs:
notify:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Generate Custom Message
id: generate_message
run: |
echo "Generating custom message..."
echo '${{ toJson(github) }}' | jq -c '.' > github_context.json
cat github_context.json # for debugging purposes
python3 .github/workflows/generate_message.py github_context.json > message.txt
- name: Send Telegram Notification
run: |
curl -X POST "https://api.telegram.org/bot${{ secrets.TELEGRAM_TOKEN }}/sendMessage" \
-d chat_id="${{ secrets.TELEGRAM_CHAT_ID }}" \
-d parse_mode="Markdown" \
-d text="$(cat message.txt)"
Loading

0 comments on commit 7f49f63

Please sign in to comment.