1111import os
1212import shutil
1313import tempfile
14+ import textwrap
1415from datetime import datetime
1516from pathlib import Path
1617from urllib .parse import urlparse
2021from django .core .management .base import CommandError
2122from git import Repo
2223
24+ from vulnerablecode .settings import ALLOWED_HOSTS
25+ from vulnerablecode .settings import VULNERABLECODE_VERSION
26+
2327logger = logging .getLogger (__name__ )
2428
2529
2630class Command (BaseCommand ):
27- help = """Commit the exported vulnerability and package in backing git repository"""
31+ help = """Commit the exported vulnerability data in the backing GitHub repository.
32+
33+ This command takes the path to the exported vulnerability data and creates a pull
34+ request in the backing GitHub repository with the changes.
35+ """
2836
2937 def add_arguments (self , parser ):
3038 parser .add_argument (
@@ -37,49 +45,74 @@ def handle(self, *args, **options):
3745 base_path = Path (path )
3846
3947 if not path or not base_path .is_dir ():
40- raise CommandError ("Enter a valid directory path" )
41-
42- export_repo_url = os .environ .get ("VULNERABLECODE_EXPORT_REPO_URL" , None )
43- github_service_token = os .environ .get ("GITHUB_SERVICE_TOKEN" , None )
44- github_service_name = os .environ .get ("GITHUB_SERVICE_NAME" , None )
45- github_service_email = os .environ .get ("GITHUB_SERVICE_EMAIL" , None )
48+ raise CommandError ("Enter a valid directory path to the exported data." )
49+
50+ vcio_export_repo_url = os .environ .get ("VULNERABLECODE_EXPORT_REPO_URL" )
51+ vcio_github_service_token = os .environ .get ("VULNERABLECODE_GITHUB_SERVICE_TOKEN" )
52+ vcio_github_service_name = os .environ .get ("VULNERABLECODE_GITHUB_SERVICE_NAME" )
53+ vcio_github_service_email = os .environ .get ("VULNERABLECODE_GITHUB_SERVICE_EMAIL" )
54+
55+ # Check for missing environment variables
56+ missing_vars = []
57+ if not vcio_export_repo_url :
58+ missing_vars .append ("VULNERABLECODE_EXPORT_REPO_URL" )
59+ if not vcio_github_service_token :
60+ missing_vars .append ("VULNERABLECODE_GITHUB_SERVICE_TOKEN" )
61+ if not vcio_github_service_name :
62+ missing_vars .append ("VULNERABLECODE_GITHUB_SERVICE_NAME" )
63+ if not vcio_github_service_email :
64+ missing_vars .append ("VULNERABLECODE_GITHUB_SERVICE_EMAIL" )
65+
66+ if missing_vars :
67+ raise CommandError (f'Missing environment variables: { ", " .join (missing_vars )} ' )
4668
4769 local_dir = tempfile .mkdtemp ()
4870 current_date = datetime .now ().strftime ("%Y-%m-%d" )
4971
5072 branch_name = f"export-update-{ current_date } "
51- commit_message = f"Update package and vulnerability data\n Signed-off-by: { github_service_name } <{ github_service_email } >"
52- pr_title = "Update package and vulnerability"
53- pr_body = ""
73+ pr_title = "Update package vulnerabilities from VulnerableCode"
74+ pr_body = f"""\
75+ Tool: pkg:github/aboutcode-org/vulnerablecode@v{ VULNERABLECODE_VERSION }
76+ Reference: https://{ ALLOWED_HOSTS [0 ]} /
77+ """
78+ commit_message = f"""\
79+ Update package vulnerabilities from VulnerableCode
5480
55- self .stdout .write ("Committing vulnerablecode Package and Vulnerability data." )
81+ Tool: pkg:github/aboutcode-org/vulnerablecode@v{ VULNERABLECODE_VERSION }
82+ Reference: https://{ ALLOWED_HOSTS [0 ]} /
83+
84+ Signed-off-by: { vcio_github_service_name } <{ vcio_github_service_email } >
85+ """
86+
87+ self .stdout .write ("Committing VulnerableCode package and vulnerability data." )
5688 repo = self .clone_repository (
57- repo_url = export_repo_url ,
89+ repo_url = vcio_export_repo_url ,
5890 local_path = local_dir ,
59- token = github_service_token ,
91+ token = vcio_github_service_token ,
6092 )
6193
62- repo .config_writer ().set_value ("user" , "name" , github_service_name ).release ()
63- repo .config_writer ().set_value ("user" , "email" , github_service_email ).release ()
94+ repo .config_writer ().set_value ("user" , "name" , vcio_github_service_name ).release ()
95+ repo .config_writer ().set_value ("user" , "email" , vcio_github_service_email ).release ()
6496
6597 self .add_changes (repo = repo , content_path = path )
6698
6799 if self .commit_and_push_changes (
68100 repo = repo ,
69101 branch = branch_name ,
70- commit_message = commit_message ,
102+ commit_message = textwrap . dedent ( commit_message ) ,
71103 ):
72104 self .create_pull_request (
73- repo_url = export_repo_url ,
105+ repo_url = vcio_export_repo_url ,
74106 branch = branch_name ,
75107 title = pr_title ,
76- body = pr_body ,
77- token = github_service_token ,
108+ body = textwrap . dedent ( pr_body ) ,
109+ token = vcio_github_service_token ,
78110 )
79111 shutil .rmtree (local_dir )
80112
81113 def clone_repository (self , repo_url , local_path , token ):
82114 """Clone repository to local_path."""
115+
83116 if os .path .exists (local_path ):
84117 shutil .rmtree (local_path )
85118
0 commit comments