Skip to content

Commit

Permalink
Merge pull request #125 from defactor-com/feat/weekly-updates-bot
Browse files Browse the repository at this point in the history
Update Weekly Updates Bot
  • Loading branch information
xavier506 committed Aug 3, 2024
2 parents 2927b43 + 6abb074 commit 271db14
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/weekly-updates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }}
ASSISTANT_ID: ${{ secrets.ASSISTANT_ID }}
run: |
python updates-bot.py
python weekly-updates-bot.py
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env
# Dependencies
/node_modules

Expand All @@ -19,3 +20,8 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*


#Python
/myenv
Development_Weekly_Updates_*
4 changes: 2 additions & 2 deletions static/mainnet/defactor-pools.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"id": "pools-landx-mainnet",
"name": "LandX Production",
"description": "Production environment for LandX.",
"web": "https://landx.defactor.dev",
"api": "https://landx-api.defactor.dev",
"web": "https://landx.factr.app",
"api": "https://landx-api.factr.app",
"chains": [
{
"id": "Ethereum",
Expand Down
30 changes: 30 additions & 0 deletions static/testnet/defactor-assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id": "assets-dev",
"name": "Assets Development Instance",
"description": "The development environment for Defactor Assets.",
"web": "https://assets-dev.defactor.dev",
"api": "https://assets-dev-api.defactor.dev",
"testnet": true,
"chains": [
{
"id": "",
"contract": ""
}
]
},
{
"id": "assets-staging",
"name": "Assets Staging Instance",
"description": "The staging environment for Defactor Assets.",
"web": "https://assets.defactor.dev",
"api": "https://assets-api.defactor.dev",
"testnet": true,
"chains": [
{
"id": "",
"contract": ""
}
]
}
]
30 changes: 30 additions & 0 deletions static/testnet/defactor-engage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"id": "engage-dev",
"name": "Engage Development Instance",
"description": "The development environment for Defactor Engage.",
"web": "https://engage-dev.defactor.dev",
"api": "https://engage-dev-api.defactor.dev",
"testnet": true,
"chains": [
{
"id": "",
"contract": ""
}
]
},
{
"id": "engage-staging",
"name": "Engage Staging Instance",
"description": "The staging environment for Defactor Engage.",
"web": "https://engage.defactor.dev",
"api": "https://engage-api.defactor.dev",
"testnet": true,
"chains": [
{
"id": "",
"contract": ""
}
]
}
]
82 changes: 57 additions & 25 deletions weekly-updates-bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,40 @@
import datetime
import logging
import re
import argparse
import ssl
import certifi

from dotenv import load_dotenv
from openai import OpenAI
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
import ssl
import certifi

# Clear all environment variables
for key in list(os.environ.keys()):
os.environ.pop(key)

# Load environment variables
load_dotenv()

# Function to load and validate environment variables
def get_env_variable(var_name):
value = os.getenv(var_name)
if not value:
logging.error(f"Environment variable {var_name} is not set or is empty.")
return None
return value

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

# Constants
GITHUB_API_URL = "https://api.github.com"
TOKEN = os.getenv("GH_TOKEN") # Get the token from environment variables
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") # Get the OpenAI API key from environment variables
SLACK_TOKEN = os.getenv("SLACK_TOKEN") # Get the Slack token from environment variables
SLACK_CHANNEL = os.getenv("SLACK_CHANNEL") # Get the Slack channel from environment variables
ASSISTANT_ID = os.getenv("ASSISTANT_ID") # Get the assistant ID from environment variables
GH_TOKEN = get_env_variable("GH_TOKEN")
OPENAI_API_KEY = get_env_variable("OPENAI_API_KEY")
ASSISTANT_ID = get_env_variable("ASSISTANT_ID")
SLACK_TOKEN = get_env_variable("SLACK_TOKEN")
SLACK_CHANNEL = "defactor-internal"

# Initialize OpenAI client
client = OpenAI(api_key=OPENAI_API_KEY)
Expand All @@ -28,12 +46,16 @@

# List of repositories
REPOS = [
{'owner': 'defactor-com', 'name': 'ui-kit'},
{'owner': 'defactor-com', 'name': 'assets-webapp'},
{'owner': 'defactor-com', 'name': 'pools-backend'},
{'owner': 'defactor-com', 'name': 'assets-backend'},
{'owner': 'defactor-com', 'name': 'pools-webapp'},
{'owner': 'defactor-com', 'name': 'documentation'},
{'owner': 'defactor-com', 'name': 'sdk'}
{'owner': 'defactor-com', 'name': 'pools-backend'},
{'owner': 'defactor-com', 'name': 'engage-webapp'},
{'owner': 'defactor-com', 'name': 'engage-backend'},
{'owner': 'defactor-com', 'name': 'ui-kit'},
{'owner': 'defactor-com', 'name': 'sdk'},
{'owner': 'defactor-com', 'name': 'ipfs'},
{'owner': 'defactor-com', 'name': 'documentation'}
]

# Helper functions
Expand All @@ -44,8 +66,9 @@ def get_previous_week_dates():
return start_of_week, end_of_week

def github_api_request(url, params=None):
headers = {"Authorization": f"token {TOKEN}"}
headers = {"Authorization": f"token {GH_TOKEN}"}
response = requests.get(url, headers=headers, params=params)
logging.debug(f"GitHub API request URL: {response.url}")
response.raise_for_status()
return response.json()

Expand All @@ -61,15 +84,19 @@ def get_issue_details(owner, repo, issue_number):
return github_api_request(issue_url)

def extract_issue_numbers(pr_body):
if not pr_body:
return []

issue_numbers = []
# More flexible regex to capture variations in references
issue_references = re.findall(r'(?:fixes|closes|resolves|references|refs|fix|close|resolve)[^\n\r#]*(?:#|GH-)(\d+)', pr_body, re.IGNORECASE)
logging.debug(f"Extracted issue references: {issue_references}")
issue_numbers.extend(map(int, issue_references))
return issue_numbers

def clean_pr_body(pr_body):
# Remove "Steps to test" and "CheckList" sections
if pr_body is None:
return "No description provided."

cleaned_body = re.sub(r'### Steps to test.*?(\n\n|$)', '', pr_body, flags=re.DOTALL)
cleaned_body = re.sub(r'#### CheckList.*?(\n\n|$)', '', cleaned_body, flags=re.DOTALL)
cleaned_body = re.sub(r'\d+\.\s.*\n', '', cleaned_body, flags=re.MULTILINE)
Expand All @@ -91,11 +118,12 @@ def generate_summary(repos, start_date, end_date):

issues_seen = set()
for pr in pulls:
logging.debug(f"Processing PR #{pr['number']} with body:\n{pr['body']}")
cleaned_body = clean_pr_body(pr['body'])
pr_body = pr.get('body', 'No description provided.')
logging.debug(f"Processing PR #{pr['number']} with body:\n{pr_body}")
cleaned_body = clean_pr_body(pr_body)
summary += f"### Pull Request #{pr['number']}: {pr['title']}\n"
summary += f"- **Description:** {cleaned_body}\n"
issue_numbers = extract_issue_numbers(pr['body'])
issue_numbers = extract_issue_numbers(pr_body)
logging.debug(f"Issue numbers for PR #{pr['number']}: {issue_numbers}")
if issue_numbers:
summary += "- **Referenced Issues:**\n"
Expand Down Expand Up @@ -141,20 +169,24 @@ def post_to_slack(channel, message):
except SlackApiError as e:
logging.error(f"Error posting message to Slack: {e.response['error']}")

def main(repos):
start_date, end_date = get_previous_week_dates()
def main(repos, start_date=None, end_date=None):
if not start_date or not end_date:
start_date, end_date = get_previous_week_dates()
summary = generate_summary(repos, start_date, end_date)
raw_summary_filename = f"Development_Weekly_Updates_{start_date}_to_{end_date}_raw.txt"
polished_summary_filename = f"Development_Weekly_Updates_{start_date}_to_{end_date}_polished.txt"
save_summary_to_file(summary, raw_summary_filename)
polished_summary = get_polished_summary(summary)
save_summary_to_file(polished_summary, polished_summary_filename)

# Generate Development Updates Article
print(polished_summary)

# Post summary to Slack (commented out for now)
post_to_slack(SLACK_CHANNEL, polished_summary)

if __name__ == "__main__":
main(REPOS)
parser = argparse.ArgumentParser(description='Generate weekly GitHub update report.')
parser.add_argument('--start_date', type=str, help='Start date in YYYY-MM-DD format')
parser.add_argument('--end_date', type=str, help='End date in YYYY-MM-DD format')
args = parser.parse_args()

start_date = datetime.datetime.strptime(args.start_date, '%Y-%m-%d').date() if args.start_date else None
end_date = datetime.datetime.strptime(args.end_date, '%Y-%m-%d').date() if args.end_date else None

main(REPOS, start_date, end_date)

0 comments on commit 271db14

Please sign in to comment.