-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
67 lines (56 loc) · 2.1 KB
/
index.js
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
import * as core from "@actions/core";
import * as github from "@actions/github";
import { GenerativeModel } from "@google/generative-ai";
try {
const apiKey = core.getInput("gemini-api-key");
const githubToken = core.getInput("github-token");
const octokit = github.getOctokit(githubToken);
// Check null issues
if (!github.context.issue) {
throw new Error("No issue context found");
} else {
core.debug(`Issue context: ${JSON.stringify(github.context.issue)}`);
}
const issue = await octokit.rest.issues.get({
...github.context.issue,
issue_number: github.context.issue.number,
});
const availableLabels = await octokit.rest.issues.listLabelsForRepo({
...github.context.repo,
});
const prompt = `
You have a role to manage a GitHub repository. Given an issue information (subject and body), choose suitable labels to it from the labels available for the repository.
Use the following format:
LABELS: "the names of the chosen labels, each name must not be surrounded double quotes, separated by a comma"
Only use the following labels:
\`\`\`
${JSON.stringify(availableLabels.data, null, 2)}
\`\`\`
## ISSUE ##
SUBJECT: ${issue.data.title}
BODY: ${issue.data.body}
`;
core.debug(`Prompt: ${prompt}`);
const model = new GenerativeModel(apiKey, {
model: "gemini-pro",
generationConfig: { temperature: 0 },
});
const completion = await model.generateContent(prompt);
core.debug({ completion });
let labels = /LABELS\: (.+)/g.exec(completion.response.text());
if (labels) {
labels = labels[1].trim().split(/,\s*/);
await octokit.rest.issues.setLabels({
owner: github.context.issue.owner,
repo: github.context.issue.repo,
issue_number: github.context.issue.number,
labels,
});
} else {
core.setFailed(
`Failed to propose labels: completion=${completion.data.choices[0].text}`,
);
}
} catch (error) {
core.setFailed(`Error Message: ${error.stack}`);
}