diff options
author | Nick Coutsos <[email protected]> | 2021-11-05 12:44:02 -0400 |
---|---|---|
committer | Nick Coutsos <[email protected]> | 2021-11-05 12:44:02 -0400 |
commit | 2aaa651352fb429061860ff4a47534dc63384d25 (patch) | |
tree | 143d1edb56f706193ed5fb00ba1b017890eb7d37 | |
parent | 8d7a91628edb5c8285c15c5fa7ec301aa204e136 (diff) | |
download | keymap-editor-2aaa651352fb429061860ff4a47534dc63384d25.tar.gz keymap-editor-2aaa651352fb429061860ff4a47534dc63384d25.zip |
Fetch branches for a selected repo
-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 } |