-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4942 from ministryofjustice/poc-repository-alerting
POC repository alerting
- Loading branch information
Showing
5 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Alarm for Old POC Repositories | ||
|
||
on: | ||
schedule: | ||
- cron: "0 4 * * *" | ||
workflow_dispatch: | ||
|
||
env: | ||
ADMIN_SLACK_TOKEN: ${{ secrets.ADMIN_SEND_TO_SLACK }} | ||
GH_TOKEN: ${{ secrets.OPS_ENG_GENERAL_ADMIN_BOT_PAT }} | ||
|
||
jobs: | ||
old-poc-repositories-alarm: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
cache: "pipenv" | ||
|
||
- name: Install Pipenv | ||
run: | | ||
pip install pipenv | ||
pipenv install | ||
- run: pipenv run python3 -m bin.alarm_for_old_poc_repositories | ||
|
||
- name: Report failure to Slack | ||
if: always() | ||
uses: ravsamhq/notify-slack-action@472601e839b758e36c455b5d3e5e1a217d4807bd # 2.5.0 | ||
with: | ||
status: ${{ job.status }} | ||
notify_when: "failure" | ||
notification_title: "Failed: Alarm for Old POC Repositories" | ||
env: | ||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import sys | ||
import os | ||
|
||
from services.github_service import GithubService | ||
from services.slack_service import SlackService | ||
from config.constants import ENTERPRISE, MINISTRY_OF_JUSTICE, SLACK_CHANNEL | ||
|
||
def construct_message(repositories): | ||
intro = "The following POC GitHub Repositories persist:\n\n" | ||
|
||
core_content = "\n".join([f"https://github.com/ministryofjustice/{repo} - {age} days old" for repo, age in repositories.items()]) | ||
|
||
action = "\n\nConsider if they are still required. If not, please archive them by removing them from the Terraform configuration: https://github.com/ministryofjustice/operations-engineering/tree/main/terraform/github/repositories/ministryofjustice" | ||
|
||
return intro + core_content + action | ||
|
||
|
||
def alert_for_old_poc_repositories(): | ||
github_token = os.environ.get("GH_TOKEN") | ||
slack_token = os.environ.get("ADMIN_SLACK_TOKEN") | ||
|
||
if github_token is None: | ||
print("No GH_TOKEN environment variable set") | ||
sys.exit(1) | ||
|
||
if slack_token is None: | ||
print("No ADMIN_SLACK_TOKEN environment variable set") | ||
sys.exit(1) | ||
|
||
github_service = GithubService(github_token, MINISTRY_OF_JUSTICE, ENTERPRISE) | ||
slack_service = SlackService(slack_token) | ||
|
||
old_poc_repositories = github_service.get_old_poc_repositories() | ||
|
||
if old_poc_repositories: | ||
slack_service.send_message_to_plaintext_channel_name(construct_message(old_poc_repositories), SLACK_CHANNEL) | ||
|
||
|
||
if __name__ == "__main__": | ||
alert_for_old_poc_repositories() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
|
||
from bin.alarm_for_old_poc_repositories import ( | ||
construct_message, | ||
alert_for_old_poc_repositories | ||
) | ||
|
||
from services.github_service import GithubService | ||
from services.slack_service import SlackService | ||
|
||
|
||
class TestOldPOCGitHubRepositoriesAlerting(unittest.TestCase): | ||
|
||
def test_construct_message(self): | ||
test_payload = {"repo1": 51, "repo2": 60} | ||
|
||
self.assertEqual(construct_message(test_payload), "The following POC GitHub Repositories persist:\n\nhttps://github.com/ministryofjustice/repo1 - 51 days old\nhttps://github.com/ministryofjustice/repo2 - 60 days old\n\nConsider if they are still required. If not, please archive them by removing them from the Terraform configuration: https://github.com/ministryofjustice/operations-engineering/tree/main/terraform/github/repositories/ministryofjustice") | ||
|
||
@patch("gql.transport.aiohttp.AIOHTTPTransport.__new__", new=MagicMock) | ||
@patch("gql.Client.__new__", new=MagicMock) | ||
@patch("github.Github.__new__") | ||
@patch("bin.alarm_for_old_poc_repositories.construct_message") | ||
@patch.object(GithubService, "get_old_poc_repositories") | ||
@patch.object(SlackService, "send_message_to_plaintext_channel_name") | ||
@patch('os.environ') | ||
def test_alert_for_old_poc_repositories_if_found( | ||
self, | ||
mock_env, | ||
mock_send_message_to_plaintext_channel_name, | ||
mock_get_old_poc_repositories, | ||
mock_construct_message, | ||
_mock_github_client_core_api | ||
): | ||
|
||
mock_env.get.side_effect = lambda k: 'mock_token' if k in ['GH_TOKEN', 'ADMIN_SLACK_TOKEN'] else None | ||
mock_get_old_poc_repositories.return_value = {"repo1": 51, "repo2": 60} | ||
mock_construct_message.return_value = "The following POC GitHub Repositories persist:\n\nhttps://github.com/ministryofjustice/repo1 - 51 days old\nhttps://github.com/ministryofjustice/repo2 - 60 days old\n\nConsider if they are still required. If not, please archive them by removing them from the Terraform configuration: https://github.com/ministryofjustice/operations-engineering/tree/main/terraform/github/repositories/ministryofjustice" | ||
|
||
alert_for_old_poc_repositories() | ||
|
||
mock_get_old_poc_repositories.assert_called_once() | ||
mock_send_message_to_plaintext_channel_name.assert_called_once_with("The following POC GitHub Repositories persist:\n\nhttps://github.com/ministryofjustice/repo1 - 51 days old\nhttps://github.com/ministryofjustice/repo2 - 60 days old\n\nConsider if they are still required. If not, please archive them by removing them from the Terraform configuration: https://github.com/ministryofjustice/operations-engineering/tree/main/terraform/github/repositories/ministryofjustice", "operations-engineering-alerts") | ||
|
||
@patch("gql.transport.aiohttp.AIOHTTPTransport.__new__", new=MagicMock) | ||
@patch("gql.Client.__new__", new=MagicMock) | ||
@patch("github.Github.__new__") | ||
@patch.object(GithubService, "get_old_poc_repositories") | ||
@patch.object(SlackService, "send_message_to_plaintext_channel_name") | ||
@patch('os.environ') | ||
def test_alert_for_old_poc_repositories_if_not_found( | ||
self, | ||
mock_env, | ||
mock_send_message_to_plaintext_channel_name, | ||
mock_get_old_poc_repositories, | ||
_mock_github_client_core_api | ||
): | ||
|
||
mock_env.get.side_effect = lambda k: 'mock_token' if k in ['GH_TOKEN', 'ADMIN_SLACK_TOKEN'] else None | ||
mock_get_old_poc_repositories.return_value = {} | ||
|
||
alert_for_old_poc_repositories() | ||
|
||
mock_get_old_poc_repositories.assert_called_once() | ||
assert not mock_send_message_to_plaintext_channel_name.called | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters