Skip to content

Commit 7445918

Browse files
committed
feat: add method upsertGithubRepository in GitHub provider
1 parent 24f477e commit 7445918

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/providers/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,21 @@ const fetchOrgReposListByLogin = async (login) => {
4242
return repoList
4343
}
4444

45+
const fetchRepoByFullName = async (fullName) => {
46+
debug(`Fetching repository (${fullName})...`)
47+
ensureGithubToken()
48+
const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN })
49+
const { data } = await octokit.request('GET /repos/{owner}/{repo}', {
50+
owner: fullName.split('/')[0],
51+
repo: fullName.split('/')[1]
52+
})
53+
return data
54+
}
55+
4556
const github = {
4657
fetchOrgByLogin,
4758
fetchOrgReposListByLogin,
59+
fetchRepoByFullName
4860
}
4961

5062
module.exports = {

src/store/index.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
const debug = require('debug')('store')
22

3+
const upsertGithubRepository = knex => async (repository, orgId) => {
4+
debug(`Upserting repository (${repository.full_name})...`)
5+
6+
const existingRepository = await knex('github_repositories').where({ full_name: repository.full_name }).first()
7+
if (existingRepository) {
8+
return knex('github_repositories').where({ full_name: repository.full_name, github_organization_id: orgId }).update(repository).returning('*')
9+
} else {
10+
return knex('github_repositories').insert({ ...repository, github_organization_id: orgId }).returning('*')
11+
}
12+
}
13+
314
const getAllGithubOrganizations = knex => async () => {
415
debug('Getting all GitHub organizations...')
516
return knex('github_organizations').select()
@@ -41,7 +52,8 @@ const initializeStore = (knex) => {
4152
addProject: addProject(knex),
4253
addGithubOrganization: addGithubOrganization(knex),
4354
getAllGithubOrganizations: getAllGithubOrganizations(knex),
44-
updateGithubOrganization: updateGithubOrganization(knex)
55+
updateGithubOrganization: updateGithubOrganization(knex),
56+
upsertGithubRepository: upsertGithubRepository(knex)
4557
}
4658
}
4759

0 commit comments

Comments
 (0)