Skip to content

Commit

Permalink
Add user email to commit log, #130.
Browse files Browse the repository at this point in the history
Signed-off-by: Pan Li <[email protected]>
  • Loading branch information
Incarnation-p-lee committed Sep 11, 2018
1 parent fd74b66 commit 3f41920
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class GithubApiWrapper {

private static final String CREATE_REPOSITORY_URL = "https://api.github.com/user/repos";

private static final String USER_EMAILS_URL = "https://api.github.com/user/emails";

protected static final ObjectMapper MAPPER = new ObjectMapper();

static {
Expand All @@ -62,6 +64,14 @@ private void appendAuthorizationHeader(@NonNull HttpUriRequest request) {
request.setHeader(AUTH_HEADER, String.format("token %s", this.token));
}

protected HttpResponse getUserEmails() throws GithubProcessException {
HttpGet request = new HttpGet(USER_EMAILS_URL);

appendAuthorizationHeader(request);

return executeRequest(request);
}

protected HttpResponse createRepository(@NonNull GithubRepository repository) throws GithubProcessException {
HttpPost request = new HttpPost(CREATE_REPOSITORY_URL);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.microsoft.azure.springcloudplayground.github;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GithubEmails {

private String email;

private boolean primary;

private boolean verified;

private String visibility;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@

public class GithubOperator extends GithubApiWrapper {

private static final String DEFAULT_EMAIL = "[email protected]";

private final List<GithubEmails> userEmails;

public GithubOperator(@NonNull String username, @NonNull String token) {
super(username, token);
this.userEmails = getGithubEmails();
}

private String getContent(@NonNull HttpResponse response) throws GithubProcessException {
Expand All @@ -47,6 +52,19 @@ private <T> T readValue(@NonNull String json, Class<T> clazz) throws GithubProce
}
}

private List<GithubEmails> getGithubEmails() {
List<GithubEmails> emails = new ArrayList<>();

try {
HttpResponse response = super.getUserEmails();
emails.addAll(Arrays.asList(readValue(getContent(response), GithubEmails[].class)));
} catch (GithubProcessException ignore) {
emails.add(new GithubEmails(DEFAULT_EMAIL, true, true, null));
}

return emails;
}

private GithubRepository createRepository(@NonNull String name) throws GithubProcessException {
GithubRepository repository = GithubRepository.builder(name).build();
HttpResponse response = super.createRepository(repository);
Expand Down Expand Up @@ -142,7 +160,7 @@ private GitDataRequestTree.TreeNode getRequestTreeNode(@NonNull String name, @No
}

private GitDataRequestCommit getGitDateRequestCommit(@NonNull GitDataCommit parent, @NonNull GithubTree tree) {
Author author = new Author(getUsername(), "fake-email");
Author author = new Author(getUsername(), this.userEmails.get(0).getEmail());
List<String> parents = Collections.singletonList(parent.getSha());

return GitDataRequestCommit.builder()
Expand Down Expand Up @@ -179,28 +197,19 @@ private GitDataRequestReference getGitDataRequestReference(@NonNull GitDataCommi
return new GitDataRequestReference(commit.getSha(), true);
}

private void updateGithubRepository(@NonNull GithubRepository repository,
@NonNull GitDataRequestReference reference) throws GithubProcessException {
HttpResponse response = super.updateGitDataReference(repository.getName(), reference);

if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new GithubProcessException("Failed to update reference from repo: " + repository.getName());
}
}

public List<String> getAllFiles(@NonNull File file) {
private List<String> getAllFiles(@NonNull File file) {
if (file.isDirectory()) {
List<String> fileNames = new ArrayList<>();
List<String> allFiles = new ArrayList<>();

for (File f : Objects.requireNonNull(file.listFiles())) {
if (f.isFile()) {
fileNames.add(f.getPath());
allFiles.add(f.getPath());
} else {
fileNames.addAll(getAllFiles(f));
allFiles.addAll(getAllFiles(f));
}
}

return fileNames;
return allFiles;
}

return Collections.singletonList(file.getName());
Expand Down Expand Up @@ -241,7 +250,16 @@ private GithubTree createGithubTree(@NonNull GithubRepository repository, @NonNu
return createGitDataTree(repository, requestTree);
}

public void createRepository(@NonNull File dir, @NonNull String repositoryName) throws GithubProcessException {
private void updateGithubRepository(@NonNull GithubRepository repository,
@NonNull GitDataRequestReference reference) throws GithubProcessException {
HttpResponse response = super.updateGitDataReference(repository.getName(), reference);

if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new GithubProcessException("Failed to update reference from repo: " + repository.getName());
}
}

public String createRepository(@NonNull File dir, @NonNull String repositoryName) throws GithubProcessException {
GithubRepository repository = createRepository(repositoryName);
GitDataCommit parentCommit = getGitDataCommit(repository, getRepositoryCommits(repository).get(0));
GithubTree githubTree = createGithubTree(repository, parentCommit, dir);
Expand All @@ -250,5 +268,7 @@ public void createRepository(@NonNull File dir, @NonNull String repositoryName)
GitDataRequestReference reference = getGitDataRequestReference(commit);

updateGithubRepository(repository, reference);

return repository.getRepositoryUrl(getUsername());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ public static GithubRepositoryBuilder builder(@NonNull String name) {
.homepage("https://github.com")
.auto_init(true);
}

public String getRepositoryUrl(@NonNull String username) {
String url = "https://github.com/%s/%s";

return String.format(url, username, name);
}
}

0 comments on commit 3f41920

Please sign in to comment.