Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit bf542357 authored by Mohammed Althaf T's avatar Mohammed Althaf T 😊
Browse files

roomservice: Check for branch before fetching

parent f0d04df3
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -149,6 +149,12 @@ def get_default_revision():
                r = remote.get('revision')
    return r.replace('refs/heads/', '').replace('refs/tags/', '')

def get_fallback_branch():
    m = ElementTree.parse(get_manifest_path())
    d = m.findall('default')[0]
    r = d.get('revision')
    return r.replace('refs/heads/', '').replace('refs/tags/', '')

def get_from_manifest(devicename):
    try:
        lm = ElementTree.parse(".repo/local_manifests/roomservice.xml")
@@ -198,12 +204,22 @@ def is_in_manifest(projectpath):
    return False

def is_on_e(repository):
    default_branch = get_default_revision()
    search_link = "{}/projects?search={}".format(gitlab_api_url, repository)
    gitlabreq = urllib.request.Request(search_link)
    try:
        result = json.loads(urllib.request.urlopen(gitlabreq).read().decode())
        if result:
            branch_url = '{}/projects/{}/repository/branches'.format(gitlab_api_url, result[0]['id'])
            branches = urllib.request.Request(branch_url)
            branches_available = json.loads(urllib.request.urlopen(branches).read().decode())
            for b in branches_available:
                if b["name"] == default_branch:
                   repos_from_e = True
                   return True
            else:
                repos_from_e = False
                return False
    except:
        print("Failed to search Gitlab or could not parse return data from Gitlab")
        return False
@@ -295,17 +311,18 @@ def get_default_or_fallback_revision(repo_name):
    print("Default revision: %s" % default_revision)
    print("Checking branch info")

    if repos_from_e:
        gitreq = urllib.request.Request("{}/projects/{}/repository/branches".format(gitlab_api_url, repository['id']))
    else:
    gitreq = urllib.request.Request("https://api.github.com/repos/LineageOS/" + repo_name + "/branches")

    if is_on_e(repo_name) and repos_from_e:
        gitreq = urllib.request.Request("{}/projects/{}/repository/branches".format(gitlab_api_url, repository['id']))

    add_auth(gitreq)
    result = json.loads(urllib.request.urlopen(gitreq).read().decode())
    if has_branch(result, default_revision):
        return default_revision

    fallbacks = [ get_default_revision_no_minor() ]
    fallbacks += [ get_fallback_branch().rsplit('.', 1)[0] ]
    if os.getenv('ROOMSERVICE_BRANCHES'):
        fallbacks += list(filter(bool, os.getenv('ROOMSERVICE_BRANCHES').split(' ')))