From 0d006ff591f13458f099867b3d1484161c3bcb6e Mon Sep 17 00:00:00 2001 From: jessiebroke <18238304+jessiebroke@users.noreply.github.com> Date: Wed, 29 Nov 2023 21:51:34 -0500 Subject: [PATCH] add vacuum.py script and vacuum.yml workflow to extract all removed labels from all issues across all repos in the Codex organization --- .github/scripts/restore_labels.py | 28 --------- .github/scripts/vacuum.py | 72 ++++++++++++++++++++++ .github/workflows/add-labels-to-issues.yml | 30 +++++++++ .github/workflows/vacuum.py | 0 4 files changed, 102 insertions(+), 28 deletions(-) delete mode 100644 .github/scripts/restore_labels.py create mode 100644 .github/scripts/vacuum.py create mode 100644 .github/workflows/add-labels-to-issues.yml create mode 100644 .github/workflows/vacuum.py diff --git a/.github/scripts/restore_labels.py b/.github/scripts/restore_labels.py deleted file mode 100644 index 21ef33a..0000000 --- a/.github/scripts/restore_labels.py +++ /dev/null @@ -1,28 +0,0 @@ -import os -import requests -import json - -# Replace with your GitHub username, organization, repository, and personal access token -username = "JessieBroke" -organization = "codex-storage" -repository = "nim-codex" -token = os.getenv("GH_PAT") - -# GitHub API endpoint for listing issue events -api_url = f"https://api.github.com/repos/{organization}/{repository}/issues/events" - -# Fetch issue events -response = requests.get(api_url, headers={"Authorization": f"Bearer {token}"}) -events = response.json() - -# Save events to a JSON file -with open("issue_events.json", "w") as file: - json.dump(events, file) - -# Restore labels based on deletion events -for event in events: - if event["event"] == "label": - label_name = event["label"]["name"] - # Restore the label using your preferred method (GitHub API, gh CLI, etc.) - print(f"Restoring label: {label_name}") - # Add logic here to restore the label using the GitHub API or other methods diff --git a/.github/scripts/vacuum.py b/.github/scripts/vacuum.py new file mode 100644 index 0000000..f926fb4 --- /dev/null +++ b/.github/scripts/vacuum.py @@ -0,0 +1,72 @@ +import os +import requests +import json +from datetime import datetime, timedelta + +# Replace with your GitHub username, organization, and personal access token +username = "JessieBroke" +organization = "TheBasedmint" +token = os.getenv("GH_PAT") + +# GitHub API endpoint for listing repositories in the organization +repositories_url = f"https://api.github.com/orgs/{organization}/repos" + +# Fetch repositories +response = requests.get(repositories_url, headers={"Authorization": f"Bearer {token}"}) +repositories_data = response.json() + +# Extract repository names from the response +repositories = [repo["name"] for repo in repositories_data] + +# Calculate the datetime 48 hours ago from now +time_threshold = datetime.utcnow() - timedelta(hours=48) + +# Create a dictionary to store unique issues based on the combination of issue URL and label name +unique_issues = {} + +# Iterate through each repository +for repository in repositories: + # GitHub API endpoint for listing issue events + api_url = f"https://api.github.com/repos/{organization}/{repository}/issues/events" + + # Fetch issue events + response = requests.get(api_url, headers={"Authorization": f"Bearer {token}"}) + events = response.json() + + # Iterate through each event in the list + for event in events: + # Extracting information for each event + event_type = event.get('event') + created_at_str = event.get('created_at') + + # Convert created_at string to datetime object + created_at = datetime.strptime(created_at_str, "%Y-%m-%dT%H:%M:%SZ") + + # Check if the event is 'unlabeled' and updated in the past 48 hours + if event_type == 'unlabeled' and created_at > time_threshold: + actor_login = event.get('actor', {}).get('login') + label_name = event.get('label', {}).get('name') + label_color = event.get('label', {}).get('color') + issue_url = event.get('issue', {}).get('html_url') + + if actor_login and label_name and label_color and issue_url: + # Use a unique identifier for each issue-label combination + unique_identifier = f"{issue_url}_{label_name}" + + # Check if the unique identifier is not already in the dictionary + if unique_identifier not in unique_issues: + unique_issues[unique_identifier] = { + 'login': actor_login, + 'label_name': label_name, + 'label_color': label_color, + 'issue_url': issue_url + } + +# Convert the dictionary values to a list +filtered_data = list(unique_issues.values()) + +# Write the filtered data to a new JSON file +output_path = os.path.join("output", "vacuum_data.json") +os.makedirs(os.path.dirname(output_path), exist_ok=True) +with open(output_path, 'w') as output_file: + json.dump(filtered_data, output_file, indent=2) diff --git a/.github/workflows/add-labels-to-issues.yml b/.github/workflows/add-labels-to-issues.yml new file mode 100644 index 0000000..a00c656 --- /dev/null +++ b/.github/workflows/add-labels-to-issues.yml @@ -0,0 +1,30 @@ +name: Add Label to Issues +on: + push: + branches: + - master + +jobs: + add-label: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Add label to open issues in "test1" repository + run: | + REPO="TheBasedmint/test1" + LABEL="Marketplace" + + # Fetch open issues + for issue in $(curl -s -H "Authorization: Bearer ${{ secrets.SYNC_LABELS2 }}" \ + -H "Accept: application/vnd.github.v3+json" \ + "https://api.github.com/repos/$REPO/issues?state=open" | jq -r '.[].number'); do + + # Apply label to each open issue + curl -X POST -H "Authorization: Bearer ${{ secrets.SYNC_LABELS2 }}" \ + -H "Accept: application/vnd.github.v3+json" \ + -d "{\"labels\": [\"$LABEL\"]}" \ + "https://api.github.com/repos/$REPO/issues/$issue/labels" + done diff --git a/.github/workflows/vacuum.py b/.github/workflows/vacuum.py new file mode 100644 index 0000000..e69de29