You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by to...@apache.org on 2022/07/20 15:05:38 UTC
[lucene-jira-archive] branch main updated: add an agent program that randomly add/update issues and comments
This is an automated email from the ASF dual-hosted git repository.
tomoko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene-jira-archive.git
The following commit(s) were added to refs/heads/main by this push:
new 4c87492e add an agent program that randomly add/update issues and comments
4c87492e is described below
commit 4c87492e08a7a6a76b0ec05dc424a567e0d5bab2
Author: Tomoko Uchida <to...@gmail.com>
AuthorDate: Thu Jul 21 00:05:30 2022 +0900
add an agent program that randomly add/update issues and comments
---
migration/src/github_issues_util.py | 27 ++++++++++
migration/src/random_agent.py | 98 +++++++++++++++++++++++++++++++++++++
2 files changed, 125 insertions(+)
diff --git a/migration/src/github_issues_util.py b/migration/src/github_issues_util.py
index e97b97e2..ce4e44f3 100644
--- a/migration/src/github_issues_util.py
+++ b/migration/src/github_issues_util.py
@@ -36,6 +36,20 @@ def get_issue_body(token: str, repo: str, issue_number: int, logger: Logger) ->
return res.json().get("body")
+def create_issue(token: str, repo: str, title: str, body: str, logger: Logger) -> Optional[tuple[str, int]]:
+ url = GITHUB_API_BASE + f"/repos/{repo}/issues"
+ headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
+ data = {"title": title, "body": body}
+ res = requests.post(url, headers=headers, json=data)
+ time.sleep(INTERVAL_IN_SECONDS)
+ if res.status_code != 201:
+ logger.error(f"Failed to create issue; status_code={res.status_code}, message={res.text}")
+ return None
+ html_url = res.json()["html_url"]
+ number = res.json()["number"]
+ return (html_url, number)
+
+
def update_issue_body(token: str, repo: str, issue_number: int, body: str, logger: Logger) -> bool:
url = GITHUB_API_BASE + f"/repos/{repo}/issues/{issue_number}"
headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
@@ -69,6 +83,19 @@ def get_issue_comments(token: str, repo: str, issue_number: int, logger: Logger)
return li
+def create_comment(token: str, repo: str, issue_number: int, body: str, logger: Logger) -> Optional[int]:
+ url = GITHUB_API_BASE + f"/repos/{repo}/issues/{issue_number}/comments"
+ headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
+ data = {"body": body}
+ res = requests.post(url, headers=headers, json=data)
+ time.sleep(INTERVAL_IN_SECONDS)
+ if res.status_code != 201:
+ logger.error(f"Failed to create comment; status_code={res.status_code}, message={res.text}")
+ return None
+ id = res.json()["id"]
+ return id
+
+
def update_comment_body(token: str, repo: str, comment_id: int, body: str, logger: Logger) -> bool:
url = GITHUB_API_BASE + f"/repos/{repo}/issues/comments/{comment_id}"
headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
diff --git a/migration/src/random_agent.py b/migration/src/random_agent.py
new file mode 100644
index 00000000..a76aaeed
--- /dev/null
+++ b/migration/src/random_agent.py
@@ -0,0 +1,98 @@
+import os
+import sys
+import time
+from enum import Enum, auto
+from random import randint, shuffle
+from github_issues_util import *;
+from common import *
+
+log_dir = Path(__file__).resolve().parent.parent.joinpath(LOG_DIRNAME)
+logger = logging_setup(log_dir, "random_agent")
+
+
+MIN_INTERVAL = 600
+MAX_INTERVAL = 1800
+
+class Command(Enum):
+ CREATE_ISSUE = auto(),
+ UPDATE_ISSUE = auto(),
+ CREATE_COMMENT = auto()
+
+
+def action(command: Command, new_issues_file: Path, token: str, repo: str) -> bool:
+ result = False
+ if command is Command.CREATE_ISSUE:
+ title = "Issue created by an agent"
+ body = "issue created"
+ res = create_issue(token, repo, title, body, logger)
+ if res:
+ (url, num) = res
+ with open(new_issues_file, "a") as fp:
+ fp.write(f"{url},{num}\n")
+ logger.debug(f"Issue created. {num}")
+ result = True
+ elif command is Command.UPDATE_ISSUE:
+ body = "issue updated"
+ issue_number = __select_random_issue(new_issues_file)
+ if update_issue_body(token, repo, issue_number, body, logger):
+ logger.debug(f"Issue updated. {issue_number}")
+ result = True
+ elif command is Command.CREATE_COMMENT:
+ body = "comment added"
+ issue_number = __select_random_issue(new_issues_file)
+ if create_comment(token, repo, issue_number, body, logger):
+ logger.debug(f"Comment added to {issue_number}.")
+ result = True
+ return result
+
+
+def __select_random_issue(issues_file: Path) -> int:
+ issues = []
+ with open(issues_file) as fp:
+ for line in fp:
+ cols = line.strip().split(",")
+ issues.append(int(cols[1]))
+ shuffle(issues)
+ return issues[0]
+
+
+if __name__ == '__main__':
+ github_token = os.getenv("GITHUB_PAT")
+ if not github_token:
+ print("Please set your GitHub token to GITHUB_PAT environment variable.")
+ sys.exit(1)
+ github_repo = os.getenv("GITHUB_REPO")
+ if not github_repo:
+ print("Please set GitHub repo location to GITHUB_REPO environment varialbe.")
+ sys.exit(1)
+
+ check_authentication(github_token)
+
+ work_dir = Path(__file__).resolve().parent.parent.joinpath(WORK_DIRNAME)
+ if not work_dir.exists():
+ work_dir.mkdir()
+ new_issues_file = work_dir.joinpath("new_issues.csv")
+ new_issues_file.unlink(missing_ok=True)
+
+ num_issues = 0
+ num_updated_issues = 0
+ num_comments = 0
+ try:
+ while True:
+ r = randint(0,9)
+ if num_issues < 5 or r <= 1:
+ if action(Command.CREATE_ISSUE, new_issues_file, github_token, github_repo):
+ num_issues += 1
+ elif r <= 5:
+ if action(Command.UPDATE_ISSUE, new_issues_file, github_token, github_repo):
+ num_updated_issues += 1
+ else:
+ if action(Command.CREATE_COMMENT, new_issues_file, github_token, github_repo):
+ num_comments += 1
+ time.sleep(randint(MIN_INTERVAL, MAX_INTERVAL))
+ except KeyboardInterrupt:
+ logger.info(f"Created issues: {num_issues}, Updated issues: {num_updated_issues}, Created comments: {num_comments}")
+ sys.exit(0)
+
+
+