eb2ff84a2c8ae45b90d36e367c6bd6bc01f8464d

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
Carlo Sala <carlosalag@protonmail.com>
Date

Message

fix(dependencies): avoid creating PR if it's already there

Diff

 1diff --git a/.github/workflows/dependencies/updater.py b/.github/workflows/dependencies/updater.py
 2index 7e8e92ca9b422062d1c4fdfd3b2a6b54943b24ef..6fa32e37878c0b228a66b86be36197100d91cf76 100644
 3--- a/.github/workflows/dependencies/updater.py
 4+++ b/.github/workflows/dependencies/updater.py
 5@@ -1,3 +1,4 @@
 6+import json
 7 import os
 8 import re
 9 import shutil
10@@ -213,8 +214,10 @@ class Dependency:
11                 new_version = status["version"] if is_tag else short_sha
12 
13                 try:
14+                    branch_name = f"update/{self.path}/{new_version}"
15+
16                     # Create new branch
17-                    branch = Git.create_branch(self.path, new_version)
18+                    branch = Git.checkout_or_create_branch(branch_name)
19 
20                     # Update dependencies.yml file
21                     self.__update_yaml(
22@@ -353,7 +356,7 @@ class Git:
23             )
24 
25     @staticmethod
26-    def create_branch(path: str, version: str):
27+    def checkout_or_create_branch(branch_name: str):
28         # Get current branch name
29         result = CommandRunner.run_or_fail(
30             ["git", "rev-parse", "--abbrev-ref", "HEAD"], stage="GetDefaultBranch"
31@@ -361,10 +364,16 @@ class Git:
32         Git.default_branch = result.stdout.decode("utf-8").strip()
33 
34         # Create new branch and return created branch name
35-        branch_name = f"update/{path}/{version}"
36-        CommandRunner.run_or_fail(
37-            ["git", "checkout", "-b", branch_name], stage="CreateBranch"
38-        )
39+        try:
40+            # try to checkout already existing branch
41+            CommandRunner.run_or_fail(
42+                ["git", "checkout", branch_name], stage="CreateBranch"
43+            )
44+        except CommandRunner.Exception:
45+            # otherwise create new branch
46+            CommandRunner.run_or_fail(
47+                ["git", "checkout", "-b", branch_name], stage="CreateBranch"
48+            )
49         return branch_name
50 
51     @staticmethod
52@@ -515,6 +524,27 @@ class GitHub:
53 
54     @staticmethod
55     def create_pr(branch: str, title: str, body: str) -> None:
56+        # first of all let's check if PR is already open
57+        check_cmd = [
58+            "gh",
59+            "pr",
60+            "list",
61+            "--state",
62+            "open",
63+            "--head",
64+            branch,
65+            "--json",
66+            "title",
67+        ]
68+        # returncode is 0 also if no PRs are found
69+        output = json.loads(
70+            CommandRunner.run_or_fail(check_cmd, stage="CheckPullRequestOpen")
71+            .stdout.decode("utf-8")
72+            .strip()
73+        )
74+        # we have PR in this case!
75+        if len(output) > 0:
76+            return
77         cmd = [
78             "gh",
79             "pr",