FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
github-commit-bot/commit_bot.py at main · varshithdupati/github-commit-bot · GitHub
varshithdupati
/
github-commit-bot
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
3
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
github-commit-bot
/
commit_bot.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
158 lines (131 loc) · 5.44 KB
Breadcrumbs
github-commit-bot
/
commit_bot.py
Copy path
File metadata and controls
executable file
·
158 lines (131 loc) · 5.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
import
os
import
random
import
datetime
import
time
import
schedule
import
subprocess
import
logging
import
logging
.
handlers
import
boto3
from
botocore
.
exceptions
import
BotoCoreError
,
ClientError
# --------------------------
# Configuration
# --------------------------
REPO_PATH
=
"/home/ubuntu/github-commit-bot"
MIN_COMMITS_RANDOM_DAY
=
6
MAX_COMMITS_RANDOM_DAY
=
24
COMMIT_FILE_NAME
=
"commit-log.txt"
# SNS Settings
SNS_REGION
=
"us-east-1"
SNS_TOPIC_ARN
=
"arn:aws:sns:us-east-1:266735828641:github-commit-bot-alerts"
# Logging Config
LOG_FILE
=
"/home/ubuntu/github-commit-bot/commit-bot.log"
# Globals
current_random_day
=
None
current_week_number
=
None
# Set up rotating file logging
logger
=
logging
.
getLogger
(
"CommitBot"
)
logger
.
setLevel
(
logging
.
INFO
)
handler
=
logging
.
handlers
.
RotatingFileHandler
(
LOG_FILE
,
maxBytes
=
5_000_000
,
backupCount
=
3
)
formatter
=
logging
.
Formatter
(
"%(asctime)s [%(levelname)s] %(name)s - %(message)s"
)
handler
.
setFormatter
(
formatter
)
logger
.
addHandler
(
handler
)
def
send_sns_alert
(
subject
,
message
):
"""
Publish a message to the SNS topic
(only called if a fatal error occurs).
"""
try
:
sns_client
=
boto3
.
client
(
"sns"
,
region_name
=
SNS_REGION
)
sns_client
.
publish
(
TopicArn
=
SNS_TOPIC_ARN
,
Subject
=
subject
,
Message
=
message
)
logger
.
info
(
"SNS alert sent: %s"
,
subject
)
except
(
BotoCoreError
,
ClientError
)
as
e
:
logger
.
error
(
"Failed to send SNS alert: %s"
,
e
)
def
get_current_week_number
():
"""Return the ISO week number of today's date."""
return
datetime
.
date
.
today
().
isocalendar
()[
1
]
def
pick_new_random_day
():
"""Pick a new random day for multiple commits (0=Mon,...,6=Sun)."""
return
random
.
randint
(
0
,
6
)
def
do_commit_action
(
commit_message
):
"""
Perform a commit and push to GitHub.
Raise an exception if something fails, so we can catch it and notify via SNS.
"""
timestamp
=
datetime
.
datetime
.
now
().
isoformat
()
file_path
=
os
.
path
.
join
(
REPO_PATH
,
COMMIT_FILE_NAME
)
# Append a line so there's always something to commit
with
open
(
file_path
,
"a"
)
as
f
:
f
.
write
(
f"
{
timestamp
}
-
{
commit_message
}
\n
"
)
logger
.
info
(
"Performing commit: %s"
,
commit_message
)
# Run git commands; check=True raises a CalledProcessError on failure
subprocess
.
run
([
"git"
,
"-C"
,
REPO_PATH
,
"add"
,
"commit-log.txt"
,
"commit-bot.log"
],
check
=
True
)
subprocess
.
run
([
"git"
,
"-C"
,
REPO_PATH
,
"commit"
,
"-m"
,
commit_message
],
check
=
True
)
subprocess
.
run
([
"git"
,
"-C"
,
REPO_PATH
,
"push"
],
check
=
True
)
logger
.
info
(
"Commit and push successful: %s"
,
commit_message
)
def
schedule_today_commits
():
"""
Determine if today is the multiple-commit day.
If so, schedule multiple commits at random times; otherwise, schedule one commit.
"""
global
current_random_day
today_weekday
=
datetime
.
datetime
.
today
().
weekday
()
# Monday=0, Sunday=6
schedule
.
clear
()
schedule
.
every
().
day
.
at
(
"00:01"
).
do
(
daily_reset
)
logger
.
info
(
"Schedule clear. today_weekday: %d"
,
today_weekday
)
if
today_weekday
==
current_random_day
:
number_of_commits
=
random
.
randint
(
MIN_COMMITS_RANDOM_DAY
,
MAX_COMMITS_RANDOM_DAY
)
logger
.
info
(
"Today is multi-commit day: scheduling %d commits."
,
number_of_commits
)
for
i
in
range
(
number_of_commits
):
hour
=
random
.
randint
(
0
,
23
)
minute
=
random
.
randint
(
0
,
59
)
schedule
.
every
().
day
.
at
(
f"
{
hour
:02d
}
:
{
minute
:02d
}
"
).
do
(
do_commit_action
,
commit_message
=
(
f"
{
datetime
.
datetime
.
now
().
isoformat
()
}
- Multiple commit #
{
i
+
1
}
"
)
)
else
:
logger
.
info
(
"Today is single-commit day: schedluing 1 commit."
)
hour
=
random
.
randint
(
0
,
23
)
minute
=
random
.
randint
(
0
,
59
)
schedule
.
every
().
day
.
at
(
f"
{
hour
:02d
}
:
{
minute
:02d
}
"
).
do
(
do_commit_action
,
commit_message
=
(
f"
{
datetime
.
datetime
.
now
().
isoformat
()
}
- Daily single commit"
)
)
def
daily_reset
():
"""
Runs once per day (00:01). Check if we have a new week; if so, pick a new multi-commit day.
Then schedule today's commits.
"""
global
current_random_day
,
current_week_number
new_week_number
=
get_current_week_number
()
if
current_week_number
is
None
or
new_week_number
!=
current_week_number
:
current_week_number
=
new_week_number
current_random_day
=
pick_new_random_day
()
logger
.
info
(
"New week. random_day set to %d, week_number: %d"
,
current_random_day
,
current_week_number
)
schedule_today_commits
()
def
main
():
logger
.
info
(
"Commit Bot starting up..."
)
try
:
global
current_random_day
,
current_week_number
# Initialize
current_week_number
=
get_current_week_number
()
current_random_day
=
pick_new_random_day
()
logger
.
info
(
"Initial random_day: %d, week_number: %d"
,
current_random_day
,
current_week_number
)
# Schedule a daily reset at 00:01
schedule
.
every
().
day
.
at
(
"00:01"
).
do
(
daily_reset
)
# Run an immediate reset in case script starts mid-day
daily_reset
()
# Main loop
while
True
:
schedule
.
run_pending
()
time
.
sleep
(
30
)
except
Exception
as
e
:
logger
.
error
(
"Unhandled exception: %s"
,
e
,
exc_info
=
True
)
# Send an alert ONLY on failure
send_sns_alert
(
"GitHub Commit Bot Failure"
,
f"Unhandled exception:
{
e
}
"
)
#raise # Re-raise so systemd (in a future step) can restart the service
return
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL