Exemplo n.º 1
0
    def start(self):
        self.start_time = time.time()
        # Check if lock file exists. If it exists and has been there for less than 30 minutes, return an error code
        if os.path.isfile(Command.FETCH_LOCK_FILE_PATH):
            # If the file is not old (has been there for less than 30 minutes), return an error code
            if not Command._lock_file_is_old():
                self.stdout.write(
                    self.style.ERROR(u"Lock is in place. Unable to fetch"))
                return False
            # Warn administrators that lock file is too old and remove it
            lock_last_modification_date_str = Command._get_lock_file_last_modification_datetime(
            ).isoformat()
            warn_administrators(
                subject=u"Lock file removed",
                message=u"Lock file was last modified on {0} and was removed".
                format(lock_last_modification_date_str))
            os.remove(Command.FETCH_LOCK_FILE_PATH)
            self.stdout.write(
                self.style.SUCCESS(u"Old lock created on {0} removed.".format(
                    lock_last_modification_date_str)))

        # Creates a new lock file
        self.stdout.write(
            self.style.SUCCESS(u"Lock does not exist. Creating..."))
        with open(Command.FETCH_LOCK_FILE_PATH, 'w',
                  encoding="utf-8") as lock_file:
            lock_file.write(u"Fetching data for boards")

        self.stdout.write(self.style.SUCCESS(u"Lock file created"))
        return True
Exemplo n.º 2
0
    def handle(self, *args, **options):

        if not self.start():
            self.stdout.write(self.style.ERROR("Unable to fetch"))
            error_message = u"Unable to do checkout. Is the lock file present?"
            warn_administrators(subject=u"Unable to do checkout", message=error_message)
            raise AssertionError(error_message)

        checkout_ok = False

        try:
            boards = Board.objects.filter(has_to_be_fetched=True).exclude(last_fetch_datetime=None)
            self.stdout.write(self.style.SUCCESS(u"You have {0} projects that could have repositories".format(
                boards.count()
            )))
            for board in boards:
                repositories = board.repositories.all()
                self.stdout.write(self.style.SUCCESS(u"There are {0} repositories of code {1}".format(
                    repositories.count(), board.name
                )))
                for repository in repositories:
                    self.stdout.write(self.style.SUCCESS(u"Repository {0}".format(repository.name)))
                    repository.checkout()
                    commits = repository.commits.filter(has_been_assessed=False)
                    self.stdout.write(
                        self.style.SUCCESS(
                            u"Repository {0} is checked out successfully and has {1} commits:".format(
                                repository.name, commits.count()
                            )
                        )
                    )
                    for commit in commits:
                        commit.assess_code_quality()
                        self.stdout.write(self.style.SUCCESS(u"- Commit {0}".format(commit.commit)))
                        self.stdout.write(self.style.SUCCESS(u"  - PHPMD Messages {0}".format(commit.phpmd_messages.count())))
                        self.stdout.write(self.style.SUCCESS(u"  - Pylint Messages {0}".format(commit.pylint_messages.count())))

                    self.stdout.write(self.style.SUCCESS(u"Repository {0} {1} commits assessed succesfully".format(
                        repository.name, commits.count()))
                    )

            checkout_ok = True

        # If after two retries the exception persists, warn the administrators
        except Exception as e:
            checkout_ok = False
            exception_message = u"We tried checkout out the code repositories but it didn't work out. {0}".format(
                    traceback.format_exc()
            )
            warn_administrators(
                subject=u"Unable to checkout repositories code",
                message=exception_message
            )
            print(exception_message)
        # Always delete the lock file
        finally:
            self.end()

        if checkout_ok:
            self.stdout.write(self.style.SUCCESS(u"All code repositories checkout out successfully {0}".format(self.elapsed_time())))
Exemplo n.º 3
0
    def handle(self, *args, **options):

        if 'member_external_username' in options and options[
                'member_external_username']:
            try:
                member_trello_username = options['member_external_username'][0]
            except (IndexError, KeyError) as e:
                self.stdout.write(
                    self.style.ERROR("member_username is mandatory"))
                raise AssertionError("member_username is mandatory")

            self.members = Member.objects.filter(
                trello_member_profile__username=member_trello_username)
        else:
            self.members = Member.objects.all()

        if not self.start():
            self.stdout.write(self.style.ERROR("Unable to fetch"))
            error_message = u"Unable to fetch boards. Is the lock /tmp/django-trello-stats-fetch-lock.txt file present?"
            warn_administrators(subject=u"Unable to fetch boards",
                                message=error_message)
            raise AssertionError(error_message)

        fetch_ok = True

        # For each selected member,
        # make two retries to the fetch procsess
        for member in self.members:
            if member.is_initialized:
                self.stdout.write(
                    self.style.SUCCESS(u"# Fetching {0} boards".format(
                        member.external_username)))
                try:
                    self.fetch_member_boards(member)
                    member.fetch_ok = True
                    self.stdout.write(
                        self.style.SUCCESS(
                            u"All boards of {0} fetched successfully".format(
                                member.external_username)))
                # If after two retries the exception persists, warn the administrators
                except Exception as e:
                    fetch_ok = True
                    member.fetch_ok = False
                    traceback_stack = traceback.format_exc()
                    warn_administrators(
                        subject=u"Unable to fetch boards",
                        message=
                        u"We tried initializing the boards but it didn't work out. {0}"
                        .format(traceback_stack))

        self.end()

        if fetch_ok:
            self.stdout.write(
                self.style.SUCCESS(
                    u"All boards of {0} members fetched successfully {1}".
                    format(self.members.count(), self.elapsed_time())))
Exemplo n.º 4
0
    def fetch_member_boards(self, member):
        # For each board that is ready and belongs to this member, fetch it
        boards = list(
            member.created_boards.filter(has_to_be_fetched=True,
                                         is_archived=False))
        failed_boards = []
        while len(boards) > 0:
            board = boards.pop()
            # Shouldn't be needed because we are getting the boards by creator, but in case
            # we add this check in case we change the method of getting boards in a future
            if board.id not in self.fetched_boards:
                if board.is_ready():
                    try:
                        self.stdout.write(
                            self.style.SUCCESS(u"Board {0} is ready".format(
                                board.name)))
                        self.fetch_board(member, board)
                        self.stdout.write(
                            self.style.SUCCESS(
                                u"Board {0} fetched successfully".format(
                                    board.name)))
                        # Mark this board as fetched
                        self.fetched_boards[board.id] = True
                    except Exception as e:
                        import traceback
                        traceback.print_exc()
                        error_message = u"Error when fetching boards. Board {0} fetch failed. Exception {1}.".format(
                            board.name, e)
                        initializer = Initializer(member, debug=False)
                        initializer.init(board.uuid)
                        # Warn the administrators messaging them the traceback and show the error on stdout
                        warn_administrators(subject=error_message,
                                            message=traceback.format_exc())
                        self.stdout.write(self.style.ERROR(error_message))
                        # Re-add the failed board to the last of the board list
                        failed_boards.append(board)
                else:
                    self.stdout.write(
                        self.style.ERROR(u"Board {0} is not ready".format(
                            board.name)))
            else:
                self.stdout.write(
                    self.style.WARNING(
                        u"Board {0} has already been fetched".format(
                            board.name)))

        if len(failed_boards) > 0:
            self.stdout.write(
                self.style.ERROR(
                    u"There are {0} boards whose fetch failed".format(
                        len(failed_boards))))
            for failed_board in failed_boards:
                self.stdout.write(
                    self.style.ERROR(u" - {0}".format(failed_board)))
Exemplo n.º 5
0
    def handle(self, *args, **options):
        now = timezone.now()

        # Calling this management action without parameteres assume that date is today
        date = now.today()

        if options['date']:
            try:
                date = datetime.datetime.strptime(options["date"], "%Y-%m-%d")
            except ValueError:
                self.stderr.write(
                    self.style.ERROR(u"Date {0} format is not valid".format(
                        options["date"])))
                return None

        # If this day is holiday, don't send anything
        iso_weekday = date.isoweekday()
        if iso_weekday == 6 or iso_weekday == 7:
            self.stdout.write(
                self.style.SUCCESS(
                    u"Daily development reports for day {0} are not sent because that day is holiday"
                    .format(date.strftime("%Y-%m-%d"))))
            return False

        start = time.time()

        developers = Member.objects.filter(is_developer=True,
                                           on_holidays=False)
        try:
            for member in developers:
                if member.daily_spent_times.filter(
                        date=date).count() > 0 and member.user:
                    daily_spent_times = member.daily_spent_times.filter(
                        date=date).order_by("date", "member")
                    Command.send_daily_development_report(
                        date, member, daily_spent_times)
                    self.stdout.write(
                        self.style.SUCCESS(
                            u"Daily report sent to developer {0}".format(
                                member.user.email)))
                elif member.user:
                    self.stdout.write(
                        self.style.WARNING(
                            u"Developer {0} has not worked on day {1}".format(
                                member.user.email, date.strftime("%Y-%m-%d"))))
                else:
                    self.stdout.write(
                        self.style.WARNING(
                            u"Developer {0} has no email".format(
                                member.external_username)))

        except Exception as e:
            warn_administrators(subject=u"Error in Daily development report",
                                message=traceback.format_exc())
            self.stdout.write(
                self.style.ERROR(u"Error in the daily development report "))

        end = time.time()
        elapsed_time = end - start

        self.stdout.write(
            self.style.SUCCESS(
                u"Daily development reports for day {0} sent successfully to {1} developers in {2} s"
                .format(date.strftime("%Y-%m-%d"), developers.count(),
                        elapsed_time)))