def ensure_watcher(owner, repo, watcher, date_added = nil)
      project = ensure_repo(owner, repo)
      new_watcher = ensure_user(watcher, false, false)

      if new_watcher.nil? or project.nil?
        warn "Could not find watcher #{watcher} or repo #{owner}/#{repo}"
        return
      end

      watchers = db[:watchers]
      watcher_exist = watchers.first(:user_id => new_watcher[:id],
                                     :repo_id => project[:id])

      retrieved = retrieve_watcher(owner, repo, watcher)

      created_at = case
                     when (not date_added.nil?)
                       date(date_added)
                     when (not retrieved.nil? and not retrieved['created_at'].nil?)
                       date(retrieved['created_at'])
                     else
                       max(date(project[:created_at]),
                           date(new_watcher[:created_at]))
                   end

      if watcher_exist.nil?

        if retrieved.nil?
          warn "Could not retrieve watcher #{watcher} of repo #{owner}/#{repo}"
          return
        end

        watchers.insert(
            :user_id => new_watcher[:id],
            :repo_id => project[:id],
            :created_at => date(created_at)
        )
        info "Added watcher #{owner}/#{repo} -> #{watcher}"
      else
        debug "Watcher #{owner}/#{repo} -> #{watcher} exists"
      end

      w = watchers.first(:user_id => new_watcher[:id],
                     :repo_id => project[:id])

      if w[:created_at]  < created_at
        watchers.filter(:user_id => new_watcher[:id],
                        :repo_id => project[:id])\
                .update(:created_at => date(created_at))
        info "Updated watcher #{owner}/#{repo} -> #{watcher}, created_at -> #{date_added}"
      end

      w
    end
def ensure_watcher(owner, repo, watcher, date_added = nil)
      project = ensure_repo(owner, repo)
      new_watcher = ensure_user(watcher, false, false)

      if new_watcher.nil? or project.nil?
        warn "Could not find watcher #{watcher} or repo #{owner}/#{repo}"
        return
      end

      watchers = db[:watchers]
      watcher_exist = watchers.first(:user_id => new_watcher[:id],
                                     :repo_id => project[:id])

      retrieved = retrieve_watcher(owner, repo, watcher)

      created_at = case
                     when (not date_added.nil?)
                       date(date_added)
                     when (not retrieved.nil? and not retrieved['created_at'].nil?)
                       date(retrieved['created_at'])
                     else
                       max(date(project[:created_at]),
                           date(new_watcher[:created_at]))
                   end

      if watcher_exist.nil?

        if retrieved.nil?
          warn "Could not retrieve watcher #{watcher} of repo #{owner}/#{repo}"
          return
        end

        watchers.insert(
            :user_id => new_watcher[:id],
            :repo_id => project[:id],
            :created_at => date(created_at)
        )
        info "Added watcher #{owner}/#{repo} -> #{watcher}"
      else
        debug "Watcher #{owner}/#{repo} -> #{watcher} exists"
      end

      w = watchers.first(:user_id => new_watcher[:id],
                     :repo_id => project[:id])

      if w[:created_at]  < created_at
        watchers.filter(:user_id => new_watcher[:id],
                        :repo_id => project[:id])\
                .update(:created_at => date(created_at))
        info "Updated watcher #{owner}/#{repo} -> #{watcher}, created_at -> #{date_added}"
      end

      w
    end
Make sure that a watcher/stargazer exists for a repository