Skip to content

Commit

Permalink
add label event (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
srz-zumix authored Aug 15, 2022
1 parent 2a0a367 commit ccb9f7d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/events.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
issue_comment:
issues:
label:
merge_group:
milestone:
page_build:
project:
Expand Down
2 changes: 2 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
source=/dev/null
disable=SC2034
56 changes: 48 additions & 8 deletions gh-act
Original file line number Diff line number Diff line change
Expand Up @@ -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}\""

}
Expand Down Expand Up @@ -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 '
Expand Down Expand Up @@ -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 '
Expand All @@ -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 '
Expand All @@ -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 '
Expand Down Expand Up @@ -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
Expand All @@ -313,17 +320,17 @@ 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%\}}

if [ -n "${ISSUE_ASSIGNEE}" ]; then
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
Expand All @@ -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
Expand All @@ -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}"
Expand Down Expand Up @@ -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}"
Expand All @@ -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"
Expand All @@ -430,4 +470,4 @@ if [ ! -f "${EVENT_PATH}" ]; then
# cat "${EVENT_PATH}"
fi

act "$@" ${ACT_OPTIONS[@]}
act "$@" "${ACT_OPTIONS[@]}"
14 changes: 14 additions & 0 deletions templates/label.json
Original file line number Diff line number Diff line change
@@ -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}
}

0 comments on commit ccb9f7d

Please sign in to comment.