# This reusable workflow is used by actions/*-versions repositories
# It is designed to validate the versions-manifest.json file
# The 'SLACK_CHANNEL_URL' secret must be added to the repository containing the caller workflow 
# in order to publish messages to Slack

name: Validate manifest
on:
  workflow_call:
    inputs:
      tool-name:
        description: 'Name of the tool for which manifest is validated'
        required: true
        type: string
      image-url:
        description: 'Tool image to be attached to Slack posts'
        required: true
        type: string
        
defaults:
  run:
    shell: pwsh

jobs:
  validation:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true

      - name: Validate manifest
        run: .\helpers\packages-generation\manifest-validator.ps1 -ManifestPath '.\versions-manifest.json'

  check_build:
    name: Check validation for failures 
    runs-on: ubuntu-latest
    needs: [validation]
    if: failure()
    steps:
      - uses: actions/checkout@v3
        with:
          submodules: true

      - name: Send Slack notification if validation fails
        run: |
          $pipelineUrl = "$env:GITHUB_SERVER_URL/$env:GITHUB_REPOSITORY/actions/runs/$env:GITHUB_RUN_ID"
          $message = "The validation of ${{ inputs.tool-name }} manifest failed. \nLink to the pipeline: $pipelineUrl"
          .\helpers\get-new-tool-versions\send-slack-notification.ps1 -Url "${{ secrets.SLACK_CHANNEL_URL }}" `
                                                                      -ToolName "${{ inputs.tool-name }}" `
                                                                      -Text "$message" `
                                                                      -ImageUrl "${{ inputs.image-url }}"