def list_jobs_jenkins(username, chann_id):
    jenkins_url = os.environ.get('JENKINS_URL')
    user_name = os.environ.get('JENKINS_USER')
    user_pass = os.environ.get('JENKINS_PASS')
    server = jenkins.Jenkins('{0}'.format(jenkins_url), username='******'.format(user_name),
                             password='******'.format(user_pass))
    jobs = server.get_jobs()
    slack_message.send_message_without_button(username, "I'm getting the jobs list from Jenkins...", chann_id)
    time.sleep(2)
    max_length = max([len(job['name']) for job in jobs])
    return ('\n'.join(
        ['{2})  <{1}|{0}> '.format(job['name'].ljust(max_length), job['url'], (counter + 1)) for counter, job in
         enumerate(jobs)]).strip())
Пример #2
0
def message_actions():
    # slack_event = json.loads(request.data)
    # return slack_event.get('challenge')
    # Parse the request payload
    form_json = json.loads(request.form["payload"])

    # Check to see what the user's selection was and update the message
    selection = form_json["actions"][0]["value"]
    chan_id = form_json["channel"]["id"]
    msg_ts = form_json["message_ts"]
    callback = form_json["callback_id"]
    username = form_json["user"]["name"]
    user_id = callback.split('_')[0]
    job_id = callback.split('_')[1]
    user_chan_id = callback.split('_')[2]

    if selection == "Yes":
        mesg = "Your request has been sent to the Admin for the Approval of job_id_{0}.".format(job_id)
        if user_id == username:
            slack_message.update_message(chan_id, msg_ts, mesg)
            slack_client = SlackClient(os.environ.get('SLACK_BOT_TOKEN'))
            SLACK_NAME = os.environ.get('APPROVER_SLACK_NAME')
            userid = slackbot.get_bot_id(SLACK_NAME, slack_client)
            im_id = slackbot.get_im_id(userid, slack_client)
            slack_message.send_interactive_message(username, job_id, im_id, user_chan_id)

    elif selection == "bad":
        mesg = "You choose not to send your request to Admin for Approval."
        if user_id == username:
            slack_message.update_message(chan_id, msg_ts, mesg)



    elif selection == "Not Approved":
        mesg = "Thanks, I will inform the user!"
        slack_message.update_message(chan_id, msg_ts, mesg)

        mesg2 = "Sorry! Your Request has been rejected by Admin."
        slack_message.send_message_without_button(user_id, mesg2, user_chan_id)

    elif selection == "Approve":
        mesg = "Thanks, I will inform the user!"
        slack_message.update_message(chan_id, msg_ts, mesg)
        job = "job_id_" + job_id
        python_mysql.update_status(job, user_id)
        mesg2 = "Your Request has been approved for {0} now you can execute the command".format(job)
        slack_message.send_message_without_button(user_id, mesg2, user_chan_id)

    return make_response("", 200)
Пример #3
0
def cmd_exec(username, job_name, chann_id):
    """
      execute the jenkins job based on provided job id and return the console output

    """
    try:
        url = get_job_url(job_name)
        if url != "not found":
            slack_message.send_message_without_button(
                username,
                'Please wait job is being executed, use below url to check the '
                'progress.\n{0}'.format(url), chann_id)
        output = execute_jenkins_job(job_name)
        return output
    except:
        return "Exception"
def list_failed_jenkins_job(username, chann_id):
    jenkins_url = os.environ.get('JENKINS_URL')
    user_name = os.environ.get('JENKINS_USER')
    user_pass = os.environ.get('JENKINS_PASS')
    server = jenkins.Jenkins('{0}'.format(jenkins_url), username='******'.format(user_name),
                             password='******'.format(user_pass))
    jobs = [job for job in server.get_jobs() if 'red' in job['color']]
    jobs_info = [server.get_job_info(job['name']) for job in jobs]
    slack_message.send_message_without_button(username, "I will get the failed jenkins job!", chann_id)
    time.sleep(2)
    if not jobs_info:
        return "There is no failed jobs!"
    else:
        return '\n\n'.join(
            ['<{1}|{0}>\n{2}'.format(job['name'], job['lastBuild']['url'], job['healthReport'][0]['description']) for
             job in jobs_info]).strip()
Пример #5
0
def cmd_exec(username, job_no):
    """
      execute the jenkins job based on provided job id and return the console output

    """
    try:

        if job_no == "1":
            slack_message.send_message_without_button(username, 'Please wait job is being executed...')
            output = execute_jenkins_job('job_ansible_1')
            return output
        elif job_no == "2":
            slack_message.send_message_without_button(username, 'Please wait job is being executed...')
            output = execute_jenkins_job('job_ansible_2')
            return output
        elif job_no == "3":
            slack_message.send_message_without_button(username, 'Please wait job is being executed...')
            output = execute_jenkins_job('job_ansible_3')
            return output
    except:
        return "Exception"
Пример #6
0
def handle_command(command, channel, msg_id, user_id):
    """
        Receives commands directed at the bot and sends to function cmd_process
        to process the command , which returns the response. Based on response it post the
        message to slack thread or message.
    """
    username = get_user_name(user_id, slack_client)
    python_mysql.add_user(username)

    if command == "member joined":
        msg = ":slack: Welcome to the channel, Here you can instruct the jenkinsbot to execute the job based on the " \
              "id.\n\nYou can use @jenkinsbot help message to get the usage details.\n\nPlease note you need to get " \
              "the Approval from Admin for every job that you will execute. "
        python_mysql.add_user(username)
        slack_message.send_message_without_button(username, msg, channel)
    else:

        response, status, color, job_id = slack_cmd_process.cmd_process(
            command, username)
        if status != "notapproved":
            if msg_id == "Thread_False":
                slack_client.api_call("chat.postMessage",
                                      channel=channel,
                                      text="<@%s> " % user_id,
                                      as_user=True,
                                      attachments=[{
                                          "text": "%s" % response,
                                          "color": "%s" % color
                                      }])
            else:
                slack_client.api_call("chat.postMessage",
                                      channel=channel,
                                      text="<@%s> " % user_id,
                                      as_user=True,
                                      thread_ts=msg_id,
                                      attachments=[{
                                          "text": "%s" % response,
                                          "color": "%s" % color
                                      }])

        else:
            slack_client.api_call("chat.postMessage",
                                  channel=channel,
                                  text="<@%s> " % user_id,
                                  as_user=True,
                                  attachments=[{
                                      "text":
                                      "%s" % response,
                                      "color":
                                      "%s" % color,
                                      "attachment_type":
                                      "default",
                                      "callback_id":
                                      "{0}_{1}_{2}".format(
                                          username, job_id, channel),
                                      "actions": [{
                                          "name": "option",
                                          "text": "Send it in!",
                                          "type": "button",
                                          "value": "Yes"
                                      }, {
                                          "name": "no",
                                          "text": "Not now, may be later!",
                                          "type": "button",
                                          "value": "bad"
                                      }]
                                  }])