-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
145 lines (125 loc) · 5.13 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
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
// imports
const core = require("@actions/core");
const github = require("@actions/github");
const { GoogleGenerativeAI } = require("@google/generative-ai");
/**
* Generate proper prompt for llm
* @param {string} title
* @param {string} description
* @param {boolean} include_emoji
* @returns
*/
const prompt = (title, description, include_emoji = false) => {
return `
# Give me conventional commit message following www.conventionalcommits.org for pull request with title and description mentioned above. Only in single Line.
## ${include_emoji ? "ALSO INCLUDE appropriate EMOJI IN STARTING OF COMMIT message" : ""}
# Documentation for conventionalcommits.org:
The Conventional Commits specification is a lightweight convention on top of commit messages. It provides an easy set of rules for creating an explicit commit history; which makes it easier to write automated tools on top of. This convention dovetails with SemVer, by describing the features, fixes, and breaking changes made in commit messages.
### The commit message should be structured as follows:
<type>[optional scope]: <description>
[optional body]
[optional footer(s)]
### The commit contains the following structural elements, to communicate intent to the consumers of your library:
fix: a commit of the type fix patches a bug in your codebase (this correlates with PATCH in Semantic Versioning).
feat: a commit of the type feat introduces a new feature to the codebase (this correlates with MINOR in Semantic Versioning).
BREAKING CHANGE: a commit that has a footer BREAKING CHANGE:, or appends a ! after the type/scope, introduces a breaking API change (correlating with MAJOR in Semantic Versioning). A BREAKING CHANGE can be part of commits of any type.
types other than fix: and feat: are allowed, for example @commitlint/config-conventional (based on the Angular convention) recommends build:, chore:, ci:, docs:, style:, refactor:, perf:, test:, and others.
footers other than BREAKING CHANGE: <description> may be provided and follow a convention similar to git trailer format.
Additional types are not mandated by the Conventional Commits specification, and have no implicit effect in Semantic Versioning (unless they include a BREAKING CHANGE). A scope may be provided to a commit's type, to provide additional contextual information and is contained within parenthesis, e.g., feat(parser): add ability to parse arrays.
# Pull request data:
Pull Request Title: ${title}
Pull Request Description: ${description}
`;
};
/**
* Logic For Workflow
*/
const initAction = async () => {
try {
/**
* Fetching inputs for actions
**/
const GEMINI_API_KEY = core.getInput("gemini_api_key", { required: true });
const token = core.getInput("token", { required: true });
const owner = github.context.repo.owner;
const repo = github.context.repo.repo;
const pull_number = github.context.payload.pull_request.number;
const includeEmoji = core.getBooleanInput("include_emoji", {
required: false,
});
/**
* Logging details
*/
core.notice(
`Repo Owner: ${owner}\nRepo Name: ${repo}\nPull Request Number: ${pull_number}\nEmojis Included: ${includeEmoji}`,
);
/**
* Inform user about emoji
*/
if (!includeEmoji) {
core.notice("Emoji shouldn't be included");
}
/**
* Now we need to create an instance of Octokit which will use to call
* GitHub's REST API endpoints.
* We will pass the token as an argument to the constructor. This token will be used to authenticate our requests.
**/
const octokit = new github.getOctokit(token);
core.debug("Created github client");
/**
* Fetching data of pull request
*/
const { data: pullReq } = await octokit.request(
`GET /repos/${owner}/${repo}/pulls/${pull_number}`,
{
owner,
repo,
pull_number,
},
);
/**
* Creating vars
*/
const prTitle = pullReq.title ?? "No Title";
const prDescription = pullReq.body ?? "No Description";
core.debug("Fetched Pull request information");
/**
* Creating new Gemini API Client Instance
* Also, new gemini-pro model
*/
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);
const gemini = genAI.getGenerativeModel({ model: "gemini-pro" });
/**
* Generating title with gemini
*/
const result = await gemini.generateContent(
prompt(prTitle, prDescription, includeEmoji),
);
/**
* Manipulating response into string
*/
const response = await result.response;
core.debug("Fetched Gemini Response");
const gemini_response = response.text();
/**
* Patching pull request with new title
*/
await octokit.request(
`PATCH /repos/${owner}/${repo}/pulls/${pull_number}`,
{
owner,
repo,
pull_number,
title: gemini_response,
},
);
core.debug("Patched pull request with new title");
} catch (error) {
/**
* If any error, inform workflow
*/
core.setFailed(error);
}
};
// Call the action
initAction();