diff --git a/.github/workflows/Daily GitHub Issues Report b/.github/workflows/Daily GitHub Issues Report new file mode 100644 index 0000000..7377588 --- /dev/null +++ b/.github/workflows/Daily GitHub Issues Report @@ -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()