-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1st_version of the github_auto_add #235
Open
SeverusYixin
wants to merge
10
commits into
main
Choose a base branch
from
auto_add_github_resources
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
881106e
1st_version of the github_auto_add
SeverusYixin fe84604
Remove the test example from the local page
SeverusYixin 2bac904
error 403, still not be solve
SeverusYixin 499050b
1.1 version, it works
SeverusYixin 49ccc5e
1.2version,Updating the output sequence
SeverusYixin f0a5956
1.3 version, update to return full name if available
SeverusYixin 6e41ca0
update git-bob config
haesleinhuepf eb36950
Revert "update git-bob config"
haesleinhuepf 27242ed
No updates, clean code redundancy
SeverusYixin b4d9577
Merge branch 'auto_add_github_resources' of https://github.com/NFDI4B…
SeverusYixin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import sys | ||
import os | ||
from _github_utilities import create_branch, get_file_in_repository, get_issue_body, write_file, send_pull_request | ||
import yaml | ||
from github import Github, GithubException | ||
|
||
def main(): | ||
""" | ||
Main function to retrieve metadata from a GitHub repository and append it to a YAML file. | ||
Creates a pull request with the updated data. | ||
|
||
Arguments: | ||
- repository (str): GitHub repository name. | ||
- issue (int): Issue number containing the GitHub link. | ||
|
||
Example(local_test): | ||
python auto_add_github_entry.py "username/repo" 123 | ||
python auto-add-github-resources.py "nfdi4bioimage/training" 236 | ||
""" | ||
# Retrieve repository and issue number from command-line arguments | ||
repository = sys.argv[1] | ||
issue = int(sys.argv[2]) | ||
|
||
yml_filename = "resources/nfdi4bioimage.yml" | ||
|
||
# Get the issue body, which should contain a GitHub repository link | ||
issue_text = get_issue_body(repository, issue) | ||
if "\n" in issue_text or not issue_text.startswith("https://github.com/"): | ||
print(issue_text, " is not a GitHub repository link. Exiting.") | ||
return | ||
|
||
github_repo_url = issue_text | ||
|
||
# Retrieve GitHub metadata | ||
github_data_dict = complete_github_data(github_repo_url) | ||
github_yml = "\n- " + yaml.dump(github_data_dict).replace("\n", "\n ") | ||
|
||
# Read the existing YAML "database" | ||
branch = create_branch(repository) | ||
file_content = get_file_in_repository(repository, branch, yml_filename).decoded_content.decode() | ||
|
||
print("YAML file content length:", len(file_content)) | ||
|
||
# Add the new entry | ||
file_content += github_yml | ||
|
||
# Save the updated content back to GitHub and create a pull request | ||
write_file(repository, branch, yml_filename, file_content, "Add " + github_repo_url) | ||
res = send_pull_request(repository, branch, "Add " + github_repo_url, f"closes #{issue}") | ||
|
||
print("Done.", res) | ||
|
||
def complete_github_data(github_repo_url): | ||
""" | ||
Retrieves metadata from the specified GitHub repository with checks for missing fields. | ||
|
||
Arguments: | ||
- github_repo_url (str): The URL of the GitHub repository to retrieve metadata from. | ||
|
||
Returns: | ||
- entry (dict): A dictionary containing the repository metadata in the correct order. | ||
""" | ||
token = os.getenv("GITHUB_API_KEY") | ||
if not token: | ||
raise Exception("GitHub API key not found. Please set GITHUB_API_KEY.") | ||
|
||
g = Github(token) | ||
repo_path = github_repo_url.replace("https://github.com/", "") | ||
repo = g.get_repo(repo_path) | ||
|
||
entry = {} | ||
|
||
# Contributors (Full name if available, otherwise username) | ||
try: | ||
contributors = repo.get_contributors() | ||
if contributors.totalCount > 0: | ||
# Use the contributor's full name if available, otherwise use the username | ||
entry['author'] = ", ".join([contrib.name if contrib.name else contrib.login for contrib in contributors]) | ||
else: | ||
entry['author'] = "" | ||
except GithubException as e: | ||
if e.status == 403: | ||
print(f"403 error: Cannot access contributors for {repo.full_name}. Skipping this step.") | ||
entry['author'] = "Contributors not accessible" | ||
else: | ||
raise e | ||
|
||
# Repository description | ||
entry['description'] = repo.description if repo.description else "" | ||
|
||
# License information (stored as "license") | ||
try: | ||
license_info = repo.get_license() | ||
entry['license'] = license_info.license.name if license_info else "" | ||
except: | ||
entry['license'] = "" | ||
|
||
# Repository name | ||
entry['name'] = repo.name if repo.name else "" | ||
|
||
# Publication date (first release date or creation date) | ||
entry['publication_date'] = get_publication_date(repo) | ||
|
||
# Tags (topics): should we use the repository topics or the README.md file? | ||
topics = repo.get_topics() | ||
entry['tags'] = ", ".join(topics) if topics else "" | ||
|
||
# Type: how should we determine this for a GitHub repository? | ||
entry['type'] = "" | ||
|
||
# Repository URL | ||
entry['url'] = github_repo_url | ||
|
||
return entry | ||
|
||
def get_publication_date(repo): | ||
""" | ||
Retrieves the publication date of the repository. If the repository has releases, | ||
the date of the first release is used. If no releases are found, the repository's | ||
creation date is used as the publication date. | ||
|
||
Arguments: | ||
- repo (github.Repository.Repository): The GitHub repository object. | ||
|
||
Returns: | ||
- publication_date (str): The ISO-formatted publication date. | ||
""" | ||
releases = repo.get_releases() | ||
if releases.totalCount > 0: | ||
first_release = releases[0] | ||
return first_release.created_at.isoformat() | ||
else: | ||
return repo.created_at.isoformat() if repo.created_at else "" | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I presume this is correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have deleted it. I wanted to test it locally or on my github, but it seems I'm not allowed to.