【问题标题】:Script to copy all the updates on gitlab repository to azure devops repository将 gitlab 存储库上的所有更新复制到 azure devops 存储库的脚本
【发布时间】:2022-11-03 13:28:25
【问题描述】:

在组织中,我们正在从 gitlab 迁移到 azure。有一些存储库有很多历史提交/分支/标签,我们需要将它们镜像到 azure-devops。如果我们可以编写一个脚本来做同样的事情,我需要一些建议吗?安排一些自动化来这样做?谁能给我一些建议/例子来开始?

【问题讨论】:

  • 推动将东西(旧分支/标签)放入新的 repos 是不够的?
  • 我们想在最后自动化它,所以一个基于脚本的开始会很棒

标签: git azure-devops gitlab


【解决方案1】:

你可以使用 REST API 从 github 迁移到 azure,参考文档在这里:Import Request-Create-REST API(Azure DevOps Git)

但是,如果您的存储库是私有的,那么首先创建“其他 git”服务连接对您来说至关重要,然后您可以使用 Rest API 将 Github Private Repo 导入到 New Repo。

1、你可以使用Rest API来创建它。文件在这里: EndPoints-Create-REST-API

例如:

网址

  POST https://dev.azure.com/{organization}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4

请求正文

    {
    "authorization":{"scheme":"UsernamePassword","parameters":{"username":"{User name}","password":"{github access token }"}},
    "data":{"accessExternalGitServer":"true"},
    "name":"{name}",
    "serviceEndpointProjectReferences":[{"description":"","name":"{Service connection name}","projectReference":{"id":"{Project Id}","name":"{Project Name}"}}],
    "type":"git",
    "url":"{Target Git URL}",
    "isShared":false,
    "owner":"library"
  }

您可以在邮递员中进行测试:

发送创建端点 API 后,它将在您的 Azure DevOps 中成功创建端点。

注意:如何获取 github 访问令牌:

路径:设置->开发设置->个人访问令牌

2 然后您可以在步骤 1 中获取 ServiceEndPointId,您可以在 Import Repo Rest API 中使用它。 例如:

网址

Post https://dev.azure.com/{Organization Name}/{Project Name}/_apis/git/repositories/{Repo Name}/importRequests?api-version=5.0-preview.1 

请求正文

{
  "parameters": {
    "gitSource": {
      "url": "Git URL"
    },
    "serviceEndpointId": "{Service EndPoint Id}",
    "deleteServiceEndpointAfterImportIsDone": false
    
  }
}

您可以在邮递员中进行测试:

3 此外,以下脚本是一个 power shell 示例:

  [String]$Org = "your organization name"
  [String]$project = "your project name"
  [String]$PAT="your PAT "
  [String]$Repo="your Repo name"
  [String]$serviceEndpointId="your serviceEndpointId"

$url = https://dev.azure.com/+$Org+"/"+"$project"+"/_apis/git/repositories/"+$Repo+"/importRequests?api-version=6.1-preview.1" 

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT )))

 $body = @{
   "parameters" = @{
        "gitSource" =@{
         # the source git repository to import and remember to replace with your correct url

          "url" = https://github.com/xxxx
        }
       "serviceEndpointId" = ]$serviceEndpointId
       "deleteServiceEndpointAfterImportIsDone" = false
   }
   
} 

 $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "",$PAT )))

 $result = Invoke-RestMethod -Method 'Post' -Uri $url -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body ($body|ConvertTo-Json)   -ContentType "application/json"
 
 $result  | ConvertTo-Json

在 power shell 中运行脚本后,您可以在 json 中获得以下响应信息,这意味着您已成功从 github 迁移到 azure 并使用 REST API:

【讨论】:

    猜你喜欢
    • 2022-11-09
    • 2021-04-30
    • 2019-12-20
    • 2020-12-19
    • 2015-09-25
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-14
    相关资源
    最近更新 更多