aboutsummaryrefslogtreecommitdiff
path: root/labbot/addons/merge-stable.py
blob: 9b2f1332f4f3a95b0fd0f1cbc632f8484bafa528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
once an issue is assigned a specific label
create merge requests for each branch related to that issue
branch relation is figured out by looking at related merge requests
"""

import logging

from labbot.config import Config

log = logging.getLogger(__name__)

config = Config()
config.set_global_data(
    stable_branch = "main",
    staging_branch = "staging",
    merge_label = "Release",
)

@config.config_decorator()
async def issue_update_hook(event, gl, *args, **kwargs):
    issue_id = event.object_attributes["iid"]
    issue_url = f"/projects/{event.project_id}/issues/{issue_id}"
    issue_data = await gl.getitem(issue_url)

    if issue_data["state"] != "opened":
        log.debug(f"{issue_id} is closed, not opening merge request")
        return

    branches = {}
    if config["merge_label"] in issue_data["labels"]:
        async for merge_data in gl.getiter(f"{issue_url}/related_merge_requests"):
            source_branch = merge_data["source_branch"]
            target_branch = merge_data["target_branch"]
            merge_id = merge_data["iid"]

            # we only want staging merges
            if target_branch != config["staging_branch"]:
                continue

            if not source_branch in branches:
                branches[source_branch] = []
            branches[source_branch].append(f"!{merge_id}")

    if branches:
        branch_str = ", ".join(branches.keys())
        log.debug(f"`{branch_str}`({event.project_id}) are ready to be merged into stable")

    merge_url = f"/projects/{event.project_id}/merge_requests"
    for branch, merge in branches.items():
        create_stable_merge = True

        MAX_AVAIL_MERGES = 3
        merges_exists = 0
        async for merge_data in gl.getiter(merge_url, params={
            "source_branch": branch,
            "target_branch": config["stable_branch"]
        }):
            if merge_data["state"] == "opened":
                # we already have an open MR
                create_stable_merge = False
                break

            if merge_data["state"] != "merged":
                # We found a merge request that has not been merged.
                # Add it to the counter
                merges_exists += 1

            # Are there more than MAX_AVAIL_MERGES available merges?
            # Stop creating new ones for gods sake
            if merges_exists > MAX_AVAIL_MERGES:
                create_stable_merge = False
                break

        if create_stable_merge:
            log.debug(f"merges for `{branch}` already exists, not opening more than 3")
        else:
            merge_string = ", ".join(merge)
            await gl.post(merge_url, data={
                "source_branch": branch,
                "target_branch": config["stable_branch"],
                "title": f"[stable] Merge `{branch}` into `{config['stable_branch']}`",
                "description": f"Related to #{issue_id}    \n    \nStaging Merges:    \n{merge_string}"
            })


def setup(bot) -> None:
    config.setup(__name__, bot.name)
    bot.register_issue_hook(issue_update_hook)