├── .gitignore
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── LICENSE
├── README.md
├── app.yml
├── constants.js
├── controllers
├── checks.js
├── marketplace.js
└── pullRequest.js
├── helpers
├── checks.js
└── pullRequest.js
├── index.js
├── middleware
└── verifyWebhooks.js
├── package-lock.json
├── package.json
├── public
├── marketplacePurchase.html
└── privacy.html
├── travis.yml
└── utilities
├── dataConverter.js
└── regexUtil.js
/.gitignore:
--------------------------------------------------------------------------------
1 | ### Code ###
2 | .vscode/*
3 | !.vscode/settings.json
4 | !.vscode/tasks.json
5 | !.vscode/launch.json
6 | !.vscode/extensions.json
7 |
8 | ### Node ###
9 | # Logs
10 | logs
11 | *.log
12 | npm-debug.log*
13 | yarn-debug.log*
14 | yarn-error.log*
15 | lerna-debug.log*
16 |
17 | # Diagnostic reports (https://nodejs.org/api/report.html)
18 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
19 |
20 | # Runtime data
21 | pids
22 | *.pid
23 | *.seed
24 | *.pid.lock
25 |
26 | # Directory for instrumented libs generated by jscoverage/JSCover
27 | lib-cov
28 |
29 | # Coverage directory used by tools like istanbul
30 | coverage
31 |
32 | # nyc test coverage
33 | .nyc_output
34 |
35 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
36 | .grunt
37 |
38 | # Bower dependency directory (https://bower.io/)
39 | bower_components
40 |
41 | # node-waf configuration
42 | .lock-wscript
43 |
44 | # Compiled binary addons (https://nodejs.org/api/addons.html)
45 | build/Release
46 |
47 | # Dependency directories
48 | node_modules/
49 | jspm_packages/
50 |
51 | # TypeScript v1 declaration files
52 | typings/
53 |
54 | # Optional npm cache directory
55 | .npm
56 |
57 | # Optional eslint cache
58 | .eslintcache
59 |
60 | # Optional REPL history
61 | .node_repl_history
62 |
63 | # Output of 'npm pack'
64 | *.tgz
65 |
66 | # Yarn Integrity file
67 | .yarn-integrity
68 |
69 | # dotenv environment variables file
70 | .env
71 | .env.test
72 |
73 | # parcel-bundler cache (https://parceljs.org/)
74 | .cache
75 |
76 | # next.js build output
77 | .next
78 |
79 | # nuxt.js build output
80 | .nuxt
81 |
82 | # vuepress build output
83 | .vuepress/dist
84 |
85 | # Serverless directories
86 | .serverless/
87 |
88 | # FuseBox cache
89 | .fusebox/
90 |
91 | # DynamoDB Local files
92 | .dynamodb/
93 |
94 | ### Serverless ###
95 | # Ignore build directory
96 | .serverless
97 |
98 | ##Apple
99 | .DS_store
100 |
101 | .vscode
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to making participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, gender identity and expression, level of experience,
9 | education, socio-economic status, nationality, personal appearance, race,
10 | religion, or sexual identity and orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies both within project spaces and in public spaces
49 | when an individual is representing the project or its community. Examples of
50 | representing a project or community include using an official project e-mail
51 | address, posting via an official social media account, or acting as an appointed
52 | representative at an online or offline event. Representation of a project may be
53 | further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the project team at asoni@isystango.com. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 |
73 | [homepage]: https://www.contributor-covenant.org
74 |
75 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## Contributing
2 |
3 | [fork]: /fork
4 | [pr]: /compare
5 | [style]: https://standardjs.com/
6 | [code-of-conduct]: CODE_OF_CONDUCT.md
7 |
8 | Hi there! We're thrilled that you'd like to contribute to this project. Your help is essential for keeping it great.
9 |
10 | Please note that this project is released with a [Contributor Code of Conduct][code-of-conduct]. By participating in this project you agree to abide by its terms.
11 |
12 | ## Issues and PRs
13 |
14 | If you have suggestions for how this project could be improved, or want to report a bug, open an issue! We'd love all and any contributions. If you have questions, too, we'd love to hear them.
15 |
16 | We'd also love PRs. If you're thinking of a large PR, we advise opening up an issue first to talk about it, though! Look at the links below if you're not sure how to open a PR.
17 |
18 | ## Submitting a pull request
19 |
20 | 1. [Fork][fork] and clone the repository.
21 | 1. Configure and install the dependencies: `npm install`.
22 | 1. Make sure the tests pass on your machine: `npm test`, note: these tests also apply the linter, so there's no need to lint separately.
23 | 1. Create a new branch: `git checkout -b my-branch-name`.
24 | 1. Make your change, add tests, and make sure the tests still pass.
25 | 1. Push to your fork and [submit a pull request][pr].
26 | 1. Pat your self on the back and wait for your pull request to be reviewed and merged.
27 |
28 | Here are a few things you can do that will increase the likelihood of your pull request being accepted:
29 |
30 | - Follow the [style guide][style] which is using standard. Any linting errors should be shown when running `npm test`.
31 | - Write and update tests.
32 | - Keep your changes as focused as possible. If there are multiple changes you would like to make that are not dependent upon each other, consider submitting them as separate pull requests.
33 | - Write a [good commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html).
34 |
35 | Work in Progress pull requests are also welcome to get feedback early on, or if there is something blocked you.
36 |
37 | ## Resources
38 |
39 | - [How to Contribute to Open Source](https://opensource.guide/how-to-contribute/)
40 | - [Using Pull Requests](https://help.github.com/articles/about-pull-requests/)
41 | - [GitHub Help](https://help.github.com)
42 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Systango Technologies Pvt. Ltd.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Commit Message Lint
2 | Github app to validate commit message and pull request title on a pull request
3 |
4 | ## Description
5 | This app runs a format check on commit messages and pull request title on the creation of a pull request.
6 |
7 | For example, let's say you specify that a commit message should have a format `DDD:message`. Here D stand for numeric digit. The app checks if the commit message follows this format. If all the commit messages follow this format, the check returns successful, otherwise failure. The reviewer can then decide if they want to go ahead with the code merge.
8 |
9 | ### App URL
10 | https://github.com/apps/commit-message-lint
11 |
12 | ## Installation
13 |
14 | Use the Github's app section or above URL to install the app to your repository.
15 |
16 | ## Configuration
17 |
18 | You would need to add a configuration folder named `.github` at the root of your repository. The folder should contain a file named `config.yml`. This file will serve as the configuration and the contents of that file will be:
19 |
20 | ```
21 | PR_TITLE_REGEX:
13 |
`;
154 | if (flags.invalidCommitsCount === 1) {
155 | flags.otherInvalidCommitMessages = singleOtherInvalidMessage;
156 | } else {
157 | flags.otherInvalidCommitMessages = multipleOtherInvalidMessage;
158 | }
159 | }
160 | }
161 | }
162 | }
163 |
164 | /**
165 | * Conclude check run params according to regex match status
166 | * @param {String} prTitleRegex
167 | * @param {String} commitMsgRegex
168 | * @param {Array} commitIds
169 | * @param {Object} flags
170 | */
171 | function concludeCheckRunParams(prTitleRegex, commitMsgRegex, commitIds, flags, configuration) {
172 | let checkRunParams = {};
173 | let output = {};
174 | outputTitleFail = configuration.OUTPUT_TITLE_FAIL || outputTitleFail;
175 | outputTitleSuccess = configuration.OUTPUT_TITLE_SUCCESS || outputTitleSuccess;
176 | console.log('outputTitleFail', outputTitleFail)
177 | console.log('outputTitleSuccess', outputTitleSuccess)
178 | let outputTitle = outputTitleFail;
179 | let conclusion = conclusionStatus.FAILURE;
180 | /**
181 | * check if both the messages are valid for regex
182 | */
183 | if (flags.pullReqTitleStatus && flags.commitMsgStatus) {
184 | conclusion = conclusionStatus.SUCCESS;
185 | outputTitle = outputTitleSuccess;
186 | }
187 |
188 | /**
189 | * set check run status
190 | */
191 | output = {
192 | title: outputTitle,
193 | summary: `${flags.pullReqTitleStatusMsg}
${flags.commitMsgStatusMsg}
${flags.invalidCommits}
`
194 | };
195 | let status = checkRunStatusCompleted;
196 | if (!prTitleRegex && !commitMsgRegex) {
197 | /**
198 | * Pull request and commit message configration regex not set
199 | */
200 | output.title = `${messages.pr_and_commit_message_configuration_not_set}`;
201 | output.summary = `${messages.pr_and_commit_message_configuration_not_set}
`;
202 | } else if (!commitMsgRegex) {
203 | /**
204 | * Commit message configration regex not set
205 | */
206 | output.title = `${messages.commit_message_configuration_not_set}`;
207 | output.summary = `${flags.pullReqTitleStatusMsg}
${messages.commit_message_configuration_not_set}
`;
208 | } else if (!prTitleRegex) {
209 | /**
210 | * Pull request configration regex not set
211 | */
212 | output.title = `${messages.pr_configuration_not_set}`;
213 | output.summary = `${messages.pr_configuration_not_set}
${flags.commitMsgStatusMsg}
${flags.invalidCommits}
`;
214 | }
215 | /**
216 | * Set invalid commit messages and count
217 | */
218 | if (flags.invalidCommitsCount && flags.invalidCommitsCount >= constants.INVALID_COMMIT_LIMIT) {
219 | output.summary += `${flags.invalidCommitsCount} ${flags.otherInvalidCommitMessages}`;
220 | }
221 | checkRunParams = {
222 | commitIds,
223 | status,
224 | checkRunName,
225 | conclusion,
226 | output
227 | };
228 | return checkRunParams;
229 | }
230 |
231 | /**
232 | * Extractor pull request details from context
233 | * @param {Object} context
234 | */
235 | function prDetailsExtractor(context) {
236 | let result = {
237 | owner: '',
238 | repository: '',
239 | pullRequestTitle: '',
240 | pullNumber: 0
241 | };
242 | if (context.payload) {
243 | /**
244 | * Extract repository details
245 | */
246 | if (context.payload.repository) {
247 | if (context.payload.repository.owner && context.payload.repository.owner.login) {
248 | result.owner = context.payload.repository.owner.login;
249 | }
250 | if (context.payload.repository.name) {
251 | result.repository = context.payload.repository.name;
252 | }
253 | }
254 | /**
255 | * Extract PR title and pull number
256 | */
257 | if (context.payload.pull_request && context.payload.pull_request.title) {
258 | result.pullRequestTitle = context.payload.pull_request.title;
259 | }
260 | if (context.payload.number) {
261 | result.pullNumber = context.payload.number;
262 | } else if (context.payload.check_run && context.payload.check_run.check_suite && context.payload.check_run.check_suite.pull_requests && context.payload.check_run.check_suite.pull_requests.length && context.payload.check_run.check_suite.pull_requests[0].number) {
263 | result.pullNumber = context.payload.check_run.check_suite.pull_requests[0].number;
264 | } else if (context.payload.check_suite && context.payload.check_suite.pull_requests.length && context.payload.check_suite.pull_requests[0].number) {
265 | result.pullNumber = context.payload.check_suite.pull_requests[0].number;
266 | }
267 | }
268 | return result;
269 | }
270 |
271 | /**
272 | * Extract regex from configuration file in .github folder in user's repository
273 | * @param {Object} configuration
274 | */
275 | function regexExtractor(configuration) {
276 | let result = {
277 | prTitleRegex: '',
278 | commitTitleRegex: ''
279 | };
280 | result.prTitleRegex = (configuration && configuration.PR_TITLE_REGEX) ? configuration.PR_TITLE_REGEX : '';
281 | result.commitTitleRegex = (configuration && configuration.COMMIT_MESSAGE_REGEX) ? configuration.COMMIT_MESSAGE_REGEX : '';
282 | return result;
283 | }
284 |
285 | /**
286 | * Create Or Update Check Run as per conditions
287 | * @param {Object} context
288 | * @param {String} owner
289 | * @param {String} repository
290 | * @param {Object} result
291 | * @param {boolean} updateCheckRunFlag
292 | * @param {boolean} createCheckRunFlag
293 | */
294 | async function createOrUpdateCheckRun(context, owner, repository, result, updateCheckRunFlag, createCheckRunFlag) {
295 | if (result && result.commitIds && Array.isArray(result.commitIds) && result.commitIds.length) {
296 | for (let index = 0; index < result.commitIds.length; index++) {
297 | const commitId = result.commitIds[index];
298 | if (updateCheckRunFlag) {
299 | /**
300 | * Update existing check run
301 | */
302 | const checkRunId = context.payload.check_run.id;
303 | await updateCheckRun(context, owner, repository, commitId, result.status, result.conclusion, result.output, checkRunId);
304 | } else if (createCheckRunFlag) {
305 | /**
306 | * Create check new run
307 | */
308 | /**
309 | * check if checkSuite exists or not for the commit
310 | */
311 | let checkSuiteList = await listCheckSuite(context, owner, repository, commitId);
312 | if (!checkSuiteList || (checkSuiteList && checkSuiteList.data && checkSuiteList.data.total_count && checkSuiteList.data.total_count === 0)) {
313 | /**
314 | * create check suite for a particular commit
315 | */
316 | await createCheckSuite(context, owner, repository, commitId);
317 | }
318 | /**
319 | * create check run
320 | */
321 | await createCheckRun(context, owner, repository, commitId, result.status, result.checkRunName, result.conclusion, result.output);
322 | }
323 | }
324 | }
325 | }
--------------------------------------------------------------------------------
/helpers/checks.js:
--------------------------------------------------------------------------------
1 | /**
2 | * create Check Suite
3 | * @param {Object} context
4 | * @param {String} owner
5 | * @param {String} repo
6 | * @param {String} commitId
7 | */
8 | module.exports.createCheckSuite = async (context, owner, repo, commitId) => {
9 | try {
10 | let params = {
11 | owner: owner,
12 |
13 | repo: repo,
14 | /**
15 | * The sha of the head commit.
16 | */
17 | head_sha: commitId,
18 | };
19 | let checkSuite = await context.github.checks.createSuite(params);
20 | return checkSuite;
21 | } catch (error) {
22 | return error;
23 | }
24 | };
25 |
26 | /**
27 | * create CheckRun for a commit
28 | * @param {Object} context
29 | * @param {String} owner
30 | * @param {String} repo
31 | * @param {String} commitId
32 | * @param {String} status
33 | * @param {String} checkRunName
34 | * @param {String} conclusion
35 | */
36 | module.exports.createCheckRun = async (context, owner, repo, commitId, status, checkRunName, conclusion, output) => {
37 | try {
38 | let params = {
39 | owner: owner,
40 | repo: repo,
41 | /**
42 | * The name of the check. For example, "code-coverage".
43 | */
44 | name: checkRunName,
45 | /**
46 | * The SHA of the commit.
47 | */
48 | head_sha: commitId,
49 | /**
50 | * The current status. Can be one of `queued`, `in_progress`, or `completed`.
51 | */
52 | status: status,
53 | /**
54 | * The time that the check run began in ISO 8601 format: `YYYY-MM-DDTHH:MM:SSZ`.
55 | */
56 | started_at: new Date().toISOString(),
57 | conclusion: conclusion,
58 | completed_at: new Date().toISOString(),
59 | output: output
60 | };
61 | let checkRun = await context.github.checks.create(params);
62 | return checkRun;
63 | } catch (error) {
64 | return error;
65 | }
66 | };
67 |
68 | /**
69 | * list Check Suite for a commit
70 | * @param {Object} context
71 | * @param {String} owner
72 | * @param {String} repo
73 | * @param {String} ref
74 | */
75 | module.exports.listCheckSuite = async (context, owner, repo, ref) => {
76 | try {
77 | let params = {
78 | owner: owner,
79 | repo: repo,
80 | ref: ref
81 | };
82 | let checkRun = await context.github.checks.listSuitesForRef(params);
83 | return checkRun;
84 | } catch (error) {
85 | return error;
86 | }
87 | };
88 |
89 | /**
90 | * Update CheckRun for a commit
91 | * @param {Object} context
92 | * @param {String} owner
93 | * @param {String} repo
94 | * @param {String} commitId
95 | * @param {String} status
96 | * @param {String} checkRunName
97 | * @param {String} conclusion
98 | */
99 | module.exports.updateCheckRun = async (context, owner, repo, commitId, status, conclusion, output, checkRunId) => {
100 | try {
101 | let params = {
102 | check_run_id: checkRunId,
103 | owner: owner,
104 | repo: repo,
105 | /**
106 | * The name of the check. For example, "code-coverage".
107 | */
108 | name: context.payload.check_run.name,
109 | /**
110 | * The SHA of the commit.
111 | */
112 | head_sha: commitId,
113 | /**
114 | * The current status. Can be one of `queued`, `in_progress`, or `completed`.
115 | */
116 | status: status,
117 | /**
118 | * The time that the check run began in ISO 8601 format: `YYYY-MM-DDTHH:MM:SSZ`.
119 | */
120 | started_at: new Date().toISOString(),
121 | conclusion: conclusion,
122 | completed_at: new Date().toISOString(),
123 | output: output
124 | };
125 | await context.github.checks.update(params);
126 | } catch (error) {
127 | return error;
128 | }
129 | };
130 |
131 | /**
132 | * list Check runs for a check suite
133 | * @param {Object} context
134 | * @param {String} owner
135 | * @param {String} repo
136 | * @param {String} checkSuiteId
137 | */
138 | module.exports.listCheckSuite = async (context, owner, repo, checkSuiteId) => {
139 | try {
140 | let params = {
141 | owner: owner,
142 | repo: repo,
143 | check_suite_id: checkSuiteId
144 | };
145 | let checkRunList = await context.github.checks.listForSuite(params);
146 | return checkRunList;
147 | } catch (error) {
148 | return error;
149 | }
150 | };
151 |
152 | /**
153 | * list of check runs For a check Suite
154 | * @param {Object} context
155 | * @param {String} owner
156 | * @param {String} repo
157 | * @param {String} checkSuiteId
158 | */
159 | module.exports.listForSuite = async (context, owner, repo, checkSuiteId) => {
160 | try {
161 | let params = {
162 | owner: owner,
163 | repo: repo,
164 | check_suite_id: checkSuiteId
165 | };
166 | let listForSuite = await context.github.checks.listForSuite(params);
167 | return listForSuite;
168 | } catch (error) {
169 | return error;
170 | }
171 | };
172 |
--------------------------------------------------------------------------------
/helpers/pullRequest.js:
--------------------------------------------------------------------------------
1 | /**
2 | * list Commits Of Pull Request
3 | * @param {Object} context
4 | * @param {String} owner
5 | * @param {String} repo
6 | * @param {String} pullNumber
7 | * @param {String} perPage
8 | * @param {String} pageNo
9 | */
10 | module.exports.listCommitsOfPullRequest = async (context, owner, repo, pullNumber, perPage, pageNo) => {
11 | try {
12 | let params = {
13 | owner: owner,
14 | repo: repo,
15 | pull_number: pullNumber
16 | };
17 | if (perPage) {
18 | params.per_page = perPage;
19 | }
20 | if (pageNo) {
21 | params.page = pageNo;
22 | }
23 | // find commits
24 | let commits = await context.github.pullRequests.listCommits(params);
25 | return commits;
26 | } catch (error) {
27 | return error;
28 | }
29 | };
30 |
31 | /**
32 | * Get Pull Request
33 | * @param {Object} context
34 | * @param {String} owner
35 | * @param {String} repo
36 | * @param {String} pullNumber
37 | */
38 | module.exports.getPullRequest = async (context, owner, repo, pullNumber) => {
39 | try {
40 | let params = {
41 | owner: owner,
42 | repo: repo,
43 | pull_number: pullNumber
44 | };
45 | // find PR
46 | let pullRequestDetails = await context.github.pullRequests.get(params);
47 | return pullRequestDetails;
48 | } catch (error) {
49 | return error;
50 | }
51 | };
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Packages
3 | */
4 | const express = require('express');
5 | const expressApp = express();
6 | const path = require('path');
7 | const bodyParser = require('body-parser');
8 | const {
9 | verifyWebhookData
10 | } = require('./middleware/verifyWebhooks');
11 | /**
12 | * Controllers
13 | */
14 | const {
15 | commitAndTitleValidator
16 | } = require('./controllers/pullRequest');
17 | const {
18 | marketplaceEventHandlers,
19 | getAccessToken
20 | } = require('./controllers/marketplace');
21 | const {
22 | listForSuite
23 | } = require('./controllers/checks');
24 |
25 | /**
26 | * Constants
27 | */
28 | const {
29 | configFileName,
30 | messages,
31 | events
32 | } = require('./constants.js');
33 | const publicDirectory = path.join(`${__dirname}`, 'public');
34 |
35 | /**
36 | * This is the main entrypoint to Probot app
37 | * @param {import('probot').Application} app
38 | */
39 | module.exports = async app => {
40 | /**
41 | * Created pull request event listener
42 | */
43 | app.on(events.PULL_REQUEST_OPEN, async (context) => {
44 | try {
45 | const configuration = await context.config(configFileName);
46 | await commitAndTitleValidator(app, context, configuration, false, true);
47 | } catch (error) {
48 | console.log('Error while performing operation in Event PULL_REQUEST_OPEN', error);
49 | app.log(error);
50 | }
51 | });
52 | /**
53 | * Check re-run event listener
54 | */
55 | app.on(events.CHECK_RUN_REREQUESTED, async (context) => {
56 | try {
57 | const configuration = await context.config(configFileName);
58 | await commitAndTitleValidator(app, context, configuration, true, false);
59 | } catch (error) {
60 | console.log('Error while performing operation in Event CHECK_RUN_REREQUESTED', error);
61 | app.log(error);
62 | }
63 | });
64 | /**
65 | * Re-run all checks (Check Suite Re-requested) event listener
66 | */
67 | app.on(events.CHECK_SUITE_REREQUESTED, async (context) => {
68 | try {
69 | const listOfCheckRuns = await listForSuite(app, context);
70 | context.payload.check_run = {
71 | id: listOfCheckRuns.data.check_runs[0].id
72 | };
73 | const configuration = await context.config(configFileName);
74 | await commitAndTitleValidator(app, context, configuration, true, false);
75 | } catch (error) {
76 | console.log('Error while performing operation in Event CHECK_SUITE_REREQUESTED', error);
77 | app.log(error);
78 | }
79 | });
80 | /**
81 | * Run all checks (Check Suite Requested) event listener
82 | */
83 | app.on(events.CHECK_SUITE_REQUESTED, async (context) => {
84 | try {
85 | const configuration = await context.config(configFileName);
86 | await commitAndTitleValidator(app, context, configuration, false, true);
87 | } catch (error) {
88 | console.log('Error while performing operation in Event CHECK_SUITE_REQUESTED', error);
89 | app.log(error);
90 | }
91 | });
92 |
93 | app.on(events.CHECK_SUITE, async context => {
94 | try {
95 | const configuration = await context.config(configFileName);
96 | await commitAndTitleValidator(app, context, configuration, false, true);
97 | } catch (error) {
98 | console.log('Error while performing operation in Event CHECK_SUITE', error);
99 | app.log(error);
100 | }
101 | });
102 |
103 | /**
104 | * Middlewares
105 | */
106 | expressApp.use(bodyParser.json());
107 | /**
108 | * Web APIs
109 | */
110 | /**
111 | * Home page API
112 | */
113 | expressApp.get('/', (req, res) => {
114 | res.send(messages.home_page_message);
115 | });
116 | /**
117 | * Privacy page API
118 | */
119 | expressApp.get('/privacy', (req, res) => {
120 | res.sendFile(path.join(`${publicDirectory}`, 'privacy.html'));
121 | });
122 | /**
123 | * Marketplace API
124 | * This API gets hit by github on any marketplace event
125 | */
126 | expressApp.post('/marketplace', verifyWebhookData, marketplaceEventHandlers);
127 |
128 | /**
129 | * This API gets hit when redirected by `POST: /marketplace` API
130 | * This API exchanges code with github for accessToken
131 | */
132 | expressApp.get('/auth', getAccessToken);
133 |
134 | app.router.use('/', expressApp);
135 | };
--------------------------------------------------------------------------------
/middleware/verifyWebhooks.js:
--------------------------------------------------------------------------------
1 | const crypto = require('crypto')
2 | const secret = process.env.WEBHOOK_SECRET;
3 | const sigHeaderName = 'x-hub-signature'
4 |
5 | module.exports.verifyWebhookData = function(req, res, next) {
6 | const payload = JSON.stringify(req.body)
7 | if (!payload) {
8 | return next('Request body empty')
9 | }
10 |
11 | const sig = req.get(sigHeaderName) || ''
12 | const hmac = crypto.createHmac('sha1', secret)
13 | const digest = Buffer.from('sha1=' + hmac.update(payload).digest('hex'), 'utf8')
14 | const checksum = Buffer.from(sig, 'utf8')
15 | if (checksum.length !== digest.length || !crypto.timingSafeEqual(digest, checksum)) {
16 | return next(`Request body digest (${digest}) did not match ${sigHeaderName} (${checksum})`)
17 | }
18 | return next()
19 | }
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "commit-message-lint-app",
3 | "version": "3.1.1",
4 | "description": "An app for validating commit messages and pull requests title",
5 | "author": "Anshul Soni Commit Message Validator
8 | Privacy Policy
9 |
10 |
We capture
12 |
14 |
19 |
24 |
13 |
24 |