從nexus上查詢api最新版本號的腳本

代碼上線前,每每要升級api的版本號。但每次都去nexus上手動查詢最新的版本號,也是挺麻煩的。python

依然是爲了更高效的摸魚,寫了一個python腳本,用於查詢指定api的最大版本號。json

# 引入第三方庫 requests
import requests

import re
import math
import json


ARTIFACT_ID = 'one-api'
GROUP_ID = 'com.one'

NEXUS_URL = 'http://repo.one.com/service/extdirect'
NEXUS_COOKIE = 'onetwothree'


def main():
    # 從nexus獲取指定項目的全部版本號
    version_list = __get_version_list(ARTIFACT_ID, GROUP_ID, NEXUS_URL, NEXUS_COOKIE)

    # 計算出最大的版本號
    max_version = __get_max_version(version_list)
    print('最新的版本號 >>>   ' + max_version)


# 從集合中,查找出最大的版本號
def __get_max_version(version_list):
    # 初始值設爲 0.0.0
    # 數據格式爲 ('完整的版本號',主版本號, 次版本號, 小版本號)
    max_version_tup = ('0.0.0', 0, 0, 0)

    for current_version in version_list:
        # 對於形如 1.1.0-SNAPSHOT 的版本號,轉換成 1.1.0
        temp_version = re.sub('-.*', '', current_version)
        num_list = temp_version.split('.')
        current_version_tup = [current_version, int(num_list[0]), int(num_list[1]), int(num_list[2])]

        # 與上一次記錄的最大版本號比較,取兩者最大值,做爲最新的最大版本號
        max_version_tup = __get_max_version_tup(max_version_tup, current_version_tup)

    return max_version_tup[0]


# 比較兩個版本號,返回其中的最大者
def __get_max_version_tup(tup_a, tup_b):
    # 依次比較3個版本號
    if tup_a[1] > tup_b[1]:
        return tup_a
    if tup_a[1] < tup_b[1]:
        return tup_b

    if tup_a[2] > tup_b[2]:
        return tup_a
    if tup_a[2] < tup_b[2]:
        return tup_b

    if tup_a[3] > tup_b[3]:
        return tup_a
    if tup_a[3] < tup_b[3]:
        return tup_b

    return tup_a


# 查詢指定項目的全部版本號
def __get_version_list(artifact_id, group_id, url, cookie):
    # 設置分頁查詢的參數
    page_index = 1
    page_size = 300
    request_dist = __build_request_dist(artifact_id, group_id, page_index, page_size)
    response = __post_json(request_dist, url, cookie)

    # 獲取版本號的總數量、版本號列表
    total = __get_total(response)
    version_list = []
    # 從響應參數中,提取出全部版本號,並追加至數組
    __fill_version(version_list, response)

    # 計算剩餘的循環次數
    # 即,向上取整(總數/頁大小)-1
    circulation_num = math.ceil(total / page_size) - 1

    while circulation_num > 0:
        # 循環分頁查詢版本號
        circulation_num -= 1
        page_index += 1
        request_dist = __build_request_dist(artifact_id, group_id, page_index, page_size)
        response = __post_json(request_dist, url, cookie)

        # 從響應參數中,提取出全部版本號,並追加至數組
        __fill_version(version_list, response)

    return version_list


# 獲取版本號的總數量
def __get_total(response):
    response = json.loads(response)
    result = response["result"]
    total = result["total"]
    return total


# 從響應參數中,提取出全部版本號,並追加至數組
def __fill_version(version_list, response):
    response = json.loads(response)
    result = response["result"]
    data = result["data"]
    for i in data:
        version_list.append(i['version'])
    return version_list


# 構建請求參數
def __build_request_dist(artifact_id, group_id, page_index, page_size):
    # 設置起始值
    start = (page_index - 1) * page_size

    # 查詢參數
    request_dist = {
        'action': 'coreui_Search',
        'method': 'read',
        'data': [
            {
                'page': page_index,
                'start': start,
                'limit': page_size,
                'filter': [
                    # 以maven的格式搜索
                    {
                        'property': 'format',
                        'value': 'maven2'
                    },
                    {
                        'property': 'attributes.maven2.artifactId',
                        'value': artifact_id
                    },
                    {
                        'property': 'attributes.maven2.groupId',
                        'value': group_id
                    },
                    # 只查詢release版本
                    {
                        'property': 'repository_name',
                        'value': 'maven-releases'
                    }
                ]
            }
        ],
        'type': 'rpc',
        'tid': 14
    }
    return request_dist


# json格式的post請求
def __post_json(request_dist, url, cookie):
    # 將數據轉爲JSON格式
    data_json = json.dumps(request_dist)

    headers = {'Cookie': cookie, 'content-type': 'application/json', 'charset': 'utf-8'}
    response = requests.post(url, data=data_json.encode('utf-8'), headers=headers)
    return response.text


if __name__ == '__main__':
    main()
相關文章
相關標籤/搜索