Skip to content

Commit 8b71948

Browse files
committed
feat: Get organisation team members
1 parent cb8e4b1 commit 8b71948

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace Open_Rails_Code_Bot.GitHub
4+
{
5+
class GraphOrganizationTeamMember
6+
{
7+
public Uri Url;
8+
public string Login;
9+
public string Name;
10+
}
11+
}

GitHub/GraphQuery.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Open_Rails_Code_Bot.GitHub
4+
{
5+
class GraphQuery
6+
{
7+
[JsonProperty("query")]
8+
public string Query;
9+
}
10+
}

GitHub/Query.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Collections.Generic;
2+
using System.Net.Http;
3+
using System.Net.Http.Headers;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
9+
namespace Open_Rails_Code_Bot.GitHub
10+
{
11+
class Query
12+
{
13+
const string Endpoint = "https://api.github.com/graphql";
14+
15+
readonly string Token;
16+
17+
HttpClient Client = new HttpClient();
18+
19+
public Query(string token)
20+
{
21+
Token = token;
22+
}
23+
24+
internal async Task<JObject> Get(string query)
25+
{
26+
var request = new HttpRequestMessage(HttpMethod.Post, Endpoint);
27+
request.Headers.UserAgent.Clear();
28+
request.Headers.UserAgent.Add(new ProductInfoHeaderValue("Open-Rails-Code-Bot", "1.0"));
29+
request.Headers.Authorization = new AuthenticationHeaderValue("bearer", Token);
30+
var graphQuery = new GraphQuery { Query = $"query {{ {query} }}" };
31+
var graphQueryJson = JsonConvert.SerializeObject(graphQuery);
32+
request.Content = new StringContent(graphQueryJson, Encoding.UTF8, "application/json");
33+
var response = await Client.SendAsync(request);
34+
var text = await response.Content.ReadAsStringAsync();
35+
return JObject.Parse(text);
36+
}
37+
38+
public async Task<IReadOnlyList<GraphOrganizationTeamMember>> GetTeamMembers(string organization, string team)
39+
{
40+
var query = @"
41+
organization(login: """ + organization + @""") {
42+
team(slug: """ + team + @""") {
43+
members {
44+
nodes {
45+
url
46+
login
47+
name
48+
}
49+
}
50+
}
51+
}
52+
";
53+
var response = await Get(query);
54+
return response["data"]["organization"]["team"]["members"]["nodes"].ToObject<GraphOrganizationTeamMember[]>();
55+
}
56+
}
57+
}

Program.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Threading.Tasks;
44
using Microsoft.Extensions.Configuration;
5+
using Open_Rails_Code_Bot.GitHub;
56

67
namespace Open_Rails_Code_Bot
78
{
@@ -11,7 +12,7 @@ static void Main(string[] args)
1112
{
1213
var config = new CommandLineParser.Arguments.FileArgument('c', "config")
1314
{
14-
DefaultValue = new FileInfo("config.json")
15+
ForcedDefaultValue = new FileInfo("config.json")
1516
};
1617

1718
var commandLineParser = new CommandLineParser.CommandLineParser()
@@ -37,7 +38,14 @@ static void Main(string[] args)
3738

3839
static async Task AsyncMain(IConfigurationRoot config)
3940
{
40-
// TODO:
41+
var gitHubConfig = config.GetSection("github");
42+
var query = new Query(gitHubConfig["token"]);
43+
44+
var members = await query.GetTeamMembers(gitHubConfig["organization"], gitHubConfig["team"]);
45+
Console.WriteLine($"Org '{gitHubConfig["organization"]}' team '{gitHubConfig["team"]}' members");
46+
foreach (var member in members) {
47+
Console.WriteLine($" {member.Login}");
48+
}
4149
}
4250
}
4351
}

0 commit comments

Comments
 (0)