diff options
-rw-r--r-- | api/routes/github.js | 16 | ||||
-rw-r--r-- | api/services/github/index.js | 4 | ||||
-rw-r--r-- | api/services/github/installations.js | 20 |
3 files changed, 37 insertions, 3 deletions
diff --git a/api/routes/github.js b/api/routes/github.js index a1f23b4..9d37a01 100644 --- a/api/routes/github.js +++ b/api/routes/github.js @@ -7,12 +7,14 @@ const { verifyUserToken, fetchInstallation, fetchInstallationRepos, + fetchRepoBranches, fetchKeyboardFiles, createOauthFlowUrl, createOauthReturnUrl, commitChanges, InvalidRepoError, } = require('../services/github') +const { createInstallationToken } = require('../services/github/auth') const { parseKeymap } = require('../services/zmk/keymap') const router = Router() @@ -81,6 +83,19 @@ const getInstallation = async (req, res, next) => { } } +const getBranches = async (req, res, next) => { + const { installationId, repository } = req.params + + try { + const { data: { token: installationToken } } = await createInstallationToken(installationId) + const branches = await fetchRepoBranches(installationToken, repository) + + res.json(branches) + } catch (err) { + next(err) + } +} + const getKeyboardFiles = async (req, res, next) => { const { installationId, repository } = req.params @@ -117,6 +132,7 @@ const receiveWebhook = (req, res) => { } router.get('/authorize', authorize) +router.get('/installation/:installationId/:repository/branches', authenticate, getBranches) router.get('/installation', authenticate, getInstallation) router.get('/keyboard-files/:installationId/:repository', authenticate, getKeyboardFiles) router.post('/keyboard-files/:installationId/:repository', authenticate, updateKeyboardFiles) diff --git a/api/services/github/index.js b/api/services/github/index.js index 8443dd7..6bf917a 100644 --- a/api/services/github/index.js +++ b/api/services/github/index.js @@ -9,7 +9,8 @@ const { const { fetchInstallation, - fetchInstallationRepos + fetchInstallationRepos, + fetchRepoBranches } = require('./installations') const { @@ -27,6 +28,7 @@ module.exports = { verifyUserToken, fetchInstallation, fetchInstallationRepos, + fetchRepoBranches, InvalidRepoError, fetchKeyboardFiles, commitChanges diff --git a/api/services/github/installations.js b/api/services/github/installations.js index 346dc45..fad5468 100644 --- a/api/services/github/installations.js +++ b/api/services/github/installations.js @@ -17,7 +17,7 @@ function fetchInstallation (user) { async function fetchInstallationRepos (installationToken, installationId) { const initialPage = `/user/installations/${installationId}/repositories` const repositories = [] - + let url = initialPage while (url) { console.log('fetching page', url) @@ -30,7 +30,23 @@ async function fetchInstallationRepos (installationToken, installationId) { return repositories } +async function fetchRepoBranches (installationToken, repo) { + const initialPage = `/repos/${repo}/branches` + const branches = [] + + let url = initialPage + while (url) { + const { headers, data } = await api.request({ url, token: installationToken }) + const paging = linkHeader.parse(headers.link || '') + branches.push(...data) + url = paging.get('rel', 'next')?.[0]?.uri + } + + return branches +} + module.exports = { fetchInstallation, - fetchInstallationRepos + fetchInstallationRepos, + fetchRepoBranches } |