Skip to content

Commit

Permalink
Merge pull request #1276 from github/get-gh-database-id
Browse files Browse the repository at this point in the history
Add call to get org database id
  • Loading branch information
begonaguereca authored Oct 7, 2024
2 parents b5767d1 + a842577 commit 68ec53e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Octoshift/Services/GithubApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,31 @@ public virtual async Task<string> GetOrganizationId(string org)
}
}

public virtual async Task<string> GetOrganizationDatabaseId(string org)
{
var url = $"{_apiUrl}/graphql";

var payload = new
{
query = "query($login: String!) {organization(login: $login) { login, databaseId, name } }",
variables = new { login = org }
};

try
{
return await _retryPolicy.Retry(async () =>
{
var data = await _client.PostGraphQLAsync(url, payload);
return (string)data["data"]["organization"]["databaseId"];
});
}
catch (Exception ex)
{
throw new OctoshiftCliException($"Failed to lookup the Organization database ID for organization '{org}'", ex);
}
}

public virtual async Task<string> GetEnterpriseId(string enterpriseName)
{
var url = $"{_apiUrl}/graphql";
Expand Down
68 changes: 68 additions & 0 deletions src/OctoshiftCLI.Tests/Octoshift/Services/GithubApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,74 @@ public async Task GetOrganizationId_Retries_On_GQL_Error()
result.Should().Be(orgId);
}

[Fact]
public async Task GetOrganizationDatabaseId_Returns_The_Database_Id()
{
// Arrange
const string databaseId = "DATABASE_ID";

var url = $"https://api.github.com/graphql";
var payload =
$"{{\"query\":\"query($login: String!) {{organization(login: $login) {{ login, databaseId, name }} }}\",\"variables\":{{\"login\":\"{GITHUB_ORG}\"}}}}";
var response = JObject.Parse($@"
{{
""data"": {{
""organization"": {{
""login"": ""{GITHUB_ORG}"",
""name"": ""github"",
""databaseId"": ""{databaseId}""
}}
}}
}}");

_githubClientMock
.Setup(m => m.PostGraphQLAsync(url, It.Is<object>(x => x.ToJson() == payload), null))
.ReturnsAsync(response);

// Act
var result = await _githubApi.GetOrganizationDatabaseId(GITHUB_ORG);

// Assert
result.Should().Be(databaseId);
}

[Fact]
public async Task GetOrganizationDatabaseId_Retries_On_GQL_Error()
{
// Arrange
const string databaseId = "DATABASE_ID";

var url = $"https://api.github.com/graphql";
var payload =
$"{{\"query\":\"query($login: String!) {{organization(login: $login) {{ login, databaseId, name }} }}\",\"variables\":{{\"login\":\"{GITHUB_ORG}\"}}}}";

var response = JObject.Parse($@"
{{
""data"":
{{
""organization"":
{{
""login"": ""{GITHUB_ORG}"",
""databaseId"": ""{databaseId}"",
""name"": ""github""
}}
}}
}}");

_githubClientMock
.SetupSequence(m => m.PostGraphQLAsync(url, It.Is<object>(x => x.ToJson() == payload), null))
.ReturnsAsync(GQL_ERROR_RESPONSE)
.ReturnsAsync(GQL_ERROR_RESPONSE)
.ReturnsAsync(response);

// Act
var result = await _githubApi.GetOrganizationDatabaseId(GITHUB_ORG);

// Assert
result.Should().Be(databaseId);
}


[Fact]
public async Task GetEnterpriseId_Returns_The_Enterprise_Id()
{
Expand Down

0 comments on commit 68ec53e

Please sign in to comment.