feat: create issue and comment with mentions

This commit is contained in:
Aravinth Manivannan 2023-09-17 23:06:21 +05:30
parent 685e6f44f7
commit 308ae3b49d
Signed by: realaravinth
GPG key ID: F8F50389936984FF
4 changed files with 118 additions and 9 deletions

1
.gitignore vendored
View file

@ -8,3 +8,4 @@ node_modules
!.env.example
vite.config.js.timestamp-*
vite.config.ts.timestamp-*
__pycache__/

View file

@ -25,6 +25,7 @@ class Forgejo:
self.login()
self.create_repository()
self.create_issue()
self.create_comment()
def __add_credentials_parser(self, parser):
group = parser.add_argument_group("credentials", "User credentials")
@ -89,7 +90,9 @@ class Forgejo:
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.login()
forgejo.create_issue(owner=args.owner, repo=args.repo)
forgejo.create_issue(
owner=args.owner, repo=args.repo, title=args.title, body=args.body
)
self.create_issue_parser = self.subparser.add_parser(
name="create_issue",
@ -106,6 +109,38 @@ class Forgejo:
"repo", type=str, help="Name of the repository"
)
self.create_issue_parser.add_argument("title", type=str, help="title")
self.create_issue_parser.add_argument("body", type=str, help="body")
def create_comment(self):
def run(args, c: Session):
forgejo = forgejo_from_args(args, c=c)
forgejo.login()
forgejo.create_comment(
owner=args.owner, repo=args.repo, issue=args.issue, body=args.body
)
self.create_comment_parser = self.subparser.add_parser(
name="create_comment",
description="Create comment on a repository owned by someone on Forgejo",
help="Create comment on Forgejo",
)
self.__add_credentials_parser(self.create_comment_parser)
self.create_comment_parser.set_defaults(func=run)
self.create_comment_parser.add_argument(
"owner", type=str, help="Owner of the repo"
)
self.create_comment_parser.add_argument(
"repo", type=str, help="Name of the repository"
)
self.create_comment_parser.add_argument(
"issue", type=int, help="ID of the issue"
)
self.create_comment_parser.add_argument("body", type=str, help="body")
class Cli:

View file

@ -205,8 +205,7 @@ class Forgejo:
f"Error while creating repository: {name} {resp.status_code}"
)
def create_issue(self, owner: str, repo: str):
def create_issue(self, owner: str, repo: str, title: str, body: str):
"""
Create issue
"""
@ -225,12 +224,11 @@ class Forgejo:
"redirect_after_creation": "",
}
return data
url = self.get_uri(f"/{owner}/{repo}/issues/new")
csrf = self.get_csrf_token(url)
data = create_issue_payload(csrf, "my issue", "my body")
data = create_issue_payload(csrf=csrf, title=title, body=body)
resp = self.c.post(url, data=data, allow_redirects=False)
print(f"Created issue")
@ -239,6 +237,34 @@ class Forgejo:
and resp.status_code != 200
and resp.status_code != 303
):
raise Exception(
f"Error while creating issue: {resp.status_code}"
)
raise Exception(f"Error while creating issue: {resp.status_code}")
def create_comment(self, owner: str, repo: str, issue: int, body: str):
"""
Create comment
"""
self.login()
def create_comment_payload(csrf: str, body: str):
data = {
"_csrf": csrf,
"content": body,
"status": "",
}
return data
url = self.get_uri(f"/{owner}/{repo}/issues/{issue}")
print(url)
csrf = self.get_csrf_token(url)
data = create_comment_payload(csrf, body)
url = self.get_uri(f"/{owner}/{repo}/issues/{issue}/comments")
resp = self.c.post(url, data=data, allow_redirects=False)
print(f"Created comment")
if (
resp.status_code != 302
and resp.status_code != 200
and resp.status_code != 303
):
raise Exception(f"Error while creating comment: {resp.status_code}")

View file

@ -86,7 +86,54 @@ init_users_repo() {
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO
$FORGEJO_USER1_SUPPORT_REPO \
"normal issue title" "normal issue body"
python -m integration \
forgejo create_comment \
$FORGEJO_TESTUSER_USERNAME $FORGEJO_TESTUSER_PASSWORD \
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO \
1 "normal body"
python -m integration \
forgejo create_comment \
$FORGEJO_TESTUSER_USERNAME $FORGEJO_TESTUSER_PASSWORD \
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO \
1 "mention body @$FORGEJO_USER1_USERNAME"
python -m integration \
forgejo create_issue \
$FORGEJO_TESTUSER_USERNAME $FORGEJO_TESTUSER_PASSWORD \
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO \
"normal issue title" "mention issue @$FORGEJO_USER1_USERNAME"
python -m integration \
forgejo create_issue \
$FORGEJO_TESTUSER_USERNAME $FORGEJO_TESTUSER_PASSWORD \
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO \
"mention issue @$FORGEJO_USER1_USERNAME" "normal issue body"
python -m integration \
forgejo create_issue \
$FORGEJO_TESTUSER_USERNAME $FORGEJO_TESTUSER_PASSWORD \
$FORGEJO_USER1_EMAIL \
$FORGEJO_URL \
$FORGEJO_USER1_USERNAME \
$FORGEJO_USER1_SUPPORT_REPO \
"mention issue @$FORGEJO_USER1_USERNAME" "mention issue @$FORGEJO_USER1_USERNAME"
}
setup_env() {