Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emailHeaders for extra mail headers #143

Merged
merged 4 commits into from
Oct 21, 2024

Conversation

dstam
Copy link
Contributor

@dstam dstam commented Oct 8, 2024

This optional configuration option, which allows you to add additional headers to the mail. We needed to set some extra headers to the mail, but instead of hard coding perhaps add it as a configuration option.

This optional configuration option, which allows you to add additional headers to the mail
@dstam dstam requested a review from neilmunday as a code owner October 8, 2024 06:31
@neilmunday neilmunday self-assigned this Oct 8, 2024
@neilmunday neilmunday added the enhancement New feature or request label Oct 8, 2024
Copy link
Owner

@neilmunday neilmunday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for supplying this pull request to add new functionality to Slurm-Mail.

Please see in-line comments below.

If you would prefer that I make the changes, please let me know and I will merge the PR into a development branch.

@@ -891,6 +898,11 @@ def send_mail_main():
options.email_from_name = config.get(section, "emailFromName")
options.email_subject = config.get(section, "emailSubject")

if config.has_option(section, "emailHeaders"):
email_headers = [ x.strip().split(":", maxsplit=1) for x in config.get(section, "emailHeaders").split(";") ]
Copy link
Owner

@neilmunday neilmunday Oct 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To populate the options.email_headers dictionary, two loops are being used here, one to create email_headers and then another to iterate through this list. Instead this can be accomplished with one loop which is slightly easier to read also:

for header in config.get(section, "emailHeaders").split(";"):
        header_name, header_value = header.split(":", maxsplit=1)
        email_headers_dict[header_name.strip()] = header_value.strip()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted based on the feedback

if config.has_option(section, "emailHeaders"):
email_headers = [ x.strip().split(":", maxsplit=1) for x in config.get(section, "emailHeaders").split(";") ]
for email_header in email_headers:
options.email_headers[email_header[0].strip()] = email_header[1].strip()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to ensure that the user had not provided e-mail headers that are set by Slurm-Mail. These are:

  • To
  • From
  • Date
  • Message-ID

Otherwise these will get overwritten. Perhaps an array of protected headers could be added which are then checked against when processing the slurm.conf file?

src/slurmmail/cli.py Show resolved Hide resolved
@neilmunday
Copy link
Owner

Note: the Pylint errors can be ignored in this PR - I am tracking this issue in #145

 - Changed the double for loop, to more readable.
 - Added an extra check to make sure we do not overrule the already set
   headers. Print a warning if this is the case and ignore that header
if options.email_headers:
for header_name, header_value in options.email_headers.items():
if header_name in msg:
logger.warning(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be logging.warning as Slurm-Mail currently uses the "root" logger rather than its own (for now).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please change to:

logging.warning("Ignoring header_name '%s' - header has already been set", header_name)

Ref: https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/logging-fstring-interpolation.html

if options.email_headers:
for header_name, header_value in options.email_headers.items():
if header_name in msg:
logger.warning(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, please change to:

logging.warning("Ignoring header_name '%s' - header has already been set", header_name)

Ref: https://pylint.readthedocs.io/en/latest/user_guide/messages/warning/logging-fstring-interpolation.html

Copy link

github-actions bot commented Oct 19, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/slurmmail
  cli.py 771-779, 909-911
  common.py
  slurm.py
Project Total  

This report was generated by python-coverage-comment-action

@neilmunday neilmunday added this to the 4.22 milestone Oct 19, 2024
@neilmunday neilmunday changed the base branch from main to pr143 October 21, 2024 20:43
@neilmunday neilmunday merged commit 043a2d4 into neilmunday:pr143 Oct 21, 2024
64 checks passed
@neilmunday
Copy link
Owner

Thank you @dstam for this PR and for adjusting the code after review.

This PR will be included in version 4.22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants