Skip to content

Commit

Permalink
Merge pull request #531 from linear-b/renovate-plugin
Browse files Browse the repository at this point in the history
Renovate plugin
  • Loading branch information
vim-zz authored Jun 15, 2024
2 parents 5e99db2 + 0cadc6e commit c091a5c
Show file tree
Hide file tree
Showing 18 changed files with 367 additions and 32 deletions.
74 changes: 53 additions & 21 deletions docs/automations/integrations/renovate/approve-renovate/README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,67 @@
---
title: Automation - Approve and Merge Renovate Changes
description: Automatically approve and merge Renovate PRs.
category: [security, renovate]
category: [efficiency, renovate, security]
quickstart: false
---
# Approve and Merge Renovate Changes

<!-- --8<-- [start:example]-->
Approve PRs from Renovate
Auto-merge Renovate PRs

=== "By Release Type"

!!! warning "Required gitStream Plugins"
This example requires you to install the [`extractRenovateVersionBump`](/filter-function-plugins/#extractrenovateversionbump), [`compareMultiSemver`](/filter-function-plugins/#comparemultisemver) and [`compareSemver`](/filter-function-plugins/#comparesemver) plugins.

[Learn more about gitStream plugins](/plugins/).

!!! info "Configuration Description"

Conditions (all must be true):

* The PR author is Renovate.
* The branch name includes 'renovate'
* The dependency change is a patch or minor update.

Automation Actions:

* Approve the PR
* Apply an `approved-renovate` label to the PR
* Merge the PR if all status checks pass.

!!! example "Configuration Example"
```yaml+jinja
--8<-- "docs/downloads/automation-library/integrations/renovate/approve_renovate_minor.cm"
```
<div class="result" markdown>
<span>
[:octicons-download-24: Download this example as a CM file.](/downloads/automation-library/integrations/renovate/approve_renovate_minor.cm){ .md-button }
</span>
</div>

=== "All Updates"
!!! info "Configuration Description"

![Approve and Merge Renovate Changes](/automations/integrations/renovate/approve-renovate/approve-renovate.png)
Conditions (all must be true):

!!! info "Configuration Description"
* The PR author is Renovate.
* The branch name includes 'renovate'

**Conditions (all must be true):**
Automation Actions:

* The PR author is Mend Renovate.
* Approve the PR
* Apply an `approved-renovate` label to the PR
* Merge the PR if all status checks pass.

**Automation Actions:**

* Approve the PR
* Apply an `approved-renovate` label to the PR
* Merge the PR if all status checks pass.
!!! example "Configuration Example"
```yaml+jinja
--8<-- "docs/downloads/automation-library/integrations/renovate/approve_renovate.cm"
```
<div class="result" markdown>
<span>
[:octicons-download-24: Download this example as a CM file.](/downloads/automation-library/integrations/renovate/approve_renovate.cm){ .md-button }
</span>
</div>

<div class="automationExample" markdown="1">
!!! example "Approve Renovate"
```yaml+jinja
--8<-- "docs/downloads/automation-library/integrations/renovate/approve-renovate/approve_renovate.cm"
```
<div class="result" markdown>
<span>
[:octicons-download-24: Download this example as a CM file.](/downloads/automation-library/integrations/renovate/approve-renovate/approve_renovate.cm){ .md-button }
</span>
</div>
<!-- --8<-- [end:example]-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
manifest:
version: 1.0

automations:
merge_renovate_minor:
if:
- {{ bump == 'minor' }}
- {{ branch.name | includes(term="renovate") }}
- {{ branch.author | includes(term="renovate") }}
run:
- action: approve@v1
- action: add-comment@v1
args:
comment: |
Renovate `minor` version bumps are approved automatically.

merge_renovate_minor_patch:
if:
- {{ bump == 'patch' }}
- {{ branch.name | includes(term="renovate") }}
- {{ branch.author | includes(term="renovate") }}
run:
- action: approve@v1
- action: merge@v1
- action: add-comment@v1
args:
comment: |
Renovate `patch` version bumps are approved and merged automatically.

bump: {{ pr.description | extractRenovateVersionBump | compareMultiSemver }}
4 changes: 4 additions & 0 deletions docs/filter-function-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ description: Implement custom gitStream filter functions with JavaScript.

JavaScript plugins that enable custom filter functions for gitStream. To learn how to use these examples, read our [guide on how to use gitStream plugins](/plugins).

--8<-- "plugins/filters/compareMultiSemver/README.md"

--8<-- "plugins/filters/compareSemver/README.md"

--8<-- "plugins/filters/extractDependabotVersionBump/README.md"

--8<-- "plugins/filters/extractRenovateVersionBump/README.md"

--8<-- "plugins/filters/extractSnykVersionBump/README.md"

--8<-- "plugins/filters/extractOrcaFindings/README.md"
Expand Down
21 changes: 21 additions & 0 deletions plugins/filters/compareMultiSemver/LICENSE
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.
21 changes: 21 additions & 0 deletions plugins/filters/compareMultiSemver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--8<-- "plugins/filters/compareMultiSemver/reference.md"

??? note "Plugin Code: compareMultiSemver"
```javascript
--8<-- "plugins/filters/compareMultiSemver/index.js"
```
<div class="result" markdown>
<span>
</span>
</div>

??? example "gitStream CM Example: compareMultiSemver"
```yaml+jinja
--8<-- "plugins/filters/compareMultiSemver/compare_multi_semver.cm"
```
<div class="result" markdown>
<span>
</span>
</div>

[Download Source Code](https://github.com/linear-b/gitstream/tree/main/plugins/filters/compareMultiSemver)
26 changes: 26 additions & 0 deletions plugins/filters/compareMultiSemver/compare_multi_semver.cm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
manifest:
version: 1.0

automations:
bump_minor:
if:
- {{ bump == 'minor' }}
run:
- action: approve@v1
- action: add-comment@v1
args:
comment: |
bot `minor` version bumps are approved automatically.

bump_patch:
if:
- {{ bump == 'patch' }}
run:
- action: approve@v1
- action: merge@v1
- action: add-comment@v1
args:
comment: |
bot `patch` version bumps are approved and merged automatically.

bump: {{ [["1.2.3", "1.2.1"], ["1.3.1", "1.2.3"]] | compareMultiSemver }}
48 changes: 48 additions & 0 deletions plugins/filters/compareMultiSemver/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @module compareMultiSemver
* @description Processes a list of pairs of semantic version numbers and determines the most significant change among them.
* Each pair consists of two versions to be compared.
* @param {string[][]} listOfPairs - An array of version pairs, where each pair is an array of two semantic version strings.
* @returns {string} It returns a string of either:
* 'major' if any pair has a major version increment.
* 'minor' if no pair has a major version increment but has a minor version increment.
* 'patch' if no pair has major or minor version increments but has a patch version increment.
* 'downgrade' if no pairs have a higher version.
* 'equal' if all pairs are equal.
* 'error' if the comparison is abnormal or cannot be determined.
* @example {{ [["1.2.3", "1.2.1"], ["1.3.1", "1.2.3"]] | compareMultiSemver == "minor" }}
* @license MIT
*/


const compareSemver = require('../compareSemver/index.js');

module.exports = (listOfPairs) => {

const priority = {
'major': 3,
'minor': 2,
'patch': 1,
'downgrade': 0,
'equal': -1,
'error': -2
};

let mostSignificantChange = 'equal';

listOfPairs.forEach(pair => {
const result = compareSemver(pair);
if (priority[result] > priority[mostSignificantChange]) {
mostSignificantChange = result;
}
});

return mostSignificantChange;
}


const compareMultiSemver = require('./index.js');
console.assert(compareMultiSemver([["1.2.3", "1.2.1"], ["1.3.1", "1.2.3"]]) === 'minor', `compareSemver([["1.2.3", "1.2.1"], ["1.3.1", "1.2.3"]]) == 'minor'`);
console.assert(compareMultiSemver([["1.2.3", "0.2.1"], ["1.3.1", "1.2.3"]]) === 'major', `compareMultiSemver([["1.2.3", "0.2.1"], ["1.3.1", "1.2.3"]]) === 'major'`);
console.assert(compareMultiSemver([["2.2.3", "0.2.1"], ["1.3.1", "1.2.3"]]) === 'major', `compareMultiSemver([["2.2.3", "0.2.1"], ["1.3.1", "1.2.3"]]) === 'major'`);
console.assert(compareMultiSemver([["1.2.3", "1.2.1"], ["1.2.4", "1.2.3"]]) === 'patch', `compareMultiSemver([["1.2.3", "1.2.1"], ["1.2.4", "1.2.3"]]) === 'patch'`);
23 changes: 23 additions & 0 deletions plugins/filters/compareMultiSemver/reference.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<a name="module_compareSemver"></a>

## compareMultiSemver
Processes a list of pairs of semantic version numbers and determines the most significant change among them.

**Returns**: <code>string</code> - It returns a string of either:
'major' if any pair has a major version increment.
'minor' if no pair has a major version increment but has a minor version increment.
'patch' if no pair has major or minor version increments but has a patch version increment.
'downgrade' if no pairs have a higher version.
'equal' if all pairs are equal.
'error' if the comparison is abnormal or cannot be determined.

**License**: MIT

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| listOfPairs | <code>Array.&lt;Array&gt;</code> | | An array of version pairs, where each pair is an array of two semantic version strings.|

**Example**
```js
{{ [["1.2.3", "0.2.1"], ["1.3.1", "1.2.3"]] | compareMultiSemver == "major" }}
```
3 changes: 2 additions & 1 deletion plugins/filters/compareSemver/compare_semver.cm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ automations:
args:
comment: |
bot `minor` version bumps are approved automatically.

bump_patch:
if:
- {{ bump == 'patch' }}
Expand All @@ -22,4 +23,4 @@ automations:
comment: |
bot `patch` version bumps are approved and merged automatically.

bump: {{ ["1.2.1", "1.2.3"] | compareSemver }}
bump: {{ ["1.2.3", "1.2.1"] | compareSemver }}
28 changes: 18 additions & 10 deletions plugins/filters/compareSemver/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
/**
* @module compareSemver
* @description Compares two software version numbers (e.g., "1.2.1" or "1.2b") and determines the type of version change.
* The first version to be compared, and the second are passed as argument 1 and 2 or as array of 2 items.
* The first version to be compared, and the second are passed as argument 1 and 2 or as array of 2 items.
* When V1 > V2 the it means and upgrade.
* @param {string[]} versions - V1 and V2 in Semver format
* @param {boolean} [lexicographical=false] - compares each part of the version strings lexicographically instead of naturally;
* this allows suffixes such as "b" or "dev" but will cause "1.10" to be considered smaller than "1.2".
* @param {boolean} [zeroExtend=true] - changes the result if one version string has less parts than the other. In
* this case the shorter string will be padded with "zero" parts instead of being considered smaller.
* @returns {string} It returns a string of either:
* 'major' if the major version is incremented.
* 'minor' if the minor version is incremented.
* 'patch' if the patch version is incremented.
* 'downgrade' if the second version is lower than the first.
* 'equal' if both versions are equal.
* 'error' if the comparison is abnormal or cannot be determined.
* @example {{ ["1.2.1", "1.2.3"] | compareSemver == "patch" }}
* @example {{ ["1.2.3", "1.2.1"] | compareSemver == "patch" }}
* @license MIT
**/


module.exports = (v1, v2, options = {}) => {
console.log("SEMVER", {v1, v2, options});
module.exports = (v1, v2) => {
const lexicographical = false;
const zeroExtend = true;

// support array as input
// support array as input
if (Array.isArray(v1) && v2 === undefined) {
[v1, v2] = v1; // Destructure the first two elements of the array into v1 and v2
}

const { lexicographical = false, zeroExtend = true } = options;
let v1parts = (v1 || "0").split('.');
let v2parts = (v2 || "0").split('.');

Expand Down Expand Up @@ -70,3 +66,15 @@ module.exports = (v1, v2, options = {}) => {

return 'equal';
}


const compareSemver = require('./index.js');
console.assert(compareSemver(["1.2.3", "1.2.1"]) === 'patch', `compareSemver(["1.2.3", "1.2.1"]) == 'patch'`);
console.assert(compareSemver(["1.2.0", "1.2.1"]) === 'downgrade', `compareSemver(["1.2.0", "1.2.1"]) === downgrade'`);
console.assert(compareSemver(["1.3.0", "1.2.1"]) === 'minor', `compareSemver(["1.3.0", "1.2.1"]) == 'minor'`);
console.assert(compareSemver(["2.0.0", "1.2.1"]) === 'major', `compareSemver(["2.0.0", "1.2.1"]) == 'major'`);
console.assert(compareSemver(["1.2.1", "1.2.1"]) === 'equal', `compareSemver(["1.2.1", "1.2.1"]) == 'equal'`);
console.assert(compareSemver(["1.2b", "1.2.1"]) === 'minor', `compareSemver(["1.2b", "1.2.1"]) == 'error'`);
console.assert(compareSemver(["1.2.0", "1.2"]) === 'equal', `compareSemver(["1.2.0", "1.2"]) == 'equal'`);
console.assert(compareSemver(["1.2.1.0", "1.2.1"]) === 'equal', `compareSemver(["1.2.1.0", "1.2.1"]) == 'equal'`);
console.assert(compareSemver(["1.2.1.0a", "1.2.1"]) === 'error', `compareSemver(["1.2.1.0a", "1.2.1"]) === downgrade'`);
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ automations:
args:
comment: |
Dependabot `minor` version bumps are approved automatically.

bump_patch:
if:
- {{ bump == 'patch' }}
Expand Down
21 changes: 21 additions & 0 deletions plugins/filters/extractRenovateVersionBump/LICENSE
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.
Loading

0 comments on commit c091a5c

Please sign in to comment.