                     repo.milestones.push {number:'none', title:'none'}
                     for milestone in repo.milestones
                         {number, title} = milestone
-                        req.github.issues {repo:full_name, milestone:number, state:'all'}, defer err[title], milestone.issues
+                        github.issues {repo:full_name, milestone:number, state:'all'}, defer err[title], milestone.issues
         # XXX comments
+    authorize_uri = github.authorize_uri req
     res.render 'index', {inspect:util.inspect, authorize_uri, token, repos, orgs}
+
 app.get '/oauth2callback', (req, res, next) ->
     {code, state} = req.query
     if state isnt req.session.state

    req.redirect_uri = urlTweak "http://#{req.hostname}:#{port}/oauth2callback"
    next()

# github api
app.use (req, res, next) ->
    {redirect_uri} = req
    {token, state} = req.session ? {}
    {access_token} = token ? {}
    req.github =
        authorize_uri: urlTweak 'https://github.com/login/oauth/authorize',
            query: {client_id, redirect_uri, scope, state}
        get: (uri, query, cb) ->
            # XXX automatic pager?
            return cb new Error "not authorized" unless access_token
            options =
                url: urlTweak uri, {query}
                headers:
                    Accept: 'application/vnd.github.v3+json'
                    Authorization: "token #{access_token}"
                    'User-Agent': user_agent
            await request.get options, defer err, response, body
            return cb err if err
            return cb null, JSON.parse body
        getAll: (uri, query, cb) ->
            all_items = []
            query.per_page = per_page
            for page in [1..max_pages]
                query.page = page
                await req.github.get uri, query, defer err, items
                return cb err if err
                break unless items?.length
                all_items.push.apply all_items, items
                # assuming this is not one of those that disregards per_page
                break if items.length < per_page
            return cb null, all_items
        orgs: (args..., cb) ->
            {username} = query = args[0] or {}
            uri = switch true
                when username?
                    delete opts.username
                    "https://api.github.com/users/#{username}/orgs"
                else
                    'https://api.github.com/user/orgs'
            req.github.get uri, query, cb
        repos: (args..., cb) ->
            {org} = query = args[0] or {}
            uri = switch true
                when org?
                    delete query.org
                    "https://api.github.com/orgs/#{org}/repos"
                else
                    'https://api.github.com/user/repos'
            req.github.get uri, query, cb
        milestones: (args..., cb) ->
            {repo} = query = args[0] or {}
            uri = "https://api.github.com/repos/#{repo}/milestones"
            req.github.get uri, query, cb
        issues: (args..., cb) ->
            {repo, org} = query = args[0] or {}
            uri = switch true
                when repo?
                    delete query.repo
                    "https://api.github.com/repos/#{repo}/issues"
                when org?
                    delete query.org
                    "https://api.github.com/orgs/#{org}/issues"
                else
                    "https://api.github.com/issues"
            req.github.getAll uri, query, cb
        comments: (args..., cb) ->
            {repo, number} = query = args[0] or {}
            uri = "https://api.github.com/repos/#{repo}/issues/#{number}/comments"
            req.github.get uri, query, cb
    next()

app.get '/', (req, res, next) ->
    {token} = req.session
    {authorize_uri} = req.github
    if token
        err = {}
        await
            req.github.repos defer err.repos, repos
            req.github.orgs defer err.orgs, orgs
        await
            for org in orgs
                {login} = org
                req.github.repos {org:login}, defer err[login], org.repos
        await
            for org in orgs
                {login} = org
                for repo in org.repos
                    repo.milestones = {}
                    {full_name, milestones} = repo
                    req.github.milestones {repo:full_name}, defer err[full_name], repo.milestones
        await
            for org in orgs
                for repo in org.repos

    return next err if err
    req.session.token = querystring.parse body
    res.redirect '/'

app.listen port
