diff --git a/.github/workflows/search_files.yml b/.github/workflows/search_files.yml new file mode 100644 index 0000000..a4dc20b --- /dev/null +++ b/.github/workflows/search_files.yml @@ -0,0 +1,22 @@ +name: Basic Test + +on: + push: + branches: [ "main" ] + +jobs: + build: + runs-on: ubuntu-latest + env: + TOKEN: ${{ secrets.READ_CONTENT }} + ORGANIZATION: ${{ secrets.ORGANIZATION }} + WEBHOOK: ${{ secrets.SLACK_WEBHOOK }} + + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: | + pip install -r requirements.txt + - name: Test the basic usage + run: | + python3 scripts/search_files.py --org $ORGANIZATION --token $TOKEN | python3 scripts/slack_webhook.py --webhook $WEBHOOK \ No newline at end of file diff --git a/scripts/search_files.py b/scripts/search_files.py new file mode 100644 index 0000000..3640670 --- /dev/null +++ b/scripts/search_files.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 + +import re +import requests +import argparse + +def repositories(org, token): + response = requests.get( + f'https://api.github.com/orgs/{org}/repos', + headers = {'Authorization': f'Bearer {token}'} + ) + + if response.status_code == 200: + repos = [f'{org}/{repo["name"]}' for repo in response.json()] + return repos + + return False + +def search_file(repository, token): + response = requests.get( + f'https://api.github.com/repos/{repository}/contents/.github/dependabot.yaml', + headers = {'Authorization': f'Bearer {token}'} + ) + + if response.status_code == 404: + return (f'The dependabot.yml file was not found in this repository: https://github.com/{repository}') + + return False + +if __name__ == '__main__': + parse = argparse.ArgumentParser(description='Search for specific files in the repositories of an organization.') + parse.add_argument('--org', help='Specify the name of the organization\n', required=True) + parse.add_argument('--token', help='Set the Github Token to use during actions.', required=True) + args = parse.parse_args() + + if args.org: + repos = repositories(args.org, args.token) + + for repository in repos: + dependabot = search_file(repository, args.token) + + print (dependabot) if dependabot else None \ No newline at end of file