---
resource_types:
  - name: http-resource
    type: registry-image
    source:
      repository: redxef/concourse-http-resource
      tag: edge

resources:
- name: source
  type: git
  source:
    uri: https://git.redxef.at/redxef/jellyfin-smart-playlist
    branch: main
    fetch_tags: true
    tag_regex: 'v.*'
- name: manifest
  type: git
  source:
    uri: ssh://git@git.redxef.at:8022/redxef/jellyfin-smart-playlist.git
    branch: manifest
    private_key: ((gitea.id_ed25519))
- name: releases
  type: http-resource
  source:
    url: https://git.redxef.at/api/v1/repos/redxef/jellyfin-smart-playlist/releases
    method: get
    auth:
      basic:
        username: ((gitea.username))
        password: ((gitea.token))
    version_check_method: none
- name: artifact
  type: http-resource
  source:
    url: https://git.redxef.at/api/v1/repos/redxef/jellyfin-smart-playlist/releases/((.:release_id))/assets
    method: get
    auth:
      basic:
        username: ((gitea.username))
        password: ((gitea.token))
    headers:
    - - Accept
      - application/json
    query:
    - - name
      - smart-playlist_((.:version)).zip
    body:
      multipart:
        files:
          attachment: artifacts/smart-playlist_((.:version)).zip
    version_check_method: none


jobs:
- name: release
  plan:
  - get: source
    trigger: true
  - get: manifest
  - get: releases
  - task: run-build
    config:
      platform: linux
      image_resource:
        type: registry-image
        source:
          repository: alpine
      inputs:
      - name: source
      - name: manifest
      - name: releases
      outputs:
      - name: artifacts
      - name: manifest
      run:
        path: sh
        args:
        - -c
        - |
          #!/bin/sh
          set -eu
          apk add --no-cache jq git dotnet8-sdk python3 py3-pip
          pip3 install --break-system-packages jprm
          cp manifest/manifest.json source/
          (
            cd source
            ./release.sh build
          )
          cp source/artifacts/*.zip artifacts/
          cd source
          VERSION="$(./release.sh version)"
          cd ..
          printf '%s' "$VERSION" > artifacts/version
          cp source/manifest.json manifest/
          (
            cd manifest
            git add .
            git config user.name 'Concourse CI/CD'
            git config user.email 'concourse@redxef.at'
            git commit -m "ci: update manifest for version ${VERSION}."
          )
          jq --arg tag "v$VERSION" -r '.[] | select(.tag_name == $tag) | .id' < releases/body > artifacts/release_id
          ls artifacts
  - load_var: release_id
    file: artifacts/release_id
    reveal: true
  - load_var: version
    file: artifacts/version
    reveal: true
  - put: artifact
    inputs:
    - artifacts
    params:
      method: post
  - put: manifest
    params:
      repository: manifest