vsagar100 commited on
Commit
df513b0
·
1 Parent(s): 14ccba6

Added token, git repo download

Browse files
Files changed (2) hide show
  1. app.py +18 -8
  2. lib/code_reviewer.py +23 -1
app.py CHANGED
@@ -3,30 +3,40 @@
3
  # Import necessary modules from lib
4
  from lib.code_reviewer import CodeReviewer, ReviewManager
5
  import os
 
 
 
 
 
 
6
 
7
  def main():
8
  # Directory structure setup
9
- # Directory for storing input Ansible files
10
- input_directory = "input_files"
11
  # Directory for storing output JSON reviews
12
  output_directory = "output_reviews"
13
 
14
  # Ensure the directories exist
15
- os.makedirs(input_directory, exist_ok=True)
16
  os.makedirs(output_directory, exist_ok=True)
17
 
18
- # Paths for testing
19
- sample_files = [os.path.join(input_directory, "example1.yml"), os.path.join(input_directory, "example2.yml")]
20
- output_json_path = os.path.join(output_directory, "code_review_results.json")
21
-
22
  # Initialize the code reviewer and review manager
23
  code_reviewer = CodeReviewer()
24
  review_manager = ReviewManager(reviewer=code_reviewer)
25
 
 
 
 
 
 
 
 
26
  # Process files and generate reviews
27
- reviews = review_manager.process_files(sample_files)
28
 
29
  # Save reviews to JSON
 
30
  review_manager.save_reviews_to_json(reviews, output_json_path)
31
  print(f"Reviews saved to {output_json_path}")
32
 
 
3
  # Import necessary modules from lib
4
  from lib.code_reviewer import CodeReviewer, ReviewManager
5
  import os
6
+ import glob
7
+
8
+ # Variables for GitHub repository details
9
+ GITHUB_REPO_URL = "https://github.com/vsagar100/ansible_tf_provisioner"
10
+ GITHUB_BRANCH = "main" # Specify the branch or tag to download
11
+ GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") # Specify your GitHub token
12
 
13
  def main():
14
  # Directory structure setup
15
+ # Directory for storing downloaded GitHub repository
16
+ download_directory = "downloaded_repo"
17
  # Directory for storing output JSON reviews
18
  output_directory = "output_reviews"
19
 
20
  # Ensure the directories exist
21
+ os.makedirs(download_directory, exist_ok=True)
22
  os.makedirs(output_directory, exist_ok=True)
23
 
 
 
 
 
24
  # Initialize the code reviewer and review manager
25
  code_reviewer = CodeReviewer()
26
  review_manager = ReviewManager(reviewer=code_reviewer)
27
 
28
+ # Download GitHub repository
29
+ review_manager.download_repo(GITHUB_REPO_URL, GITHUB_BRANCH, GITHUB_TOKEN, download_directory)
30
+
31
+ # Find all YAML files in the downloaded repository
32
+ yaml_files = glob.glob(os.path.join(download_directory, "**", "*.yml"), recursive=True)
33
+ yaml_files += glob.glob(os.path.join(download_directory, "**", "*.yaml"), recursive=True)
34
+
35
  # Process files and generate reviews
36
+ reviews = review_manager.process_files(yaml_files)
37
 
38
  # Save reviews to JSON
39
+ output_json_path = os.path.join(output_directory, "code_review_results.json")
40
  review_manager.save_reviews_to_json(reviews, output_json_path)
41
  print(f"Reviews saved to {output_json_path}")
42
 
lib/code_reviewer.py CHANGED
@@ -5,6 +5,9 @@ import os
5
  import json
6
  import torch
7
  from transformers import AutoModelForCausalLM, AutoTokenizer
 
 
 
8
 
9
  # Custom Imports
10
  from typing import List, Dict
@@ -30,7 +33,7 @@ class CodeReviewer:
30
  Returns:
31
  Dict: The code standards in dictionary form.
32
  """
33
- standards_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "standards", "ansible_ode_standards.json")
34
  with open(standards_path, 'r') as f:
35
  return json.load(f)
36
 
@@ -79,6 +82,25 @@ class ReviewManager:
79
  """
80
  self.reviewer = reviewer
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  def process_files(self, file_paths: List[str]) -> List[Dict[str, str]]:
83
  """
84
  Processes multiple files for review.
 
5
  import json
6
  import torch
7
  from transformers import AutoModelForCausalLM, AutoTokenizer
8
+ import requests
9
+ import zipfile
10
+ import io
11
 
12
  # Custom Imports
13
  from typing import List, Dict
 
33
  Returns:
34
  Dict: The code standards in dictionary form.
35
  """
36
+ standards_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), "standards", "code_standards.json")
37
  with open(standards_path, 'r') as f:
38
  return json.load(f)
39
 
 
82
  """
83
  self.reviewer = reviewer
84
 
85
+ def download_repo(self, repo_url: str, branch: str, token: str, download_path: str):
86
+ """
87
+ Downloads a GitHub repository as a ZIP file and extracts it.
88
+
89
+ Args:
90
+ repo_url (str): The GitHub repository URL.
91
+ branch (str): The branch or tag to download.
92
+ token (str): The GitHub personal access token for authentication.
93
+ download_path (str): The path to extract the downloaded repository.
94
+ """
95
+ zip_url = f"{repo_url}/archive/refs/heads/{branch}.zip"
96
+ headers = {"Authorization": f"token {token}"}
97
+ response = requests.get(zip_url, headers=headers)
98
+ if response.status_code == 200:
99
+ with zipfile.ZipFile(io.BytesIO(response.content)) as zip_ref:
100
+ zip_ref.extractall(download_path)
101
+ else:
102
+ raise Exception(f"Failed to download repository. Status code: {response.status_code}")
103
+
104
  def process_files(self, file_paths: List[str]) -> List[Dict[str, str]]:
105
  """
106
  Processes multiple files for review.