How HelloGitHub Bot Filtering Logic Prevents Self-Project Inclusion in Reports
The HelloGitHub bot filters out its own repositories by checking if the configured ACCOUNT['username'] appears in the repository name within the check_condition function, ensuring only external projects appear in generated reports.
The 521xueweihan/HelloGitHub repository operates an automated bot that monitors GitHub Events API data to curate trending projects for its monthly newsletter. To maintain editorial integrity and avoid self-promotion, the bot implements a specific filtering mechanism that excludes stars received on its own repositories from appearing in the final reports.
Core Filtering Mechanism in check_condition
The primary filtering logic resides in the check_condition function within script/github_bot/github_bot.py. This function evaluates every event retrieved from the GitHub Events API against three strict criteria before inclusion in the report dataset.
Event Type and Date Validation
First, the function verifies that the event is a WatchEvent (GitHub's internal name for star events) and that it occurred within the configured time window. The bot converts UTC timestamps to local time and compares them against the DAY configuration variable:
create_time = datetime.datetime.strptime(
data['created_at'], "%Y-%m-%dT%H:%M:%SZ") + datetime.timedelta(hours=8)
date_condition = create_time >= (datetime.datetime.now()
- datetime.timedelta(days=DAY))
Only events satisfying both the type requirement (WatchEvent) and the recency condition proceed to the next validation layer.
Star Action Verification
Second, the function inspects the event payload to confirm the action represents a new star rather than an unstar event. The bot specifically checks for the started action within the payload data:
if data['payload']['action'] == 'started':
# Proceed to ownership check
This ensures the bot only counts positive star interactions, filtering out noise from users who unstar and restar repositories repeatedly.
Self-Project Exclusion Logic
Third, and most critical for preventing self-reporting, the bot compares the configured GitHub username against the repository identifier. The check_condition function contains the decisive guard clause at lines 113-116:
if ACCOUNT['username'] not in data['repo']['name']:
data['date_time'] = create_time.strftime("%Y-%m-%d %H:%M:%S")
return True
The ACCOUNT['username'] variable is loaded from the bot's configuration dictionary. When this username appears anywhere in the repository name (formatted as owner/repo), the function returns False, effectively dropping the event from consideration. This simple string containment check ensures that stars on the bot's own projects never reach the reporting stage.
Integration with the Data Processing Pipeline
The check_condition function operates within a larger data processing pipeline defined in the analyze function (lines 118-127 of the same file). This function iterates through all JSON events retrieved from the GitHub Events API and applies the filtering logic:
def analyze(json_data):
"""
分析获取的数据
:return 符合过滤条件的数据
"""
result_data = []
for fi_data in json_data:
if check_condition(fi_data): # <-- Self-project filter applied here
result_data.append(fi_data)
return result_data
Only events passing the check_condition filter are appended to result_data. This filtered list then feeds into get_stars and ultimately make_content, which generates the markdown files for the monthly newsletter. Because the self-project exclusion happens at this early filtering stage, the downstream reporting functions remain unaware of any stars on the bot's own repositories.
Configuration Requirements
For the self-project filtering to function correctly, the bot requires proper configuration of the ACCOUNT dictionary, typically defined in the script's configuration section or imported from environment variables:
ACCOUNT = {
'username': '521xueweihan', # Must match the GitHub username exactly
# ... other account settings
}
DAY = 7 # Number of days to look back for events
The username value must exactly match the GitHub username that owns the repositories to be excluded. The filtering uses simple string containment (not in), so partial matches would also trigger exclusion, though the standard format owner/repo makes exact username matching the reliable approach.
Summary
The HelloGitHub bot prevents self-project inclusion through a multi-layered filtering approach:
- Event validation: Only recent
WatchEventtypes withstartedactions are considered - Username exclusion: The
check_conditionfunction explicitly checks thatACCOUNT['username']does not appear indata['repo']['name'] - Pipeline integration: Filtered results flow through
analyzetoget_stars, ensuring excluded events never reach report generation - Configuration-driven: The exclusion logic relies on the
ACCOUNT['username']configuration variable to identify which repositories to skip
Frequently Asked Questions
How does the bot identify which repositories belong to itself?
The bot identifies its own repositories by comparing the configured ACCOUNT['username'] against the repository name field in the GitHub Events API payload. If the username string appears within the repository identifier (formatted as owner/repo), the event is discarded immediately within the check_condition function.
What happens if the username configuration is incorrect?
If the ACCOUNT['username'] value does not match the actual GitHub username owning the bot's repositories, the self-exclusion logic fails. Consequently, stars on the bot's own projects would pass through the filter and appear in the generated reports, effectively allowing the bot to promote its own repositories inadvertently.
Can the filtering logic exclude multiple user accounts?
The current implementation in script/github_bot/github_bot.py uses a single string containment check against ACCOUNT['username']. To exclude multiple accounts, you would need to modify the check_condition function to iterate through a list of excluded usernames or use a more complex matching logic, as the current code only supports a single configured username.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →