name: Modify changelog for dependency updates

on:
  pull_request:
    types: [labeled]

jobs:
  build:
    if: github.event.label.name == 'dependencies' && github.repository == 'KonradSzwarc/devscard'
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - uses: actions/checkout@v3
        with:
          token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
      - name: Get project information
        id: projectinfo
        uses: gregoranders/nodejs-project-info@v0.0.20
      - name: Get next possible versions
        id: semvers
        uses: WyriHaximus/github-action-next-semvers@v1
        with:
          version: ${{ steps.projectinfo.outputs.version }}
      - name: Update package.json version
        uses: reedyuk/npm-version@1.2.1
        with:
          version: ${{ steps.semvers.outputs.patch }}
      - name: Update CHANGELOG.md
        uses: actions/github-script@v6
        with:
          script: |
            const script = require('.github/scripts/update-changelog.js');
            const version = '${{ steps.semvers.outputs.patch }}';
            const prNumber = '${{ github.event.pull_request.number }}';

            script(version, prNumber);
      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
