# This workflow is centrally managed in https://github.com/asyncapi/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo

# Purpose of this workflow is to enable anyone to label PR with the following labels:
# `ready-to-merge` and `do-not-merge` labels to get stuff merged or blocked from merging
# `autoupdate` to keep a branch up-to-date with the target branch

name: Label PRs # if proper comment added

on:
  issue_comment:
    types:
      - created

jobs:
  add-ready-to-merge-label:
    if: >
      github.event.issue.pull_request && 
      github.event.issue.state != 'closed' && 
      github.actor != 'asyncapi-bot' && 
      (
        contains(github.event.comment.body, '/ready-to-merge') || 
        contains(github.event.comment.body, '/rtm' )
      )

    runs-on: ubuntu-latest
    steps:
      - name: Add ready-to-merge label
        uses: actions/github-script@v7
        env:
          GITHUB_ACTOR: ${{ github.actor }}
        with:
          github-token: ${{ secrets.GH_TOKEN }}
          script: |
            const prDetailsUrl = context.payload.issue.pull_request.url;
            const { data: pull } = await github.request(prDetailsUrl);
            const { draft: isDraft} = pull;
            if(!isDraft) {
              console.log('adding ready-to-merge label...');
              github.rest.issues.addLabels({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                labels: ['ready-to-merge']
              })  
            }

            const { data: comparison } =
            await github.rest.repos.compareCommitsWithBasehead({
              owner: pull.head.repo.owner.login,
              repo: pull.head.repo.name,
              basehead: `${pull.base.label}...${pull.head.label}`,
            });
            if (comparison.behind_by !== 0 && pull.mergeable_state === 'behind') {
              console.log(`This branch is behind the target by ${comparison.behind_by} commits`)
              console.log('adding out-of-date comment...');
              github.rest.issues.createComment({
                issue_number: context.issue.number,
                owner: context.repo.owner,
                repo: context.repo.repo,
                body: `Hello, @${process.env.GITHUB_ACTOR}! 👋🏼
                       This PR is not up to date with the base branch and can't be merged.
                       Please update your branch manually with the latest version of the base branch.
                       PRO-TIP: To request an update from the upstream branch, simply comment \`/u\` or \`/update\` and our bot will handle the update operation promptly.
                       
                       The only requirement for this to work is to enable [Allow edits from maintainers](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork) option in your PR. Also the update will not work if your fork is located in an organization, not under your personal profile.
                       Thanks 😄`
              })
            }

  add-do-not-merge-label:
    if: >
      github.event.issue.pull_request &&
      github.event.issue.state != 'closed' &&
      github.actor != 'asyncapi-bot' &&
      (
        contains(github.event.comment.body, '/do-not-merge') ||
        contains(github.event.comment.body, '/dnm' )
      )
    runs-on: ubuntu-latest
    steps:
      - name: Add do-not-merge label
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GH_TOKEN }}
          script: |
            github.rest.issues.addLabels({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              labels: ['do-not-merge']
            })
  add-autoupdate-label:
    if: >
      github.event.issue.pull_request && 
      github.event.issue.state != 'closed' && 
      github.actor != 'asyncapi-bot' &&
      (
        contains(github.event.comment.body, '/autoupdate') ||
        contains(github.event.comment.body, '/au' )
      )
    runs-on: ubuntu-latest
    steps:
      - name: Add autoupdate label
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GH_TOKEN }}
          script: |
            github.rest.issues.addLabels({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              labels: ['autoupdate']
            })
