-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummy.py
126 lines (102 loc) · 4.67 KB
/
dummy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import time
from github import Github
import jwt
import requests
import structlog
from logconfig import configure_logging
from github.GithubException import GithubException
from datetime import datetime, timedelta
import sys
from pr_gen_service import PullRequestAutomationService
configure_logging()
logger = structlog.get_logger(__name__)
# GITHUB_REMOTE = "[email protected]:"
# ORIGIN = "origin"
# DEFAULT_BRANCHES = ["main", "master"]
class PullRequestForNewRepos(PullRequestAutomationService):
GITHUB_REMOTE = "[email protected]:"
ORIGIN = "origin"
DEFAULT_BRANCHES = ["main", "master"]
def __init__(self):
logger.info("Start")
self.org_name = "signavio"
self.app_id_value = sys.argv[1]
self.private_key_path_value = sys.argv[2]
self.installation_id_value = sys.argv[3]
# self.access_token = self.authenticate_github()
super().__init__(False)
self.token = self.create_access_token()
self.tokens = Github(self.token)
self.org_name = "signavio"
self.org = self.tokens.get_organization(self.org_name)
# self.token = self.authenticate_github()
self.git_commit_msg = "Added GitHub action for mirroring automation required for SAP compliance."
self.git_pr_title = "CloudOS Managed Services: applying git-mirror automation required for SAP compliance."
self.git_pr_test = "No action needed."
self.branch_name = "parvathy/" + "SIGMANSER-1234" + "_gitMirror"
self.tmp_dir = "/tmp/repo_clone/"
self.file_to_sync = ".github/workflows/git_mirror.yaml"
self.dir_to_sync = ".github"
logger.info("Done")
# def authenticate_github(self):
# try:
# token = self.create_access_token()
# self.github_instance = Github(token)
# self.org = self.github_instance.get_organization(self.org_name)
# return token
# except GithubException as e:
# logger.error(f"GitHub authentication error: {e}")
# raise
def create_access_token(self):
payload = {
# Issued at time
'iat': int(time.time()),
# JWT expiration time (10 minutes maximum)
'exp': int(time.time()) + 600,
# GitHub App's identifier
'iss': self.app_id_value
}
encoded_jwt = jwt.encode(payload, self.private_key_path_value, algorithm='RS256')
response = requests.post(
f"https://api.github.com/app/installations/{self.installation_id_value}/access_tokens",
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {encoded_jwt}",
},
timeout=60,
)
logger.info("Access token created successfully")
return response.json()["token"]
def create_prs_in_batches(self):
"""Creates PRs for repositories created within the last 30 days.
It pushes the files to be delivered as per the configuration env var files.
"""
cutoff_date = datetime.now() - timedelta(days=30)
# repocount_tracker = 0
# repo_within_30_days = []
# total_repos = len(repo_within_30_days)
logger.info(f"Filtering repositories in org: {self.org} by creation time asc and creating PRs.")
print(type(self.org))
for repo in self.org.get_repos(direction="desc", sort="created", type="all"):
if repo.name == "Manser-repo-trigger-prgen":
creation_date = repo.created_at.replace(tzinfo=None)
if creation_date >= cutoff_date:
# repocount_tracker += 1
# repo_within_30_days.extend(repo.name)
try:
self.base_branch_name = repo.default_branch
# git_link = f"[email protected]:{self.org_name}/{repo.name}.git"
git_link = f"https://x-access-token:{self.token}@github.com/{self.org_name}/{repo.name}.git"
self.set_gitlink_n_repopath(repo.name, git_link)
self.clone_repository(repo.name)
self.commit_and_push(repo.name)
self.create_pr(repo)
except GithubException as e:
raise e
# Check if all PRs are done for all repositories in the organization
# self.check_if_all_prs_done(repocount_tracker, total_repos)
if __name__ == "__main__":
logger.info("Starting pull request creation for Managed Services GitHub mirror automation...")
pr_service = PullRequestForNewRepos()
pr_service.create_prs_in_batches()
logger.info('Successfuly completed PR generation for this run..🎉🎉🎉 ')