1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GithubCLIを用いてGithub ActionsのBash Scriptでプルリクエスト作成時に自動的にAssigneesとReviewersを指定する

Last updated at Posted at 2024-01-08
#!/bin/bash

#チームメンバーのGithubをloginIDを定数で保持する
readonly member1="\"login1"\"
readonly member2="\"login2"\"
readonly member3="\"login3"\"
readonly members=(${member1} ${member2} ${member3})
assignees=()
reviewers=()

for assingnee in $(gh pr view ${{ github.event.pull_request.number }} --json assignees | jq .assignees[].login); do
  assignees+=("$assingnee")
done

for reviewer in $(gh pr view ${{ github.event.pull_request.number }} --json reviewRequests | jq .reviewRequests[].login); do
  reviewers+=("$reviewer")
done

if [ -z "${assignees[*]}" ]; then
  echo "The assignees is empty."
  #github.token (secrets.GITHUB_TOKEN) を使用した場合 --add-assignee @me にすると github-actions[bot] をアサインしようとしてエラーになる。ローカルで実行するとOK
  #gh pr edit ${{ github.event.pull_request.number }} --add-assignee "@me"
  gh pr edit ${{ github.event.pull_request.number }} --add-assignee ${{ github.event.pull_request.user.login }}
  for assingnee in $(gh pr view ${{ github.event.pull_request.number }} --json assignees | jq .assignees[].login); do
    assignees+=("$assingnee")
  done
else
  echo "The assignees is not empty."
fi

if [ -z "${reviewers[*]}" ]; then
  echo "The reviewers is empty."
  length=${#assignees[@]}
  case $length in
  0)
    echo "assigneesに要素はありません。"
    ;;
  1)
    echo "assigneesには1つの要素があります: ${assignees[0]}"
    case ${assignees[0]} in
    ${member1})
      reviewers+=(${member2})
      reviewers+=(${member3})
      ;;
    ${member2})
      reviewers+=(${member1})
      reviewers+=(${member3})
      ;;
    ${member3})
      reviewers+=(${member1})
      reviewers+=(${member2})
      ;;
    esac
    ;;
  2)
    echo "assigneesには2つの要素があります: ${assignees[0]}${assignees[1]}"
    if { [[ ${assignees[0]} == ${member1} ]] || [[ ${assignees[0]} == ${member2} ]]; } &&
      { [[ ${assignees[1]} == ${member1} ]] || [[ ${assignees[1]} == ${member2} ]]; } &&
      [[ "${assignees[0]}" != "${assignees[1]}" ]]; then
      reviewers+=(${member3})
    elif { [[ ${assignees[0]} == ${member2} ]] || [[ ${assignees[0]} == ${member3} ]]; } &&
      { [[ ${assignees[1]} == ${member2} ]] || [[ ${assignees[1]} == ${member3} ]]; } &&
      [[ "${assignees[0]}" != "${assignees[1]}" ]]; then
      reviewers+=(${member1})
    elif { [[ ${assignees[0]} == ${member1} ]] || [[ ${assignees[0]} == ${member3} ]]; } &&
      { [[ ${assignees[1]} == ${member1} ]] || [[ ${assignees[1]} == ${member3} ]]; } &&
      [[ "${assignees[0]}" != "${assignees[1]}" ]]; then
      reviewers+=(${member2})
    fi
    ;;
  *)
    echo "assigneesには2つ以上の要素があります。"
    ;;
  esac
else
  echo "The reviewers is not empty."
  length=${#reviewers[@]}
  case $length in
  0)
    echo "reviewersに要素はありません。"
    ;;
  1)
    echo "reviewersには1つの要素があります: ${reviewers[0]}"
    length=${#assignees[@]}
    case $length in
    0)
      echo "assigneesに要素はありません。"
      ;;
    1)
      echo "assigneesには1つの要素があります: ${assignees[0]}"
      if { [[ ${assignees[0]} == ${member1} ]] && [[ ${reviewers[0]} == ${member2} ]]; } ||
        { [[ ${assignees[0]} == ${member2} ]] && [[ ${reviewers[0]} == ${member1} ]]; }; then
        reviewers+=(${member3})
      elif { [[ ${assignees[0]} == ${member2} ]] && [[ ${reviewers[0]} == ${member3} ]]; } ||
        { [[ ${assignees[0]} == ${member3} ]] && [[ ${reviewers[0]} == ${member2} ]]; }; then
        reviewers+=(${member1})
      elif { [[ ${assignees[0]} == ${member1} ]] && [[ ${reviewers[0]} == ${member3} ]]; } ||
        { [[ ${assignees[0]} == ${member3} ]] && [[ ${reviewers[0]} == ${member1} ]]; }; then
        reviewers+=(${member2})
      fi
      ;;
    2)
      echo "assigneesには2つの要素があります: ${assignees[0]}${assignees[1]}"
      ;;
    *)
      echo "assigneesには2つ以上の要素があります。"
      ;;
    esac
    ;;
  2)
    echo "reviewersには2つの要素があります: ${assignees[0]}${assignees[1]}"
    ;;
  *)
    echo "reviewersには2つ以上の要素があります。"
    ;;
  esac
fi

for reviewer in ${reviewers[@]}; do
  echo "reviewer: ${reviewer}"
  gh pr edit ${{ github.event.pull_request.number }} --add-reviewer ${reviewer}
done

echo "member: ${members[@]}"
echo "assignees: ${assignees[@]}"
echo "reviewers: ${reviewers[@]}"

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?