-
-
Notifications
You must be signed in to change notification settings - Fork 201
153 lines (121 loc) · 5.35 KB
/
bot.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: gptme-bot
on:
issue_comment:
types: [created]
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
jobs:
check-comment:
runs-on: ubuntu-latest
outputs:
gptme_command: ${{ steps.detect_command.outputs.gptme_command }}
steps:
- name: Detect gptme command
id: detect_command
run: |
COMMENT_BODY="${{ github.event.comment.body }}"
# Check if the comment starts with "@gptme"
if [[ $COMMENT_BODY == "@gptme "* ]]; then
# Extract the command
GPTME_COMMAND=${COMMENT_BODY#"@gptme "}
echo "gptme_command=${GPTME_COMMAND}" >> $GITHUB_OUTPUT
fi
- name: Fail if author not on whitelist
if: steps.detect_command.outputs.gptme_command
run: |
WHITELIST=("ErikBjare") # Add your allowed usernames here
COMMENT_AUTHOR="${{ github.event.comment.user.login }}"
if [[ ! " ${WHITELIST[@]} " =~ " ${COMMENT_AUTHOR} " ]]; then
echo "Error: Author ${COMMENT_AUTHOR} is not on the whitelist."
exit 1
else
echo "Author ${COMMENT_AUTHOR} is on the whitelist."
fi
execute-command:
needs: check-comment
if: needs.check-comment.outputs.gptme_command
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install ctags
run: sudo apt install universal-ctags
- name: Checkout PR branch if comment is on a PR
run: |
# Fetch details about the "issue" the comment is on
DATA=$(gh api /repos/${{ github.repository }}/issues/${{ github.event.issue.number }})
# Extract whether this "issue" is a PR and
IS_PR=$(echo "$DATA" | jq -r .pull_request)
# If this is a PR, checkout its branch
if [[ "$IS_PR" != "null" ]]; then
# Fetch details about the PR the comment is on
DATA=$(gh api /repos/${{ github.repository }}/pulls/${{ github.event.issue.number }})
# Get the PR's branch name
PR_BRANCH=$(echo "$DATA" | jq -r .head.ref)
git fetch origin $PR_BRANCH
git checkout $PR_BRANCH
fi
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Install poetry
run: pipx install poetry
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'poetry'
- name: Install dependencies
run: |
make build
poetry install -E datascience
- name: Run gptme
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
GPTME_COMMAND="${{ needs.check-comment.outputs.gptme_command }}"
ISSUE_NUMBER="${{ github.event.issue.number }}"
gh issue view $ISSUE_NUMBER > issue.md
gh issue view $ISSUE_NUMBER -c > comments.md
# Run gptme with the extracted command
# NOTE: always include issue context? risk of bloating context? perhaps only include if 'issue' mentioned in command?
poetry run gptme --non-interactive "$GPTME_COMMAND" issue.md comments.md
rm issue.md comments.md
- name: Commit, push, comment
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
GPTME_COMMAND="${{ needs.check-comment.outputs.gptme_command }}"
ISSUE_TYPE="${{ github.event.issue.pull_request && 'pull_request' || 'issue' }}"
ISSUE_NUMBER="${{ github.event.issue.number }}"
REPO_NAME="${{ github.event.repository.name }}"
USER_NAME="${{ github.event.repository.owner.login }}"
BRANCH_NAME="gptme-bot-changes-$(date +'%Y%m%d%H%M%S')"
BRANCH_BASE="master"
RUN_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
COMMENT_URL="https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }}#issuecomment-${{ github.event.comment.id }}"
COMMIT_MSG="gptme '$GPTME_COMMAND'
Made in this run: $RUN_URL
Triggered by this comment: $COMMENT_URL"
git config user.name "gptme-bot"
git config user.email "[email protected]"
if [[ $ISSUE_TYPE == "pull_request" ]]; then
# Push changes to the PR branch
git add -A
git commit -m "$COMMIT_MSG"
git push
# Comment on the PR
echo "Changes have been pushed to this pull request." | gh pr comment $ISSUE_NUMBER -R $USER_NAME/$REPO_NAME --body-file=-
else
# Create a new branch and push changes
git checkout -b $BRANCH_NAME
git add -A
git commit -m "$COMMIT_MSG"
git push -u origin $BRANCH_NAME
# Some say this helps! https://github.com/cli/cli/issues/2691#issuecomment-1289521962
sleep 1
# Create a PR
PR_URL=$(gh pr create --title "Changes for issue #$ISSUE_NUMBER" --body "$COMMIT_MSG" --repo $USER_NAME/$REPO_NAME | grep -o 'https://github.com[^ ]*')
# These are redundant/implied: --base $BRANCH_BASE --head $USER_NAME:$BRANCH_NAME
# Comment on the issue with the PR link
echo "A pull request has been created for this issue: $PR_URL" | gh issue comment $ISSUE_NUMBER -R $USER_NAME/$REPO_NAME --body-file=-
fi