Skip to content

docs/openapi/v2.yamlにbotを弾くクエリパラメータの記述を追加#1103

Merged
w4mally merged 13 commits intomainfrom
dev/adding-bot-exclude-parameter
Apr 24, 2025
Merged

docs/openapi/v2.yamlにbotを弾くクエリパラメータの記述を追加#1103
w4mally merged 13 commits intomainfrom
dev/adding-bot-exclude-parameter

Conversation

@w4mally
Copy link
Copy Markdown
Contributor

@w4mally w4mally commented Jan 13, 2025

User description

タイトルの通りです。
他の部分を参考に見よう見まねで書きました。


PR Type

  • Enhancement
  • Tests
  • Documentation

Description

  • ボット除外パラメータを追加

  • ユーザフィルタリングロジック更新

  • テストケースとモック更新対応

  • Goバージョン/Docker依存修正


Changes walkthrough 📝

Relevant files
Enhancement
5 files
users.go
GetUsersハンドラにbotクエリパラメータ実装                                                             
+12/-2   
user.go
GetAllActiveUser関数にincludeBot引数追加                                               
+17/-3   
user.go
UserInfoコンストラクタにbotフラグ導入                                                                 
+9/-3     
go.mod, Dockerfile
Goバージョン/Dockerイメージを1.23系に調整                                                           
依存関係関連ファイル
モックパッケージimportをgithub.com/golang/mockへ更新                                 
Documentation
1 files
v2.yaml
OpenAPI定義にbotパラメータ記述&operationId修正                                             
+11/-1   
Tests
1 files
複数のテストファイル (例: game_test.go, admin_test.go, users_test.go 等)
各テストケースにbotフラグfalseを設定                                                                     
Additional files
96 files
ai-review.yaml +1/-1     
ci.yaml +2/-2     
.golangci.yaml +26/-35 
README.md +0/-3     
Dockerfile +1/-1     
Dockerfile +2/-2     
go.mod +81/-82 
go.sum +139/-133
oidc.go +1/-1     
oidc_test.go +1/-1     
user.go +9/-1     
user_test.go +14/-1   
user.go +1/-1     
user_test.go +13/-1   
user.go +1/-1     
app.go +1/-1     
auth.go +1/-1     
cache.go +1/-1     
handler.go +1/-1     
repository.go +1/-1     
service.go +1/-1     
storage.go +1/-1     
session_test.go +1/-1     
admin_test.go +10/-10 
checker_test.go +1/-1     
edition.go +1/-1     
edition_auth_test.go +0/-794 
edition_test.go +0/-1820
game_file_test.go +1/-1     
game_genre_test.go +3/-3     
game_image_test.go +1/-1     
game_role_test.go +5/-5     
game_test.go +33/-1   
game_version.go +0/-2     
game_version_test.go +2/-24   
game_video_test.go +1/-1     
oauth2_test.go +1/-1     
session_test.go +1/-1     
users_test.go +57/-3   
access_token.go +1/-1     
edition.go +1/-1     
game_genre.go +1/-1     
game_management_role.go +1/-1     
db_test.go +1/-1     
current.go +4/-4     
migrate.go +0/-1     
v15.go +0/-142 
v2_game_version.go +0/-6     
v2_game_version_test.go +13/-4   
launcher_session.go +1/-1     
launcher_user.go +1/-1     
db.go +1/-1     
product_key.go +1/-1     
v2_admin_auth.go +1/-1     
v2_game.go +1/-1     
v2_game_file.go +1/-1     
v2_game_image.go +1/-1     
v2_game_version.go +1/-1     
v2_game_video.go +1/-1     
edition.go +1/-1     
edition_auth.go +1/-1     
game_genre.go +1/-1     
user_test.go +44/-2   
admin_auth.go +4/-7     
admin_auth_test.go +12/-25 
edition_test.go +1/-1     
game.go +4/-0     
game_file_test.go +1/-1     
game_genre_test.go +1/-1     
game_image_test.go +1/-1     
game_test.go +34/-1   
game_version.go +0/-4     
game_version_test.go +2/-55   
game_video_test.go +1/-1     
oidc_test.go +3/-1     
v2_admin_auth.go +1/-1     
v2_game.go +1/-1     
v2_game_file.go +1/-1     
v2_game_image.go +1/-1     
v2_game_role.go +1/-1     
v2_game_version.go +1/-2     
v2_game_video.go +1/-1     
v2_oidc.go +1/-1     
directory_manager_test.go +1/-1     
game_file_test.go +1/-1     
game_image_test.go +1/-1     
game_video_test.go +1/-1     
game_file.go +5/-1     
game_image.go +1/-1     
game_video.go +1/-1     
client_test.go +7/-10   
client_test.go +1/-1     
game_file_test.go +1/-1     
game_image_test.go +1/-1     
game_video_test.go +1/-1     
tools.go +1/-1     

Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • @w4mally w4mally requested a review from ikura-hamu January 13, 2025 15:57
    @w4mally w4mally linked an issue Jan 13, 2025 that may be closed by this pull request
    @github-actions
    Copy link
    Copy Markdown

    github-actions bot commented Jan 13, 2025

    PR Reviewer Guide 🔍

    (Review updated until commit 809d0b0)

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Botフラグの取扱い

    ユーザー情報関連の処理において追加されたボット判定用の false リテラルが、全体として一貫した意味を持っているか確認してください。定数化や明示的なコメントの追加を検討すると、意図がより明確になるかもしれません。

    func (ui *UserInfo) GetStatus() values.TraPMemberStatus {
    	return ui.status
    }
    
    func (ui *UserInfo) GetBot() bool {
    	return ui.bot
    }
    クエリパラメータ解析

    新たに追加された bot クエリパラメータの変換処理が期待通りの挙動をするか、エッジケースも含めて動作確認をお願いします。特に、パラメータが存在しない場合と不正な値が渡された場合のデフォルト動作の確認が望まれます。

    botParam := c.QueryParam("bot")
    includeBot := true
    if botParam != "" {
    	includeBot, err = strconv.ParseBool(botParam)
    	if err != nil {
    		return echo.NewHTTPError(http.StatusBadRequest)
    	}
    }

    @github-actions
    Copy link
    Copy Markdown

    github-actions bot commented Jan 13, 2025

    PR Code Suggestions ✨

    Latest suggestions up to 809d0b0
    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    存在チェックでnil回避

    adminIDactiveUsersMapに存在しない場合、nilポインタのメソッド呼び出しでパニックが発生する可能性があります。存在チェックを追加してください。

    src/service/v2/admin_auth.go [64-67]

     for _, adminID := range adminIDs {
    -		if activeUsersMap[adminID].GetStatus() == values.TrapMemberStatusActive {
    -			adminInfos = append(adminInfos, activeUsersMap[adminID])
    +		if activeAdmin, ok := activeUsersMap[adminID]; ok && activeAdmin.GetStatus() == values.TrapMemberStatusActive {
    +			adminInfos = append(adminInfos, activeAdmin)
     		}
     }
    Suggestion importance[1-10]: 8

    __

    Why: The suggestion correctly identifies a potential nil pointer dereference by adding an existence check for each adminID, preventing a possible panic when retrieving from activeUsersMap, and the improved code accurately reflects the intended change.

    Medium
    無効URLのテスト修正

    無効なURLのテストでは空白や不正な文字が含まれることを想定しているので、無効な文字列を含むURLに戻すか見直してください。

    src/handler/v2/game_version_test.go [555]

    -invalidURL := " https://example.com"
    +invalidURL := "https://example.com with spaces"
    Suggestion importance[1-10]: 6

    __

    Why: This suggestion addresses the test case for an invalid URL by reintroducing extra invalid characters ("with spaces"), which improves the test’s accuracy; it is a minor but useful correction.

    Low
    General
    初期値を見直し

    初期値のincludeBotがtrueになっていますが、多くのテストケースではfalseが想定されているようです。期待する動作に合わせて初期値を見直してください。

    src/handler/v2/users.go [66-68]

     botParam := c.QueryParam("bot")
    -includeBot := true
    +includeBot := false
     if botParam != "" {
    Suggestion importance[1-10]: 5

    __

    Why: The suggestion proposes changing the default value of includeBot from true to false based on test expectations, which is a moderate-impact design change; however, it may conflict with intended behavior if the default true was deliberate.

    Low

    Previous suggestions

    Suggestions up to commit edb7deb
    CategorySuggestion                                                                                                                                    Score
    General
    クエリパラメータのデフォルト値を明示的に指定することで、意図を明確にします。


    parametersセクションに追加されたbotクエリパラメータのデフォルト値が明確に指定されていないため、defaultフィールドを追加してデフォルト値を明示してください。

    docs/openapi/v2.yaml [170-176]

     - name: bot
       in: query
       required: false
       schema:
         type: boolean
    +    default: true
       description: |
         falseの場合botを除外します。
         デフォルトではbotも含めます。
    Suggestion importance[1-10]: 9

    Why: Adding a default field to the bot query parameter clarifies its intended behavior and ensures consistency in its usage. This is a significant improvement for API documentation, as it removes ambiguity and aids developers in understanding the default behavior of the parameter.

    9

    Copy link
    Copy Markdown
    Member

    @ikura-hamu ikura-hamu left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    okです。task generateを実行するとコード生成が実行されるので、それをやってからこのブランチで開発を進めてください

    @w4mally
    Copy link
    Copy Markdown
    Contributor Author

    w4mally commented Jan 25, 2025

    users.goにクエリパラメータの取得部分を書きました。
    あとはuserInfoからusersをappendする前に?bot=falseかつそのユーザーがbotならappendせずに次のユーザーを見る、みたいな処理を書けばいいのかなと思ったのですが、user.go内のUserInfoの定義などを見てもユーザーがbotかどうかを判別する方法が分からなかったのでいったん作業途中でpushしました。

    @w4mally
    Copy link
    Copy Markdown
    Contributor Author

    w4mally commented Mar 3, 2025

    v1/service/src/user.goにbotを弾くフィルタリングを実装しました
    src/auth/traQ/user_test.goなどにエラーが多数出ていますがいったん確認お願いします。

    @w4mally w4mally marked this pull request as draft March 3, 2025 13:13
    Copy link
    Copy Markdown
    Member

    @ikura-hamu ikura-hamu left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ありがとうございます。処理は問題ないと思います。1か所だけ抜けてそうなとこがあったので、そこは修正お願いします。
    コンパイルエラーの部分はいろんな関数やinterfaceが書き換わった影響だと思うので、頑張って地道に直してください。interfaceを書き換えてるので、テストで使ってるモックを生成しなおすために task generate をするといいと思います。

    users = filteredUsers
    return users, nil
    }

    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    この下の userAuth.GetAllActiveUsers の結果も、botかどうかのフィルターをかけて返すようにしてください。キャッシュにセットする値はbotも含めていいと思います。

    Copy link
    Copy Markdown
    Member

    @ikura-hamu ikura-hamu left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ありがとうございます!大まかな流れは正しく実装できています。テストコードでできるだけ多くのケースを抜け漏れなく試せるよう意識してみてください。VSCodeであれば、コマンドパレットのGo: Toggle Test Ccoverage In Current Package でどれだけテストがカバーしているか可視化できて便利なので試してみてください

    if botParam != "" {
    includeBot, err = strconv.ParseBool(botParam)
    if err != nil {
    log.Printf("error: invalid query parameter 'bot': %v\n", err)
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    正常な処理とエラーの区別がつきにくくなるので、 ログを残すのはInternalServerErrorのときのみにしてほしいです。

    req := httptest.NewRequest(http.MethodPost, "/users/me", nil)
    rec := httptest.NewRecorder()
    c := e.NewContext(req, rec)
    bot := true
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ここでbotを指定してしまうとbot=falseのときのテストができないと思うので、上の方のtest構造体にフィールドを足して、bot=trueとbot=falseの両方で正しく動くことを確認するようなテストケースを追加してください

    if err != nil {
    return nil, fmt.Errorf("failed to get user info: %w", err)
    }
    if err == nil {
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ここは上でerrがnilではないことが分かってるので、ifに入れなくても良いです。(Goではネストが深くなることを嫌う文化があります)

    // cacheから取り出した場合はそれを返す
    if err == nil {
    filteredUsers := make([]*service.UserInfo, 0, len(users))
    for _, user := range users {
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ここと下のやつとでfilterする処理が同じなので、適当な関数に切り出しちゃってください

    values.NewOIDCAccessToken("access token"),
    time.Now(),
    )
    includeBot := true
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ここでもテストケースでbotを含むパターンと含まないパターンの両方の動作を確認するようにしてください

    filteredUsers = append(filteredUsers, user)
    }
    users = filteredUsers
    return users, nil
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ここでreturnしちゃうとキャッシュへのセットが行われないので、テストが落ちています

    @w4mally
    Copy link
    Copy Markdown
    Contributor Author

    w4mally commented Apr 10, 2025

    春休み忙しくてとても遅くなってすみません:pray:
    指摘もらったところ直してテストを4ケース追加しました
    手元で動かしたときはテスト全部通ったはずです、確認お願いします

    Copy link
    Copy Markdown
    Member

    @ikura-hamu ikura-hamu left a comment

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    ありがとうございます、リファクタリングはいい感じです。まだバグがあるっぽいので、そこの修正をお願いします

    }
    }

    func FilteringUsers(users []*service.UserInfo, includeBot bool){
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    この関数はパッケージの外部で使われてほしくないので、private(最初を小文字)にしてください。
    あと、たぶんこの関数だとfilterがかかってないので、新しいスライスを返す形の関数にしてください。
    https://go.dev/play/p/WumURkHqC5k
    Goのスライスを引数に渡した時の挙動としては、参照が渡されるけど代入されると書き換わるぽいです

    values.TrapMemberStatusActive,
    false,
    ),
    }
    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

    The reason will be displayed to describe this comment to others. Learn more.

    今はbot以外のユーザーしか想定したテストケースになっていないので、キャッシュから得られるユーザーやtraQから得られるユーザーにbotが含まれているパターンもあるとよいと思います。

    @w4mally
    Copy link
    Copy Markdown
    Contributor Author

    w4mally commented Apr 10, 2025

    関数がフィルタリング後の結果をreturnで返すようにしたのと、テストの追加をやりました
    openapiの自動生成されているファイルのコンフリクトってどうやって解消するのがいいんでしたっけ

    @ikura-hamu
    Copy link
    Copy Markdown
    Member

    最新のmainブランチをこのブランチにマージして、コード生成をもう一回かけるといけると思います

    @w4mally w4mally marked this pull request as ready for review April 16, 2025 14:01
    @github-actions
    Copy link
    Copy Markdown

    Persistent review updated to latest commit 809d0b0

    @codecov
    Copy link
    Copy Markdown

    codecov bot commented Apr 16, 2025

    Codecov Report

    Attention: Patch coverage is 60.86957% with 18 lines in your changes missing coverage. Please review.

    Project coverage is 49.78%. Comparing base (d7c7d60) to head (f701691).
    Report is 24 commits behind head on main.

    Files with missing lines Patch % Lines
    src/handler/v2/openapi/openapi.gen.go 0.00% 9 Missing ⚠️
    src/handler/v2/users.go 44.44% 4 Missing and 1 partial ⚠️
    src/service/user.go 0.00% 4 Missing ⚠️
    Additional details and impacted files
    @@            Coverage Diff             @@
    ##             main    #1103      +/-   ##
    ==========================================
    + Coverage   49.74%   49.78%   +0.04%     
    ==========================================
      Files         123      123              
      Lines       11118    11157      +39     
    ==========================================
    + Hits         5531     5555      +24     
    - Misses       5290     5304      +14     
    - Partials      297      298       +1     

    ☔ View full report in Codecov by Sentry.
    📢 Have feedback on the report? Share it here.

    🚀 New features to boost your workflow:
    • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

    @w4mally w4mally merged commit 2a074c2 into main Apr 24, 2025
    10 checks passed
    @w4mally w4mally deleted the dev/adding-bot-exclude-parameter branch April 24, 2025 13:05
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    GET /usersでbotを除外するパラメータを付ける

    2 participants