-
Notifications
You must be signed in to change notification settings - Fork 13
91 lines (84 loc) · 3.06 KB
/
bot.yaml
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
name: BOT
on:
issues:
types:
- opened
issue_comment:
types:
- created
jobs:
bot:
runs-on: ubuntu-latest
if: contains(github.event.comment.body || github.event.issue.body, '/copilot-ops')
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.18
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: go build
- uses: actions/github-script@v6
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_ORG_ID: ${{ secrets.OPENAI_ORG_ID }}
with:
script: |
if (!process.env.OPENAI_API_KEY) {
throw new Error('OPENAI_API_KEY is not set')
}
let reply = '';
const body =
context?.payload?.comment?.body ||
context?.payload?.issue?.body;
for (const line of body.split('\n')) {
if (!/^\s*\/copilot-ops\s*/.test(line)) continue;
console.log('found command line:', line);
// extracting args from the command line:
const args = [];
const re = /\s*(?:"((?:\\.|[^"])*)"|((?:\\.|[^"\s])+))\s*/y;
while (re.lastIndex !== line.length) {
const m = re.exec(line);
if (!m) {
args.length = 0;
break;
}
args.push((m[1] || m[2] || '').replace(/\\(.)/g, '$1'));
}
if (!args.length) {
console.warn('failed to parse args:', line);
reply += '\n### failed: `' + line + '`\n';
continue;
}
// executing command with args
try {
console.log('exec with args:', args);
if (reply) reply += '\n\n---\n\n';
reply += '\n### exec: `' + JSON.stringify(args) + '`\n';
const { exitCode, stdout, stderr } =
await exec.getExecOutput('./copilot-ops', args.slice(1), { ignoreReturnCode: true });
console.log('exitCode:', exitCode);
console.log('stdout:', stdout);
console.log('stderr:', stderr);
reply += '\n### exitCode:' + '\n```\n' + exitCode + '\n```\n';
reply += '\n### stdout:' + '\n```\n' + stdout + '\n```\n';
reply += '\n### stderr:' + '\n```\n' + stderr + '\n```\n';
} catch (err) {
console.warn('exec failed:', err);
reply += '\n### error:' + '\n```\n' + err.stack + '\n```\n';
}
}
if (reply) {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: reply,
});
}