pythonslack-apislack-block-kit

how to add colorful vertical bar in Slack message using Python


I'm using slack-sdk in my Python project and trying to send the message to Slack. The message was successful but I do not see any color. My intent is to add colorful vertical bar based on different code coverage in the If-Else condition. Here is my code.

def send_slack_notification(url, stability, app_name, analysis_type, metrics, cqm_instance, project, computed_metrics):
    webhook = WebhookClient(url)

    coverage = ":red_circle: [" + str(metrics['coverage']) + "% Coverage]\n(" + cqm_instance + "/component_measures?id=" + project + "&metric=coverage)"
    duplication = ":red_circle: [" + str(metrics['duplicated_lines_density']) + "% Duplications]\n(" + cqm_instance + "/component_measures?id=" + project + "&metric=duplicated_lines_density) "

    print("sending to slack")
    if computed_metrics:
        bugs = ":beetle: New bugs: " + str(metrics['bugs'])
        vulnerabilities = ":lock: New Vulnerabilities: " + str(metrics['vulnerabilities'])
        code_smells = ":red_circle: New Code Smells: " + str(metrics['code_smells'])
    else:
        bugs = ":beetle: bugs: " + str(metrics['bugs']) + "(" + cqm_instance + "/project/issues?id=" + project + "&resolved=false&types=BUG)"
        vulnerabilities = ":lock: Vulnerabilities: " + str(metrics['vulnerabilities']) + "(" + cqm_instance +"/project/issues?id=" + project + "&resolved=false&types=VULNERABILITY)"
        code_smells = ":red_circle: Code Smells: " + str(metrics['code_smells']) + "(" + cqm_instance + "/project/issues?id=" + project + "&resolved=false&types=CODE_SMELL)"

    color = '#2091fa'
    if float(metrics['coverage']) < 30:
        color = '#FF0000'
    elif 30 < float(metrics['coverage']) < 80:
        color = '#FFA500'

    try:
        response = webhook.send(
            text="SonarQube code analysis",
        attachments=[{
            color:color
        }],
            blocks=[
                {
                    "type": "header",
                    "text": {
                        "type": "plain_text",
                        "text": "SonarQube Code Quality Report"
                    }
                },
                {
                    "type": "divider"
                },
                {
                    "type": "section",
                    "fields": [
                        {
                            "type": "mrkdwn",
                            "text": "*Application Name:* " + app_name
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Stability:* " + stability
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*Analysis Type:* " + analysis_type + " branch"
                        },
                        {
                            "type": "mrkdwn",
                            "text": "*SonarQube project dashboard link:* " + cqm_instance + "/dashboard?id=" + project
                        }
                    ]
                },
       

My requirement.txt:-

slack_sdk==3.19.4
Jinja2==3.1.2
python-sonarqube-api==1.3.0
python-gitlab==2.5.0

I do not see any bar at all. Please suggest.


Solution

  • Αt the time of finding a solution about the same issue that I had, I found your question but some hours after I found the solution. Since, I don't know if you have solved it out or not, I will show you what probably must be the issue about why you are not seeing the vertical bar.

    You can see on the webhook.send() my answer on your issue.

    Also you can find more about it on this link.

    def send_slack_notification(url, stability, app_name, analysis_type, metrics, cqm_instance, project, computed_metrics):
        webhook = WebhookClient(url)
    
        coverage = ":red_circle: [" + str(metrics['coverage']) + "% Coverage]\n(" + cqm_instance + "/component_measures?id=" + project + "&metric=coverage)"
        duplication = ":red_circle: [" + str(metrics['duplicated_lines_density']) + "% Duplications]\n(" + cqm_instance + "/component_measures?id=" + project + "&metric=duplicated_lines_density) "
    
        print("sending to slack")
        if computed_metrics:
            bugs = ":beetle: New bugs: " + str(metrics['bugs'])
            vulnerabilities = ":lock: New Vulnerabilities: " + str(metrics['vulnerabilities'])
            code_smells = ":red_circle: New Code Smells: " + str(metrics['code_smells'])
        else:
            bugs = ":beetle: bugs: " + str(metrics['bugs']) + "(" + cqm_instance + "/project/issues?id=" + project + "&resolved=false&types=BUG)"
            vulnerabilities = ":lock: Vulnerabilities: " + str(metrics['vulnerabilities']) + "(" + cqm_instance +"/project/issues?id=" + project + "&resolved=false&types=VULNERABILITY)"
            code_smells = ":red_circle: Code Smells: " + str(metrics['code_smells']) + "(" + cqm_instance + "/project/issues?id=" + project + "&resolved=false&types=CODE_SMELL)"
    
        color = '#2091fa'
        if float(metrics['coverage']) < 30:
            color = '#FF0000'
        elif 30 < float(metrics['coverage']) < 80:
            color = '#FFA500'
    
        try:
            response = webhook.send(
                {
                    "text": "SonarQube code analysis", 
                    "attachments":[
                        {
                            "markdown_in": ["text"],
                            "color":color,
                            "title": "SonarQube Code Quality Report",
                            "fields":[
                                {
                                    "title": "Application Name",
                                    "value": app_name,
                                    "short": True
                                },
                                {
                                    "title": "Stability",
                                    "value": stability,
                                    "short": True
                                },
                                {
                                    "title": "Analysis Type",
                                    "value": analysis_type + " branch" + " :test_tube:",
                                    "short": True
                                },
                                {
                                    "title": "SonarQube project dashboard link",
                                    "value": cqm_instance + "/dashboard?id=" + project,
                                    "short": True
                                }
                            ],
                            "footer": "If you want a footer, put it here, otherwise remove this line"
                        }
                    ]
                })