         Array::toDict = (key) ->
             @reduce ((dict, obj) -> dict[ obj[key] ] = obj if obj[key]?; return dict), {}
 
+        # Retrieve information on projects where autodeployment is enabled
         filterProjects = ->
-            console.log 'Filtering projects ...'
             for p in $scope.cfg.projects
                 if p.stages? and p.stages.length > 0
                     $scope.projectsFiltered.push(p)
 
-
-        initVersions = ->
-            console.log 'Initializing the versions ...'
+        # Init all the objects that will help for the mapping between BB Travis builds and GIT data (commits, tags)
+        initMappingBuildGitData = ->
             for p in $scope.projectsFiltered
+                # Object to store the latest versions (GIT tags) deployed in each stage per project
                 latestDeployedVersion = {}
                 latestDeployedVersion.projectName = p.name
                 latestDeployedVersion.stages_sorted = {}

                $scope.fscheduler = forcesched
                $scope.builderName = project + '-deploy'

                $scope.submit()

                modal = {}
                modal.modal = $uibModal.open
                    templateUrl: 'buildbot_travis/views/deploy_latest_dialog.html'

            else
                return true

        retrieveLatestCommits = (builder) ->
            data.getBuilds(limit: 50, order: '-complete_at', builderid: builder.builderid, complete: 'true', results: 0).onChange = (builds) ->
                for build in builds
                    build.getProperties().onNew = (properties) ->
                        if properties['branch']?
                            # NOT GIT tagged versions - 'post-commit' stages candidates
                            if not properties['branch'][0].match(/tags/)
                                projectName = properties['codebase'][0]
                                if properties['revision']?
                                    for version in $scope.untaggedVersionsByProject
                                        if projectName == version.projectName and properties['revision'][0] not in version.versions
                                            new_version = properties['revision'][0]
                                            version.versions.push(new_version)
                                    $scope.untaggedVersionsByProjectDict = $scope.untaggedVersionsByProject.toDict('projectName')

                            # GIT tagged versions
                            if properties['branch'][0].match(/tags/)
                                projectName = properties['codebase'][0]
                                if properties['commit-description']?
                                    for g in gitTagsByProject
                                       if projectName == g.projectName
                                            if properties['commit-description'][0] not in g.projectGitTags
                                                new_version = []
                                                new_version = properties['commit-description'][0]
                                                if g.projectGitTags[0] == 'NA'
                                                    g.projectGitTags = []
                                                if new_version[projectName] not in g.projectGitTags
                                                    g.projectGitTags.push(new_version[projectName])
                                                for r in gitTagRevisionMap
                                                    if projectName == r.projectName
                                                        new_entry_map = {}
                                                        new_entry_map.tag = properties['commit-description'][0][projectName]
                                                        new_entry_map.rev = properties['got_revision'][0][projectName]
                                                        r.map.push(new_entry_map)
                                                gitTagRevisionMapDict = gitTagRevisionMap.toDict('projectName')
                                    $scope.gitTagsByProjectDict = gitTagsByProject.toDict('projectName')


        retrieveDeployedVersions = (builder) ->
            data.getBuilds(limit: 50, order: '-complete_at', builderid: builder.builderid, complete: 'true', results: 0).onChange = (builds) ->
                for build in builds
                    build.getProperties().onNew = (properties) ->
                        if properties?
                            projectName = properties['codebase'][0]
                            # With a defined version property (ie commit has been GIT tagged)
                            if properties['version'][0]? and properties['version'][0] != ''
                                if properties['stage'][0]? and properties['stage'][0] != ''
                                    if latestDeployedVersionByProject != []
                                        for p in latestDeployedVersionByProject
                                            if p.projectName == projectName
                                                for s in p.stages
                                                    if s.stage == properties['stage'][0] and s.versions[0] == 'NA'
                                                        s.versions = []
                                                        s.versions.push(properties['version'][0])
                                                    else
                                                        new_version = properties['version'][0]
                                                        if new_version not in s.versions
                                                            s.versions.push(new_version)
                                                stage_info = {}
                                                stage_info = p.stages.toDict('stage')
                                                p.stages_sorted = stage_info
                                        $scope.latestDeployedVersionByProjectDict = latestDeployedVersionByProject.toDict('projectName')
                            # Without a defined version property (ie commit has not been GIT tagged yet, GIT tag is generated by the deploy build)
                            else if properties['revision'][0]? and properties['revision'][0] != '' and properties['version'][0] == ''
                                if properties['stage'][0]? and properties['stage'][0] != ''
                                    if latestDeployedVersionByProject != []
                                        for p in latestDeployedVersionByProject
                                            if p.projectName == projectName
                                                for x in gitTagRevisionMapDict[projectName].map
                                                    if x.rev == properties['revision'][0]
                                                        for s in p.stages
                                                            if s.stage == properties['stage'][0] and s.versions[0] == 'NA'
                                                                s.versions = []
                                                                s.versions.push(x.tag)
                                                            else
                                                                new_version = x.tag
                                                                if new_version not in s.versions
                                                                    s.versions.push(new_version)
                                                        stage_info = {}
                                                        stage_info = p.stages.toDict('stage')
                                                        p.stages_sorted = stage_info
                                        $scope.latestDeployedVersionByProjectDict = latestDeployedVersionByProject.toDict('projectName')

        retrieveInfo = ->
            # Retrieve the commit-description properties that contain ALL the versions (tagged or not)
            # First case : dev have been successfully merged, ready to be deployed in 'post commit' stages - not GIT tagged yet
            # Second case : dev have been deployed in 'post commit' stages - GIT tagged
            data.getBuilders().onNew = (builder) ->
                name = ''
                name = builder.name

                for p, k in $scope.projectsFiltered
                    if name.match(p.name)
                        if not name.match(/-deploy/) and not name.match(/-job/) and not name.match(/-try/)
                            retrieveLatestCommits(builder)

                        else if name.match(/-deploy/)
                            retrieveDeployedVersions(builder)

                        else
                            return

        filterProjects()
        initMappingBuildGitData()
        retrieveInfo()

        $scope.deploy = (tag, project, stage) ->
            $scope.selectedTag = tag
