Skip to content

Commit

Permalink
Merge pull request #35 from aarontorres0/main
Browse files Browse the repository at this point in the history
Fetch latest internship repo
  • Loading branch information
DavidSalazar123 authored Nov 20, 2024
2 parents 304a323 + 9ebe48c commit 06b551a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
python3 -m pytest tests/
32 changes: 17 additions & 15 deletions src/DiscordBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import discord
import redis

from DatabaseConnector import DatabaseConnector
from discord.ext import commands, tasks
from dotenv import load_dotenv

from GitHubUtilities import GitHubUtilities
from JobsUtilities import JobsUtilities

Expand Down Expand Up @@ -71,21 +71,25 @@ async def health_check_task():


@tasks.loop(seconds=60)
async def scheduled_task(
internship_github: GitHubUtilities, newgrad_github: GitHubUtilities, job_utilities: JobsUtilities
):
async def scheduled_task(job_utilities: JobsUtilities):
"""
A scheduled task that runs every 60 seconds to check for new commits in the GitHub repository.
Parameters:
- internship_github: Internship object
- newgrad_github: New Grad GitHubUtilities object
- job_utilities: The JobsUtilities object
"""
async with lock:
try:
start_time = datetime.now()

latest_repo = JobsUtilities.get_latest_internship_repo()
logger.info(f"Using latest internship repository: {latest_repo}")

internship_github = GitHubUtilities(
token=GITHUB_TOKEN, repo_name=f"SimplifyJobs/{latest_repo}", isSummer=True, isCoop=True
)
newgrad_github = GitHubUtilities(token=GITHUB_TOKEN, repo_name="SimplifyJobs/New-Grad-Positions")

internship_repo = internship_github.createGitHubConnection()
internship_sha = internship_github.getSavedSha(internship_repo, False)
newgrad_repo = newgrad_github.createGitHubConnection()
Expand Down Expand Up @@ -120,7 +124,7 @@ async def scheduled_task(

logger.info("All internship jobs have been posted!")

# Process all new gradjobs
# Process all new grad jobs
if newgrad_github.isNewCommit(newgrad_repo, newgrad_sha):
logger.info("New grad commit has been found. Finding new jobs...")
newgrad_github.setComparison(newgrad_repo, True)
Expand Down Expand Up @@ -205,14 +209,12 @@ async def on_ready():
Event that is triggered when the bot is ready to start sending messages.
"""
logger.info(f"Logged in as {bot.user.name}")

github_internship = GitHubUtilities(
token=GITHUB_TOKEN, repo_name="SimplifyJobs/Summer2025-Internships", isSummer=True, isCoop=True
)
github_newgrad = GitHubUtilities(token=GITHUB_TOKEN, repo_name="SimplifyJobs/New-Grad-Positions")
job_utilities = JobsUtilities()
scheduled_task.start(github_internship, github_newgrad, job_utilities) # Start the loop
health_check_task.start() # Start health check task
try:
job_utilities = JobsUtilities()
scheduled_task.start(job_utilities) # Start the loop
health_check_task.start() # Start health check task
except Exception as e:
logger.error(f"Failed to start scheduled tasks: {e}", exc_info=True)


if __name__ == "__main__":
Expand Down
51 changes: 50 additions & 1 deletion src/JobsUtilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@

import asyncio
import logging
import os
import re
import redis
from collections.abc import Iterable
from datetime import datetime, timedelta

import discord
import redis
from dotenv import load_dotenv
from github import Github, GithubException

load_dotenv()

GITHUB_TOKEN = os.getenv("GIT_TOKEN")

class JobsUtilities:
NOT_US = ["canada", "uk", "united kingdom", "eu"]
latest_cached_repo = None

def __init__(self):
self.previous_job_title = ""
Expand Down Expand Up @@ -195,3 +202,45 @@ async def getJobs(
except Exception as e:
logging.exception("Failed to process job posting: %s\nJob: %s", e, job)
continue

@staticmethod
def get_cached_latest_repo():
return JobsUtilities.latest_cached_repo

@staticmethod
def set_cached_latest_repo(repo_name):
JobsUtilities.latest_cached_repo = repo_name

@staticmethod
def get_latest_internship_repo():

# Try to use the cached repository first
cached_repo = JobsUtilities.get_cached_latest_repo()
if cached_repo:
try:
if org.get_repo(cached_repo):
return cached_repo
except GithubException as e:
logging.warning(f"Cached repo '{cached_repo}' not valid: {e}")

# Fallback if cached repository is invalid
g = Github(GITHUB_TOKEN)
org = g.get_organization("SimplifyJobs")
repos = org.get_repos()

matching_repos = []
for repo in repos:
if repo.name.startswith("Summer"):
suffix = repo.name[len("Summer"):]
year = suffix.split("-")[0]
if len(year) == 4 and year.isdigit():
matching_repos.append(repo.name)

if not matching_repos:
raise ValueError(
"No repositories matching the pattern 'SummerYYYY-Internships' were found in the organization SimplifyJobs. Make sure the naming format hasn't changed and that your GitHub token has the necessary permissions."
)

latest_repo = max(matching_repos)
JobsUtilities.set_cached_latest_repo(latest_repo)
return latest_repo
4 changes: 2 additions & 2 deletions tests/test_GitHub.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def test_create_github_connection(mock_getenv):
@patch("github.Repository.Repository")
def test_get_last_commit(mock_repo):
utilities = GitHubUtilities("token", "repo")
mock_branch = MagicMock() # Fake a respository
mock_branch = MagicMock()
mock_branch.commit.sha = "123abc"
mock_repo.get_branches.return_value = [mock_branch]
mock_repo.get_branch.return_value = mock_branch
result = utilities.getLastCommit(mock_repo)
assert result == "123abc"

Expand Down
20 changes: 9 additions & 11 deletions tests/test_Internship.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import pytest

# How to test the code
# 1) Run the cmd: pytest tests/test_Internship.py
# To test the code run cmd: make test

with patch.dict(
"sys.modules",
Expand Down Expand Up @@ -48,7 +47,8 @@ def test_save_company_name():
async def test_valid_job_posting():
# Directly create the mock bot
mock_bot = MagicMock()
mock_bot.get_channel = MagicMock(return_value=AsyncMock())
mock_channel = AsyncMock()
mock_bot.get_channel.return_value = mock_channel

channels = [123456789, 987654321]
job = """
Expand All @@ -59,23 +59,21 @@ async def test_valid_job_posting():
<img src="https://i.imgur.com/aVnQdox.png" width="30" alt="Simplify"></a>
| Feb 05 |
"""
job_postings = [job]
current_date = datetime(2024, 1, 8)
job_postings = [job]
redis_mock = MagicMock()
redis_mock.exists.return_value = False

# Create an instance of your class
instance = JobsUtilities()
instance.saveCompanyName = MagicMock()
instance.isWithinDateRange = MagicMock(return_value=True)
instance.previous_job_title = ""
instance.job_cache = set()
instance.total_jobs = 0

# Use 'with patch' to mock any external dependencies if needed
with patch("discord.ext.commands.Bot", new=mock_bot):
# Execute the method
await instance.getInternships(mock_bot, channels, job_postings, current_date, True)
# Act
await instance.getJobs(mock_bot, redis_mock, channels, job_postings, "Summer")

# Assertions to verify the behavior
# Assert
assert len(instance.job_cache) == 1 # Ensure the job link was added to the cache
assert instance.total_jobs == 1 # Ensure the job count was incremented
mock_bot.get_channel.assert_called() # Ensure get_channel was called for each channel

0 comments on commit 06b551a

Please sign in to comment.