Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: filter users by github user id in the users list CLI command#17029

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hugodutka merged 6 commits intomainfromhugodutka/list-users-by-github-user-id
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletionscli/testdata/coder_users_list_--help.golden
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,9 @@ OPTIONS:
-c, --column [id|username|email|created at|updated at|status] (default: username,email,created at,status)
Columns to display in table output.

--github-user-id int
Filter users by their GitHub user ID.

-o, --output table|json (default: table)
Output format.

Expand Down
18 changes: 17 additions & 1 deletioncli/userlist.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@ func (r *RootCmd) userList() *serpent.Command {
cliui.JSONFormat(),
)
client := new(codersdk.Client)
var githubUserID int64

cmd := &serpent.Command{
Use: "list",
Expand All@@ -27,8 +28,23 @@ func (r *RootCmd) userList() *serpent.Command {
serpent.RequireNArgs(0),
r.InitClient(client),
),
Options: serpent.OptionSet{
{
Name: "github-user-id",
Description: "Filter users by their GitHub user ID.",
Default: "",
Flag: "github-user-id",
Required: false,
Value: serpent.Int64Of(&githubUserID),
},
},
Handler: func(inv *serpent.Invocation) error {
res, err := client.Users(inv.Context(), codersdk.UsersRequest{})
req := codersdk.UsersRequest{}
if githubUserID != 0 {
req.Search = fmt.Sprintf("github_com_user_id:%d", githubUserID)
}

res, err := client.Users(inv.Context(), req)
if err != nil {
return err
}
Expand Down
10 changes: 10 additions & 0 deletionscoderd/database/dbmem/dbmem.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6577,6 +6577,16 @@ func (q *FakeQuerier) GetUsers(_ context.Context, params database.GetUsersParams
users = usersFilteredByLastSeen
}

if params.GithubComUserID != 0 {
usersFilteredByGithubComUserID := make([]database.User, 0, len(users))
for i, user := range users {
if user.GithubComUserID.Int64 == params.GithubComUserID {
usersFilteredByGithubComUserID = append(usersFilteredByGithubComUserID, users[i])
}
}
users = usersFilteredByGithubComUserID
}

beforePageCount := len(users)

if params.OffsetOpt > 0 {
Expand Down
1 change: 1 addition & 0 deletionscoderd/database/modelqueries.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -393,6 +393,7 @@ func (q *sqlQuerier) GetAuthorizedUsers(ctx context.Context, arg GetUsersParams,
arg.LastSeenAfter,
arg.CreatedBefore,
arg.CreatedAfter,
arg.GithubComUserID,
arg.OffsetOpt,
arg.LimitOpt,
)
Expand Down
31 changes: 19 additions & 12 deletionscoderd/database/queries.sql.go
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

5 changes: 5 additions & 0 deletionscoderd/database/queries/users.sql
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -223,6 +223,11 @@ WHERE
created_at >= @created_after
ELSE true
END
AND CASE
WHEN @github_com_user_id :: bigint != 0 THEN
github_com_user_id = @github_com_user_id
ELSE true
Comment on lines +227 to +229
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is worth blocking on, but the comment for this column mentions:

At time of implementation, this is used to check if the user has starred the Coder repository.

It may be worth updating this comment in a follow-up now that it's becoming more of a load-bearing number.

hugodutka reacted with thumbs up emoji
END
-- End of filters

-- Authorize Filter clause will be injected below in GetAuthorizedUsers
Expand Down
14 changes: 14 additions & 0 deletionscoderd/httpapi/queryparams.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -82,6 +82,20 @@ func (p *QueryParamParser) Int(vals url.Values, def int, queryParam string) int
return v
}

func (p *QueryParamParser) Int64(vals url.Values, def int64, queryParam string) int64 {
v, err := parseQueryParam(p, vals, func(v string) (int64, error) {
return strconv.ParseInt(v, 10, 64)
}, def, queryParam)
if err != nil {
p.Errors = append(p.Errors, codersdk.ValidationError{
Field: queryParam,
Detail: fmt.Sprintf("Query param %q must be a valid 64-bit integer: %s", queryParam, err.Error()),
})
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: explicitly return the zero value here

hugodutka reacted with thumbs up emoji
return 0
}
return v
}

// PositiveInt32 function checks if the given value is 32-bit and positive.
//
// We can't use `uint32` as the value must be within the range <0,2147483647>
Expand Down
15 changes: 8 additions & 7 deletionscoderd/searchquery/search.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -80,13 +80,14 @@ func Users(query string) (database.GetUsersParams, []codersdk.ValidationError) {

parser := httpapi.NewQueryParamParser()
filter := database.GetUsersParams{
Search: parser.String(values, "", "search"),
Status: httpapi.ParseCustomList(parser, values, []database.UserStatus{}, "status", httpapi.ParseEnum[database.UserStatus]),
RbacRole: parser.Strings(values, []string{}, "role"),
LastSeenAfter: parser.Time3339Nano(values, time.Time{}, "last_seen_after"),
LastSeenBefore: parser.Time3339Nano(values, time.Time{}, "last_seen_before"),
CreatedAfter: parser.Time3339Nano(values, time.Time{}, "created_after"),
CreatedBefore: parser.Time3339Nano(values, time.Time{}, "created_before"),
Search: parser.String(values, "", "search"),
Status: httpapi.ParseCustomList(parser, values, []database.UserStatus{}, "status", httpapi.ParseEnum[database.UserStatus]),
RbacRole: parser.Strings(values, []string{}, "role"),
LastSeenAfter: parser.Time3339Nano(values, time.Time{}, "last_seen_after"),
LastSeenBefore: parser.Time3339Nano(values, time.Time{}, "last_seen_before"),
CreatedAfter: parser.Time3339Nano(values, time.Time{}, "created_after"),
CreatedBefore: parser.Time3339Nano(values, time.Time{}, "created_before"),
GithubComUserID: parser.Int64(values, 0, "github_com_user_id"),
}
parser.ErrorExcessParams(values)
return filter, parser.Errors
Expand Down
21 changes: 11 additions & 10 deletionscoderd/users.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -297,16 +297,17 @@ func (api *API) GetUsers(rw http.ResponseWriter, r *http.Request) ([]database.Us
}

userRows, err := api.Database.GetUsers(ctx, database.GetUsersParams{
AfterID: paginationParams.AfterID,
Search: params.Search,
Status: params.Status,
RbacRole: params.RbacRole,
LastSeenBefore: params.LastSeenBefore,
LastSeenAfter: params.LastSeenAfter,
CreatedAfter: params.CreatedAfter,
CreatedBefore: params.CreatedBefore,
OffsetOpt: int32(paginationParams.Offset),
LimitOpt: int32(paginationParams.Limit),
AfterID: paginationParams.AfterID,
Search: params.Search,
Status: params.Status,
RbacRole: params.RbacRole,
LastSeenBefore: params.LastSeenBefore,
LastSeenAfter: params.LastSeenAfter,
CreatedAfter: params.CreatedAfter,
CreatedBefore: params.CreatedBefore,
GithubComUserID: params.GithubComUserID,
OffsetOpt: int32(paginationParams.Offset),
LimitOpt: int32(paginationParams.Limit),
})
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Expand Down
28 changes: 28 additions & 0 deletionscoderd/users_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@ package coderd_test

import (
"context"
"database/sql"
"fmt"
"net/http"
"slices"
Expand DownExpand Up@@ -1873,6 +1874,33 @@ func TestGetUsers(t *testing.T) {
require.NoError(t, err)
require.ElementsMatch(t, active, res.Users)
})
t.Run("GithubComUserID", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

client, db := coderdtest.NewWithDatabase(t, nil)
first := coderdtest.CreateFirstUser(t, client)
_ = dbgen.User(t, db, database.User{
Email: "test2@coder.com",
Username: "test2",
})
// nolint:gocritic // Unit test
err := db.UpdateUserGithubComUserID(dbauthz.AsSystemRestricted(ctx), database.UpdateUserGithubComUserIDParams{
ID: first.UserID,
GithubComUserID: sql.NullInt64{
Int64: 123,
Valid: true,
},
})
require.NoError(t, err)
res, err := client.Users(ctx, codersdk.UsersRequest{
SearchQuery: "github_com_user_id:123",
})
require.NoError(t, err)
require.Len(t, res.Users, 1)
require.Equal(t, res.Users[0].ID, first.UserID)
})
}

func TestGetUsersPagination(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletionsdocs/reference/cli/users_list.md
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp