-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
GitHubからリポジトリの情報を取得できるようにした #18
Merged
+561
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,43 @@ | ||
package net.tuzigiri.domain.github; | ||
|
||
import net.tuzigiri.domain.identity.AccessToken; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
|
||
@Component | ||
public class GithubApi { | ||
|
||
private final RestTemplate restOperations; | ||
|
||
public GithubApi(RestTemplate restOperations) { | ||
this.restOperations = restOperations; | ||
} | ||
|
||
/** | ||
* アクセストークンに紐づくユーザーのリポジトリ一覧を返します | ||
* | ||
* @param accessToken アクセストークン | ||
* @return アクセストークンに紐づくユーザーのリポジトリ一覧 | ||
*/ | ||
public List<GithubRepository> fetchRepositories(AccessToken accessToken) { | ||
try { | ||
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); | ||
headers.add("Authorization", String.format("token %s", accessToken.getTokenValue())); | ||
RequestEntity requestEntity = new RequestEntity<>(headers, HttpMethod.GET, new URI("https://api.github.com/user/repos")); | ||
ResponseEntity<List<GithubRepository>> entity = restOperations.exchange(requestEntity, new ParameterizedTypeReference<List<GithubRepository>>() { | ||
}); | ||
return entity.getBody(); | ||
} catch (URISyntaxException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
src/main/java/net/tuzigiri/domain/github/GithubRepository.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,135 @@ | ||
package net.tuzigiri.domain.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
/** | ||
* @see <a href="https://developer.github.com/v3/repos/#response">github document</a> | ||
*/ | ||
public class GithubRepository { | ||
private final long id; | ||
private final String name; | ||
private final String fullName; | ||
private final boolean isPrivate; | ||
private final GithubUser owner; | ||
private final String repositoryUrl; | ||
private final LocalDateTime createdAt; | ||
private final LocalDateTime updatedAt; | ||
private final LocalDateTime pushedAt; | ||
private final String gitUrl; | ||
private final String sshUrl; | ||
private final String cloneUrl; | ||
private final long size; | ||
private final long starsCount; | ||
private final long watchersCount; | ||
private final long forksCount; | ||
private final String language; | ||
|
||
@JsonCreator | ||
public GithubRepository( | ||
@JsonProperty("id") long id, | ||
@JsonProperty("name") String name, | ||
@JsonProperty("full_name") String fullName, | ||
@JsonProperty("private") boolean isPrivate, | ||
@JsonProperty("owner") GithubUser owner, | ||
@JsonProperty("html_url") String repositoryUrl, | ||
@JsonProperty("created_at") LocalDateTime createdAt, | ||
@JsonProperty("updated_at") LocalDateTime updatedAt, | ||
@JsonProperty("pushed_at") LocalDateTime pushedAt, | ||
@JsonProperty("git_url") String gitUrl, | ||
@JsonProperty("ssh_url") String sshUrl, | ||
@JsonProperty("clone_url") String cloneUrl, | ||
@JsonProperty("size") long size, | ||
@JsonProperty("stargazers_count") long starsCount, | ||
@JsonProperty("watchers_count") long watchersCount, | ||
@JsonProperty("forks_count") long forksCount, | ||
@JsonProperty("language") String language) { | ||
this.id = id; | ||
this.name = name; | ||
this.fullName = fullName; | ||
this.isPrivate = isPrivate; | ||
this.owner = owner; | ||
this.repositoryUrl = repositoryUrl; | ||
this.createdAt = createdAt; | ||
this.updatedAt = updatedAt; | ||
this.pushedAt = pushedAt; | ||
this.gitUrl = gitUrl; | ||
this.sshUrl = sshUrl; | ||
this.cloneUrl = cloneUrl; | ||
this.size = size; | ||
this.starsCount = starsCount; | ||
this.watchersCount = watchersCount; | ||
this.forksCount = forksCount; | ||
this.language = language; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getFullName() { | ||
return fullName; | ||
} | ||
|
||
public boolean isPrivate() { | ||
return isPrivate; | ||
} | ||
|
||
public GithubUser getOwner() { | ||
return owner; | ||
} | ||
|
||
public String getRepositoryUrl() { | ||
return repositoryUrl; | ||
} | ||
|
||
public LocalDateTime getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public LocalDateTime getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
|
||
public LocalDateTime getPushedAt() { | ||
return pushedAt; | ||
} | ||
|
||
public String getGitUrl() { | ||
return gitUrl; | ||
} | ||
|
||
public String getSshUrl() { | ||
return sshUrl; | ||
} | ||
|
||
public String getCloneUrl() { | ||
return cloneUrl; | ||
} | ||
|
||
public long getSize() { | ||
return size; | ||
} | ||
|
||
public long getStarsCount() { | ||
return starsCount; | ||
} | ||
|
||
public long getWatchersCount() { | ||
return watchersCount; | ||
} | ||
|
||
public long getForksCount() { | ||
return forksCount; | ||
} | ||
|
||
public String getLanguage() { | ||
return language; | ||
} | ||
} |
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,39 @@ | ||
package net.tuzigiri.domain.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class GithubUser { | ||
private final String userId; | ||
private final long id; | ||
private final String avatarUrl; | ||
private final String profileUrl; | ||
|
||
@JsonCreator | ||
public GithubUser( | ||
@JsonProperty("login") String userId, | ||
@JsonProperty("id") long id, | ||
@JsonProperty("avatar_url") String avatarUrl, | ||
@JsonProperty("html_url") String profileUrl) { | ||
this.userId = userId; | ||
this.id = id; | ||
this.avatarUrl = avatarUrl; | ||
this.profileUrl = profileUrl; | ||
} | ||
|
||
public String getUserId() { | ||
return userId; | ||
} | ||
|
||
public long getId() { | ||
return id; | ||
} | ||
|
||
public String getAvatarUrl() { | ||
return avatarUrl; | ||
} | ||
|
||
public String getProfileUrl() { | ||
return profileUrl; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GithubRepositoryを取得するのはGithubRepositoryRepository?やばいな。と一瞬考えたがこれが一番わかり易いと思った。いい名前案絶賛募集中です