# yamllint disable rule:line-length
---
name: Readme Issues linker

on:
  issues:
    types: [opened, edited, closed]
  workflow_dispatch:

jobs:
  ISSUES_linked:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout Repo
      uses: actions/checkout@v6
    - name: Assign issues
      run: |
        # Init
        echo "Starting"

        # Get issues list
        curl -s -L https://api.github.com/repos/alexbelgium/hassio-addons/issues > issueslist
        # Go through all folders, add to filters if not existing
        for f in $( find -- * -maxdepth 0 -type d | sort -r ); do
          if [ -f "$f"/config.json ] && [ -f "$f"/README.md ]; then
            
            # Clean previously reported issues
            sed -i "/Open Issue :/d" "$f"/README.md
            sed -i "/Open Request :/d" "$f"/README.md
            # If there is an issue with the addon name in title, put a message
            COUNTER=0
            while [[ $(jq -r --arg COUNTER "$COUNTER" ".[$COUNTER].title" issueslist) != null ]]; do

              #Increment counter
              (( COUNTER=COUNTER+1 )) || true

              #Get variables
              TITLE="$(jq -r --arg COUNTER "$COUNTER" ".[$COUNTER].title" issueslist)"
              TITLE="${TITLE//[<>\$\'\"]/}"
              #Check if relevant to addon
              SLUG="$(jq -r --arg f "$f" ".slug" "$f"/config.json)"
              NAME="$(jq -r --arg f "$f" ".name" "$f"/config.json)"
              if [[ "${TITLE,,}" == *"${f,,}"* ]] || [[ "${TITLE,,}" == *"${SLUG,,}"* ]] || [[ "${TITLE,,}" == *"${NAME,,}"* ]]; then
                echo "Project $TITLE, $SLUG, $NAME has an issue"
                REQ="$(jq -r --arg COUNTER "$COUNTER" ".[$COUNTER].user.login" issueslist)"
                URL="$(jq -r --arg COUNTER "$COUNTER" ".[$COUNTER].html_url" issueslist)"
                LABEL="$(jq -r --arg COUNTER "$COUNTER" ".[$COUNTER].labels[].name" issueslist)"
                DATEISSUE="$(jq -r --arg COUNTER "$COUNTER" ".[$COUNTER].created_at" issueslist)"
                DATEISSUE="${DATEISSUE%T*}"
                if [[ "$LABEL" == *"bug"* ]]; then
                  sed -i "1i ## &#9888; Open Issue : [$TITLE (opened ${DATEISSUE})]($URL) by [@${REQ}](https://github.com/$REQ)" "$f"/README.md
                elif [[ "$LABEL" == *"enhancement"* ]]; then
                  sed -i "1i ## &#9888; Open Request : [$TITLE (opened ${DATEISSUE})]($URL) by [@${REQ}](https://github.com/$REQ)" "$f"/README.md
                fi
              fi
            done
          fi
        done
        # Remove issues list
        rm issueslist
    - name: Commit if needed
      uses: EndBug/add-and-commit@v9
      with:
        message: "Github bot : issues linked to readme"
        default_author: github_actions
        commit: -u
        fetch: --force
        push: --force

