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/16 00:24:06 UTC

[lucene-jira-archive] branch label-colors created (now c815ddbe)

This is an automated email from the ASF dual-hosted git repository.

tomoko pushed a change to branch label-colors
in repository https://gitbox.apache.org/repos/asf/lucene-jira-archive.git


      at c815ddbe add a script to update issue labels and descriptions

This branch includes the following new commits:

     new c815ddbe add a script to update issue labels and descriptions

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[lucene-jira-archive] 01/01: add a script to update issue labels and descriptions

Posted by to...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tomoko pushed a commit to branch label-colors
in repository https://gitbox.apache.org/repos/asf/lucene-jira-archive.git

commit c815ddbee9fdb180ed7d2f83c02441aaf2c2848c
Author: Tomoko Uchida <to...@gmail.com>
AuthorDate: Sat Jul 16 09:23:55 2022 +0900

    add a script to update issue labels and descriptions
---
 migration/README.md                  | 11 +++++++
 migration/src/github_issues_util.py  | 58 ++++++++++++++++++++++++++++--------
 migration/src/update_issue_labels.py | 42 ++++++++++++++++++++++++++
 3 files changed, 98 insertions(+), 13 deletions(-)

diff --git a/migration/README.md b/migration/README.md
index f0297376..700d8648 100644
--- a/migration/README.md
+++ b/migration/README.md
@@ -123,6 +123,17 @@ Second pass: `src/update_issues.py` updates issues and comments with updated iss
 [2022-07-06 15:35:06,532] INFO:update_issues: Done.
 ```
 
+### 7. Update issue labels
+
+`src/update_issue_labels.py` updates issue colors and descriptions.
+
+```
+(.venv) migration $ python src/update_issue_labels.py 
+[2022-07-16 09:18:39,764] INFO:update_issue_labels: Retrieving labels.
+[2022-07-16 09:18:42,274] INFO:update_issue_labels: 63 labels are found.
+Done.
+```
+
 ### How to Generate Account Mapping
 
 This optional step creates Jira username - GitHub account mapping. To associate Jira user with GitHub account, Jira user's "Full Name" and GitHub account's "Name" needs to be set to exactly the same value. See https://github.com/apache/lucene-jira-archive/issues/3.
diff --git a/migration/src/github_issues_util.py b/migration/src/github_issues_util.py
index 14fb12e8..e97b97e2 100644
--- a/migration/src/github_issues_util.py
+++ b/migration/src/github_issues_util.py
@@ -29,10 +29,10 @@ def get_issue_body(token: str, repo: str, issue_number: int, logger: Logger) ->
     url = GITHUB_API_BASE + f"/repos/{repo}/issues/{issue_number}"
     headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
     res = requests.get(url, headers=headers)
+    time.sleep(INTERVAL_IN_SECONDS)
     if res.status_code != 200:
         logger.error(f"Failed to get issue {issue_number}; status_code={res.status_code}, message={res.text}")
         return None
-    time.sleep(INTERVAL_IN_SECONDS)
     return res.json().get("body")
 
 
@@ -41,10 +41,10 @@ def update_issue_body(token: str, repo: str, issue_number: int, body: str, logge
     headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
     data = {"body": body}
     res = requests.patch(url, headers=headers, json=data)
+    time.sleep(INTERVAL_IN_SECONDS)
     if res.status_code != 200:
         logger.error(f"Failed to update issue {issue_number}; status_code={res.status_code}, message={res.text}")
         return False
-    time.sleep(INTERVAL_IN_SECONDS)
     return True
 
 
@@ -57,6 +57,7 @@ def get_issue_comments(token: str, repo: str, issue_number: int, logger: Logger)
     while not stop:
         url_with_paging = url + f"&page={page}"
         res = requests.get(url_with_paging, headers=headers)
+        time.sleep(INTERVAL_IN_SECONDS)
         if res.status_code != 200:
             logger.error(f"Failed to get issue comments for {issue_number}; status_code={res.status_code}, message={res.text}")
             break
@@ -65,7 +66,6 @@ def get_issue_comments(token: str, repo: str, issue_number: int, logger: Logger)
         for comment in res.json():
             li.append(GHIssueComment(id=comment.get("id"), body=comment.get("body")))
         page += 1
-        time.sleep(INTERVAL_IN_SECONDS)
     return li
 
 
@@ -74,10 +74,10 @@ def update_comment_body(token: str, repo: str, comment_id: int, body: str, logge
     headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
     data = {"body": body}
     res = requests.patch(url, headers=headers, json=data)
+    time.sleep(INTERVAL_IN_SECONDS)
     if res.status_code != 200:
         logger.error(f"Failed to update comment {comment_id}; status_code={res.status_code}, message={res.text}")
         return False
-    time.sleep(INTERVAL_IN_SECONDS)
     return True
 
 
@@ -85,9 +85,9 @@ def import_issue(token: str, repo: str, issue_data: dict, logger: Logger) -> str
     url = GITHUB_API_BASE + f"/repos/{repo}/import/issues"
     headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.golden-comet-preview+json"}
     res = requests.post(url, headers=headers, json=issue_data)
+    time.sleep(INTERVAL_IN_SECONDS)
     if res.status_code != 202:
         logger.error(f"Failed to import issue {issue_data['issue']['title']}; status_code={res.status_code}, message={res.text}")
-    time.sleep(INTERVAL_IN_SECONDS)
     return res.json().get("url")
 
 
@@ -116,10 +116,10 @@ def search_users(token: str, q: str, logger: Logger) -> list[str]:
     url = GITHUB_API_BASE + f"/search/users?q={quote_plus(q)}"
     headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
     res = requests.get(url, headers=headers)
+    time.sleep(SEARCH_INTERVAL_IN_SECONDS)    
     if res.status_code != 200:
         logger.error(f"Failed to search users with query {q}; status_code={res.status_code}, message={res.text}")
         return []
-    time.sleep(SEARCH_INTERVAL_IN_SECONDS)
     return [item["login"] for item in res.json()["items"]]
 
 
@@ -127,10 +127,10 @@ def get_user(token: str, username: str, logger: Logger) -> Optional[dict[str, An
     url = GITHUB_API_BASE + f"/users/{username}"
     headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
     res = requests.get(url, headers=headers)
+    time.sleep(INTERVAL_IN_SECONDS)
     if res.status_code != 200:
         logger.error(f"Failed to get user {username}; status_code={res.status_code}, message={res.text}")
         return None
-    time.sleep(INTERVAL_IN_SECONDS)
     return res.json()
 
 
@@ -141,15 +141,15 @@ def list_organization_members(token: str, org: str, logger: Logger) -> list[str]
     users = []
     while True:
         res = requests.get(f"{url}&page={page}", headers=headers)
-        if len(res.json()) == 0:
-            break
+        time.sleep(INTERVAL_IN_SECONDS)
         if res.status_code != 200:
             logger.error(f"Failed to get organization members for {org}; status_code={res.status_code}, message={res.text}")
             return users
+        if len(res.json()) == 0:
+            break
         users.extend(x["login"] for x in res.json())
         logger.debug(f"{len(users)} members found.")
         page += 1
-        time.sleep(INTERVAL_IN_SECONDS)
     return users
 
 
@@ -160,17 +160,49 @@ def list_commit_authors(token: str, repo: str, logger: Logger) -> list[str]:
     authors = set([])
     while True:
         res = requests.get(f"{url}&page={page}", headers=headers)
-        if len(res.json()) == 0:
-            break
+        time.sleep(INTERVAL_IN_SECONDS)
         if res.status_code != 200:
             logger.error(f"Failed to get commits for {repo}; status_code={res.status_code}, message={res.text}")
             return authors
+        if len(res.json()) == 0:
+            break
         for commit in res.json():
             author = commit.get("author")
             if author:
                 authors.add(author["login"])
         logger.debug(f"{len(authors)} authors found.")
         page += 1
-        time.sleep(INTERVAL_IN_SECONDS)
     return authors
 
+
+def list_labels(token: str, repo: str, logger: Logger) -> list[str]:
+    url = GITHUB_API_BASE + f"/repos/{repo}/labels?per_page=100"
+    headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
+    page = 1
+    labels = []
+    while True:
+        res = requests.get(f"{url}&page={page}", headers=headers)
+        time.sleep(INTERVAL_IN_SECONDS)
+        if res.status_code != 200:
+            logger.error(f"Failed to get labels for {repo}; status_code={res.status_code}, message={res.text}")
+            return labels
+        if len(res.json()) == 0:
+            break
+        for l in res.json():
+            name = l["name"]
+            labels.append(name)
+        page += 1
+    return labels
+
+
+def update_label(token: str, repo: str, name: str, color: str, description: str, logger: Logger) -> bool:
+    url = GITHUB_API_BASE + f"/repos/{repo}/labels/{name}"
+    headers = {"Authorization": f"token {token}", "Accept": "application/vnd.github.v3+json"}
+    data = {"color": color, "description": description}
+    res = requests.patch(url, headers=headers, json=data)
+    time.sleep(INTERVAL_IN_SECONDS)
+    if res.status_code != 200:
+        logger.error(f"Failed to update label {name}; status_code={res.status_code}, message={res.text}")
+        return False
+    logger.debug(f"Label {name} updated.")
+    return True
\ No newline at end of file
diff --git a/migration/src/update_issue_labels.py b/migration/src/update_issue_labels.py
new file mode 100644
index 00000000..dfb2f2b1
--- /dev/null
+++ b/migration/src/update_issue_labels.py
@@ -0,0 +1,42 @@
+from pathlib import Path
+import os
+import sys
+from common import LOG_DIRNAME, logging_setup
+from github_issues_util import *
+
+log_dir = Path(__file__).resolve().parent.parent.joinpath(LOG_DIRNAME)
+logger = logging_setup(log_dir, "update_issue_labels")
+
+
+# label prefix -> color, description
+LABEL_DETAILS_MAP = {
+    "type:": ("ffbb00", ""),
+    "fixVersion:": ("7ebea5", ""),
+    "affectsVersion:": ("f19072", ""),
+    "component:": ("a0d8ef", "")
+}
+
+
+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)
+
+    logger.info("Retrieving labels.")
+
+    labels = list_labels(github_token, github_repo, logger)
+    logger.info(f"{len(labels)} labels are found.")
+
+    for label in labels:
+        for prefix, detail in LABEL_DETAILS_MAP.items():
+            if label.startswith(prefix):
+                update_label(github_token, github_repo, label, detail[0], detail[1], logger)
+
+    print("Done.")
\ No newline at end of file