aber ich bin mir nicht sicher, ob dies möglich ist, da ich „MethodNotAllowed“ erhalte. Rückkehrcode
PAT ist Vollzugriff und POST sollte die Möglichkeit sein, einen Push durchzuführen
Wenn ich den Code verfolge, ist der Endpunkt:< /p>
Code: Select all
https://*********/_apis/git/repositories/******-****-****-****-************/pushes?api-version=7.1-preview.1
hier ist der Code:
Code: Select all
public async Task 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;
}