name: Core

on:
  push:
    branches: [ master ]
    paths-ignore:
    - 'app/**'
    - '**.md'
  pull_request:
    paths-ignore:
    - 'app/**'
    - '**.md'

jobs:
  build:
    runs-on: ubuntu-latest
    if: ${{ !startsWith(github.event.head_commit.message, '[skip ci]') }}

    steps:
    - uses: actions/checkout@v2
      with:
        submodules: 'recursive'
    - name: set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    - name: Get version code
      run: echo APPVEYOR_BUILD_NUMBER=$(expr $GITHUB_RUN_NUMBER + 4999) >> $GITHUB_ENV
    - name: Build with Gradle
      env:
        KEYSTORE_PASS: ${{ secrets.KEYSTORE_PASS }}
        ALIAS_NAME: ${{ secrets.ALIAS_NAME }}
        ALIAS_PASS: ${{ secrets.ALIAS_PASS }}
      run:  bash ./gradlew zipRelease zipDebug
    - name: Prepare artifact
      if: success()
      run: unzip core/release/LSPosed-v*-release.zip -d LSPosed-release;
           unzip core/release/LSPosed-v*-debug.zip -d LSPosed-debug;
    - name: Upload release
      uses: actions/upload-artifact@v2
      with:
        name: LSPosed-release
        path: './LSPosed-release/*'
    - name: Upload debug
#      if: ${{ github.event_name == 'pull_request' && success() }}
      uses: actions/upload-artifact@v2
      with:
        name: LSPosed-debug
        path: './LSPosed-debug/*'
    - name: Post to channel
      if: ${{ github.event_name != 'pull_request' && success() && github.ref == 'refs/heads/master' }}
      env:
        CHANNEL_ID: ${{ secrets.CHANNEL_ID }}
        BOT_TOKEN: ${{ secrets.BOT_TOKEN }}
        COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
      run: |
        OUTPUT="core/release/"
        export release=$(find $OUTPUT -name "LSPosed-v*-release.zip")
        export debug=$(find $OUTPUT -name "LSPosed-v*-debug.zip")
        ESCAPED=`python3 -c 'import json,os,urllib.parse; print(urllib.parse.quote(json.dumps(os.environ["COMMIT_MESSAGE"])))'`
        curl -v "https://api.telegram.org/bot${BOT_TOKEN}/sendMediaGroup?chat_id=${CHANNEL_ID}&media=%5B%7B%22type%22:%22document%22,%20%22media%22:%22attach://release%22%7D,%7B%22type%22:%22document%22,%20%22media%22:%22attach://debug%22,%22caption%22:${ESCAPED}%7D%5D"  -F release="@$release" -F debug="@$debug"
