name: ChangeLog

on:
  workflow_dispatch:
  release:
    types: [published]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/setup-node@v2-beta
      with:
        node-version: '12'
    - uses: actions/checkout@v2
      with:
        fetch-depth: 0 
    
    - name: Checkout Tool
      uses: actions/checkout@v2
      with:
        repository: konakonall/auto-changelog
        path: 'auto-changelog'
    - name: Build Tool
      run: |
        cd auto-changelog
        npm install
        npm link
        
    - name: Generate ChangeLog
      env: # Or as an environment variable
          TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        auto-changelog --token $TOKEN
    - name: Cat ChangeLog
      run: cat CHANGELOG.md
    
    - name: Commit files
      env:
        CI_USER: "gouki0123"
        CI_EMAIL: "gouki0123@gmail.com"
      run: |
        git config --local user.email "$CI_EMAIL"
        git config --local user.name "$CI_USER"
        git add CHANGELOG.md && git commit -m 'Updated CHANGELOG.md' && echo "push=1" >> $GITHUB_ENV || echo "No changes to CHANGELOG.md"

    - name: Push changes
      if: env.push == 1
      env:
        CI_USER: "gouki0123"
        CI_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        git push "https://$CI_USER:$CI_TOKEN@github.com/$GITHUB_REPOSITORY.git" HEAD:master
