-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
125 lines (110 loc) · 4.2 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
name: 'Telnyx PR Reviewer'
description: 'A GitHub Action to review pull requests using Telnyx AI'
branding:
icon: 'check-circle'
color: 'green'
author: 'team-telnyx'
inputs:
telnyx_api_key:
description: 'Telnyx API Key'
required: true
model_name:
description: 'Model Name'
default: 'meta-llama/Meta-Llama-3.1-8B-Instruct'
custom_message:
description: 'Custom message for the AI review prompt'
required: false
runs:
using: 'composite'
steps:
- name: Install prerequisites
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y jq curl
- name: Review PR
shell: bash
run: |
echo "Reviewing PR in $GITHUB_REPOSITORY"
# Check if TELNYX_API_KEY is set
if [ -z "$TELNYX_API_KEY" ]; then
echo "Error: TELNYX_API_KEY environment variable is not set."
exit 1
fi
echo "Fetching PR diff"
# Get the diff of the current branch against the base branch
set -eo pipefail
# Create a temporary file to store the diff
DIFF_FILE=$(mktemp)
PROCESSED_FILE=$(mktemp)
trap 'rm -f "$DIFF_FILE" "$PROCESSED_FILE"' EXIT
# Fetch the diff into the temp file
if ! gh pr diff ${{ github.event.number }} --patch > "$DIFF_FILE"; then
echo "::error::Failed to fetch PR diff"
exit 1
fi
# Remove empty lines and store in intermediate file
if ! grep -vE "^$" "$DIFF_FILE" > "$PROCESSED_FILE"; then
echo "::error::Failed to process PR diff"
exit 1
fi
# Truncate and process the diff
if ! pr_diff=$(dd if="$PROCESSED_FILE" bs=100000 count=1 2>/dev/null | \
jq -Rs . | \
sed -E 's/(^"|"$)//g'); then
echo "::error::Failed to format PR diff"
exit 1
fi
# Check if the diff was truncated
original_size=$(wc -c < "$PROCESSED_FILE")
if [ "$original_size" -gt 100000 ]; then
echo "Note: PR diff was truncated to 100KB for processing."
fi
if [ -z "$pr_diff" ]; then
echo "::error::PR diff is empty"
exit 1
fi
# Use custom message if provided, otherwise use the default message
if [ -n "$CUSTOM_MESSAGE" ]; then
review_message=$(echo "$CUSTOM_MESSAGE" | grep -vE "^$" | jq -Rsa . | sed -E 's/(^.)|(.$)//g')
else
review_message="Review this PR for me, be descriptive but straightforward, tell me what it does, a brief summary of the changes and if there are any reasons to reject this PR:"
fi
echo "Building payload"
payload_file=$(mktemp)
cat > "$payload_file" <<EOF
{
"model": "$MODEL_NAME",
"messages": [
{"role": "system", "content": "$review_message"},
{"role": "user", "content": "$pr_diff"}
]
}
EOF
# Query TelnyxInference and extract only the response content
response=$(curl -s https://api.telnyx.com/v2/ai/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TELNYX_API_KEY" \
-d "@$payload_file" | jq -r ".choices[].message.content")
# Clean up temp file
rm -f "$payload_file"
if [ $? -ne '0' ]; then
echo "Unable to contact TelnyxInference"
echo "Exiting with status 0 so we don't block merges and this is probably caused by a large diff"
exit 0
fi
# Publish the review comment using gh CLI
set +e
gh pr comment ${{ github.event.number }} -b "Automated $MODEL_NAME review: $response" --edit-last
if [ $? -ne '0' ]; then
gh pr comment ${{ github.event.number }} -b "Automated $MODEL_NAME review: $response"
fi
set -e
env:
TELNYX_API_KEY: ${{ inputs.telnyx_api_key }}
MODEL_NAME: ${{ inputs.model_name }}
CUSTOM_MESSAGE: ${{ inputs.custom_message }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_REPOSITORY: ${{ github.repository }}
GITHUB_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}