From 2d0ec996618e4df79d4f4ae75c965d41e85a2639 Mon Sep 17 00:00:00 2001 From: Eirik Stanghelle Morland Date: Wed, 8 Jun 2022 08:22:11 +0200 Subject: [PATCH] Add option for automerge (#17) * Add option for automerge * Add option for security only. Add test coverage --- src/Config.php | 21 ++++++ tests/UnitTest.php | 94 +++++++++++++++++++++++++ tests/fixtures/automerge.json | 7 ++ tests/fixtures/automerge2.json | 7 ++ tests/fixtures/automerge3.json | 7 ++ tests/fixtures/automerge4.json | 7 ++ tests/fixtures/automerge5.json | 7 ++ tests/fixtures/automerge_security.json | 8 +++ tests/fixtures/automerge_security2.json | 8 +++ tests/fixtures/automerge_security3.json | 8 +++ 10 files changed, 174 insertions(+) create mode 100644 tests/fixtures/automerge.json create mode 100644 tests/fixtures/automerge2.json create mode 100644 tests/fixtures/automerge3.json create mode 100644 tests/fixtures/automerge4.json create mode 100644 tests/fixtures/automerge5.json create mode 100644 tests/fixtures/automerge_security.json create mode 100644 tests/fixtures/automerge_security2.json create mode 100644 tests/fixtures/automerge_security3.json diff --git a/src/Config.php b/src/Config.php index 264385c..1f5f1ad 100644 --- a/src/Config.php +++ b/src/Config.php @@ -34,6 +34,8 @@ public function getDefaultConfig() 'branch_prefix' => '', 'commit_message_convention' => '', 'allow_update_indirect_with_direct' => 0, + 'automerge' => 0, + 'automerge_security' => 0, ]; } @@ -72,6 +74,25 @@ public function hasConfigForKey($key) return !empty($this->configOptionsSet[$key]); } + public function shouldAutoMerge($is_security_update = false) + { + if (!$is_security_update) { + // It's not a security update. Let's use the option found in the config. + return (bool) $this->config->automerge; + } + if ($this->shouldAutoMergeSecurity()) { + // Meaning we should automerge, no matter what the general automerge config says. + return true; + } + // Fall back to using the actual option. + return (bool) $this->config->automerge; + } + + public function shouldAutoMergeSecurity() + { + return (bool) $this->config->automerge_security; + } + public function shouldUpdateIndirectWithDirect() { return (bool) $this->config->allow_update_indirect_with_direct; diff --git a/tests/UnitTest.php b/tests/UnitTest.php index 6b2a5ed..0763f97 100644 --- a/tests/UnitTest.php +++ b/tests/UnitTest.php @@ -213,6 +213,28 @@ public function testAlwaysUpdateAll($filename, $expected_result) self::assertEquals($expected_result, $data->shouldAlwaysUpdateAll()); } + /** + * Test the automerge option. + * + * @dataProvider getAutoMerge + */ + public function testAutoMerge($filename, $expected_result) + { + $data = $this->createDataFromFixture($filename); + self::assertEquals($expected_result, $data->shouldAutoMerge()); + } + + /** + * Test the automerge_security option. + * + * @dataProvider getAutoMergeSecurity + */ + public function testAutoMergeSecurity($filename, $expected_result) + { + $data = $this->createDataFromFixture($filename); + self::assertEquals($expected_result, $data->shouldAutoMerge(true)); + } + /** * Test the security updates config option. * @@ -278,6 +300,78 @@ public function getUpdateWithDeps() ]; } + public function getAutoMerge() + { + return [ + [ + 'empty.json', + false, + ], + [ + 'automerge.json', + true, + ], + [ + 'automerge2.json', + true, + ], + [ + 'automerge3.json', + true, + ], + [ + 'automerge4.json', + false, + ], + [ + 'automerge5.json', + false, + ] + ]; + } + + public function getAutoMergeSecurity() + { + return [ + [ + 'empty.json', + false, + ], + [ + 'automerge.json', + true, + ], + [ + 'automerge2.json', + true, + ], + [ + 'automerge3.json', + true, + ], + [ + 'automerge4.json', + false, + ], + [ + 'automerge5.json', + false, + ], + [ + 'automerge_security.json', + true, + ], + [ + 'automerge_security2.json', + true, + ], + [ + 'automerge_security3.json', + true, + ], + ]; + } + public function getAlwaysUpdateAll() { return [ diff --git a/tests/fixtures/automerge.json b/tests/fixtures/automerge.json new file mode 100644 index 0000000..e8e41ec --- /dev/null +++ b/tests/fixtures/automerge.json @@ -0,0 +1,7 @@ +{ + "extra": { + "violinist": { + "automerge": "yes_of_course_this_should_be_an_int_but_this_evaluates_to_true" + } + } +} diff --git a/tests/fixtures/automerge2.json b/tests/fixtures/automerge2.json new file mode 100644 index 0000000..c9d55c8 --- /dev/null +++ b/tests/fixtures/automerge2.json @@ -0,0 +1,7 @@ +{ + "extra": { + "violinist": { + "automerge": "1" + } + } +} diff --git a/tests/fixtures/automerge3.json b/tests/fixtures/automerge3.json new file mode 100644 index 0000000..a4d965b --- /dev/null +++ b/tests/fixtures/automerge3.json @@ -0,0 +1,7 @@ +{ + "extra": { + "violinist": { + "automerge": 1 + } + } +} diff --git a/tests/fixtures/automerge4.json b/tests/fixtures/automerge4.json new file mode 100644 index 0000000..6aac445 --- /dev/null +++ b/tests/fixtures/automerge4.json @@ -0,0 +1,7 @@ +{ + "extra": { + "violinist": { + "automerge": null + } + } +} diff --git a/tests/fixtures/automerge5.json b/tests/fixtures/automerge5.json new file mode 100644 index 0000000..c10f019 --- /dev/null +++ b/tests/fixtures/automerge5.json @@ -0,0 +1,7 @@ +{ + "extra": { + "violinist": { + "automerge": 0 + } + } +} diff --git a/tests/fixtures/automerge_security.json b/tests/fixtures/automerge_security.json new file mode 100644 index 0000000..60fe9fb --- /dev/null +++ b/tests/fixtures/automerge_security.json @@ -0,0 +1,8 @@ +{ + "extra": { + "violinist": { + "automerge": 0, + "automerge_security": 1 + } + } +} diff --git a/tests/fixtures/automerge_security2.json b/tests/fixtures/automerge_security2.json new file mode 100644 index 0000000..3706d3d --- /dev/null +++ b/tests/fixtures/automerge_security2.json @@ -0,0 +1,8 @@ +{ + "extra": { + "violinist": { + "automerge": 1, + "automerge_security": 1 + } + } +} diff --git a/tests/fixtures/automerge_security3.json b/tests/fixtures/automerge_security3.json new file mode 100644 index 0000000..b2fcd39 --- /dev/null +++ b/tests/fixtures/automerge_security3.json @@ -0,0 +1,8 @@ +{ + "extra": { + "violinist": { + "automerge": 1, + "automerge_security": 0 + } + } +}