Skip to content

Commit

Permalink
fix script to fetch stars
Browse files Browse the repository at this point in the history
  • Loading branch information
chirag-jn authored Feb 9, 2024
1 parent e8433f1 commit fd0f412
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/github-stars-fetch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v2

- name: Fetch data from GitHub API
id: fetchdata
- name: Update files after fetching data from GitHub API
run: |
echo "Fetching"
- name: Update files
run: |
echo "Updating"
python docs/tools/vdb_table/fetch-github-stars.py
- name: Commit and push if there are changes
run: |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import requests

# Constants for the script
DIRECTORY = "docs/tools/vdb_table"
DIRECTORY = "docs/tools/vdb_table/data"
GITHUB_API_URL = "https://api.github.com/repos/"


def get_github_stars(github_url, headers):
# Extract the owner and repo name from the GitHub URL
global GITHUB_API_URL
parts = github_url.split("/")
owner_repo = "/".join(parts[-2:])
response = requests.get(f"{GITHUB_API_URL}{owner_repo}", headers=headers)
Expand All @@ -20,27 +21,22 @@ def get_github_stars(github_url, headers):
return None


def update_json_files(directory, headers):
def update_json_files(directory, headers=None):
for filename in os.listdir(directory):
if filename.endswith(".json"):
file_path = os.path.join(directory, filename)
with open(file_path, "r+", encoding="utf-8") as json_file:
data = json.load(json_file)
github_url = data.get("links", {}).get("github", "")
github_url = data.get("github_stars", {}).get("source_url", "")
if github_url:
stars = get_github_stars(github_url, headers)
if stars is not None:
data["github_stars"] = stars
data["github_stars"]["value"] = stars
# Write the updated data back to the file
json_file.seek(0) # Rewind to the start of the file
json.dump(data, json_file, indent=2)
json_file.truncate() # Remove any leftover content


# if __name__ == "__main__":
# # The GitHub token should be set as a secret in your GitHub repo settings
# # and passed to the script via environment variable.
# token = os.environ.get("GITHUB_TOKEN", "YOUR_GITHUB_TOKEN")
# headers = {"Authorization": f"token {token}"}

update_json_files(DIRECTORY) # past it was like update_json_files(DIRECTORY , headers)
if __name__ == "__main__":
update_json_files(DIRECTORY)

0 comments on commit fd0f412

Please sign in to comment.