merge_test.py 2.17 KB
Newer Older
canterburym's avatar
commit  
canterburym committed
1
2
import os
import pprint
canterburym's avatar
canterburym committed
3
4
import requests
import json
canterburym's avatar
canterburym committed
5
import subprocess
canterburym's avatar
commit  
canterburym committed
6

canterburym's avatar
canterburym committed
7
crCommitBranch = os.environ.get("CI_COMMIT_REF_NAME", "NOTFOUND")
canterburym's avatar
canterburym committed
8
9
10
11
12
13
14
15
apiUrl = os.environ.get("CI_API_V4_URL", "https://forge.3gpp.org/rep/api/v4")
projectId = os.environ.get("CI_PROJECT_ID", "13")

def gapi (query):
    url = f"{apiUrl}/projects/{projectId}/{query}"
    r = requests.get(url)
    return json.loads(r.text)

canterburym's avatar
canterburym committed
16
def do (commandline):
canterburym's avatar
canterburym committed
17
    #print ("   Attempting: " + commandline)
canterburym's avatar
canterburym committed
18
    completedProc = subprocess.run(commandline, capture_output=True, shell=True)
canterburym's avatar
canterburym committed
19
20
21
    #print ("   STDOUT > " + ("empty" if completedProc.stdout is None else completedProc.stdout.decode('utf-8')))
    #print ("   STDERR > " + ("empty" if completedProc.stderr is None else completedProc.stderr.decode('utf-8')))
    #print (f"   Completed with code {completedProc.returncode}")
canterburym's avatar
canterburym committed
22
    return (completedProc.returncode == 0,  completedProc.stdout.decode('utf-8'))
canterburym's avatar
canterburym committed
23
24
25

print ("Searching for corresponding MR...")

26
mrs = gapi(f"merge_requests?source_branch={crCommitBranch}&state=opened")
canterburym's avatar
canterburym committed
27
28
29
30
31
32
if len(mrs) == 0:
    print ("No MR found... aborting")
    exit()

if len(mrs) > 1:
    print (f"{len(mrs)} MRs found, 1 expected - aborting")
33
34
    for m in mrs:
        pprint.pprint(m)
canterburym's avatar
canterburym committed
35
36
37
38
39
40
41
42
43
    exit(-1)

mr = mrs[0]

print (f"Found MR {mr['reference']} ({mr['title']})")
print (f"Target branch is {mr['target_branch']}")
print ("Searching for open MRs targeting same branch...")

mrs = gapi(f"merge_requests?target_branch={mr['target_branch']}&state=opened")
canterburym's avatar
canterburym committed
44
mrs = [m for m in mrs if m['reference'] != mr['reference']]
canterburym's avatar
canterburym committed
45
46
print (f"{len(mrs)} MRs found")

canterburym's avatar
canterburym committed
47
mergeConflicts = {}
canterburym's avatar
canterburym committed
48

canterburym's avatar
canterburym committed
49
50
51
52
53
54
for mr in mrs:
    source_branch = mr['source_branch']
    print (source_branch)

    try:
        do(f"git fetch origin {source_branch}:{source_branch}")
canterburym's avatar
canterburym committed
55
56
        success, errStr = do(f"git merge --no-commit {source_branch}")
        if not success:
canterburym's avatar
canterburym committed
57
            print ("Merge NOT OK")
canterburym's avatar
canterburym committed
58
            mergeConflicts[source_branch] = errStr
canterburym's avatar
canterburym committed
59
60
        else:
            print ("Merge OK")
canterburym's avatar
canterburym committed
61
62
    except Exception as ex:
        mergeConflicts[source_branch] = str(ex)
canterburym's avatar
canterburym committed
63
64
65
66
67
        raise
    finally:
        do("git merge --abort")

print (f"Merge conflicts with following branches: {mergeConflicts}")
canterburym's avatar
canterburym committed
68
exit(len(mergeConflicts.keys()))