Exemplo n.º 1
0
class ParticipantHandler(object):
    """Participant class as defined by the SkyNET API."""
    def __init__(self):
        self.oscrc = None
        self.obs = None

    def handle_wi_control(self, ctrl):
        """Job control thread."""
        pass

    def handle_lifecycle_control(self, ctrl):
        """Participant control thread."""
        if ctrl.message == "start":
            if ctrl.config.has_option("obs", "oscrc"):
                self.oscrc = ctrl.config.get("obs", "oscrc")

    def setup_obs(self, namespace):
        """Setup the Buildservice instance.

        Using the namespace as an alias to the apiurl.
        """

        self.obs = BuildService(oscrc=self.oscrc, apiurl=namespace)

    def handle_request(self, wid):
        """Request handling implementation."""

        wid.result = False
        if not wid.fields.msg:
            wid.fields.msg = []
        rid = str(wid.fields.ev.id)

        # https://api.opensuse.org/apidocs/request.xsd
        obj_type = "request"
        newstate = None
        if wid.params.action == 'accept':
            newstate = "accepted"
        elif wid.params.action == 'reject' or wid.params.action == 'decline':
            newstate = "declined"
        elif wid.params.action == 'add review':
            obj_type = "review"
            newstate = "add"
        elif wid.params.action == 'accept review':
            obj_type = "review"
            newstate = "accepted"
        elif wid.params.action == 'decline review':
            obj_type = "review"
            newstate = "declined"

        Verify.assertNotNull("Request ID field must not be empty", rid)
        Verify.assertNotNull("Participant action should be one of accept, "\
                             "decline, add review, accept review, "\
                             "decline review", newstate)

        try:
            if obj_type == "review":
                reviewer = wid.params.reviewer
                extra_msg = ""

                if wid.params.comment:
                    extra_msg = "%s\n" % wid.params.comment

                if not reviewer:
                    reviewer = self.obs.getUserName()
                if newstate == "add":
                    res = self.obs.addReview(rid, extra_msg, reviewer)
                else:
                    res = self.obs.setReviewState(rid, newstate, extra_msg,
                                                  reviewer)
            elif obj_type == "request":

                extra_msg = ""

                if wid.params.comment:
                    extra_msg = "%s\n" % wid.params.comment

                msgstring = "%sBOSS %s this %s because:\n %s" % (
                    extra_msg, newstate, obj_type, "\n ".join(wid.fields.msg))

                res = self.obs.setRequestState(rid, newstate, msgstring)

            if res:
                self.log.info("%s %s %s" % (newstate, obj_type, rid))
                wid.result = True
            else:
                self.log.info("Failed to %s %s %s" %
                              (wid.params.action, obj_type, rid))

        except HTTPError, exc:
            if exc.code == 403:
                wid.fields.msg.append("Applying the actions required to "\
                                      "automate this process has failed, "\
                                      "because the %s user was not authorized "\
                                      "to do so. "\
                                      "Please add %s as a maintainer in the "\
                                      "target projet %s" %
                                      (self.obs.getUserName(),
                                       self.obs.getUserName(),
                                       wid.fields.project))
                self.log.info("Forbidden to %s %s %s" %
                              (wid.params.action, obj_type, rid))
            elif exc.code == 401:
                wid.fields.msg.append("Credentials for the '%s' user were "\
                                      "refused. Please update the skynet "\
                                      "configuration." %
                                      self.obs.getUserName())
                self.log.info(exc)
                self.log.info("User is %s" % self.obs.getUserName())
            else:
                import traceback
                self.log.info(exc)
                traceback.print_exc()
                raise
class ParticipantHandler(object):

    """Participant class as defined by the SkyNET API."""

    def __init__(self):
        self.oscrc = None
        self.obs = None

    def handle_wi_control(self, ctrl):
        """Job control thread."""
        pass

    def handle_lifecycle_control(self, ctrl):
        """Participant control thread."""
        if ctrl.message == "start":
            if ctrl.config.has_option("obs", "oscrc"):
                self.oscrc = ctrl.config.get("obs", "oscrc")

    def setup_obs(self, namespace):
        """Setup the Buildservice instance.

        Using the namespace as an alias to the apiurl.
        """

        self.obs = BuildService(oscrc=self.oscrc, apiurl=namespace)

    def handle_request(self, wid):
        """Request handling implementation."""

        wid.result = False
        if not wid.fields.msg :
            wid.fields.msg = []
        rid = str(wid.fields.ev.id)

        # https://api.opensuse.org/apidocs/request.xsd
        obj_type = "request"
        newstate = None
        if wid.params.action == 'accept':
            newstate = "accepted"
        elif wid.params.action == 'reject' or wid.params.action == 'decline' :
            newstate = "declined"
        elif wid.params.action == 'add review':
            obj_type = "review"
            newstate = "add"
        elif wid.params.action == 'accept review':
            obj_type = "review"
            newstate = "accepted"
        elif wid.params.action == 'decline review':
            obj_type = "review"
            newstate = "declined"

        Verify.assertNotNull("Request ID field must not be empty", rid)
        Verify.assertNotNull("Participant action should be one of accept, "\
                             "decline, add review, accept review, "\
                             "decline review", newstate)

        try:
            if obj_type == "review":
                reviewer = wid.params.reviewer
                extra_msg = ""

                if wid.params.comment:
                    extra_msg = "%s\n" % wid.params.comment

                if not reviewer:
                    reviewer = self.obs.getUserName()
                if newstate == "add":
                    res = self.obs.addReview(rid, extra_msg, reviewer)
                else:
                    res = self.obs.setReviewState(rid, newstate, extra_msg,
                                                  reviewer)
            elif obj_type == "request":

                extra_msg = ""

                if wid.params.comment:
                    extra_msg = "%s\n" % wid.params.comment

                msgstring = "%sBOSS %s this %s because:\n %s" % (
                    extra_msg, newstate, obj_type, "\n ".join(wid.fields.msg) )

                res = self.obs.setRequestState(rid, newstate, msgstring)

            if res:
                self.log.info("%s %s %s" % (newstate , obj_type, rid))
                wid.result = True
            else:
                self.log.info("Failed to %s %s %s" % (wid.params.action , obj_type, rid))

        except HTTPError, exc:
            if exc.code == 403:
                wid.fields.msg.append("Applying the actions required to "\
                                      "automate this process has failed, "\
                                      "because the %s user was not authorized "\
                                      "to do so. "\
                                      "Please add %s as a maintainer in the "\
                                      "target projet %s" %
                                      (self.obs.getUserName(),
                                       self.obs.getUserName(),
                                       wid.fields.project))
                self.log.info("Forbidden to %s %s %s" % (wid.params.action, obj_type, rid))
            elif exc.code == 401:
                wid.fields.msg.append("Credentials for the '%s' user were "\
                                      "refused. Please update the skynet "\
                                      "configuration." %
                                      self.obs.getUserName())
                self.log.info(exc)
                self.log.info("User is %s" % self.obs.getUserName())
            else:
                import traceback
                self.log.info(exc)
                traceback.print_exc()
                raise
Exemplo n.º 3
0
class ParticipantHandler(object):

    """Participant class as defined by the SkyNET API."""

    def __init__(self):
        self.oscrc = None
        self.obs = None

    def handle_wi_control(self, ctrl):
        """Job control thread."""
        pass

    def handle_lifecycle_control(self, ctrl):
        """Participant control thread."""
        if ctrl.message == "start":
            if ctrl.config.has_option("obs", "oscrc"):
                self.oscrc = ctrl.config.get("obs", "oscrc")

    def setup_obs(self, namespace):
        """Setup the Buildservice instance.

        Using the namespace as an alias to the apiurl.
        """

        self.obs = BuildService(oscrc=self.oscrc, apiurl=namespace)

    def handle_request(self, wid):
        """Request handling implementation."""

        wid.result = False
        if not wid.fields.msg :
            wid.fields.msg = []
        rid = str(wid.fields.ev.id)

        # https://api.opensuse.org/apidocs/request.xsd
        obj_type = "request"
        newstate = None
        if wid.params.action == 'accept':
            newstate = "accepted"
        elif wid.params.action == 'reject' or wid.params.action == 'decline' :
            newstate = "declined"
        elif wid.params.action == 'add review':
            obj_type = "review"
            newstate = "add"
        elif wid.params.action == 'accept review':
            obj_type = "review"
            newstate = "accepted"
        elif wid.params.action == 'decline review':
            obj_type = "review"
            newstate = "declined"

        Verify.assertNotNull("Request ID field must not be empty", rid)
        Verify.assertNotNull("Participant action should be one of accept, "\
                             "decline, add review, accept review, "\
                             "decline review", newstate)

        # So sometimes it seems that the request is not available
        # immediately. In that case we wait a short while and retry
        retries = 0
        retry = True
        while retry:
            retry = False # Only try once unless specified
            try:
                if obj_type == "review":
                    reviewer = wid.params.reviewer
                    extra_msg = ""

                    if wid.params.comment:
                        extra_msg = "%s\n" % wid.params.comment

                    if not reviewer:
                        reviewer = self.obs.getUserName()
                    if newstate == "add":
                        res = self.obs.addReview(rid, extra_msg, reviewer)
                    else:
                        res = self.obs.setReviewState(rid, newstate, extra_msg,
                                                      reviewer)
                elif obj_type == "request":

                    extra_msg = ""

                    if wid.params.comment:
                        extra_msg = "%s\n" % wid.params.comment

                    msgstring = "%sBOSS %s this %s because:\n %s" % (
                        extra_msg, newstate, obj_type, "\n ".join(wid.fields.msg) )

                    print msgstring
                    res = self.obs.setRequestState(str(rid), str(newstate), str(msgstring))

                if res:
                    self.log.info("%s %s %s" % (newstate , obj_type, rid))
                    wid.result = True
                else:
                    self.log.info("Failed to %s %s %s" % (wid.params.action , obj_type, rid))

            except HTTPError, exc:
                if exc.code == 403:
                    self.log.info("Forbidden to %s %s %s" % (wid.params.action, obj_type, rid))
                elif exc.code == 401:
                    wid.fields.msg.append("Credentials for the '%s' user were "\
                                          "refused. Please update the skynet "\
                                          "configuration." %
                                          self.obs.getUserName())
                    self.log.info(exc)
                    self.log.info("User is %s" % self.obs.getUserName())
                elif exc.code == 404: # How did we get a request ID which is 404?
                    retries += 1 # Maybe a race thing? So retry.
                    self.log.info(exc)
                    if retries < 3:
                        import time
                        time.sleep(10)
                        retry = True
                        self.log.info(
                            "Failed Request ID is %s (retrying now)" %
                            rid)
                    else:
                        wid.fields.msg.append(
                            "Request ID '%s' doesn't seem to exist (even"\
                            "after %d retries)" %
                            (rid, retries))
                        raise
                else:
                    import traceback
                    self.log.info(exc)
                    traceback.print_exc()
                    raise