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K,GACtB,OAAI,GAAM,EAAG,QAAU,EACZ,EAAG,GAAG,OAEV,IACX,CAQA,cAAA,CAAe,GACX,MAAO,GAAG,KAAK,IAAI,uBAAuB,KAAK,IAAI,OAAO,GAC9D,CAaA,2BAAM,CAAsB,EAAK,GAC7B,MAAM,QAAiB,KAAK,cACtB,EAAkB,KAAK,mBAAmB,GAC1C,EAAmB,mBAAgB,aAAY,kBAAqB,WAC1E,IAAI,EAAU,EACd,GAAI,EAAiB,CACjB,EAAU,EAAgB,KAC1B,MAAM,EAAO,KAAK,cAAc,EAAK,EAAgB,MAEjD,EADA,EACU,EAAQ,QAAQ,EAAM,GAGtB,GAAG,MAAY,GAEjC,CACA,IACI,MAAM,EAAM,EACN,SAAS,KAAK,IAAI,YAAY,KAAK,IAAI,wBAAwB,EAAgB,KAC/E,SAAS,KAAK,IAAI,YAAY,KAAK,IAAI,eAAe,KAAK,IAAI,oBAC/D,SAAS,QAAQ,EAAK,CACxB,MAAO,QAAQ,IAAI,aACnB,KAAM,CACF,KAAM,GAEV,OAAQ,EAAkB,QAAU,QAE5C,CACA,MAAO,GACH,MAAM,CACV,CACJ,EAEJ,OAAO,QAAU","file":"/npm/circleci-pr-commenter@0.1.1/dist/index.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.37.0.\n * Original file: /npm/circleci-pr-commenter@0.1.1/dist/index.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","\"use strict\";\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n    return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nconst gh_got_1 = __importDefault(require(\"gh-got\"));\nconst path_1 = require(\"path\");\nif (process.env.NODE_ENV !== 'production') {\n    require('dotenv').config();\n}\nconst REQUIRED_ENVIRONMENT_VARS = [\n    'userName',\n    'repo',\n    'branch',\n    'buildURL',\n    'pr',\n    'token',\n    'tokenUserName',\n    'sha1',\n    'home',\n];\n/**\n * Class that helps you with creating or updating GitHub comments on pull requests\n *\n * @class Commenter\n */\nclass Commenter {\n    constructor() {\n        this.env = {\n            userName: process.env.CIRCLE_PROJECT_USERNAME,\n            repo: process.env.CIRCLE_PROJECT_REPONAME,\n            branch: process.env.CIRCLE_BRANCH,\n            buildURL: process.env.CIRCLE_BUILD_URL,\n            pr: parseInt(path_1.basename(process.env.CIRCLE_PULL_REQUEST || '')) || undefined,\n            token: process.env.GITHUB_TOKEN,\n            tokenUserName: process.env.GITHUB_TOKEN_USERNAME,\n            sha1: process.env.CIRCLE_SHA1,\n            home: process.env.HOME,\n        };\n        const missing = REQUIRED_ENVIRONMENT_VARS.filter(key => {\n            return !this.env[key];\n        });\n        if (missing.length > 0) {\n            throw new Error('Missing environment variables:\\n' + missing.join(', '));\n        }\n    }\n    /**\n     * Checks if the user set in `GITHUB_TOKEN_USERNAME` already has a comment on this pull request.\n     *\n     * @private\n     * @param {Comment[]} comments\n     * @returns {Comment} The first comment by `GITHUB_TOKEN_USERNAME` if exists\n     * @memberof Commenter\n     */\n    getExistingComment(comments) {\n        const existingComments = comments.filter(comment => comment.user.login === this.env.tokenUserName);\n        if (existingComments.length > 0) {\n            return existingComments[0];\n        }\n        return null;\n    }\n    /**\n     * Gets all the GitHub comments for the given pull request\n     *\n     * @private\n     * @returns {Promise<Comment[]>}\n     * @memberof Commenter\n     */\n    async getComments() {\n        try {\n            const { body } = await gh_got_1.default(`repos/${this.env.userName}/${this.env.repo}/issues/${this.env.pr}/comments`, {\n                token: process.env.GITHUB_TOKEN,\n            });\n            return body;\n        }\n        catch (error) {\n            throw error;\n        }\n    }\n    /**\n     * Searches for the given key in the comment body\n     *\n     * @private\n     * @param {string} key The key we look for when updating comments, matches `<!-- start:${key} -->(.*)<!-- end:${key} -->`\n     * @param {string} body The comment body to search in\n     * @returns {string} The matched body or null\n     * @memberof Commenter\n     */\n    getDataForKey(key, body) {\n        const match = new RegExp(`<!-- start:${key} -->(.*)<!-- end:${key} -->`, 'gms');\n        const re = match.exec(body);\n        if (re && re.length >= 2) {\n            return re[1].trim();\n        }\n        return null;\n    }\n    /**\n     * Gets the artifact URL for the given path, useful for e.g. linking to a coverage report or lighthouse results\n     *\n     * @param {string} path The path to the stored artifact, should be set in the circleci config with `store_artifacts` key\n     * @returns {string} The artifact url\n     * @memberof Commenter\n     */\n    getArtifactURL(path) {\n        return `${this.env.buildURL}/artifacts/0${this.env.home}${path}`;\n    }\n    /**\n     * Creates or updates a comment on the pull request referenced through the environment variable `CIRCLE_PULL_REQUEST`\n     * If the username set in `GITHUB_TOKEN_USERNAME` already has an existing comment on this pull request\n     *  this method will instead update the existing comment with a new body.\n     *\n     * If it matches the pattern `<!-- start:${key} -->(.*)<!-- end:${key} -->` in the body, it will replace the content with `message`.\n     * If it does not match the pattern, it will append the `message` to the body.\n     *\n     * @param {string} key The key we look for when updating comments, matches `<!-- start:${key} -->(.*)<!-- end:${key} -->`\n     * @param {string} message The message body, GitHub markdown or HTML.\n     * @memberof Commenter\n     */\n    async createOrUpdateComment(key, message) {\n        const comments = await this.getComments();\n        const existingComment = this.getExistingComment(comments);\n        const formattedMessage = `\\n<!-- start:${key} -->\\n${message}\\n<!-- end:${key} -->`;\n        let comment = formattedMessage;\n        if (existingComment) {\n            comment = existingComment.body;\n            const data = this.getDataForKey(key, existingComment.body);\n            if (data) {\n                comment = comment.replace(data, message);\n            }\n            else {\n                comment = `${comment}\\n${formattedMessage}`;\n            }\n        }\n        try {\n            const url = existingComment\n                ? `repos/${this.env.userName}/${this.env.repo}/issues/comments/${existingComment.id}`\n                : `repos/${this.env.userName}/${this.env.repo}/issues/${this.env.pr}/comments`;\n            await gh_got_1.default(url, {\n                token: process.env.GITHUB_TOKEN,\n                body: {\n                    body: comment,\n                },\n                method: existingComment ? 'PATCH' : 'POST',\n            });\n        }\n        catch (error) {\n            throw error;\n        }\n    }\n}\nmodule.exports = Commenter;\n"]}