-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubMetaScraper.cs
More file actions
90 lines (75 loc) · 3.23 KB
/
Copy pathGithubMetaScraper.cs
File metadata and controls
90 lines (75 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Diagnostics;
using Octokit;
using System.Text.Json;
using ProductHeaderValue = Octokit.ProductHeaderValue;
namespace GitHubInformationGrabber
{
internal class GithubMetaScraper
{
private GitHubClient github;
public bool IsRateLimited
{
get; private set;
}
public GithubMetaScraper(string token = "") {
github = new GitHubClient(new ProductHeaderValue("MetaDataGrabber"))
{
Credentials = new Credentials(token)
};
IsRateLimited = false;
}
private async Task WriteListToJsonFileAsync<T>(IReadOnlyList<T> list, string filePath)
{
using (FileStream fs = new FileStream(filePath, System.IO.FileMode.OpenOrCreate))
{
using (StreamWriter writer = new StreamWriter(fs))
{
string jsonString = JsonSerializer.Serialize(list);
await writer.WriteAsync(jsonString);
}
}
}
public async Task<List<string>> GetMetaData(string organization, int ratelimit = 2000)
{
var timestamp = DateTime.Now.ToString("yyyy-MM-ddHHmmss");
var repofile = $"{timestamp}-repos.json";
var userFile = $"{timestamp}-members.json";
var issueFile = $"{timestamp}-issues.json";
var commitFile = $"{timestamp}-commits.json";
var pullRequestFile = $"{timestamp}-pullrequests.json";
var files = new List<string> { repofile, userFile, issueFile, pullRequestFile, commitFile };
var members = await github.Organization.Member.GetAll(organization);
await WriteListToJsonFileAsync(members, userFile);
IReadOnlyList<Repository> allRepos = await github.Repository.GetAllForOrg(organization);
await WriteListToJsonFileAsync(allRepos, repofile);
foreach (var repo in allRepos)
{
await RateLimitMonitor(ratelimit);
try
{
var commits = await github.Repository.Commit.GetAll(organization, repo.Name);
await WriteListToJsonFileAsync<GitHubCommit>(commits, commitFile);
var issues = await github.Issue.GetAllForRepository(organization, repo.Name);
await WriteListToJsonFileAsync(issues, issueFile);
var pullRequest = await github.PullRequest.GetAllForRepository(organization, repo.Name);
await WriteListToJsonFileAsync(pullRequest, pullRequestFile);
}
catch (Octokit.ApiException ex)
{
//do nothing repo is probably empty
}
}
return files;
}
private async Task RateLimitMonitor(int ratelimit)
{
var rateLimitRemaing = github.GetLastApiInfo().RateLimit.Remaining;
while (rateLimitRemaing <= ratelimit)
{
IsRateLimited = rateLimitRemaing <= ratelimit;
await Task.Delay(TimeSpan.FromMinutes(1));
rateLimitRemaing = github.GetLastApiInfo().RateLimit.Remaining;
}
}
}
}