-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #530 from linear-b/checklist-plugin
Create checklist plugin
- Loading branch information
Showing
9 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Automation - PR Checklist General | ||
description: Automatically evaluate PRs against code requirement checklists. | ||
--- | ||
# PR Checklist General | ||
|
||
<!-- --8<-- [start:example]--> | ||
Automatically evaluate PRs against code requirement checklists. | ||
|
||
<div class="automationImage" markdown="1"> | ||
![PR Checklist General](/automations/standard/pr-checklist-general/pr-checklist-general.png) | ||
</div> | ||
<div class="automationDescription" markdown="1"> | ||
!!! info "Configuration Description" | ||
There are no conditions for this action - if included as presented in the demo, it's run every time. | ||
|
||
Automation Actions: | ||
|
||
* Post a comment containing a checklist with each completed item checked off. | ||
|
||
</div> | ||
<div class="automationExample" markdown="1"> | ||
!!! example "PR Checklist General" | ||
```yaml+jinja | ||
--8<-- "docs/downloads/automation-library/pr_checklist_general.cm" | ||
``` | ||
<div class="result" markdown> | ||
<span> | ||
[:octicons-download-24: Download this example as a CM file.](/downloads/automation-library/pr_checklist_general.cm){ .md-button } | ||
</span> | ||
</div> | ||
</div> | ||
<!-- --8<-- [end:example]--> | ||
|
||
## Additional Resources | ||
|
||
--8<-- "docs/snippets/general.md" | ||
|
||
--8<-- "docs/snippets/automation-footer.md" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# -*- mode: yaml -*- | ||
|
||
manifest: | ||
version: 1.0 | ||
|
||
automations: | ||
checklist: | ||
if: | ||
- true | ||
run: | ||
- action: add-comment@v1 | ||
args: | ||
comment: {{ "" | checklist(branch, files, pr, repo, env, source) }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 LinearB | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--8<-- "plugins/filters/checklist/reference.md" | ||
|
||
With this plugin, you can easily customize the checklist using the object in the JavaScript code. To add a new check to the list, just add a new object with a descriptive `title` for your own benefit, a `label` that'll get posted in the comment, and the `condition` that, if true, would cause the entry in the checklist to be checked off. | ||
|
||
??? note "Plugin Code: checklist" | ||
```javascript | ||
--8<-- "plugins/filters/checklist/index.js" | ||
``` | ||
<div class="result" markdown> | ||
<span> | ||
</span> | ||
</div> | ||
|
||
|
||
??? example "gitStream CM Example: checklist" | ||
```yaml+jinja | ||
--8<-- "docs/downloads/automation-library/pr_checklist_general.cm" | ||
``` | ||
<div class="result" markdown> | ||
<span> | ||
</span> | ||
</div> | ||
|
||
[Download Source Code](https://github.com/linear-b/gitstream/tree/main/plugins/filters/checklist) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @module checklist | ||
* @description Automatically check PRs against a checklist of conditions. | ||
* This is useful if you want to ensure that PRs meet certain criteria before they can be merged. | ||
* @param {string} Input - A blank string (no input variable is required) | ||
* @param {object} branch - The branch context variable. | ||
* @param {object} files - The files context variable. | ||
* @param {object} pr - The pr context variable. | ||
* @param {object} repo - The repo context variable. | ||
* @param {object} env - The env context variable. | ||
* @param {object} source - The source context variable. | ||
* @returns {string} Returns a formatted GitHub comment with a checklist of conditions that the PR meets. | ||
* @example | ||
* - action: add-comment@v1 | ||
args: | ||
comment: {{ "" | checklist(branch, files, pr, repo, env, source) }} | ||
* @license MIT | ||
**/ | ||
|
||
const checklistFilter = async (empty, branch, files, pr, repo, env, source, callback) => { // made sync temporarily | ||
|
||
const checks = [ | ||
{ | ||
title: "low-risk", | ||
label: "The PR is a low-risk change", | ||
// our sample definition of a low-risk change is a docs-only PR from designated docs writers | ||
condition: files.every(file => /docs\//.test(file)) && pr.author_teams.includes("tech-writers") | ||
}, | ||
{ | ||
title: "has-jira", | ||
label: "The PR has a Jira reference in the title", | ||
condition: /\b[A-Za-z]+-\d+\b/.test(pr.title) | ||
}, | ||
{ | ||
title: "updates-tests", | ||
label: "The PR includes updates to tests", | ||
condition: files.some(file => /[^a-zA-Z0-9](spec|test|tests)[^a-zA-Z0-9]/.test(file)) | ||
}, | ||
{ | ||
title: "includes-docs", | ||
label: "The PR includes changes to the documentation", | ||
condition: files.some(file => /docs\//.test(file)) | ||
}, | ||
{ | ||
title: "first-time", | ||
label: "The PR author is a first-time contributor", | ||
condition: repo.author_age < 1 && repo.age > 0 // if the PR author made their first contirbution on the current day | ||
}, | ||
{ | ||
title: "requires-opsec", | ||
label: "The PR doesn't expose any secrets", | ||
condition: source.diff.files | ||
.map(file => file.new_content) | ||
.every(file_content => | ||
[ | ||
"MY_SECRET_ENVIRONMENT_VARIABLE" | ||
].every(env_var => !file_content.includes(env_var)) | ||
// nothing added to any file during this comment contains any of the secret environment variables in this array | ||
) | ||
} | ||
]; | ||
|
||
const comment = await Promise.resolve(checks | ||
.map(check => `- [${check.condition ? "x" : " "}] ${check.label}`) | ||
.join("\n")); | ||
|
||
return callback( | ||
null, | ||
JSON.stringify(comment) | ||
); | ||
}; | ||
|
||
module.exports = { | ||
async: true, | ||
filter: checklistFilter | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<a name="module_checklist"></a> | ||
|
||
## checklist | ||
Automatically check PRs against a checklist of conditions. | ||
This is useful if you want to ensure that PRs meet certain criteria before they can be merged. | ||
|
||
**Returns**: <code>string</code> - Returns a formatted GitHub comment with a checklist of conditions that the PR meets. | ||
**License**: MIT | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| Input | <code>string</code> | A blank string (no input variable is required) | | ||
| branch | <code>object</code> | The branch context variable. | | ||
| files | <code>object</code> | The files context variable. | | ||
| pr | <code>object</code> | The pr context variable. | | ||
| repo | <code>object</code> | The repo context variable. | | ||
| env | <code>object</code> | The env context variable. | | ||
| source | <code>object</code> | The source context variable. | | ||
|
||
**Example** | ||
```js | ||
- action: add-comment@v1 | ||
args: | ||
comment: {{ "" | checklist(branch, files, pr, repo, env, source) }} | ||
``` |