Skip to content

Commit

Permalink
Create Daily GitHub Issues Report
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianosticca-pagopa authored Jun 13, 2024
1 parent 05c4ffc commit 3197b15
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions .github/workflows/Daily GitHub Issues Report
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
name: Daily GitHub Issues Report

on:
schedule:
- cron: '0 9 * * *' # Esegue ogni giorno alle 9:00 UTC
workflow_dispatch: # Permette di eseguire manualmente il workflow

jobs:
report:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests

- name: Generate and send report
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
run: |
import requests
import json
from datetime import datetime

GITHUB_API_URL = 'https://api.github.com/repos/pagopa/pagopa-api/issues'
GITHUB_TOKEN = '${{ secrets.GITHUB_TOKEN_ISSUES_DAILY_REPORT }}'
SLACK_WEBHOOK_URL = '${{ secrets.SLACK_WEBHOOK_URL }}'

def get_github_issues():
headers = {
'Authorization': f'token {GITHUB_TOKEN}'
}
response = requests.get(GITHUB_API_URL, headers=headers)
issues = response.json()
return issues

def filter_issues_by_label(issues, label):
filtered_issues = [issue for issue in issues if label in [lbl['name'] for lbl in issue['labels']]]
return filtered_issues

def create_slack_message(issues_by_label):
today = datetime.now().strftime('%Y-%m-%d')
message = {
"text": f"Issue Report for {today}",
"blocks": []
}

for label, issues in issues_by_label.items():
message["blocks"].append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*Label: {label}*"
}
})
message["blocks"].append({"type": "divider"})
for issue in issues:
issue_text = f"<{issue['html_url']}|{issue['title']}>"
message["blocks"].append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": issue_text
}
})
return message

def send_slack_message(message):
headers = {
'Content-Type': 'application/json'
}
response = requests.post(SLACK_WEBHOOK_URL, headers=headers, data=json.dumps(message))
if response.status_code != 200:
raise ValueError(f"Request to Slack returned an error {response.status_code}, the response is:\n{response.text}")

def main():
issues = get_github_issues()
labels = set(lbl['name'] for issue in issues for lbl in issue['labels'])
issues_by_label = {label: filter_issues_by_label(issues, label) for label in labels}
slack_message = create_slack_message(issues_by_label)
send_slack_message(slack_message)

if __name__ == "__main__":
main()

0 comments on commit 3197b15

Please sign in to comment.