【问题标题】:Creating atlassian stash repo using rest and powershell使用rest和powershell创建atlassian stash repo
【发布时间】:2014-07-09 13:59:56
【问题描述】:

我在尝试使用 powershell 自动创建新的 git 存储库时遇到了僵局。

据我了解,有人使用 url 上的 POST 方法创建新的存储库

/rest/api/1.0/projects/$ProjectKey/repos  

https://developer.atlassian.com/static/rest/stash/3.0.1/stash-rest.html#idp1178320

因为您必须是管理员才能修改内容,所以我在 webrequest 中添加了一个授权标头字段。

$ByteArr      = [System.Text.Encoding]::UTF8.GetBytes('admin:pwd')
$Base64Creds  = [Convert]::ToBase64String($ByteArr)

$ProjectKey = 'SBOX'
$CreateRepoUri = "$BaseUri/rest/api/1.0/projects/$ProjectKey/repos/"
Invoke-RestMethod -Method Post `
              -Headers @{ Authorization="Basic $Base64Creds" } `
              -Uri $CreateRepoUri `
              -ContentType 'application/json' `
              -Body @{ slug='test'; name='RestCreatedRepo';}

但是在执行时我得到一个内部服务器错误 (500)。没有更多详细信息或 InnerExceptions 确切原因。

使用 GET 检索存储库列表有效,因此身份验证有效(至少对于 Get 请求)

按照这个说法,应该是正确的说法:

这个 slug 或 scmId 到底是什么(有人听说过)?

如果你们中的一位天才能在我刚开始使用网络服务时为我指出正确的方向,那就太好了。

谢谢, 迈克尔

【问题讨论】:

    标签: git powershell bitbucket-server bitbucket-api


    【解决方案1】:

    REST API documentation 周围有点模糊,但我认为您不能根据this section 设置存储库 slug。两者都没有具体说明您可以或不能更改 slug,而是演示使用名称并暗示如果名称更改,slug 可能会更改。

    存储库的 slug 源自其名称。如果名称改变,蛞蝓也可能改变。

    在示例中,创建了一个名为“My Repo”的 repo,导致 slug 为“my-repo”。所以,我相信 slug 基本上是存储库名称的“标准化”版本。 ScmId 标识用于存储库的源代码控制管理类型(例如“git”)。

    对于您的请求正文,我也不确定Invoke-RestMethod 是否会自动为您将其转换为 JSON。您可以为此使用ConvertTo-Json Cmdlet,或者在较小的情况下只需手动创建 JSON 字符串。这对我有用:

    $baseUrl = 'http://example.com'
    $usernamePwd = 'admin:passwd'
    $project = 'SBOX'
    $repoName = 'RestCreatedRepo'
    
    $headers = @{}
    $headers.Add('Accept', 'application/json')
    
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($usernamePwd)
    $creds = 'Basic ' + [Convert]::ToBase64String($bytes)
    $headers.Add('Authorization', $creds)
    
    $data = '{"name": "{0}","scmId": "git","forkable": true}' -f $repoName
    $url = '{0}/rest/api/1.0/projects/{1}/repos' -f $baseUrl,$project
    $response = try {
        Invoke-RestMethod -Method Post `
                -Uri $url `
                -Headers $headers `
                -ContentType 'application/json' `
                -Body $data
    } catch {
        $_.Exception.Response
    }
    

    【讨论】:

    • “您可以使用 ConvertTo-Json Cmdlet”是一个很好的提示...我假设 Body 参数会根据我的请求自动转换为“Content-Type”。
    • 这里的警告词如果您使用此答案,则在设置 $data 时需要使用双 {{ }} 因为 -f 如果不是,则表示格式无效。 $data = '{{"name": "{0}","scmId": "git","forkable": true}}' -f $repoName
    【解决方案2】:

    我的 2 美分。

    这不是对您关于 atlassian 问题的问题的回答,而是关于如何从回复中查看更多详细信息的一般指导。

    $response = try {
        Invoke-RestMethod -Method Post `
                  -Headers @{ Authorization="Basic $Base64Creds" } `
                  -Uri $CreateRepoUri `
                  -ContentType 'application/json' `
                  -Body @{ slug='test'; name='RestCreatedRepo';}
    } catch {
        $_.Exception.Response
    }
    

    您可以检查$response 以查看失败的实际原因。

    【讨论】:

    • 感谢您的信息!这肯定会让生活更轻松:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多