├── .circleci └── config.yml ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── demo.js ├── demo └── index.html ├── package-lock.json ├── package.json ├── src ├── index.ts └── tsconfig.json └── tslint.json /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | references: 4 | save_cache: &save_cache 5 | key: v1-deps-{{ checksum "package.json" }} 6 | paths: 7 | - node_modules 8 | restore_cache: &restore_cache 9 | keys: 10 | - v1-deps-{{ checksum "package.json" }} 11 | - v1-deps 12 | 13 | jobs: 14 | install: 15 | docker: 16 | - image: circleci/node:8.10.0 17 | steps: 18 | - checkout 19 | - run: npm install 20 | - save_cache: *save_cache 21 | 22 | build: 23 | docker: 24 | - image: circleci/node:8.10.0 25 | steps: 26 | - checkout 27 | - restore_cache: *restore_cache 28 | - run: npm run build 29 | - persist_to_workspace: 30 | root: '.' 31 | paths: 32 | - dist 33 | 34 | lint: 35 | docker: 36 | - image: circleci/node:8.10.0 37 | steps: 38 | - checkout 39 | - restore_cache: *restore_cache 40 | - run: npm run lint 41 | 42 | demo: 43 | docker: 44 | - image: circleci/node:8.10.0 45 | steps: 46 | - checkout 47 | - restore_cache: *restore_cache 48 | - attach_workspace: 49 | at: '.' 50 | - run: ./demo.js 51 | - store_artifacts: 52 | path: demo 53 | 54 | publish: 55 | docker: 56 | - image: circleci/node:8.10.0 57 | steps: 58 | - checkout 59 | - restore_cache: *restore_cache 60 | - run: npm run -s check_version 61 | - run: npm run build 62 | - run: echo "//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}" >> .npmrc 63 | - run: chmod 0600 .npmrc 64 | - run: npm publish 65 | 66 | workflows: 67 | version: 2 68 | circle-comment-bot: 69 | jobs: 70 | - install: 71 | filters: 72 | tags: 73 | only: /.*/ 74 | - build: 75 | requires: [install] 76 | - lint: 77 | requires: [install] 78 | - demo: 79 | requires: [install, build] 80 | - publish: 81 | requires: [install, lint] 82 | filters: 83 | tags: 84 | only: /^release-.*/ 85 | branches: 86 | ignore: /.*/ 87 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | .npm 4 | .node_repl_history 5 | 6 | dist 7 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | **/* 2 | !README.md 3 | !LICENSE 4 | !package.json 5 | !src/**/* 6 | !dist/**/* 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | > Now compatible with Circle 2.0 3 | 4 | # Circle Github Bot 5 | 6 | This library helps you submit a comment on the PR from inside your CircleCI 7 | build and link to a static artifact from the build. 8 | 9 | When reviewing a PR on github, it's useful to read the code but even more useful 10 | to test out the code on that branch in a live working web app. 11 | 12 | See an example PR on this github repo https://github.com/themadcreator/circle-github-bot/pull/3 13 | 14 | It works like so: 15 | 16 | 1. Someone creates a pull request on your github project 17 | 1. This triggers a CircleCI build, which: 18 | 1. Runs tests 19 | 1. Builds your static demo site 20 | 1. Runs your demo.js script, submitting a comment back to the PR 21 | 1. A comment shows up on the github PR 22 | 1. CircleCI "collects" the artifacts from the build and makes them available on the web 23 | 1. You click the link on the PR and see the static site! 24 | 25 | # Setting Up Demo Comments on PRs 26 | 27 | ### Write the Bot Comment Script 28 | Create a `demo.js` script using this library to post a comment on github 29 | referencing the current PR. 30 | 31 | Example: 32 | ```javascript 33 | #!/usr/bin/env node 34 | 35 | const bot = require("circle-github-bot").create(); 36 | 37 | bot.comment(` 38 |

${bot.env.commitMessage}

39 | Demo: ${bot.artifactLink('demo/index.html', 'demo')} 40 | `); 41 | ``` 42 | 43 | With that "shebang" at the top, you can `chmod +x` your script file from the 44 | command line to make it self-executable. 45 | 46 | ### Integrate CircleCI into your Repo 47 | 1. Add CircleCI service integration to your github project in your repo's project settings 48 | 1. Settings > Integrations & Services > Services 49 | 1. Once CircleCI is following your github project, it will add its own deploy key to this repo 50 | 1. Add `.circleci/config.yml` file to the root of your repo 51 | 1. Store your `demo/` directory as a build artifact 52 | 1. Include job that runs your demo script 53 | 54 | ```yaml 55 | version: 2 56 | 57 | jobs: 58 | demo: 59 | docker: 60 | - image: circleci/node:8.10.0 61 | steps: 62 | - checkout 63 | - run: npm install 64 | - run: npm run build 65 | - run: ./demo.js 66 | - store_artifacts: 67 | path: demo 68 | 69 | workflows: 70 | version: 2 71 | your-project-workflow: 72 | jobs: 73 | - demo 74 | ``` 75 | 76 | ### Add Github Auth Token to CircleCI Environment 77 | Make sure your script can actually post the comment to github 78 | 79 | 1. Go to your github profile settings 80 | 1. Add a new OAuth token under **"Developer Settings"** -> **"Personal access tokens"** 81 | 1. The only permissions required are those needed to comment on your repo. For 82 | example `public_repo` is enough for commenting on a public repository. 83 | 1. Once created, add the token string to your CircleCI build's environment variables 84 | 1. Build Settings > Environment variables 85 | 1. Name the variable **"GH_AUTH_TOKEN"** 86 | 87 | ### Require CircleCI Build for PRs 88 | Optional, but helpful. This makes sure your builds actually pass before a PR can be submitted. 89 | 90 | 1. Set your main branch (e.g. master) to protected 91 | 1. Enabled **"required status checks"** 92 | 1. Select your **"ci/circleci"** workflow jobs as a required status checks 93 | -------------------------------------------------------------------------------- /demo.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | const bot = require(".").create(); 4 | 5 | bot.comment(process.env.GH_AUTH_TOKEN, ` 6 |

${bot.commitMessage()}

7 | Demo: ${bot.artifactLink('demo/index.html', 'demo')} 8 | `); 9 | -------------------------------------------------------------------------------- /demo/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Sample Demo 4 | 13 | 14 | 15 |

Hello, Demo!

16 | 17 | 18 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "circle-github-bot", 3 | "version": "2.1.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@blueprintjs/tslint-config": { 8 | "version": "1.7.0", 9 | "resolved": "https://registry.npmjs.org/@blueprintjs/tslint-config/-/tslint-config-1.7.0.tgz", 10 | "integrity": "sha512-Hs0lvlrxSnmVziGav1yiJdv5kXjzJOhIZ55cdIQmLWEttPmZDI8+i3FLFuiTeLUgdmgYLEfahOA2ANkgXBFKAA==", 11 | "dev": true, 12 | "requires": { 13 | "prettier": "1.15.3", 14 | "tslib": "1.9.3", 15 | "tslint-config-prettier": "1.17.0", 16 | "tslint-plugin-prettier": "1.3.0", 17 | "tslint-react": "3.6.0" 18 | } 19 | }, 20 | "@types/node": { 21 | "version": "8.10.38", 22 | "resolved": "https://registry.npmjs.org/@types/node/-/node-8.10.38.tgz", 23 | "integrity": "sha512-EibsnbJerd0hBFaDjJStFrVbVBAtOy4dgL8zZFw0uOvPqzBAX59Ci8cgjg3+RgJIWhsB5A4c+pi+D4P9tQQh/A==", 24 | "dev": true 25 | }, 26 | "ansi-regex": { 27 | "version": "2.1.1", 28 | "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", 29 | "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=", 30 | "dev": true 31 | }, 32 | "ansi-styles": { 33 | "version": "2.2.1", 34 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", 35 | "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=", 36 | "dev": true 37 | }, 38 | "argparse": { 39 | "version": "1.0.10", 40 | "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", 41 | "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", 42 | "dev": true, 43 | "requires": { 44 | "sprintf-js": "1.0.3" 45 | } 46 | }, 47 | "babel-code-frame": { 48 | "version": "6.26.0", 49 | "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", 50 | "integrity": "sha1-Y/1D99weO7fONZR9uP42mj9Yx0s=", 51 | "dev": true, 52 | "requires": { 53 | "chalk": "1.1.3", 54 | "esutils": "2.0.2", 55 | "js-tokens": "3.0.2" 56 | }, 57 | "dependencies": { 58 | "chalk": { 59 | "version": "1.1.3", 60 | "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", 61 | "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", 62 | "dev": true, 63 | "requires": { 64 | "ansi-styles": "2.2.1", 65 | "escape-string-regexp": "1.0.5", 66 | "has-ansi": "2.0.0", 67 | "strip-ansi": "3.0.1", 68 | "supports-color": "2.0.0" 69 | } 70 | } 71 | } 72 | }, 73 | "balanced-match": { 74 | "version": "1.0.0", 75 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", 76 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", 77 | "dev": true 78 | }, 79 | "brace-expansion": { 80 | "version": "1.1.11", 81 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", 82 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", 83 | "dev": true, 84 | "requires": { 85 | "balanced-match": "1.0.0", 86 | "concat-map": "0.0.1" 87 | } 88 | }, 89 | "builtin-modules": { 90 | "version": "1.1.1", 91 | "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", 92 | "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", 93 | "dev": true 94 | }, 95 | "chalk": { 96 | "version": "2.4.1", 97 | "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", 98 | "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", 99 | "dev": true, 100 | "requires": { 101 | "ansi-styles": "3.2.1", 102 | "escape-string-regexp": "1.0.5", 103 | "supports-color": "5.5.0" 104 | }, 105 | "dependencies": { 106 | "ansi-styles": { 107 | "version": "3.2.1", 108 | "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", 109 | "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", 110 | "dev": true, 111 | "requires": { 112 | "color-convert": "1.9.3" 113 | } 114 | }, 115 | "supports-color": { 116 | "version": "5.5.0", 117 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", 118 | "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", 119 | "dev": true, 120 | "requires": { 121 | "has-flag": "3.0.0" 122 | } 123 | } 124 | } 125 | }, 126 | "color-convert": { 127 | "version": "1.9.3", 128 | "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", 129 | "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", 130 | "dev": true, 131 | "requires": { 132 | "color-name": "1.1.3" 133 | } 134 | }, 135 | "color-name": { 136 | "version": "1.1.3", 137 | "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", 138 | "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", 139 | "dev": true 140 | }, 141 | "commander": { 142 | "version": "2.19.0", 143 | "resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz", 144 | "integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==", 145 | "dev": true 146 | }, 147 | "concat-map": { 148 | "version": "0.0.1", 149 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", 150 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", 151 | "dev": true 152 | }, 153 | "diff": { 154 | "version": "3.5.0", 155 | "resolved": "https://registry.npmjs.org/diff/-/diff-3.5.0.tgz", 156 | "integrity": "sha512-A46qtFgd+g7pDZinpnwiRJtxbC1hpgf0uzP3iG89scHk0AUC7A1TGxf5OiiOUv/JMZR8GOt8hL900hV0bOy5xA==", 157 | "dev": true 158 | }, 159 | "escape-string-regexp": { 160 | "version": "1.0.5", 161 | "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", 162 | "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", 163 | "dev": true 164 | }, 165 | "eslint-plugin-prettier": { 166 | "version": "2.7.0", 167 | "resolved": "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-2.7.0.tgz", 168 | "integrity": "sha512-CStQYJgALoQBw3FsBzH0VOVDRnJ/ZimUlpLm226U8qgqYJfPOY/CPK6wyRInMxh73HSKg5wyRwdS4BVYYHwokA==", 169 | "dev": true, 170 | "requires": { 171 | "fast-diff": "1.2.0", 172 | "jest-docblock": "21.2.0" 173 | } 174 | }, 175 | "esprima": { 176 | "version": "4.0.1", 177 | "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", 178 | "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", 179 | "dev": true 180 | }, 181 | "esutils": { 182 | "version": "2.0.2", 183 | "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.2.tgz", 184 | "integrity": "sha1-Cr9PHKpbyx96nYrMbepPqqBLrJs=", 185 | "dev": true 186 | }, 187 | "fast-diff": { 188 | "version": "1.2.0", 189 | "resolved": "https://registry.npmjs.org/fast-diff/-/fast-diff-1.2.0.tgz", 190 | "integrity": "sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==", 191 | "dev": true 192 | }, 193 | "fs.realpath": { 194 | "version": "1.0.0", 195 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", 196 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", 197 | "dev": true 198 | }, 199 | "glob": { 200 | "version": "7.1.3", 201 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.3.tgz", 202 | "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", 203 | "dev": true, 204 | "requires": { 205 | "fs.realpath": "1.0.0", 206 | "inflight": "1.0.6", 207 | "inherits": "2.0.3", 208 | "minimatch": "3.0.4", 209 | "once": "1.4.0", 210 | "path-is-absolute": "1.0.1" 211 | } 212 | }, 213 | "has-ansi": { 214 | "version": "2.0.0", 215 | "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", 216 | "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", 217 | "dev": true, 218 | "requires": { 219 | "ansi-regex": "2.1.1" 220 | } 221 | }, 222 | "has-flag": { 223 | "version": "3.0.0", 224 | "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", 225 | "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", 226 | "dev": true 227 | }, 228 | "inflight": { 229 | "version": "1.0.6", 230 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", 231 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", 232 | "dev": true, 233 | "requires": { 234 | "once": "1.4.0", 235 | "wrappy": "1.0.2" 236 | } 237 | }, 238 | "inherits": { 239 | "version": "2.0.3", 240 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 241 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", 242 | "dev": true 243 | }, 244 | "jest-docblock": { 245 | "version": "21.2.0", 246 | "resolved": "https://registry.npmjs.org/jest-docblock/-/jest-docblock-21.2.0.tgz", 247 | "integrity": "sha512-5IZ7sY9dBAYSV+YjQ0Ovb540Ku7AO9Z5o2Cg789xj167iQuZ2cG+z0f3Uct6WeYLbU6aQiM2pCs7sZ+4dotydw==", 248 | "dev": true 249 | }, 250 | "js-tokens": { 251 | "version": "3.0.2", 252 | "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-3.0.2.tgz", 253 | "integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls=", 254 | "dev": true 255 | }, 256 | "js-yaml": { 257 | "version": "3.12.0", 258 | "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.0.tgz", 259 | "integrity": "sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A==", 260 | "dev": true, 261 | "requires": { 262 | "argparse": "1.0.10", 263 | "esprima": "4.0.1" 264 | } 265 | }, 266 | "minimatch": { 267 | "version": "3.0.4", 268 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", 269 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", 270 | "dev": true, 271 | "requires": { 272 | "brace-expansion": "1.1.11" 273 | } 274 | }, 275 | "once": { 276 | "version": "1.4.0", 277 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", 278 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 279 | "dev": true, 280 | "requires": { 281 | "wrappy": "1.0.2" 282 | } 283 | }, 284 | "path-is-absolute": { 285 | "version": "1.0.1", 286 | "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", 287 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", 288 | "dev": true 289 | }, 290 | "path-parse": { 291 | "version": "1.0.6", 292 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", 293 | "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", 294 | "dev": true 295 | }, 296 | "prettier": { 297 | "version": "1.15.3", 298 | "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.15.3.tgz", 299 | "integrity": "sha512-gAU9AGAPMaKb3NNSUUuhhFAS7SCO4ALTN4nRIn6PJ075Qd28Yn2Ig2ahEJWdJwJmlEBTUfC7mMUSFy8MwsOCfg==", 300 | "dev": true 301 | }, 302 | "resolve": { 303 | "version": "1.8.1", 304 | "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.8.1.tgz", 305 | "integrity": "sha512-AicPrAC7Qu1JxPCZ9ZgCZlY35QgFnNqc+0LtbRNxnVw4TXvjQ72wnuL9JQcEBgXkI9JM8MsT9kaQoHcpCRJOYA==", 306 | "dev": true, 307 | "requires": { 308 | "path-parse": "1.0.6" 309 | } 310 | }, 311 | "semver": { 312 | "version": "5.6.0", 313 | "resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz", 314 | "integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==", 315 | "dev": true 316 | }, 317 | "sprintf-js": { 318 | "version": "1.0.3", 319 | "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", 320 | "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", 321 | "dev": true 322 | }, 323 | "strip-ansi": { 324 | "version": "3.0.1", 325 | "resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", 326 | "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", 327 | "dev": true, 328 | "requires": { 329 | "ansi-regex": "2.1.1" 330 | } 331 | }, 332 | "supports-color": { 333 | "version": "2.0.0", 334 | "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", 335 | "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=", 336 | "dev": true 337 | }, 338 | "tslib": { 339 | "version": "1.9.3", 340 | "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.9.3.tgz", 341 | "integrity": "sha512-4krF8scpejhaOgqzBEcGM7yDIEfi0/8+8zDRZhNZZ2kjmHJ4hv3zCbQWxoJGz1iw5U0Jl0nma13xzHXcncMavQ==", 342 | "dev": true 343 | }, 344 | "tslint": { 345 | "version": "5.11.0", 346 | "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.11.0.tgz", 347 | "integrity": "sha1-mPMMAurjzecAYgHkwzywi0hYHu0=", 348 | "dev": true, 349 | "requires": { 350 | "babel-code-frame": "6.26.0", 351 | "builtin-modules": "1.1.1", 352 | "chalk": "2.4.1", 353 | "commander": "2.19.0", 354 | "diff": "3.5.0", 355 | "glob": "7.1.3", 356 | "js-yaml": "3.12.0", 357 | "minimatch": "3.0.4", 358 | "resolve": "1.8.1", 359 | "semver": "5.6.0", 360 | "tslib": "1.9.3", 361 | "tsutils": "2.29.0" 362 | } 363 | }, 364 | "tslint-config-prettier": { 365 | "version": "1.17.0", 366 | "resolved": "https://registry.npmjs.org/tslint-config-prettier/-/tslint-config-prettier-1.17.0.tgz", 367 | "integrity": "sha512-NKWNkThwqE4Snn4Cm6SZB7lV5RMDDFsBwz6fWUkTxOKGjMx8ycOHnjIbhn7dZd5XmssW3CwqUjlANR6EhP9YQw==", 368 | "dev": true 369 | }, 370 | "tslint-plugin-prettier": { 371 | "version": "1.3.0", 372 | "resolved": "https://registry.npmjs.org/tslint-plugin-prettier/-/tslint-plugin-prettier-1.3.0.tgz", 373 | "integrity": "sha512-6UqeeV6EABp0RdQkW6eC1vwnAXcKMGJgPeJ5soXiKdSm2vv7c3dp+835CM8pjgx9l4uSa7tICm1Kli+SMsADDg==", 374 | "dev": true, 375 | "requires": { 376 | "eslint-plugin-prettier": "2.7.0", 377 | "tslib": "1.9.3" 378 | } 379 | }, 380 | "tslint-react": { 381 | "version": "3.6.0", 382 | "resolved": "https://registry.npmjs.org/tslint-react/-/tslint-react-3.6.0.tgz", 383 | "integrity": "sha512-AIv1QcsSnj7e9pFir6cJ6vIncTqxfqeFF3Lzh8SuuBljueYzEAtByuB6zMaD27BL0xhMEqsZ9s5eHuCONydjBw==", 384 | "dev": true, 385 | "requires": { 386 | "tsutils": "2.29.0" 387 | } 388 | }, 389 | "tsutils": { 390 | "version": "2.29.0", 391 | "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz", 392 | "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==", 393 | "dev": true, 394 | "requires": { 395 | "tslib": "1.9.3" 396 | } 397 | }, 398 | "typescript": { 399 | "version": "3.2.1", 400 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.1.tgz", 401 | "integrity": "sha512-jw7P2z/h6aPT4AENXDGjcfHTu5CSqzsbZc6YlUIebTyBAq8XaKp78x7VcSh30xwSCcsu5irZkYZUSFP1MrAMbg==", 402 | "dev": true 403 | }, 404 | "wrappy": { 405 | "version": "1.0.2", 406 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", 407 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", 408 | "dev": true 409 | } 410 | } 411 | } 412 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "circle-github-bot", 3 | "version": "2.1.0", 4 | "description": "CircleCI comments on github", 5 | "main": "dist/index.js", 6 | "typings": "dist/index.d.ts", 7 | "files": [ 8 | "dist" 9 | ], 10 | "scripts": { 11 | "build": "tsc -p src", 12 | "lint": "tslint -p src", 13 | "check_version": "echo ${npm_package_version} $(npm view ${npm_package_name} version) | awk '{ if ($1 == $2) { print \"Duplicate version\"; exit 1 }}'" 14 | }, 15 | "devDependencies": { 16 | "@blueprintjs/tslint-config": "^1.7.0", 17 | "@types/node": "^8.0.0", 18 | "tslint": "^5.11.0", 19 | "typescript": "~3.2.0" 20 | }, 21 | "repository": { 22 | "type": "git", 23 | "url": "git+https://github.com/themadcreator/circle-github-bot.git" 24 | }, 25 | "keywords": [ 26 | "circle", 27 | "github", 28 | "ops" 29 | ], 30 | "license": "Apache-2.0" 31 | } 32 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | /*! 2 | Copyright 2017-present TheMadCreator 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | import { execSync, ExecSyncOptions } from "child_process"; 18 | import { basename } from "path"; 19 | 20 | interface IOptions { 21 | /** 22 | * GitHub API domain, for enterprise support. 23 | * @default "api.github.com" 24 | */ 25 | githubDomain?: string; 26 | } 27 | 28 | interface IEnvironment { 29 | readonly buildUrl: string; 30 | readonly repo: string; 31 | readonly sha1: string; 32 | readonly username: string; 33 | readonly commitMessage: string; 34 | readonly prNumber: string; 35 | readonly githubDomain: string; 36 | } 37 | 38 | // IEnvironment fields that must be defined or an Error is thrown. 39 | const REQUIRED_ENV: Array = ["buildUrl", "repo", "sha1", "username"]; 40 | 41 | // Synchronously execute command and return trimmed stdout as string 42 | function exec(command: string, options?: ExecSyncOptions) { 43 | return execSync(command, options) 44 | .toString("utf8") 45 | .trim(); 46 | } 47 | 48 | // Syncronously POST to `url` with `data` content 49 | function curl(url: string, data: string) { 50 | return exec(`curl --silent --data @- ${url}`, { input: data }); 51 | } 52 | 53 | class Bot { 54 | public static create(options: IOptions = {}) { 55 | const { githubDomain = "api.github.com" } = options; 56 | 57 | // CI_PULL_REQUEST was deprecated in favor of CIRCLE_PULL_REQUEST in Circle 2.0. 58 | const prNumber = basename(process.env.CIRCLE_PULL_REQUEST || process.env.CI_PULL_REQUEST || ""); 59 | 60 | const env: IEnvironment = { 61 | buildUrl: process.env.CIRCLE_BUILD_URL, 62 | commitMessage: exec('git --no-pager log --pretty=format:"%s" -1').replace(/\\"/g, '\\\\"'), 63 | githubDomain, 64 | prNumber, 65 | repo: process.env.CIRCLE_PROJECT_REPONAME, 66 | sha1: process.env.CIRCLE_SHA1, 67 | username: process.env.CIRCLE_PROJECT_USERNAME, 68 | }; 69 | 70 | const missing = REQUIRED_ENV.filter(key => !env[key]); 71 | if (missing.length > 0) { 72 | throw new Error("Missing required environment variables:\n " + missing.join(", ")); 73 | } 74 | 75 | return new Bot(env); 76 | } 77 | 78 | /** @internal */ 79 | private constructor(private readonly env: IEnvironment) {} 80 | 81 | /** Get an absolute URL for the given artifact path. */ 82 | public artifactUrl(artifactPath: string) { 83 | return `${this.env.buildUrl}/artifacts/0/${artifactPath}`; 84 | } 85 | 86 | /** Render an HTML link to the path with the given text. */ 87 | public artifactLink(artifactPath: string, text: string) { 88 | return `${text}`; 89 | } 90 | 91 | /** Get the message from the latest commit. */ 92 | public commitMessage() { 93 | return this.env.commitMessage; 94 | } 95 | 96 | /** 97 | * Post a comment with the given body. 98 | * 99 | * Requires a GitHub auth token. We recommend generating a new token 100 | * and saving it as an environment variable called `GH_AUTH_TOKEN`. Then 101 | * pass this variable when calling this method. 102 | * 103 | * ```js 104 | * bot.comment(process.env.GH_AUTH_TOKEN, "..."); 105 | * ``` 106 | */ 107 | public comment(authToken: string, body: string = "") { 108 | if (!authToken) { 109 | throw new Error("Bot.comment() requires auth token."); 110 | } 111 | if (this.env.prNumber !== "") { 112 | return this.curl(authToken, `issues/${this.env.prNumber}/comments`, body); 113 | } else { 114 | return this.curl(authToken, `commits/${this.env.sha1}/comments`, body); 115 | } 116 | } 117 | 118 | /** @internal */ 119 | private githubUrl(authToken: string, path: string) { 120 | const { githubDomain, username, repo } = this.env; 121 | return `https://${authToken}:x-oauth-basic@${githubDomain}/repos/${username}/${repo}/${path}`; 122 | } 123 | 124 | /** @internal */ 125 | private curl(authToken: string, path: string, body: string) { 126 | // tslint:disable:no-console 127 | console.log(`Posting to ${path}...`); 128 | console.log(curl(this.githubUrl(authToken, path), JSON.stringify({ body }))); 129 | // tslint:enable:no-console 130 | } 131 | } 132 | 133 | export = Bot; 134 | -------------------------------------------------------------------------------- /src/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "outDir": "../dist", 5 | "target": "es5", 6 | "stripInternal": true 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@blueprintjs/tslint-config" 3 | } 4 | --------------------------------------------------------------------------------