# Send commits info to SCST mailing list on push
name: Mailing list

on:
  push:
    branches:
      - 3.8.x
      - master

jobs:
  send_mails:
    name: ${{matrix.commit.message}}
    if: github.repository == 'SCST-project/scst'
    runs-on: ubuntu-latest
    strategy:
      max-parallel: 1
      matrix:
        commit: ${{github.event.commits}}
    steps:
      - name: Checkout Project
        uses: actions/checkout@main
        with:
          ref: ${{matrix.commit.id}}
          fetch-depth: 2

      - name: Get short ref
        id: get_short_ref
        run: |
          echo "short_ref=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT

      - name: Get commit id
        id: get_commit_id
        run: |
          echo "commit_id=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

      - name: Get commit message
        id: get_commit_message
        run: |
          echo "commit_message=$(git log --format=%s -n 1 HEAD)" >> $GITHUB_OUTPUT

      - name: Get list of files
        id: get_list_files
        run: |
          EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)

          echo "files<<$EOF" >> $GITHUB_OUTPUT
          echo "$(git diff-tree --no-commit-id --stat --stat-name-width=60 --stat-graph-width=15 -r HEAD)" >> $GITHUB_OUTPUT
          echo "$EOF" >> $GITHUB_OUTPUT

      - name: Get commit diff
        id: get_commit_diff
        run: |
          if [ $(git show --no-patch --format="%P" | wc -w) -eq 1 ]; then
            EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)

            echo "commit_diff<<$EOF" >> $GITHUB_OUTPUT
            echo "$(git --no-pager diff -p HEAD^1)" >> $GITHUB_OUTPUT
            echo "$EOF" >> $GITHUB_OUTPUT
          fi

      - name: Send mail
        uses: dawidd6/action-send-mail@v3
        with:
          # Required mail server address:
          server_address: smtp.yandex.com
          # Required mail server port:
          server_port: 465
          # Optional (recommended): mail server username:
          username: ${{secrets.MAIL_SERVER_USERNAME}}
          # Optional (recommended) mail server password:
          password: ${{secrets.MAIL_SERVER_PASSWORD}}
          # Required mail subject:
          subject: |
            [${{steps.get_short_ref.outputs.short_ref}}] ${{steps.get_commit_message.outputs.commit_message}}
          # Required recipients' addresses:
          to: scst-svn@lists.sourceforge.net
          # Required sender full name (address can be skipped):
          from: ${{matrix.commit.committer.name}}
          # Optional whether this connection use TLS (default is true if server_port is 465)
          # secure: true

          # Optional plain body:
          body: |
            Commit: ${{steps.get_commit_id.outputs.commit_id}}
            GitHub URL: ${{matrix.commit.url}}
            Author: ${{matrix.commit.author.name}}
            Date: ${{matrix.commit.timestamp}}
            Log Message:
            -----------
            ${{matrix.commit.message}}

            Modified Paths:
            --------------
            ${{steps.get_list_files.outputs.files}}

            ===================================================================
            ${{steps.get_commit_diff.outputs.commit_diff}}

          # Optional unsigned/invalid certificates allowance:
          ignore_cert: true
          # Optional converting Markdown to HTML (set content_type to text/html too):
          # convert_markdown: true
          # Optional attachments:
          # attachments: attachments.zip,git.diff,./dist/static/*.js
          # Optional priority: 'high', 'normal' (default) or 'low'
          # priority: low
