name: User-Agent Update

on:
   workflow_dispatch:
   schedule:
     - cron: '0 0 * * 0'

permissions:
   contents: write
   pull-requests: write

jobs:
   build:
     runs-on: ubuntu-latest
     steps:
       - name: Checkout Repo
         uses: actions/checkout@v4
         with:
           fetch-depth: 0

       - name: Setup golang
         uses: actions/setup-go@v4
         with:
           go-version: 1.21.x

       - name: Fetch JSONParser
         run: |
           go get github.com/buger/jsonparser
         working-directory: _uaupdate

       - name: Sort UA Data
         run: |
           go run .
         working-directory: _uaupdate

       - name: Reset gomod
         run: |
           go mod tidy
   
       - name: Create local changes
         run: |
           git add protocol/http-user-agent.txt
       - name: Commit files
         run: |
           git config --local user.email "action@github.com"
           git config --local user.name "GitHub Action"
           git commit --allow-empty -m "HTTP User Agent update"
       - name: Create Pull Request
         uses: peter-evans/create-pull-request@v6
         with:
           token: ${{ secrets.GH_TOKEN }}
           branch: ua-update
           title: HTTP User-Agent update
