Reputation: 451
I try to create the first commit and the main branch, in c#, calling ADO api on my server
but I am not sure this can be done as I get "MethodNotAllowed" return code
PAT is full access and POST should be the way to make a push
when I trace the code, the end point is :
https://*********/_apis/git/repositories/******-****-****-****-************/pushes?api-version=7.1-preview.1
(note that the obfuscated repository id above is valid and used for other working requests)
here is the code :
public async Task<bool> CreateFirstCommitAndMainBranchAsync(string repositoryName, string readmeContent)
{
string endpoint = $"https://{m_adoSspServer}/_apis/git/repositories/{repositoryInfos.Id}/pushes?api-version=7.1-preview.1";
var commitData = new
{
commits = new[]
{
new
{
comment = "Initial commit with README.md",
changes = new[]
{
new
{
changeType = "Add",
item = new
{
path = "/README.md"
},
newContent = new
{
content = readmeContent,
contentType = "rawText"
}
}
}
}
},
refUpdates = new[]
{
new
{
name = "refs/heads/main", // Creating the main branch
oldObjectId = "0000000000000000000000000000000000000000" // not sure what to put here
}
}
};
string jsonContent = JsonConvert.SerializeObject(commitData);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.ASCII.GetBytes($":{pat}")));
var response = await client.PostAsync(endpoint, new StringContent(jsonContent, Encoding.UTF8, "application/json"));
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
Console.WriteLine($"Failed to create commit and branch: {response.StatusCode} - {response.ReasonPhrase}"); // MethodNotAllowed
return false;
}
}
return true;
}
Upvotes: 0
Views: 55
Reputation: 8811
Commit can be done as the official document https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-7.2&tabs=HTTP.
I tested your code and get the same error.But after change to latest version to "api-version=7.2-preview.3" it works.
I think it is because they didn't fix the old preview vesion anymore. Cause "api-version=7.1" also works.
Upvotes: 0