3

我在尝试使用 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 到底是什么(有人听说过)?

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

谢谢,迈克尔

4

2 回答 2

2

REST API 文档对此有点模糊,但我认为您不能根据此部分设置存储库 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
}
于 2014-10-22T22:13:00.560 回答
1

我的 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以查看失败的实际原因。

于 2014-07-09T16:42:31.393 回答