diff --git a/dist/index.js b/dist/index.js index d0ac9e2..d43cc8d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -47143,15 +47143,15 @@ var __webpack_exports__ = {}; */ const { getInput, setFailed, info } = __nccwpck_require__(6024); -const { getOctokit } = __nccwpck_require__(5016); +const { context, getOctokit } = __nccwpck_require__(5016); const OpenAI = __nccwpck_require__(2140); /** * @typedef {Object} ActionInputs * @property {string} language - The language (defaults to "en") * @property {string} openaiApiKey - The OpenAI API key + * @property {string} version - The release version * @property {string} [token] - The token (optional) - * @property {string} [customPrompt] - The custom prompt (optional) */ /** @@ -47159,14 +47159,16 @@ const OpenAI = __nccwpck_require__(2140); * @returns {ActionInputs} The parsed action inputs */ function parseInputs() { - const openaiApiKey = getInput("openai-api-key", { required: true }); + const openaiApiKey = getInput("openai-api-key"); const language = getInput("language"); const token = getInput("token"); + const version = getInput("version"); return { language, openaiApiKey, token, + version, }; } @@ -47175,41 +47177,77 @@ function parseInputs() { */ async function run() { info("Running ai release notes action"); - const { openaiApiKey, language, token } = parseInputs(); - // const octokit = getOctokit(token); - //const release = context.payload.release; + const { openaiApiKey, language, token, version } = parseInputs(); + const octokit = getOctokit(token); - const customPrompt = "This is a test prompt"; + if (context.eventName !== "pull_request") { + throw new Error("This action can only be run on pull requests"); + } + + // Retrieve all commits from the PR + const prNumber = context.payload.pull_request.number; + const commits = octokit.rest.pulls.listCommits({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: prNumber, + }); // Read PR merged related commits and PR body try { - /* if (!release && !customPrompt) { - throw new Error("This action can only be run on pull requests"); - } */ - // Get the release notes - const releaseNotes = customPrompt; // release.body; - info(`Release notes: ${releaseNotes}`); - info(`OpenAI key: ${openaiApiKey.slice(0, 4)}...`); // Call the OpenAI API - const openai = new OpenAI({ apiKey: openaiApiKey }); + const openai = new OpenAI({ + apiKey: openaiApiKey, + }); + + const prompt = + "You are a DEV OP enginner, your responsability is write changelog of the new software version." + + "The changelog consist on useful information about the new features and bug fixes of the software." + + "The changelog must be clear and concise, so the users can understand the changes." + + "The changelog must be written in markdown format." + + `The changelog must be written in [${language}].` + + "The changelog must use words 'add' for features, changes, improvements, updates and 'fix' for hotfixes, fixes" + + "The changelog must be written in the following structure" + + "```markdown" + + "## What's Changed" + + "- Add new feature by @user" + + "- Fix bug by @user" + + "```" + + "Do not ask for more information, use the following information to write the changelog." + + "The following information that made in this version (commit message, author):" + + `${JSON.stringify( + commits.data.map((c) => ({ + message: c.commit.message, + author: c.author.name, + authorUrl: c.author.html_url, + })), + null, + 2 + )}`; const completion = await openai.chat.completions.create({ messages: [ { role: "user", - content: - "You are a helpful assistant. Give me a fake changelog os some app, be creative.", + content: prompt, }, ], model: "gpt-4o", }); - if (completion && completion.data) { - const response = completion.data.choices[0].message.content; + if (completion) { + const response = completion.choices[0].message.content; info(`Response: ${response}`); + + // Create the release + await octokit.rest.repos.createRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + tag_name: version, + body: response, + }); } else { throw new Error("Failed to generate release notes"); }