我有一个 jq 进程,它查看 JSON 文件的值,检查一些内容,然后确定当前对象是否可用于执行某些操作。 我现在遇到的问题是,我可以让它遍历所有项目,但是当它将“可用”元素存储到我的变量中时,它将它们存储为
,
逗号分隔列表,但它不会存储到我的 GitHub CLI 环境变量。
这是我的 GitHub 工作内容:
- name: Process Pull Requests
id: process-prs
run: |
CURRENT_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "Current Time: $CURRENT_TIME"
# Process each PR
READY_FOR_RELEASE_PRs=$(
jq -c '.' pr_list.json | while read -r pr; do
PR_NUMBER=$(echo "$pr" | jq -r '.number')
UPDATED_AT=$(echo "$pr" | jq -r '.updatedAt')
# Check if the PR was updated more than an hour ago
UPDATED_AT_TIMESTAMP=$(date -d "$UPDATED_AT" +%s)
CURRENT_TIMESTAMP=$(date -u +%s)
DIFF_IN_SECS=$((CURRENT_TIMESTAMP - UPDATED_AT_TIMESTAMP))
if [ "$DIFF_IN_SECS" -lt 3600 ]; then
continue
fi
# Get our merge state status so we know if we can apply tags to it.
REVIEW_DECISION=$(gh pr view "$PR_NUMBER" --json reviewDecision --jq .reviewDecision)
# echo "Review Decision: $REVIEW_DECISION"
# Check to make sure that the PR isn't blocked from being merged.
if [ "$REVIEW_DECISION" = "APPROVED" ]; then
# echo "PR #$PR_NUMBER meets criteria for 'Ready for Release'."
# prs+=("$PR_NUMBER")
# echo "Ready for Release PRs Array: ${prs[@]}"
# echo "READY_PRS=${READY_PRS}${READY_PRS:+,}$PR_NUMBER" >> $GITHUB_ENV
echo "$PR_NUMBER,"
fi
done
)
# Output the list of PRs ready for release
echo "Ready for Release PRs: $READY_FOR_RELEASE_PRs"
echo "ready_prs=${READY_FOR_RELEASE_PRs[@]}" >> $GITHUB_ENV
我已经尝试了几件事,所有这些我都不太记得了,但我希望实现以下任一目标:a)我可以在工作的下一部分中循环遍历的元素数组;或 b) 一组以逗号分隔的值。 这是该过程的下一步:
- name: Add "Ready for Release" Label
if: ${{ env.ready_prs != '' }}
run: |
echo "Ready for Release PRs: ${{ env.ready_prs }}"
for PR_NUMBER in ${{ env.ready_prs }}; do
echo "Adding 'Ready for Release' label to PR #$PR_NUMBER"
done
--- 更新 ---
我已经更新了代码以查看正确的变量,因为第一个变量输入错误。 这是我得到的:
Ready for Release PRs: 3307,
3176,
Error: Unable to process file command 'env' successfully.
Error: Invalid format '3176,'
所以,对于我得到的结果来说,是
3307,
3176,
我期望得到的是
3307,3306
这些是拉取请求 ID,我需要在工作的下一部分中解析它们。
改变:
echo "$PR_NUMBER,"
到
echo -n "$PR_NUMBER,"
不要在末尾添加换行符。这将使其成为单行而不是多行。这应该适合您的用例。但是,您需要注意后面的逗号。我相信你可以轻松应对。
除此之外,您正在处理的步骤似乎不处理 CSV 值。您也需要更新。
但是,这只能通过
gh
和 jq
来完成。
这是一个 CSV 示例(live):
gh pr list \
--json number,updatedAt,reviewDecision \
--jq '. | map(select((.updatedAt | fromdate - now > 3600) and (.reviewDecision == "APPROVED"))) | [.[].number] | @csv'
对于此示例 JSON:
[
{
"number": 357,
"reviewDecision": "APPROVED",
"updatedAt": "2025-01-14T23:26:30Z"
},
{
"number": 354,
"reviewDecision": "",
"updatedAt": "2024-12-20T22:43:01Z"
},
{
"number": 342,
"reviewDecision": "",
"updatedAt": "2025-01-06T04:35:49Z"
},
{
"number": 341,
"reviewDecision": "",
"updatedAt": "2024-12-19T07:36:12Z"
},
{
"number": 336,
"reviewDecision": "",
"updatedAt": "2024-12-20T15:25:57Z"
},
{
"number": 283,
"reviewDecision": "",
"updatedAt": "2024-11-12T13:49:23Z"
},
{
"number": 263,
"reviewDecision": "APPROVED",
"updatedAt": "2025-01-14T09:51:00Z"
}
]
输出为:
"357,263"
对于循环,您可以使用空格分隔的值,即使用
join(" ")
而不是 @csv
:
gh pr list \
--json number,updatedAt,reviewDecision \
--jq '. | map(select((.updatedAt | fromdate - now > 3600) and (.reviewDecision == "APPROVED"))) | [.[].number] | join(" ")'
输出:
"357 263"
如果需要,您可以删除周围的双引号:
tr -d '"'