-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pan Li <[email protected]>
- Loading branch information
1 parent
fd74b66
commit 3f41920
Showing
4 changed files
with
71 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/com/microsoft/azure/springcloudplayground/github/GithubEmails.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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); | ||
|
@@ -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() | ||
|
@@ -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()); | ||
|
@@ -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); | ||
|
@@ -250,5 +268,7 @@ public void createRepository(@NonNull File dir, @NonNull String repositoryName) | |
GitDataRequestReference reference = getGitDataRequestReference(commit); | ||
|
||
updateGithubRepository(repository, reference); | ||
|
||
return repository.getRepositoryUrl(getUsername()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters