Skip to content

Commit

Permalink
Merge pull request #27 from tomoish/shio=_re
Browse files Browse the repository at this point in the history
fix func
  • Loading branch information
kou7306 authored Feb 1, 2024
2 parents b119fb7 + 8653919 commit 2627bb5
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/funcs/draw_user_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package funcs

import (
"bytes"
"github.com/fogleman/gg"

Check failure on line 5 in src/funcs/draw_user_status.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed with -local github.com/tomoish/readme (goimports)
"strconv"
)

// GenerateGitHubStatsImage 関数はユーザー統計情報を受け取り、グラフィカルな表現を画像として生成します。
func GenerateGitHubStatsImage(stats UserStats, width, height int) ([]byte, error) {
const padding = 20.0
const lineHeight = 30.0

dc := gg.NewContext(width, height)

// 背景色を設定します。
dc.SetRGB(0.2, 0.24, 0.31)
dc.Clear()

// タイトルを描画します。
dc.SetRGB(1, 1, 1) // 白色
dc.DrawStringAnchored("@hashfx-Github-stats", float64(width)/2, padding, 0.5, 0.5)

// 統計情報をリストとして描画します。
statsList := []struct {
Icon string
Label string
Value int
}{
{"★", "Total Stars Earned:", stats.TotalStars},
{"⧗", "Total Commits:", stats.TotalCommits},
{"⬆️", "Total PRs:", stats.TotalPRs},
{"⬇️", "Total Issues:", stats.TotalIssues},
{"⬈", "Contributed to:", stats.ContributedTo},
}

// 各統計情報をリスト形式で描画します。
for i, stat := range statsList {
y := padding*2 + lineHeight*float64(i)
dc.DrawStringAnchored(stat.Icon, padding, y, 0, 0.5)
dc.DrawStringAnchored(stat.Label+" "+strconv.Itoa(stat.Value), padding+40, y, 0, 0.5)
}

// 画像をバッファにエンコードします。
buf := bytes.Buffer{}
err := dc.EncodePNG(&buf)
if err != nil {
return nil, err
}

// エンコードしたバッファをバイト配列として返します。
return buf.Bytes(), nil
}
110 changes: 110 additions & 0 deletions src/funcs/get_user_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package funcs

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)

// UserStats はGitHubユーザーの統計情報を保持します。
type UserStats struct {
TotalStars int `json:"totalStars"`
TotalCommits int `json:"totalCommits"`
TotalPRs int `json:"totalPRs"`
TotalIssues int `json:"totalIssues"`
ContributedTo int `json:"contributedTo"`
}

// GetUserInfo は指定されたユーザー名に対する統計情報を取得します。
func GetUserInfo(username, token string) (UserStats, error) {
userStats := UserStats{}

// GraphQLクエリをbytes:\xe5\xaebytes:\x9f行し、bytes:\xe7\xb5bytes:\x90果を取得
query := `
{
user(login: "` + username + `") {
contributionsCollection {
totalCommitContributions
totalIssueContributions
totalPullRequestContributions
totalRepositoryContributions
}
starredRepositories {
totalCount
}
}
}`
data, err := executeQuery(query, token)
if err != nil {
return userStats, fmt.Errorf("query execution failed: %w", err)
}

//bytes: \xe3\x83bytes:\xacスポンスデbytes:\xe3\x83\xbc\xe3\x82bytes:\xbfのbytes:\xe6bytes:\xa7bytes:\x8b造を定bytes:\xe7\xbebytes:\xa9
type Response struct {
Data struct {
User struct {
ContributionsCollection struct {
TotalCommitContributions int `json:"totalCommitContributions"`
TotalIssueContributions int `json:"totalIssueContributions"`
TotalPullRequestContributions int `json:"totalPullRequestContributions"`
TotalRepositoryContributions int `json:"totalRepositoryContributions"`
} `json:"contributionsCollection"`
StarredRepositories struct {
TotalCount int `json:"totalCount"`
} `json:"starredRepositories"`
} `json:"user"`
} `json:"data"`
}

//bytes: \xe3\x83bytes:\xacスポンスデbytes:\xe3\x83\xbc\xe3\x82bytes:\xbfを解析
var response Response
if err := json.Unmarshal(data, &response); err != nil {
return userStats, fmt.Errorf("failed to unmarshal response data: %w", err)
}

// UserStatsbytes: \xe6bytes:\xa7bytes:\x8b造体にbytes:\xe7\xb5bytes:\xb1計情報を格bytes:\xe7\xb4bytes:\x8d
userStats.TotalStars = response.Data.User.StarredRepositories.TotalCount
userStats.TotalCommits = response.Data.User.ContributionsCollection.TotalCommitContributions
userStats.TotalIssues = response.Data.User.ContributionsCollection.TotalIssueContributions
userStats.TotalPRs = response.Data.User.ContributionsCollection.TotalPullRequestContributions
userStats.ContributedTo = response.Data.User.ContributionsCollection.TotalRepositoryContributions

return userStats, nil
}

// executeQuery は指定されたGraphQLクエリをbytes:\xe5\xaebytes:\x9f行し、bytes:\xe7\xb5bytes:\x90果をバイト配列でbytes:\xe8\xbfbytes:\x94します。
func executeQuery(query, token string) ([]byte, error) {
reqBody := map[string]string{"query": query}
reqBodyJSON, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal request body: %w", err)
}

req, err := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(reqBodyJSON))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
bodyBytes, _ := io.ReadAll(resp.Body) // エラーを無視してはいけませんが、ここでは簡略化しています。
return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(bodyBytes))
}

bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}

return bodyBytes, nil
}

Check failure on line 110 in src/funcs/get_user_status.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)

0 comments on commit 2627bb5

Please sign in to comment.