forked from MathieuLamiot/TechTeamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #70 from wp-media/feature/GH-release-to-Notion-and…
…-Slack Feature/gh release to notion and slack
- Loading branch information
Showing
14 changed files
with
337 additions
and
3 deletions.
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
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,3 @@ | ||
{ | ||
"release-note-db-id": "9bf76ffc56074d1d9ca81b65ce00f4da" | ||
} |
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
{ | ||
"dev-team-escalation-channel": "C056ZJMHG0P" | ||
"dev-team-escalation-channel": "C056ZJMHG0P", | ||
"engineering-service-team-channel": "C069W48E47N", | ||
"release-channel" : "C05PGTQHHJ9", | ||
"ops-channel": "C88N0811V" | ||
} |
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,132 @@ | ||
""" | ||
This module defines the factory for Notion API | ||
""" | ||
from datetime import date | ||
import json | ||
from pathlib import Path | ||
import requests | ||
from flask import current_app | ||
import sources.utils.Constants as cst | ||
from sources.models.GithubReleaseParam import GithubReleaseParam | ||
|
||
|
||
class NotionFactory(): | ||
""" | ||
Class managing the API for OVH | ||
""" | ||
def __init__(self): | ||
""" | ||
The factory instanciates the objects it needed to complete the processing of the request. | ||
""" | ||
self.api_key = None | ||
with open(Path(__file__).parent.parent.parent / "config" / "notion.json", encoding='utf-8') as file_notion_config: | ||
self.notion_config = json.load(file_notion_config) | ||
|
||
def _get_notion_api_key(self, app_context): | ||
""" | ||
Return the Notion API key and creates it if needed | ||
""" | ||
if self.api_key is None: | ||
app_context.push() | ||
self.api_key = current_app.config[cst.APP_CONFIG_TOKEN_NOTION_API_KEY] | ||
return self.api_key | ||
|
||
def _create_notion_db_row(self, app_context, db_id, properties, children): | ||
""" | ||
Requests the creation of a row in a Notion DB through the API. | ||
Properties should match the DB columns, and children is the content of the created page. | ||
""" | ||
headers = { | ||
'Authorization': 'Bearer ' + self._get_notion_api_key(app_context), | ||
'Content-Type': 'application/json', | ||
'Notion-Version': '2022-06-28', | ||
} | ||
|
||
data = { | ||
'parent': {'database_id': db_id}, | ||
'properties': properties, | ||
'children': children | ||
} | ||
|
||
response = requests.post( | ||
'https://api.notion.com/v1/pages', | ||
headers=headers, | ||
json=data, | ||
timeout=3000 | ||
) | ||
|
||
if response.status_code != 200: | ||
raise ValueError('Notion API could not create the DB row.') | ||
page_id = response.json().get('id') | ||
page_url = f'https://www.notion.so/{page_id}' | ||
return page_url | ||
|
||
def create_release_note(self, app_context, release_params: GithubReleaseParam): | ||
""" | ||
Creates a release note in Notion for a GitHub release. | ||
""" | ||
today = date.today() | ||
|
||
properties = { | ||
"Version": { | ||
"title": [ | ||
{ | ||
"text": { | ||
"content": release_params.version | ||
} | ||
} | ||
] | ||
}, | ||
"Product": { | ||
"select": { | ||
"name": release_params.repository_name | ||
} | ||
}, | ||
'date_property': { | ||
'date': { | ||
'start': today.strftime("%Y-%m-%d") | ||
} | ||
} | ||
} | ||
|
||
content = [ | ||
{ | ||
"object": "block", | ||
"heading_2": { | ||
"rich_text": [ | ||
{ | ||
"text": { | ||
"content": "Complete Changelog" | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
{ | ||
"object": "block", | ||
"paragraph": { | ||
"rich_text": [ | ||
{ | ||
"text": { | ||
"content": release_params.body | ||
}, | ||
} | ||
], | ||
"color": "default" | ||
} | ||
}, | ||
{ | ||
"object": "block", | ||
"heading_2": { | ||
"rich_text": [ | ||
{ | ||
"text": { | ||
"content": "User notes" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
return self._create_notion_db_row(app_context, self.notion_config["release-note-db-id"], properties, content) |
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
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,42 @@ | ||
""" | ||
This module defines the handler for GitHub Release related logic. | ||
""" | ||
import json | ||
from pathlib import Path | ||
from sources.factories.SlackMessageFactory import SlackMessageFactory | ||
from sources.factories.NotionFactory import NotionFactory | ||
|
||
|
||
class GithubReleaseHandler(): | ||
""" | ||
Class managing the business logic related to Github releases | ||
""" | ||
def __init__(self): | ||
""" | ||
The handler instanciates the objects it needed to complete the processing of the request. | ||
""" | ||
self.slack_message_factory = SlackMessageFactory() | ||
self.notion_factory = NotionFactory() | ||
with open(Path(__file__).parent.parent.parent / "config" / "github.json", encoding='utf-8') as file_github_config: | ||
self.github_config = json.load(file_github_config) | ||
|
||
def process_release(self, app_context, release_params): | ||
""" | ||
Processing method when a github release is released | ||
""" | ||
# Replace the repository name by its readable name | ||
repository_readable_name = self.github_config["repoNameToReadable"][release_params.repository_name] | ||
release_params.repository_name = repository_readable_name | ||
# Create a page in the Notion database | ||
notion_url = self.notion_factory.create_release_note(app_context, release_params) | ||
|
||
# Send a message to Slack | ||
text = "The draft release note for " + repository_readable_name + " " + release_params.version | ||
text += " is available on Notion: " + notion_url | ||
|
||
blocks = self.slack_message_factory.get_release_note_review_blocks(text) | ||
|
||
self.slack_message_factory.post_message(app_context, | ||
self.slack_message_factory.get_channel('ops'), | ||
text, blocks) |
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
Oops, something went wrong.