Spaces:
Sleeping
Sleeping
Update functions.py
Browse files- functions.py +23 -2
functions.py
CHANGED
@@ -19,7 +19,7 @@ GITHUB_API = "https://api.github.com"
|
|
19 |
# Function to update headers with a new token
|
20 |
def get_headers(updated_token=None):
|
21 |
# Use the provided token or fallback to the existing secret token
|
22 |
-
token = updated_token if updated_token else secrets['GITHUB_API_TOKEN']
|
23 |
return {
|
24 |
"Authorization": f"token {token}",
|
25 |
"Accept": "application/vnd.github.v3+json"
|
@@ -32,4 +32,25 @@ def get_contributors(repo, updated_token=None):
|
|
32 |
response = requests.get(url, headers=headers)
|
33 |
return [contributor['login'] for contributor in response.json()]
|
34 |
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Function to update headers with a new token
|
20 |
def get_headers(updated_token=None):
|
21 |
# Use the provided token or fallback to the existing secret token
|
22 |
+
token = updated_token if updated_token!="" else secrets['GITHUB_API_TOKEN']
|
23 |
return {
|
24 |
"Authorization": f"token {token}",
|
25 |
"Accept": "application/vnd.github.v3+json"
|
|
|
32 |
response = requests.get(url, headers=headers)
|
33 |
return [contributor['login'] for contributor in response.json()]
|
34 |
|
35 |
+
# Function to get repository branches
|
36 |
+
def get_repo_branches(repo_name, updated_token=None):
|
37 |
+
"""
|
38 |
+
Fetch and return a list of branches from a GitHub repository.
|
39 |
+
|
40 |
+
Parameters:
|
41 |
+
- repo_name: str - The full name of the repository (e.g., "owner/repo").
|
42 |
+
- updated_token: str - Optional. A GitHub token for authentication.
|
43 |
+
|
44 |
+
Returns:
|
45 |
+
- A list of branch names (str) of the specified repository.
|
46 |
+
"""
|
47 |
+
headers = get_headers(updated_token)
|
48 |
+
url = f"{GITHUB_API}/repos/{repo_name}/branches"
|
49 |
+
try:
|
50 |
+
response = requests.get(url, headers=headers)
|
51 |
+
response.raise_for_status() # Raises an HTTPError if the response status code is 4XX or 5XX
|
52 |
+
branches = [branch['name'] for branch in response.json()]
|
53 |
+
return branches
|
54 |
+
except requests.exceptions.RequestException as e:
|
55 |
+
print(f"Failed to fetch branches: {e}")
|
56 |
+
return [] # Return an empty list in case of failure
|