How to prevent a GitHub issue report from being posted and send notification to user in Python?

  Kiến thức lập trình

I have created a Flask application that interacts with the GitHub API using the PyGithub library. The application listens for GitHub issue comment events, checks the content of the comment for certain conditions using a prediction model, and posts a comment if the condition is met.

Here is the relevant part of my current implementation:


import os
import requests
from flask import Flask, request
from github import Github, GithubIntegration
from secret_finding_for_tool import prediction
import pandas as pd

app = Flask(__name__)

app_id = ******
with open(
        os.path.normpath(os.path.expanduser(r"****.2024-02-15.private-key.pem")),
        'r'
) as cert_file:
    app_key = cert_file.read()

git_integration = GithubIntegration(
    app_id,
    app_key,
)

markdown_message = "Alert"

@app.route("/", methods=['POST'])
def bot():
    payload = request.json    

    if 'issue' in payload and payload['action'] == 'created':
        owner = payload['repository']['owner']['login']
        repo_name = payload['repository']['name']
        issue_number = payload['issue']['number']
        comment_text = payload['comment']['body']
        comment_author = payload['comment']['user']['login']

        git_connection = Github(
            login_or_token=git_integration.get_access_token(
                git_integration.get_installation(owner, repo_name).id
            ).token
        )
        repo = git_connection.get_repo(f"{owner}/{repo_name}")
        issue = repo.get_issue(number=issue_number)
        
        if comment_author != "secret-detection-tool[bot]":  
            dict = {}
            dict[0] = {'comment_or_body': comment_text}
            data = pd.DataFrame.from_dict(dict, "index")
            data.to_csv('comment.csv', index=False)
            
            df = pd.read_csv('comment.csv')
            content = df['comment_or_body'][0]

            if prediction(content):
                issue.create_comment(markdown_message)
            
if __name__ == "__main__":
    app.run(debug=True, port=5000)

However, this code only triggers after the user has posted the report. Is there a way to intercept and prevent the user from posting the report if a certain condition is met and instead send them a notification?

I expected to find a way to intercept the issue report before it gets posted to GitHub. Ideally, I would like to prevent the report from being posted altogether if it meets certain conditions and notify the user about it.However, I am unsure how to achieve this interception and notification before the comment is actually posted. My current implementation only allows for action after the comment has already been posted. Any insights or alternative approaches to achieve the desired behavior would be greatly appreciated.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT