#
#  Copyright (c) 2026, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

name: Size Report

on:
  workflow_run:
    workflows: ["Size Check"]
    types:
      - completed

permissions:
  contents: read
  pull-requests: write

jobs:

  size-report:
    if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
    runs-on: ubuntu-24.04
    steps:
    - name: Download report
      uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
      with:
        name: report_pr
        path: /tmp/ot-size-report
        run-id: ${{ github.event.workflow_run.id }}
        github-token: ${{ secrets.GITHUB_TOKEN }}

    - name: Post Report
      uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3
      id: post-report
      with:
        script: |
          const fs = require('fs')

          const report = fs.readFileSync('/tmp/ot-size-report/report_pr', 'utf8');
          const pr_number = parseInt(fs.readFileSync('/tmp/ot-size-report/pr_number', 'utf8').trim());

          const params = {
              issue_number: pr_number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: report,
          }

          const response = await github.rest.issues.listComments({
            issue_number: pr_number,
            owner: context.repo.owner,
            repo: context.repo.repo,
          });

          const kMagicHeader = '<!-- Size Report of **OpenThread** -->'
          const comment = response.data.find(comment => comment.body.startsWith(kMagicHeader))

          if (comment) {
            params.comment_id = comment.id;
            await github.rest.issues.updateComment(params)
          } else {
            await github.rest.issues.createComment(params)
          }
