Michael
Michael

Reputation: 672

Creating atlassian stash repo using rest and powershell

I ran into an impasse trying to automate the creation of new git repositories using powershell.

As I understand one creates new repos using the POST Method on the url

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

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

Because you have to be admin to modify stuff I add an authorization header field to the 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';}

But when executing I get an Internal Server Error (500). No more details or InnerExceptions on why exactly.

Retrieving a list of repositories with GET worked so authentication works (at least for the Get requests)

According to this, it should be a correct statement:

What exactly is this slug or scmId (Anybody heard of it)?

It would be greate if one of you geniuses could point me in the right direction as I just gotten into using webservises.

Thanks, Michael

Upvotes: 3

Views: 1950

Answers (2)

areyling
areyling

Reputation: 2191

The REST API documentation is a little vague around it, but I don't think you can set the repository slug based on this section. Neither specifically says you can or cannot change the slug, but demonstrate using name and hint that the slug could change if the name changes.

The repository's slug is derived from its name. If the name changes the slug may also change.

In the example a repo is created with the name "My Repo", causing the slug to be "my-repo". So, I believe the slug is basically a "normalized" version of the repository's name. ScmId identifies what type of source control management to use for the repository (e.g. "git").

For the body of your request, I'm also not sure that Invoke-RestMethod will automatically convert that to JSON for you. You can use the ConvertTo-Json Cmdlet for this, or in smaller cases just manually create the JSON string. This works for me:

$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
}

Upvotes: 2

ravikanth
ravikanth

Reputation: 25810

My 2 cents.

This is not an answer to your question on atlassian issue but general guidance on how you can see more details from the response.

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

You can examine $response to see the actual reason for the failure.

Upvotes: 1

Related Questions