Skip to content

Commit 4808f21

Browse files
authored
Fix GH bugs (#50)
* fix gh token lifetime and updating existing file in a branch * create GH client transiently * few more bug fixes
1 parent 9168d11 commit 4808f21

File tree

6 files changed

+23
-8
lines changed

6 files changed

+23
-8
lines changed

samples/gh-flow/infra/app/gh-flow.bicep

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module app '../core/host/container-app.bicep' = {
103103
value: location
104104
}
105105
{
106-
name: 'AzureOptions__ManagedIdentity'
106+
name: 'AZURE_CLIENT_ID'
107107
value: ghFlowIdentity.properties.clientId
108108
}
109109
{

samples/gh-flow/src/Microsoft.AI.DevTeam/Agents/Sandbox.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async Task IRemindable.ReceiveReminder(string reminderName, TickStatus status)
5353
{
5454
if (!_state.State.IsCompleted)
5555
{
56-
var sandboxId = $"sk-sandbox-{_state.State.Org}-{_state.State.Repo}-{_state.State.ParentIssueNumber}-{_state.State.IssueNumber}";
56+
var sandboxId = $"sk-sandbox-{_state.State.Org}-{_state.State.Repo}-{_state.State.ParentIssueNumber}-{_state.State.IssueNumber}".ToLower();
5757
if (await _azService.IsSandboxCompleted(sandboxId))
5858
{
5959
await _azService.DeleteSandbox(sandboxId);

samples/gh-flow/src/Microsoft.AI.DevTeam/Program.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
builder.Services.AddTransient(CreateMemory);
2222
builder.Services.AddHttpClient();
2323

24-
builder.Services.AddSingleton(s =>
24+
25+
builder.Services.AddTransient(s =>
2526
{
2627
var ghOptions = s.GetService<IOptions<GithubOptions>>();
2728
var logger = s.GetService<ILogger<GithubAuthService>>();
@@ -30,11 +31,10 @@
3031
return client;
3132
});
3233

33-
3434
builder.Services.AddAzureClients(clientBuilder =>
3535
{
36-
clientBuilder.UseCredential(new DefaultAzureCredential());
3736
clientBuilder.AddArmClient(default);
37+
clientBuilder.UseCredential(new DefaultAzureCredential());
3838
});
3939

4040
builder.Services.AddSingleton<GithubAuthService>();

samples/gh-flow/src/Microsoft.AI.DevTeam/Services/AzureService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public async Task RunInSandbox(string org, string repo, long parentIssueNumber,
6666
{
6767
try
6868
{
69-
var runId = $"sk-sandbox-{org}-{repo}-{parentIssueNumber}-{issueNumber}";
69+
var runId = $"sk-sandbox-{org}-{repo}-{parentIssueNumber}-{issueNumber}".ToLower();
7070
var resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(_azSettings.SubscriptionId, _azSettings.ContainerInstancesResourceGroup);
7171
var resourceGroupResource = _client.GetResourceGroupResource(resourceGroupResourceId);
7272
var scriptPath = $"/azfiles/output/{org}-{repo}/{parentIssueNumber}/{issueNumber}/run.sh";

samples/gh-flow/src/Microsoft.AI.DevTeam/Services/GithubService.cs

+16-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,23 @@ public async Task CommitToBranch(string org, string repo, long parentNumber, lon
5151
{
5252
var value = reader.ReadToEnd();
5353

54-
await _ghClient.Repository.Content.CreateFile(
54+
// Check if the file exists
55+
var existingFiles = await _ghClient.Repository.Content.GetAllContentsByRef(org, repo, filePath, branch);
56+
if (existingFiles.Any())
57+
{
58+
var existingFile = existingFiles.First();
59+
// If the file exists, update it
60+
var updateChangeSet = await _ghClient.Repository.Content.UpdateFile(
5561
org, repo, filePath,
56-
new CreateFileRequest($"Commit message", value, branch)); // TODO: add more meaningfull commit message
62+
new UpdateFileRequest("Updated file via AI", value, existingFile.Sha, branch)); // TODO: add more meaningfull commit message
63+
}
64+
else
65+
{
66+
// If the file doesn't exist, create it
67+
var createChangeSet = await _ghClient.Repository.Content.CreateFile(
68+
org, repo, filePath,
69+
new CreateFileRequest("Created file via AI", value, branch)); // TODO: add more meaningfull commit message
70+
}
5771
}
5872
}
5973
catch (Exception ex)

src/Microsoft.AI.Agents.Orleans/AiAgent.cs

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public virtual async Task<string> CallFunction(string template, KernelArguments
4444
var function = _kernel.CreateFunctionFromPrompt(template, propmptSettings);
4545
var result = (await _kernel.InvokeAsync(function, arguments)).ToString();
4646
AddToHistory(result, ChatUserType.Agent);
47+
await _state.WriteStateAsync();
4748
return result;
4849
}
4950

0 commit comments

Comments
 (0)