name: Notify Merged PR to AI Log Agent

on:
  push:
    branches:
      - master

jobs:
  find-pr-details:
    runs-on: ubuntu-latest
    outputs:
      url: ${{ steps.pr.outputs.url }}
    steps:
      - name: Get merged PR info
        id: pr
        uses: actions/github-script@v7
        with:
          script: |
            const commitSha = context.sha;

            const prs = await github.rest.pulls.list({
              owner: context.repo.owner,
              repo: context.repo.repo,
              state: 'closed',
              sort: 'updated',
              direction: 'desc',
              per_page: 10
            });

            const mergedPr = prs.data.find(pr =>
              pr.merge_commit_sha === commitSha
            );

            if (!mergedPr) {
              core.setFailed("No merged PR found for this commit.");
              return;
            }

            core.info(`Found merged PR: ${mergedPr.html_url} (#${mergedPr.number})`);
            core.setOutput("url", mergedPr.html_url);
  invoke-ai-log-agent:
    name: Invoke AI Log Agent
    needs: find-pr-details
    uses: wso2/wso2-organization-templates/.github/workflows/log-pr-comment-analyzer.yml@main
    with:
      prUrl: ${{ needs.find-pr-details.outputs.url }}
    secrets:
      clientId: ${{ secrets.AI_LOG_AGENT_CLIENT_ID }}
      clientSecret: ${{ secrets.AI_LOG_AGENT_CLIENT_SECRET }}
