From ccb9f7da276bbd664af94263fa07922a8c34e081 Mon Sep 17 00:00:00 2001 From: srz_zumix Date: Tue, 16 Aug 2022 00:18:13 +0900 Subject: [PATCH] add label event (#18) --- .github/workflows/events.yml | 1 + .shellcheckrc | 2 ++ gh-act | 56 ++++++++++++++++++++++++++++++------ templates/label.json | 14 +++++++++ 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 .shellcheckrc create mode 100644 templates/label.json diff --git a/.github/workflows/events.yml b/.github/workflows/events.yml index 51ab104..371704c 100644 --- a/.github/workflows/events.yml +++ b/.github/workflows/events.yml @@ -14,6 +14,7 @@ on: issue_comment: issues: label: + merge_group: milestone: page_build: project: diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..3978a03 --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1,2 @@ +source=/dev/null +disable=SC2034 diff --git a/gh-act b/gh-act index 92077f2..92a8321 100755 --- a/gh-act +++ b/gh-act @@ -84,7 +84,7 @@ parse_params() { parse_params "$@" function parts_json { - TEMPLATE=$(cat "${SCRIPT_DIR}/templates/parts/$1.json" | sed -e 's/"/\\"/g') + TEMPLATE=$(sed -e 's/"/\\"/g' "${SCRIPT_DIR}/templates/parts/$1.json") eval "echo -E \"${TEMPLATE}\"" } @@ -130,6 +130,7 @@ function repo_json { function pr_env { ACT_ENV="${TEMPFILE_DIR}/.pr.env" + # shellcheck disable=SC2016,SC2086 gh pr view ${1:-} --json \ additions,baseRefName,body,closedAt,comments,commits,createdAt,deletions,files,headRefName,headRepositoryOwner,headRepository,id,isDraft,maintainerCanModify,mergedAt,mergedBy,milestone,number,reviews,state,title,updatedAt,url \ --template ' @@ -165,6 +166,7 @@ PR_URL="{{.url}}" function pr_review_env { # FIXME: PR_REVIEW_ID ACT_ENV="${TEMPFILE_DIR}/.pr_review.env" + # shellcheck disable=SC2016,SC2086 gh pr view ${1:-} --json \ reviews \ --template ' @@ -183,6 +185,7 @@ PR_REVIEW_STATE="{{$last_review.state}}" function issue_env { ACT_ENV="${TEMPFILE_DIR}/.issue.env" + # shellcheck disable=SC2016 gh issue view "$1" --json \ author,assignees,body,closedAt,comments,createdAt,id,reactionGroups,state,title,updatedAt,url \ --template ' @@ -206,6 +209,7 @@ ISSUE_REACTION_TOTAL_COUNT=$((0{{range $index, $element := .reactionGroups}}+{{$ function issue_comment_env { ACT_ENV="${TEMPFILE_DIR}/.issue.comment.env" + # shellcheck disable=SC2016 gh issue view "$1" --json \ comments \ --template ' @@ -278,15 +282,18 @@ function pull_request_event_envs() { fi pr_env "${PULL_REQUEST_SPEC}" + # shellcheck disable=SC2086 HEAD=$(gh pr view ${PULL_REQUEST_SPEC} --json commits --jq .commits[].oid | tail -1) + # shellcheck disable=SC2086 HEAD_USER=$(gh pr view ${PULL_REQUEST_SPEC} --json commits --jq .commits[].authors[].login | tail -1) + # shellcheck disable=SC2086 BEFORE=$(gh pr view ${PULL_REQUEST_SPEC} --json commits --jq .commits[].oid | tail -2 | head -1) # base repo_env "${OWNER}/${REPO}" "BASE" BASE_USER_JSON=$(user_json "${OWNER}") BASE_REPO_JSON=$(repo_json "${OWNER}" "${REPO}" "${BASE_USER_JSON}") - BASE_SHA=$(gh api --hostname ${GITHUB_HOSTNAME} --method GET -H "Accept: application/vnd.github.v3+json" /repos/${OWNER}/${REPO}/git/refs/heads/${BASE_REF} --jq .object.sha) + BASE_SHA=$(gh api --hostname "${GITHUB_HOSTNAME}" --method GET -H "Accept: application/vnd.github.v3+json" "/repos/${OWNER}/${REPO}/git/refs/heads/${BASE_REF}" --jq .object.sha) if [[ "${EVENT_NAME}" =~ .*review.* ]]; then if [ "${PR_REVIEWS}" == "0" ]; then @@ -313,7 +320,7 @@ function issue_event_envs() { issue_env "${ISSUE_NUMBER}" - ISSUE_LABELS=$(gh issue view ${ISSUE_NUMBER} --json labels) + ISSUE_LABELS=$(gh issue view "${ISSUE_NUMBER}" --json labels) ISSUE_LABELS=${ISSUE_LABELS#{} ISSUE_LABELS=${ISSUE_LABELS%\}} @@ -321,9 +328,9 @@ function issue_event_envs() { ASSIGNEE_USER_JSON=$(user_json "${ISSUE_ASSIGNEE}") fi if [ -n "${ISSUE_ASSIGNEES}" ]; then - for assignee in "${ISSUE_ASSIGNEES}"; do + for assignee in ${ISSUE_ASSIGNEES}; do if [ -n "${ASSIGNEES_JSON:-}" ]; then - ASSIGNEES_JSON+="," + ASSIGNEES_JSON+="," fi ASSIGNEES_JSON+=$(user_json "${assignee}") done @@ -348,6 +355,36 @@ function issue_event_envs() { } +function label_event_envs() { + LABEL_COUNT=$(gh label list --json name --jq length) + if [ "${LABEL_COUNT}" == "0" ]; then + LABEL_COLOR="d73a4a" + LABEL_DESCRIPTION="gh-act label" + LABEL_NODE_ID="" + LABEL_IS_DEFAULT="false" + LABEL_NAME="gh act" + LABEL_URL="https://${GITHUB_API}/repos/${OWNER}/${REPO}/labels/gh%20act" + else + ACT_ENV="${TEMPFILE_DIR}/.label.env" + # shellcheck disable=SC2016 + gh label list --json \ + color,description,id,isDefault,name,url \ + --template ' +{{$last := 0}} +{{range $index, $element := .}}{{$last = $index}}{{end}} +{{$label := index . $last}} +LABEL_COLOR="{{$label.color}}" +LABEL_DESCRIPTION="{{$label.description | js}}" +LABEL_NODE_ID="{{$label.id}}" +LABEL_IS_DEFAULT="{{$label.isDefault}}" +LABEL_NAME="{{$label.name}}" +LABEL_URL="{{$label.url}}" +' | filter_env > "${ACT_ENV}" + . "${ACT_ENV}" + fi +} + + function create_event_json() { EVENT_FILE_PATH="$1" if [ -f "${SCRIPT_DIR}/templates/${EVENT_NAME}.json" ]; then @@ -366,6 +403,9 @@ function create_event_json() { COMMIT_MESSAGE=$(git log -1 --pretty=format:"%B%-" | sed -e 's/$/\\\\n/g' | tr -d '\n' | sed -e 's/\\\\n$//g') COMMITS=$(git log -1 --pretty=format:"{%n \"author\": {%n \"email\": \"%ae\",%n \"name\":\"%an\",%n \"username\":\"%an\"%n },%n \"committer\":{%n \"email\":\"%ce\",%n \"name\":\"%cn\",%n \"username\":\"%cn\"%n },%n \"distinct\":true,%n \"id\":\"%H\",%n \"message\":\"${COMMIT_MESSAGE}\",%n \"timestamp\":\"%cI\",%n \"tree_id\":\"%T\",%n \"url\":\"https://${GITHUB_HOSTNAME}/${OWNER}/${REPO}/commit/%H\"%n}") fi + if [ "${EVENT_NAME}" == "label" ]; then + label_event_envs + fi HEAD=$(git rev-parse HEAD) BEFORE=$(git rev-parse HEAD^) HEAD_OWNER="${OWNER}" @@ -394,7 +434,7 @@ function create_event_json() { fi fi - TEMPLATE=$(cat "${SCRIPT_DIR}/templates/${EVENT_NAME}.json" | sed -e 's/"/\\"/g') + TEMPLATE=$(sed -e 's/"/\\"/g' "${SCRIPT_DIR}/templates/${EVENT_NAME}.json") eval "echo -E \"${TEMPLATE}\"" | sed -e 's/"null"/null/g' > "${EVENT_FILE_PATH}" else echo "{}" > "${EVENT_FILE_PATH}" @@ -419,7 +459,7 @@ fi if [ ! -f "${EVENT_PATH}" ]; then TEMPFILE_DIR=$(mktemp -d gh-act.XXXXXX) - trap "rm -rf ${TEMPFILE_DIR}" EXIT + trap 'rm -rf "${TEMPFILE_DIR}"' EXIT if [ -z "${EVENT_PATH}" ]; then EVENT_PATH="${TEMPFILE_DIR}/event.json" @@ -430,4 +470,4 @@ if [ ! -f "${EVENT_PATH}" ]; then # cat "${EVENT_PATH}" fi -act "$@" ${ACT_OPTIONS[@]} +act "$@" "${ACT_OPTIONS[@]}" diff --git a/templates/label.json b/templates/label.json new file mode 100644 index 0000000..4817e46 --- /dev/null +++ b/templates/label.json @@ -0,0 +1,14 @@ +{ + "action": "created", + "label": { + "color": "${LABEL_COLOR}", + "default": ${LABEL_IS_DEFAULT}, + "description": "${LABEL_DESCRIPTION}", + "id": 0, + "name": "${LABEL_NAME}", + "node_id": "${LABEL_NODE_ID}", + "url": "${LABEL_URL}" + }, + "repository": ${HEAD_REPO_JSON}, + "sender": ${HEAD_USER_JSON} +}