def load_community_repos(timeout=10): """ 获取社区repo :param timeout: :return: """ repos = {} def analysis(response): """ requests回调 :param response: requests response object :return: """ handler = yaml.safe_load(response.text) repos.update({ item["name"]: item["type"] for item in handler["repositories"] }) logger.info("repos from community: {}".format(len(repos))) community_repo_url = "https://gitee.com/openeuler/community/raw/master/repository/src-openeuler.yaml" logger.info( "requests repos from community, this will take multi seconds") do_requests("get", url=community_repo_url, timeout=timeout, obj=analysis) return repos
def pkgship_notify(self, notify_url, notify_token, package_url, package_arch, notify_jenkins_user, notify_jenkins_password): """ notify :param notify_url: notify url :param notify_token: notify token :param package_url: package addr :param package_arch: cpu arch :param notify_jenkins_user: :param notify_jenkins_password: :return: """ package = self._rpm_package.last_main_package(package_arch, package_url) querystring = { "token": notify_token, "PACKAGE_URL": package, "arch": package_arch } ret = do_requests("get", notify_url, querystring=querystring, auth={ "user": notify_jenkins_user, "password": notify_jenkins_password }, timeout=1) if ret in [0, 2]: # send async, don't care about response, timeout will be ok logger.info("notify ...ok") else: logger.error("notify ...fail")
def delete_tag_of_pr(self, pr, tag): """ 删除pr tag :param pr: 本仓库PR的序数 :param tag: 标签 :return: 0成功,其它失败 """ logger.debug("delete tag {} of pull request {}".format(tag, pr)) pr_tag_url = "https://gitee.com/api/v5/repos/{}/{}/pulls/{}/labels/{}?access_token={}".format( self._owner, self._repo, pr, tag, self._token) rs = do_requests("delete", pr_tag_url, timeout=10) if rs != 0: logger.warning("delete tags failed") return False return True
def comment_pr(self, pr, comment): """ 评论pull request :param pr: 本仓库PR的序数 :param comment: 评论内容 :return: 0成功,其它失败 """ logger.debug("comment pull request {}".format(pr)) comment_pr_url = "https://gitee.com/api/v5/repos/{}/{}/pulls/{}/comments".format( self._owner, self._repo, pr) data = {"access_token": self._token, "body": comment} rs = do_requests("post", comment_pr_url, body=data, timeout=10) if rs != 0: logger.warning("comment pull request failed") return False return True
def check_homepage(self, timeout=30, retrying=3, interval=1): """ 检查主页是否可访问 :param timeout: 超时时间 :param retrying: 重试次数 :param interval: 重试间隔 :return: """ homepage = self._spec.url logger.debug("homepage: {}".format(homepage)) if not homepage: return SUCCESS for _ in range(retrying): if 0 == do_requests("get", homepage, timeout=timeout): return SUCCESS time.sleep(interval) return FAILED
def replace_all_tags_of_pr(self, pr, *tags): """ 替换所有pr tag :param pr: 本仓库PR的序数 :param tags: 标签 :return: 0成功,其它失败 """ if not tags: logger.debug("replace tags, but no tags") return True logger.debug("replace all tags with {} of pull request {}".format( tags, pr)) pr_tag_url = "https://gitee.com/api/v5/repos/{}/{}/pulls/{}/labels?access_token={}".format( self._owner, self._repo, pr, self._token) rs = do_requests("put", pr_tag_url, body=list(tags), timeout=10) if rs != 0: logger.warning("replace tags failed") return False return True
def get_last_pr_committer(self, branch, state="merged"): """ 获取指定分支的最后一个pr的提交者 :param branch: pr合入分支 :param state: pr状态 :return: str or None """ logger.debug( "get last pull request committer, branch: {}, state: {}".format( branch, state)) pr_url = "https://gitee.com/api/v5/repos/{}/{}/pulls?access_token={}&state={}&base={}" \ "&page=1&per_page=1".format(self._owner, self._repo, self._token, state, branch) # python2 not have nonlocal committer = [None] def analysis(response): """ requests回调,解析pr列表 :param response: requests response object :return: """ handler = response.json() if handler: try: committer[0] = handler[0]["user"]["login"] logger.debug("get last pr committer: {}".format(committer)) except KeyError: logger.exception( "extract committer info from gitee exception") rs = do_requests("get", pr_url, timeout=10, obj=analysis) if rs != 0: logger.warning("get last pr committer failed") return committer[0]
def generate_repo_info(self, branch, arch, repo_name_prefix): """ 不同的分支生成不同的repo :param branch: :param arch: :param repo_name_prefix: :return: """ repo_config = "" if branch == "master": obs_path_part = "openEuler:/Mainline" elif "openstack" in branch: branch = branch.replace("_oe-", "_openEuler-") # openEuler abbr. vendor, openstack, os = branch.split("_") obs_path_part = (":/").join([ os.replace("-", ":/"), vendor.replace("-", ":/"), openstack.replace("-", ":/") ]) obs_path_part_common = (":/").join([ os.replace("-", ":/"), vendor.replace("-", ":/"), "openstack:/common" ]) obs_path_part_base = (":/").join([os.replace("-", ":/")]) # openstack need common and base if "openstack-common" != openstack: # openstack common url = "{}/{}/standard_{}".format(self._current_repo_host, obs_path_part_common, arch) if do_requests("GET", url) == 0: logger.debug("add openstack common repo: {}".format(url)) repo_config += self.repo_format( "openstack_common", repo_name_prefix + "_openstack_common", url) # openstack base url = "{}/{}/standard_{}".format(self._current_repo_host, obs_path_part_base, arch) if do_requests("GET", url) == 0: logger.debug("add openstack base repo: {}".format(url)) repo_config += self.repo_format( "openstack_base", repo_name_prefix + "_openstack_base", url) else: obs_path_part = branch.replace("-", ":/") logger.debug("branch={}, obs_path_part={}".format( branch, obs_path_part)) # main url = "{}/{}/standard_{}".format(self._current_repo_host, obs_path_part, arch) if do_requests("GET", url) == 0: logger.debug("add main repo: {}".format(url)) repo_config += self.repo_format(repo_name_prefix + "_main", repo_name_prefix + "_main", url) # epol url = "{}/{}/standard_{}".format(self._current_repo_host, obs_path_part + ":/Epol", arch) if do_requests("GET", url) == 0: logger.debug("add epol repo: {}".format(url)) repo_config += self.repo_format(repo_name_prefix + "_epol", repo_name_prefix + "_epol", url) # extras url = "{}/{}/standard_{}".format(self._current_repo_host, obs_path_part + ":/Extras", arch) if do_requests("GET", url) == 0: logger.debug("add extras repo: {}".format(url)) repo_config += self.repo_format(repo_name_prefix + "_extras", repo_name_prefix + "_extras", url)\ return repo_config