Skip to content

Commit 9638298

Browse files
authored
Add debug logging to identify problematic IDP group
- Gracefully handle empty results when fetching IDP groups - Add tests
1 parent c8e241d commit 9638298

File tree

3 files changed

+115
-54
lines changed

3 files changed

+115
-54
lines changed

src/Octoshift/Commands/CreateTeam/CreateTeamCommandHandler.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,32 @@ public async Task Handle(CreateTeamCommandArgs args)
4444
}
4545
else
4646
{
47+
_log.LogInformation($"Attempting to link team '{args.TeamName}' to IDP group '{args.IdpGroup}'");
48+
4749
var members = await _githubApi.GetTeamMembers(args.GithubOrg, teamSlug);
50+
_log.LogInformation($"Found {members.Count()} existing team members to remove before linking to IDP group");
4851

4952
foreach (var member in members)
5053
{
54+
_log.LogInformation($"Removing team member '{member}' from team '{teamSlug}'");
5155
await _githubApi.RemoveTeamMember(args.GithubOrg, teamSlug, member);
5256
}
5357

54-
var idpGroupId = await _githubApi.GetIdpGroupId(args.GithubOrg, args.IdpGroup);
58+
_log.LogInformation($"Searching for IDP group '{args.IdpGroup}' in organization '{args.GithubOrg}'");
59+
60+
int idpGroupId;
61+
try
62+
{
63+
idpGroupId = await _githubApi.GetIdpGroupId(args.GithubOrg, args.IdpGroup);
64+
_log.LogInformation($"Found IDP group '{args.IdpGroup}' with ID: {idpGroupId}");
65+
}
66+
catch (OctoshiftCliException ex)
67+
{
68+
_log.LogError($"Failed to find IDP group: {ex.Message}");
69+
throw;
70+
}
5571

72+
_log.LogInformation($"Adding IDP group '{args.IdpGroup}' (ID: {idpGroupId}) to team '{teamSlug}'");
5673
await _githubApi.AddEmuGroupToTeam(args.GithubOrg, teamSlug, idpGroupId);
5774

5875
_log.LogSuccess("Successfully linked team to Idp group");

src/Octoshift/Services/GithubApi.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ mutation startRepositoryMigration(
390390
$lockSource: Boolean)";
391391
var gql = @"
392392
startRepositoryMigration(
393-
input: {
393+
input: {
394394
sourceId: $sourceId,
395395
ownerId: $ownerId,
396396
sourceRepositoryUrl: $sourceRepositoryUrl,
@@ -456,7 +456,7 @@ mutation startOrganizationMigration (
456456
$targetEnterpriseId: ID!,
457457
$sourceAccessToken: String!)";
458458
var gql = @"
459-
startOrganizationMigration(
459+
startOrganizationMigration(
460460
input: {
461461
sourceOrgUrl: $sourceOrgUrl,
462462
targetOrgName: $targetOrgName,
@@ -620,8 +620,15 @@ public virtual async Task<int> GetIdpGroupId(string org, string groupName)
620620
{
621621
var url = $"{_apiUrl}/orgs/{org.EscapeDataString()}/external-groups";
622622

623-
var group = await _client.GetAllAsync(url, data => (JArray)data["groups"])
624-
.SingleAsync(x => string.Equals((string)x["group_name"], groupName, StringComparison.OrdinalIgnoreCase));
623+
var allGroups = await _client.GetAllAsync(url, data => (JArray)data["groups"]).ToListAsync();
624+
625+
var group = allGroups.SingleOrDefault(x => string.Equals((string)x["group_name"], groupName, StringComparison.OrdinalIgnoreCase));
626+
627+
if (group == null)
628+
{
629+
var availableGroups = string.Join(", ", allGroups.Select(g => $"'{g["group_name"]}'"));
630+
throw new OctoshiftCliException($"IDP group '{groupName}' not found in organization '{org}'. Available groups: {availableGroups}");
631+
}
625632

626633
return (int)group["group_id"];
627634
}
@@ -1077,7 +1084,7 @@ mutation abortRepositoryMigration(
10771084
)";
10781085
var gql = @"
10791086
abortRepositoryMigration(
1080-
input: {
1087+
input: {
10811088
migrationId: $migrationId
10821089
})
10831090
{ success }";

0 commit comments

Comments
 (0)