-
Notifications
You must be signed in to change notification settings - Fork 7
/
GitHubService.java
81 lines (67 loc) · 2.36 KB
/
GitHubService.java
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
package org.tothought.web.github;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.egit.github.core.IRepositoryIdProvider;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.CommitService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tothought.data.entities.Commit;
import org.tothought.data.entities.Tag;
public class GitHubService {
private Logger logger = LoggerFactory.getLogger(GitHubService.class);
private GitHubClient client;
private RepositoryService repoService;
private CommitService commitService;
private String user;
private List<Commit> commits = new ArrayList<Commit>();
public GitHubService(String user, String password) {
this.user = user;
this.client = new GitHubClient();
this.client.setCredentials(user, password);
this.repoService = new RepositoryService(this.client);
this.commitService = new CommitService(this.client);
}
public void pullCommits() {
List<Repository> repos;
try {
repos = repoService.getRepositories();
for (Repository repo : repos)
{
IRepositoryIdProvider repoId = new RepositoryId(this.user, repo.getName());
List<RepositoryCommit> ghCommits = commitService.getCommits(repoId);
for (RepositoryCommit ghCommit : ghCommits)
{
String[] tokens = ghCommit.getCommit().getMessage().split("\\r?\\n");
for (String token : tokens) {
this.commits.add(new Commit(ghCommit, token));
}
}
}
} catch (IOException e) {
logger.error("Could not pull commits from GitHub.");
e.printStackTrace();
}
}
public List<org.tothought.data.entities.Commit> getCommitsByTag(Tag tag) {
List<Commit> matches = new ArrayList<Commit>();
if(this.commits.size() == 0){
this.pullCommits();
}
for(Commit commit:this.commits){
if(commit != null && tag != null && commit.getMessage() != null && StringUtils.containsIgnoreCase(commit.getMessage(), tag.getName()))
{
//Taking advantage of reference to add tag
commit.getTags().add(tag);
matches.add(commit);
}
}
return matches;
}
}