我正在使用 action 构建一个 github CI,并且能够运行 pylint 并将结果写入 PR 消息。但是,我想解析确切的分数,这样如果分数低于某个阈值,我就可以使操作失败,这是我的相关代码:
- name: Lint with pylint
working-directory: ./
run: |
echo '${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }}'
pip install pylint
OUTPUT=$(pylint ${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }} --exit-zero --jobs=0)
SCORE=$OUTPUT > sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p'
echo "Pylint finished with score: $SCORE"
echo 'MESSAGE<<EOF' >> $GITHUB_ENV
echo "$OUTPUT" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
解析当前保存为 $OUTPUT 的 pylint 结果并解析精确分数以将其保存在 $SCORE 中的命令是什么?
OUTPUT=$(pylint ${{ steps.files.outputs.files_updated }} ${{ steps.files.outputs.files_created }} --exit-zero --jobs=0)
SCORE=$OUTPUT > sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p'
这是输出示例,我需要解析“0.18”部分:
************* Module src.server
src/server.py:1:0: C0114: Missing module docstring (missing-module-docstring)
src/server.py:5:0: W0401: Wildcard import src.endpoints (wildcard-import)
...
-----------------------------------
Your code has been rated at 0.18/10
答案1
使用sed
SCORE=$(sed -n '$s/[^0-9]*\([0-9.]*\).*/\1/p' <<< "$OUTPUT")