-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: github workflow to track issues, commits and PRs along with basi…
…c go module
- Loading branch information
0 parents
commit 7f49f63
Showing
4 changed files
with
756 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" |
Oops, something went wrong.