# Copyright 2019 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Generated by nuclio.export.NuclioExporter

import requests
import os
from mlrun import DataItem, get_run_db, mlconf


def pr_comment(
    context, repo: str, issue: int, message: str = "", message_file: DataItem = None
):

    token = context.get_secret("GITHUB_TOKEN") or os.environ.get("GITHUB_TOKEN")
    if message_file and not message:
        message = message_file.get()
    elif not message and not message_file:
        raise ValueError("pr message or message file must be provided")

    headers = {
        "Accept": "application/vnd.github.v3+json",
        "Authorization": f"token {token}",
    }
    url = f"https://api.github.com/repos/{repo}/issues/{issue}/comments"

    resp = requests.post(url=url, json={"body": str(message)}, headers=headers)
    if not resp.ok:
        errmsg = f"bad pr comment resp!!\n{resp.text}"
        context.logger.error(errmsg)
        raise IOError(errmsg)


def run_summary_comment(context, workflow_id, repo: str, issue: int, project=""):
    db = get_run_db().connect()
    project = project or context.project
    runs = db.list_runs(project=project, labels=f"workflow={workflow_id}")

    had_errors = i = 0
    for r in runs:
        name = r["metadata"]["name"]
        if r["status"].get("state", "") == "error":
            had_errors += 1
        if name == context.name:
            del runs[i]
        i += 1

    print("errors:", had_errors)

    html = "### Run Results\nWorkflow {} finished with {} errors".format(
        workflow_id, had_errors
    )
    html += "
click the hyper links below to see detailed results
" html += runs.show(display=False, short=True) if repo: pr_comment(context, repo, issue, html) else: print("repo not defined") print(html)