feat: create application token

This commit is contained in:
Aravinth Manivannan 2023-09-18 17:14:19 +05:30
parent 308ae3b49d
commit f4002b6a46
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 61 additions and 9 deletions

View file

@ -26,6 +26,7 @@ class Forgejo:
self.create_repository() self.create_repository()
self.create_issue() self.create_issue()
self.create_comment() self.create_comment()
self.create_access_token()
def __add_credentials_parser(self, parser): def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials") group = parser.add_argument_group("credentials", "User credentials")
@ -142,6 +143,27 @@ class Forgejo:
self.create_comment_parser.add_argument("body", type=str, help="body") self.create_comment_parser.add_argument("body", type=str, help="body")
def create_access_token(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.login()
forgejo.create_access_token(name=args.name, file=args.file)
self.create_access_token_parser = self.subparser.add_parser(
name="create_access_token",
description="Create access token for user",
help="Create access toekn for user",
)
self.__add_credentials_parser(self.create_access_token_parser)
self.create_access_token_parser.set_defaults(func=run)
self.create_access_token_parser.add_argument(
"name", type=str, help="name of the access token"
)
self.create_access_token_parser.add_argument(
"file", type=str, help="filepath to write the token value"
)
class Cli: class Cli:
def __init__(self): def __init__(self):

View file

@ -8,14 +8,6 @@ class ParseCSRF(HTMLParser):
HTMLParser.__init__(self) HTMLParser.__init__(self)
self.name = name self.name = name
# @classmethod
# def dashboard_parser(cls) -> "ParseCSRF":
# return cls(name="csrfmiddlewaretoken")
#
# @classmethod
# def forgejo_parser(cls) -> "ParseCSRF":
# return cls(name="_csrf")
#
def handle_starttag(self, tag: str, attrs: (str, str)): def handle_starttag(self, tag: str, attrs: (str, str)):
if self.token: if self.token:
return return

View file

@ -255,7 +255,6 @@ class Forgejo:
return data return data
url = self.get_uri(f"/{owner}/{repo}/issues/{issue}") url = self.get_uri(f"/{owner}/{repo}/issues/{issue}")
print(url)
csrf = self.get_csrf_token(url) csrf = self.get_csrf_token(url)
data = create_comment_payload(csrf, body) data = create_comment_payload(csrf, body)
@ -268,3 +267,31 @@ class Forgejo:
and resp.status_code != 303 and resp.status_code != 303
): ):
raise Exception(f"Error while creating comment: {resp.status_code}") raise Exception(f"Error while creating comment: {resp.status_code}")
def create_access_token(self, name: str, file: str):
"""
Create access token
"""
def create_access_token_payload(name: str):
data = {
"name": name,
}
return data
url = self.get_uri("/api/v1/users/owner_user/tokens")
data = create_access_token_payload(name)
session = Session()
session.auth = (self.username, self.password)
resp = session.post(url, json=data, allow_redirects=False)
if resp.status_code != 201:
raise Exception(f"Error while creating comment: {resp.status_code}")
print("Created access token")
data = resp.json()
with open(file, "w") as f:
f.write(f"{data['sha1']}")
print(f"Wrote access token to {file}")

View file

@ -24,6 +24,8 @@ readonly FORGEJO_USER1_USERNAME=owner_user
readonly FORGEJO_USER1_PASSWORD=supercomplicatedpassword readonly FORGEJO_USER1_PASSWORD=supercomplicatedpassword
readonly FORGEJO_USER1_EMAIL="$FORGEJO_USER1_USERNAME@example.org" readonly FORGEJO_USER1_EMAIL="$FORGEJO_USER1_USERNAME@example.org"
readonly FORGEJO_USER1_SUPPORT_REPO="support" readonly FORGEJO_USER1_SUPPORT_REPO="support"
readonly FORGEJO_USER1_ACCESS_TOKEN_NAME="coreaccesstoken"
readonly FORGEJO_USER1_ACCESS_TOKEN_FILE_PATH=$(realpath secrets/user1-accesstoken)
readonly FORGEJO_TESTUSER_USERNAME=test_user readonly FORGEJO_TESTUSER_USERNAME=test_user
readonly FORGEJO_TESTUSER_PASSWORD=supercomplicatedpassword readonly FORGEJO_TESTUSER_PASSWORD=supercomplicatedpassword
@ -134,6 +136,15 @@ init_users_repo() {
$FORGEJO_USER1_USERNAME \ $FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO \ $FORGEJO_USER1_SUPPORT_REPO \
"mention issue @$FORGEJO_USER1_USERNAME" "mention issue @$FORGEJO_USER1_USERNAME" "mention issue @$FORGEJO_USER1_USERNAME" "mention issue @$FORGEJO_USER1_USERNAME"
python -m integration \
forgejo create_access_token \
$FORGEJO_TESTUSER_USERNAME $FORGEJO_TESTUSER_PASSWORD \
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_ACCESS_TOKEN_NAME \
$FORGEJO_USER1_ACCESS_TOKEN_FILE_PATH
} }
setup_env() { setup_env() {