/github-api

This shell script interacts with the GitHub API to list users who have read access to a specified repository. It fetches collaborator information using GitHub API authentication and filters out users with read permissions using jq for JSON parsing.

Primary LanguageShell

This shell script is designed to interact with the GitHub API to list users who have read access to a specified repository. By providing the repository owner and repository name as command-line arguments, the script fetches collaborator information and filters out users who have read permissions. This is achieved using GitHub API authentication and jq for JSON parsing, ensuring that only relevant user data is displayed.

The script starts by defining the GitHub API URL and credentials, which include the username and personal access token. These credentials are necessary to authenticate API requests and retrieve data from private repositories. The user and repository information are passed as arguments to the script, allowing flexibility and reusability for different repositories.

The core functionality is implemented in two functions:github_api_get and list_users_with_read_access. The github_api_get function constructs the API endpoint URL and sends a GET request using curl, with authentication headers included. This function simplifies the process of making authenticated requests to various GitHub API endpoints.

The list_users_with_read_access function focuses on fetching and processing the list of collaborators. It constructs the API endpoint for retrieving collaborators and calls github_api_get to get the data. The response is then piped to jq, which filters and extracts the usernames of collaborators who have read (pull) access to the repository. The filtered list of usernames is displayed, or a message indicating no users with read access is shown if the list is empty.

The script concludes with the main section, which calls the list_users_with_read_access function and displays the results. This modular approach ensures that the script is easy to maintain and extend. By leveraging GitHub API and command-line tools like curl and jq, the script provides a powerful yet straightforward solution for managing repository access permissions.