11import os
22import requests
33
4- def main ():
5- repo = os .getenv ('REPO' )
6- cli_version = os .getenv ('CLI_VERSION' )
7- github_token = os .getenv ('GITHUB_TOKEN' )
4+ def trigger_github_workflow (org , repo , cli_version , github_token ):
5+ """
6+ Trigger a GitHub Actions workflow using the repository_dispatch event.
87
9- if not repo or not github_token :
10- raise ValueError ("REPO and GITHUB_TOKEN environment variables must be set." )
11-
12- api_url = f"https://api.github.com/repos/{ repo } /dispatches"
8+ :param org: GitHub organization or user name (e.g., 'Checkmarx')
9+ :param repo: Repository name (e.g., 'ast-github-action')
10+ :param cli_version: The CLI version to be dispatched
11+ :param github_token: Personal Access Token (PAT) with appropriate permissions
12+ """
13+ url = f'https://api.github.com/repos/{ org } /{ repo } /dispatches'
1314 headers = {
14- 'Accept' : 'application/vnd.github+json' ,
15+ 'Accept' : 'application/vnd.github.v3 +json' ,
1516 'Authorization' : f'token { github_token } '
1617 }
1718 payload = {
@@ -21,11 +22,20 @@ def main():
2122 }
2223 }
2324
24- response = requests .post (api_url , headers = headers , json = payload )
25+ response = requests .post (url , json = payload , headers = headers )
2526 if response .status_code == 204 :
26- print (f"Successfully dispatched event to { repo } " )
27+ print (f'Success: Dispatched event to { org } / { repo } .' )
2728 else :
28- print (f"Failed to dispatch event to { repo } : { response .status_code } { response .text } " )
29+ print (f'Error: Failed to dispatch event to { org } /{ repo } . Status code: { response .status_code } ' )
30+ print (f'Response: { response .json ()} ' )
2931
3032if __name__ == "__main__" :
31- main ()
33+ GH_ORG = os .getenv ('GH_ORG' )
34+ GH_REPO = os .getenv ('GH_REPO' )
35+ CLI_VERSION = os .getenv ('CLI_VERSION' , '' )
36+ GITHUB_TOKEN = os .getenv ('GITHUB_TOKEN' )
37+
38+ if not GH_ORG or not GH_REPO or not GITHUB_TOKEN :
39+ raise ValueError ("GH_ORG, GH_REPO, and GITHUB_TOKEN environment variables must be set." )
40+
41+ trigger_github_workflow (GH_ORG , GH_REPO , CLI_VERSION , GITHUB_TOKEN )
0 commit comments