Skip to content

Commit

Permalink
Merge pull request #247 from trz42/refactor_reading_job_metadata
Browse files Browse the repository at this point in the history
add function that reads PR section from job metadata and use it in job manager
  • Loading branch information
bedroge authored Feb 12, 2024
2 parents 91e39ff + ec2a317 commit 7d822c4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
26 changes: 4 additions & 22 deletions eessi_bot_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from connections import github
from tools import config, run_cmd
from tools.args import job_manager_parse
from tools.job_metadata import read_metadata_file
from tools.job_metadata import read_job_metadata_from_file, read_metadata_file
from tools.pr_comments import get_submitted_job_comment, update_comment


Expand Down Expand Up @@ -251,24 +251,6 @@ def determine_finished_jobs(self, known_jobs, current_jobs):

return finished_jobs

def read_job_pr_metadata(self, job_metadata_path):
"""
Read job metadata file and return the contents of the 'PR' section.
Args:
job_metadata_path (string): path to job metadata file
Returns:
(ConfigParser): instance of ConfigParser corresponding to the 'PR'
section or None
"""
# reuse function from module tools.job_metadata to read metadata file
metadata = read_metadata_file(job_metadata_path, self.logfile)
if metadata and "PR" in metadata:
return metadata["PR"]
else:
return None

def read_job_result(self, job_result_file_path):
"""
Read job result file and return the contents of the 'RESULT' section.
Expand Down Expand Up @@ -350,7 +332,7 @@ def process_new_job(self, new_job):

# assuming that a bot job's working directory contains a metadata
# file, its existence is used to check if the job belongs to the bot
metadata_pr = self.read_job_pr_metadata(job_metadata_path)
metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile)

if metadata_pr is None:
log(f"No metadata file found at {job_metadata_path} for job {job_id}, so skipping it",
Expand Down Expand Up @@ -446,7 +428,7 @@ def process_running_jobs(self, running_job):
job_metadata_path = os.path.join(job_dir, metadata_file)

# check if metadata file exist
metadata_pr = self.read_job_pr_metadata(job_metadata_path)
metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile)
if metadata_pr is None:
raise Exception("Unable to find metadata file")

Expand Down Expand Up @@ -591,7 +573,7 @@ def process_finished_job(self, finished_job):
# obtain id of PR comment to be updated (from file '_bot_jobID.metadata')
metadata_file = f"_bot_job{job_id}.metadata"
job_metadata_path = os.path.join(new_symlink, metadata_file)
metadata_pr = self.read_job_pr_metadata(job_metadata_path)
metadata_pr = read_job_metadata_from_file(job_metadata_path, self.logfile)
if metadata_pr is None:
raise Exception("Unable to find metadata file ... skip updating PR comment")

Expand Down
23 changes: 2 additions & 21 deletions tests/test_eessi_bot_job_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,16 @@
#
# license: GPLv2
#
import os

import shutil

from eessi_bot_job_manager import EESSIBotSoftwareLayerJobManager


def test_read_job_pr_metadata(tmpdir):
def test_determine_running_jobs():
# copy needed app.cfg from tests directory
shutil.copyfile("tests/test_app.cfg", "app.cfg")

# if metadata file does not exist, we should get None as return value
job_manager = EESSIBotSoftwareLayerJobManager()
path = os.path.join(tmpdir, 'test.metadata')
assert job_manager.read_job_pr_metadata(path) is None

with open(path, 'w') as fp:
fp.write('''[PR]
repo=test
pr_number=12345''')

metadata_pr = job_manager.read_job_pr_metadata(path)
expected = {
"repo": "test",
"pr_number": "12345",
}
assert metadata_pr == expected


def test_determine_running_jobs():
job_manager = EESSIBotSoftwareLayerJobManager()

assert job_manager.determine_running_jobs({}) == []
Expand Down
33 changes: 33 additions & 0 deletions tests/test_tools_job_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Tests for 'tools/job_metadata.py' of the EESSI build-and-deploy bot,
# see https://github.com/EESSI/eessi-bot-software-layer
#
# The bot helps with requests to add software installations to the
# EESSI software layer, see https://github.com/EESSI/software-layer
#
# author: Thomas Roeblitz (@trz42)
#
# license: GPLv2
#

import os

from tools.job_metadata import read_job_metadata_from_file


def test_read_job_metadata_from_file(tmpdir):
logfile = os.path.join(tmpdir, 'test_read_job_metadata_from_file.log')
# if metadata file does not exist, we should get None as return value
path = os.path.join(tmpdir, 'test.metadata')
assert read_job_metadata_from_file(path, logfile) is None

with open(path, 'w') as fp:
fp.write('''[PR]
repo=test
pr_number=12345''')

metadata_pr = read_job_metadata_from_file(path, logfile)
expected = {
"repo": "test",
"pr_number": "12345",
}
assert metadata_pr == expected
25 changes: 25 additions & 0 deletions tools/job_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,28 @@ def read_metadata_file(metadata_path, log_file=None):
else:
log(f"No metadata file found at {metadata_path}.", log_file)
return None


def read_job_metadata_from_file(filepath, log_file=None):
"""
Read job metadata from file
Args:
filepath (string): path to job metadata file
log_file (string): path to log file
Returns:
job_metadata (dict): dictionary containing job metadata or None
"""

metadata = read_metadata_file(filepath, log_file=log_file)
if metadata:
# get PR section
if "PR" in metadata:
metadata_pr = metadata["PR"]
else:
metadata_pr = {}
return metadata_pr
else:
log(f"Metadata file '{filepath}' does not exist or could not be read")
return None

0 comments on commit 7d822c4

Please sign in to comment.