检索存储库信息和令牌

get_repo_params ()

函数 get-repo-params 的目的是在提供 Repo URL 和 Repo 标记路径的情况下,检索与 Repo 相关的信息。

参数

函数 get-repo-params 需要以下参数:

  • repo_url 资源库 URL。 提供需要查找参数的 repo 的正确 url。
  • repo_token_path 与存储库关联的令牌的路径。

输出

get-repo-params 函数由管道提供。 该函数返回以下信息:

  • repo_name: 存储库名称。
  • repo_owner:repo 的用户或创建者。
  • scm_type:repo 的源代码控制管理类型(Git、GRIT)。
  • api_url: API URL 与托管 repo 的 Git 服务相关联。

函数调用

   read -r <repo_name> <repo_owner> <scm_type> <api_url> \
    < <(get_repo_params "<repo_url>" "<token_path>")

在读取命令中使用下划线变量 (_) 作为变量的占位符,其值在脚本的后续部分将被丢弃或不使用。

   read -r <repo_name> <_> <scm_type> <_> \
    < <(get_repo_params "<repo_url>" "<token_path>")

在脚本中使用这些变量作为 shell 变量。

示例

   ### <repo_url> = $EVIDENCE_LOCKER_URL
   ### <token_path> = ${WORKSPACE}/$EVIDENCE_TOKEN_PATH

   source "${ONE_PIPELINE_PATH}"/tools/get_repo_params

   read -r EVIDENCE_REPO_NAME EVIDENCE_REPO_ORG EVIDENCE_SCM_TYPE EVIDENCE_API_URL \
   < <(get_repo_params "$EVIDENCE_LOCKER_URL" "${WORKSPACE}/$EVIDENCE_TOKEN_PATH")

   ### Printing the value of EVIDENCE_LOCKER_URL

   echo "EVIDENCE_LOCKER_URL $EVIDENCE_LOCKER_URL"

get_repo_specific_token ()

get_repo_specific_token 函数会检索与之前使用的 repo URL 相关联的令牌。 版本库令牌用于验证和授权对特定版本库的访问。

它从环境属性表中选取 (git-token-[repo_name]-[repo_org]) 变量的值。

示例

let repo_url = <repo_git_url> 其中:

<repo_name> = compliance-app
<repo_org> = user_name

函数调用

   parameter
   ### repo_url
   token=$(get_repo_specific_token "<repo_url>")

输出:

将生成以下输出:

   echo "$repo_token"

示例:

   ### parameter:
   # <repo_url>: URL of repo of which token is required.

   source "${ONE_PIPELINE_PATH}"/tools/get_repo_params

   repo_url= <repo_url>
   token=$(get_repo_specific_token "$repo_url")

   ### Output
   # repo_token

   echo "Repository Token: $token"

get_api_url ()

get_api_url 函数可为不同类型的源代码库确定适当的 API URL

参数:

  • repo_url:源代码库的 URL。

函数调用:

   api_url=$(get_api_url "<repo_url>")

返回值:

该函数显示给定存储库的 API URL 值。

示例:


   source "${ONE_PIPELINE_PATH}/tools/get_repo_params"

   repo_url= <repo_url>
   api_url=$(get_api_url "$repo_url")

   echo "API URL for $repo_url is: $api_url"

SCM 类型和相应的 API URL:

  1. GitHub 集成:

    • SCM 类型: github_integrated
    • API URL: <api_url>
  2. GitHub 整合:

    • SCM 类型: githubconsolidated
    • 集成的 GitHub 存储库:
      • 应用程序接口 URL: https://<repo_server_name>/api/v3 (其中 <repo_server_name> 是从资源库 URL 提取的 )
    • 非集成 GitHub 存储库:
      • API URL: https://api.github.com
  3. 托管 Git:

    • SCM 类型: hostedgit
    • 应用程序接口 URL: https://<repo_server_name>/api/v4 (其中 <repo_server_name> 是从资源库 URL 提取的 )

如果 SCM 类型不是其中一种受支持的类型,那么将显示一条警告消息。

get_repo_scm_type ()

该功能可确定给定版本库 URL 的源代码管理 (SCM) 类型。

参数:

  • repo_url:需要确定 SCM 类型的版本库 URL。

函数调用:

   scm_type=$(get_repo_scm_type "<repo_url>")

输出:

表示单片机平台的标签,可以是以下标签之一:

  • 对于 GitLab-hosted版本库:gitlab
  • 对于 GitHub-integrated版本库:github -对于其他情况 (缺省为 GitHub): github

示例用法:

   source "${ONE_PIPELINE_PATH}/tools/get_repo_params"

   repository_url= <repo_url>
   result=$(get_repo_scm_type "$repository_url")

   echo "SCM Type: $result"

本示例将 github 显示为输出值,因为先前提供的存储库 URL 已与 GitHub 集成。

get_repo_owner ()

该函数从 Git 资源库 URL 中提取所有者或子组信息。

参数:

  • repo_url: URL 的 Git 资源库。

函数调用:

   Owner_name=$(get_repo_owner "<repo_url>")

输出:

  • 从资源库 URL 中提取的所有者或分组信息。

示例用法:

   source "${ONE_PIPELINE_PATH}/tools/get_repo_params"

   repository_url= <repo_url>
   owner=$(get_repo_owner "$repository_url")

   echo "Repository Owner: $owner"

此示例的输出显示为 user_name,因为 GitHub 承载着 URL 这个 "username "账户或子群的存储库。

  • 如果找不到所有者,则提取 URL 的第一段作为所有者。

get_repo_name ()

该函数从给定的 URL 资源库中提取 Git 资源库的名称。

参数:

  • repo_url: URL 的 Git 资源库。

函数调用:

   Repo_name=$(get_repo_name "<repo_url>")

输出:

此功能调用将显示从版本库 URL 提取的版本库名称,作为输出结果。

示例用法:

   source "${ONE_PIPELINE_PATH}/tools/get_repo_params"

   repository_url= <repo_url>
   repo_name=$(get_repo_name "$repository_url")

   echo "Repository Name: $repo_name"

本示例将输出 compliance-app,因为所提供的资源库 URL 在 GitHub 上被命名为 "compliance-app"。

如果找不到名称,则会尝试使用 URL 倒数第二段作为名称。