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
feature: add git service token management #1136
base: main
Are you sure you want to change the base?
Conversation
Initializing git_tokens table
make git token manage API /func/git-tokens * GET : get git token list * PATCH: modify git tokens
1183d16
to
8620616
Compare
fix select uuid on SQLAlchemy
Refactor get token list
modify to insert git token data using key-value json data
add git token management queries - delete non-exist domains - insert git service tokens (if it exists, update data)
when update git_token, fill modified_at date
Add news fragment
a1f1637
to
a44a495
Compare
fix alembic's multiple heads
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.
I left some comment!
Plus, it will be good to make a new alembic migration file rather than merge them because we had lots of work to resolve migration sequence between 22.09 and 23.03. I am afraid that this merged file will cause problems.
git_tokens = sa.Table( | ||
"git_tokens", | ||
metadata, | ||
sa.Column( | ||
"user_id", | ||
GUID, | ||
sa.ForeignKey("users.uuid", onupdate="CASCADE", ondelete="CASCADE"), | ||
index=True, | ||
nullable=False, | ||
primary_key=True, | ||
), | ||
sa.Column("domain", sa.String(length=200), unique=True, primary_key=True, nullable=False), | ||
sa.Column("token", sa.String(length=200)), | ||
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()), | ||
sa.Column( | ||
"modified_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), | ||
onupdate=sa.func.current_timestamp(), | ||
), | ||
) |
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.
Since we decided to use ORM table, please define the table in ORM
This is an example code of our Session table.
class GitTokenRow(Base): | ||
__table__ = git_tokens |
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.
If we define the table in ORM, we can remove this overriding part.
t.Key("params"): t.String, | ||
}, | ||
) | ||
) | ||
async def update_git_tokens(request: web.Request, params: Any) -> web.Response: | ||
resp = [] | ||
root_ctx: RootContext = request.app["_root.context"] | ||
user_uuid = request["user"]["uuid"] | ||
|
||
# get token list then | ||
token_list = params["params"] |
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.
t.Key("params"): t.String, | |
}, | |
) | |
) | |
async def update_git_tokens(request: web.Request, params: Any) -> web.Response: | |
resp = [] | |
root_ctx: RootContext = request.app["_root.context"] | |
user_uuid = request["user"]["uuid"] | |
# get token list then | |
token_list = params["params"] | |
t.Key("tokens"): t.Mapping(t.String, t.String), | |
}, | |
) | |
) | |
async def update_git_tokens(request: web.Request, params: Any) -> web.Response: | |
resp = [] | |
root_ctx: RootContext = request.app["_root.context"] | |
user_uuid = request["user"]["uuid"] | |
# get token dictionary then | |
tokens: dict[str, str] = params["tokens"] |
First, the name of parameter is too vague. Let's change it.
If we successfully change the parameter type, then let's update git_token.insert_update_git_tokens()
!
user_uuid = request["user"]["uuid"] | ||
|
||
async with root_ctx.db.begin_readonly() as conn: | ||
query = sa.select([git_tokens.c.domain, git_tokens.c.token]).where( | ||
git_tokens.c.user_id == user_uuid | ||
) |
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.
Is there any use case of getting other user's git tokens?
For example, admins would like to get other user's tokens.
async def update_git_tokens(request: web.Request, params: Any) -> web.Response: | ||
resp = [] | ||
root_ctx: RootContext = request.app["_root.context"] | ||
user_uuid = request["user"]["uuid"] |
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.
Same here.
Please let me know if there is any use case of getting or updating other user's git token,
primary_key=True, | ||
), | ||
sa.Column("domain", sa.String(length=200), unique=True, primary_key=True, nullable=False), | ||
sa.Column("token", sa.String(length=200)), |
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.
Just a question!
How long git token usually is??
sa.Column( | ||
"modified_at", | ||
sa.DateTime(timezone=True), | ||
server_default=sa.func.now(), |
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.
I think that this field should be nullable and default value should be null if we think of a token row without any modification
"user_id": user_uuid, | ||
"domain": key, | ||
"token": value, | ||
"modified_at": datetime.datetime.now(), |
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.
I think that we do not need to fill this part because of the field's onupdate
function.
token_list = json.loads(token_list_str) | ||
domain_list = [] |
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.
This variable is actually of type dict
, but it has list
in its name, which confuses me into thinking it is of type list
.
let's rename them and add type hint!
token_list = json.loads(token_list_str) | |
domain_list = [] | |
tokens: dict[str, Any] = json.loads(token_list_str) | |
domain_list: list[str] = [] |
sa.Column( | ||
"user_id", | ||
GUID, | ||
sa.ForeignKey("users.uuid", onupdate="CASCADE", ondelete="CASCADE"), | ||
index=True, | ||
nullable=False, | ||
primary_key=True, | ||
), |
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.
Is git token unique for every user?
token_list = json.loads(token_list_str) | ||
domain_list = [] | ||
# check domain_list | ||
for key, value in token_list.items(): |
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.
for key, value in token_list.items(): | |
for domain_name, token in token_list.items(): |
Let's use more clear name!
) | ||
await conn.execute(delete_stmt) | ||
|
||
for key, value in token_list.items(): |
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.
for key, value in token_list.items(): | |
for domain_name, token in token_list.items(): |
Same here
nullable=False, | ||
primary_key=True, | ||
), | ||
sa.Column("domain", sa.String(length=200), unique=True, primary_key=True, nullable=False), |
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.
And domains
table has name
field which is a primary key.
How about defining this field as a ForeignKey
?
According to your opinion, I make a new alembic version. |
make a new alembic migration file because, merge alembic working can easily broke migration sequence
ad30e01
to
a80ede2
Compare
update alembic heads
fix the broken alembic heads
feature: add git service token management