File size: 2,558 Bytes
8d3475a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
name: Run Unit Tests

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  workflow_dispatch:

jobs:
  run-tests:
    runs-on: ubuntu-latest

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

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.9'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt

    - name: Run tests
      run: pytest tests/

    - name: Notify Discord
      if: always()
      env:
        DISCORD_WEBHOOK: ${{ secrets.DISCORD_WEBHOOK }}
      run: |
        if [ "${{ job.status }}" == "success" ]; then
          COLOR="3066993"
          TITLE="โœ… Unit Tests Passed"
          DESCRIPTION="All unit tests have passed successfully."
          EMOJI="๐ŸŽ‰"
        else
          COLOR="15158332"
          TITLE="โŒ Unit Tests Failed"
          DESCRIPTION="Some unit tests have failed. Please check the logs for more details."
          EMOJI="๐Ÿšจ"
        fi
        
        PAYLOAD=$(cat <<EOF
        {
          "embeds": [{
            "title": "$TITLE",
            "description": "$DESCRIPTION",
            "color": $COLOR,
            "fields": [
              {
                "name": "Repository",
                "value": "${{ github.repository }}",
                "inline": true
              },
              {
                "name": "Branch",
                "value": "${{ github.ref_name }}",
                "inline": true
              },
              {
                "name": "Triggered by",
                "value": "${{ github.event_name }}",
                "inline": true
              },
              {
                "name": "Run by",
                "value": "${{ github.actor }}",
                "inline": true
              }
            ],
            "footer": {
              "text": "$EMOJI Test run completed at $(date -u "+%Y-%m-%d %H:%M:%S UTC")"
            }
          }],
          "username": "GitHub Actions",
          "avatar_url": "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
        }
        EOF
        )
        
        RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" -H "Content-Type: application/json" -d "$PAYLOAD" $DISCORD_WEBHOOK)
        
        if [ $RESPONSE -ne 204 ]; then
          echo "Failed to send Discord notification. HTTP response code: $RESPONSE"
          exit 1
        else
          echo "Discord notification sent successfully!"
        fi