Skip to content
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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from

Conversation

studioego
Copy link
Contributor

@studioego studioego commented Mar 7, 2023

feature: add git service token management

  • Initializing git_tokens table
  • make git_tokens API call (mockup)
  • CRUD feature with git_tokens table

Initializing git_tokens table
@studioego studioego added type:refactor Refactoring current implementation. type:enhance Enhance component, behavior, internals without user-facing features labels Mar 7, 2023
@studioego studioego self-assigned this Mar 7, 2023
@github-actions github-actions bot added the comp:manager Related to Manager component label Mar 7, 2023
make git token manage API

/func/git-tokens

 * GET : get git token list
 * PATCH: modify git tokens
@studioego studioego force-pushed the feature/add-git-token-management branch from 1183d16 to 8620616 Compare March 13, 2023 17:48
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)
@Yaminyam Yaminyam added the size:L 100~500 LoC label Apr 13, 2023
@github-actions github-actions bot added the require:db-migration Automatically set when alembic migrations are added or updated label Aug 29, 2023
@studioego studioego marked this pull request as ready for review August 29, 2023 08:06
@studioego studioego force-pushed the feature/add-git-token-management branch from a1f1637 to a44a495 Compare August 30, 2023 05:47
fix alembic's multiple heads
Copy link
Contributor

@fregataa fregataa left a 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.

Comment on lines +30 to +50
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(),
),
)
Copy link
Contributor

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.

Comment on lines +92 to +93
class GitTokenRow(Base):
__table__ = git_tokens
Copy link
Contributor

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.

Comment on lines +268 to +278
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"]
Copy link
Contributor

@fregataa fregataa Aug 30, 2023

Choose a reason for hiding this comment

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

Suggested change
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()!

Comment on lines +252 to +257
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
)
Copy link
Contributor

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"]
Copy link
Contributor

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)),
Copy link
Contributor

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(),
Copy link
Contributor

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(),
Copy link
Contributor

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.

Comment on lines +56 to +57
token_list = json.loads(token_list_str)
domain_list = []
Copy link
Contributor

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!

Suggested change
token_list = json.loads(token_list_str)
domain_list = []
tokens: dict[str, Any] = json.loads(token_list_str)
domain_list: list[str] = []

Comment on lines +33 to +40
sa.Column(
"user_id",
GUID,
sa.ForeignKey("users.uuid", onupdate="CASCADE", ondelete="CASCADE"),
index=True,
nullable=False,
primary_key=True,
),
Copy link
Contributor

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():
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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():
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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),
Copy link
Contributor

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?

@studioego
Copy link
Contributor Author

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.

According to your opinion, I make a new alembic version.
Because, merged file can easily cause problems.

make a new alembic migration file
because, merge alembic working can easily broke migration sequence
@studioego studioego force-pushed the feature/add-git-token-management branch from ad30e01 to a80ede2 Compare August 30, 2023 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated size:L 100~500 LoC type:enhance Enhance component, behavior, internals without user-facing features type:refactor Refactoring current implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants