From 9c54baee5e07702842fc7605e3e263e9921c9dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Tue, 30 Jul 2024 17:20:00 +0200 Subject: [PATCH] chore: add built JS file (hook) --- dist/index.js | 68 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/dist/index.js b/dist/index.js index f0a3af3..8809eec 100644 --- a/dist/index.js +++ b/dist/index.js @@ -45310,6 +45310,9 @@ var axios_default = axios; // src/index.ts var import_sanitize_html = __toESM(require_sanitize_html(), 1); import {readFile} from "node:fs/promises"; +var freeloMention = function(username) { + return Object.keys(userPairing).includes(username) ? `
@${username}
` : `${username}`; +}; var email = core.getInput("email"); var apiKey = core.getInput("api-key"); var projectId = core.getInput("project-id"); @@ -45325,14 +45328,29 @@ var action = github.context.payload.action; var issue = github.context.payload.issue; var comment = github.context.payload.comment; var apiEndpoint = "https://api.freelo.io/v1"; -var defaultHeaders = { - "User-Agent": "Freelo GitHub Action/1.0.0", - "Content-Type": "application/json" +var defaultOptions = { + auth: { + username: email, + password: apiKey + }, + headers: { + "User-Agent": "Freelo GitHub Action/1.0.0", + "Content-Type": "application/json" + } }; var sanitizeOptions = { allowedTags: ["a", "p", "i", "b", "strong"], allowedAttributes: false }; +var userPairing = {}; +try { + for (const u of (await readFile("./.github/freelo.txt", { encoding: "utf-8" })).split("\n")) { + const p = u.split(":"); + userPairing[p[0]] = p[1]; + } +} catch (e) { + console.log("No valid freelo.txt found in .github folder, skipping"); +} try { if (!action) { throw new Error("No action was passed"); @@ -45347,21 +45365,14 @@ try { if (!tasklistId && !taskId) { throw new Error("Either task-id or tasklist-id needs to be set!"); } - let userPairing; - try { - userPairing = (await readFile("./.github/freelo.txt", { encoding: "utf-8" })).split("\n"); - } catch (e) { - console.log("No freelo.txt found in .github folder, skipping"); - } if (tasklistId) { switch (action) { case "opened": { - const author = userPairing && userPairing.filter((u) => u.includes(issue.user.login)).length > 0 ? `
@${issue.user.login}
` : `${issue.user.login}`; - console.log(userPairing?.filter((u) => u.includes(issue.user.login)).length); const taskComment = ` - Created by: ${author}
+ Created by: ${freeloMention(issue.user.login)}
Description: ${import_sanitize_html.default(issue.body ?? "None", sanitizeOptions)}
GitHub issue: #${issue.number}
+ Assigned to: ${issue.assignee ? `${freeloMention(issue.assignee.login)}` : "Nobody"}
(This action was performed automatically) `; const taskContent = { @@ -45370,13 +45381,7 @@ try { content: taskComment } }; - const res = await axios_default.post(`${apiEndpoint}/project/${projectId}/tasklist/${tasklistId}/tasks`, taskContent, { - headers: defaultHeaders, - auth: { - username: email, - password: apiKey - } - }); + const res = await axios_default.post(`${apiEndpoint}/project/${projectId}/tasklist/${tasklistId}/tasks`, taskContent, defaultOptions); if (res.status > 399) { console.error(res.data); throw new Error("Got an error response from Freelo API"); @@ -45391,8 +45396,31 @@ try { } case "edited": break; - case "closed": + case "closed": { + const comment2 = (await octokit.rest.issues.listComments({ + owner: github.context.payload.repository?.owner.login ?? "", + repo: github.context.payload.repository?.name ?? "", + issue_number: issue.number, + mediaType: { + format: "html" + } + })).data.filter((i) => i.user?.login === "github-actions" && i.user.type === "Bot"); + if (comment2.length === 0) + break; + console.log(comment2.length); + console.log(comment2[0].body_html); + const taskId2 = /https:\/\/app.freelo.io\/task\/(\d+)/.exec(comment2[0].body_html ?? ""); + if (!taskId2 || taskId2.length === 0) { + console.log("Comment found, but no Freelo task ID identified"); + break; + } + const res = await axios_default.post(`${apiEndpoint}/task/${taskId2[1]}`, null, defaultOptions); + if (res.status > 399) { + console.error(res.data); + throw new Error("Got an error response from Freelo API"); + } break; + } case "reopened": break; case "assigned":