Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

gitHub api enhance #553

Merged
merged 3 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions containers/AvatarAdder/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import R from 'ramda'
import { useEffect } from 'react'

import { buildLog } from '@utils'
import { githubApi } from '@services'
import { githubAPI } from '@services'

/* eslint-disable-next-line */
const log = buildLog('L:AvatarAdder')
Expand All @@ -14,13 +14,13 @@ export const onSearch = e => {
log('store.searchValue: ', store.searchValue)
store.mark({ searching: true, searchValue: e.target.value })

githubApi
githubAPI
.searchUser(store.searchValue)
.then(res => {
store.mark({ githubUser: githubApi.transformUser(res) })
store.mark({ githubUser: githubAPI.transformUser(res) })
store.mark({ searching: false })
})
.catch(e => store.handleError(githubApi.parseError(e)))
.catch(e => store.handleError(githubAPI.parseError(e)))
}
}

Expand Down
6 changes: 3 additions & 3 deletions containers/CheatsheetThread/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Prism from 'mastani-codehighlight'
import { TYPE, EVENT, ERR, THREAD } from '@constant'
import { asyncSuit, buildLog, errRescue, BStore, nilOrEmpty } from '@utils'

import { githubApi } from '@services'
import { githubAPI } from '@services'
import S from './schema'

/* eslint-disable-next-line */
Expand Down Expand Up @@ -45,15 +45,15 @@ export const syncCheetsheetFromGithub = () => {
return store.mark({ showSyncWarning: true })
}

githubApi
githubAPI
.searchCheatsheet(store.curCommunity.raw)
.then(res => {
if (!res || R.startsWith('404', res))
return store.mark({ curView: TYPE.NOT_FOUND })

syncCheatsheet(res)
})
.catch(e => store.handleError(githubApi.parseError(e)))
.catch(e => store.handleError(githubAPI.parseError(e)))
}

export const addContributor = user => {
Expand Down
8 changes: 4 additions & 4 deletions containers/RepoEditor/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
BStore,
errRescue,
} from '@utils'
import { githubApi } from '@services'
import { githubAPI } from '@services'

import S from './schema'

Expand Down Expand Up @@ -41,16 +41,16 @@ export const onGithubSearch = () => {
const { owner, name } = store

store.mark({ searching: true })
githubApi
githubAPI
.searchRepo(owner, name)
.then(res => {
store.mark({
editRepo: githubApi.transformRepo(res),
editRepo: githubAPI.transformRepo(res),
searching: false,
curView: 'show',
})
})
.catch(e => store.handleError(githubApi.parseError(e)))
.catch(e => store.handleError(githubAPI.parseError(e)))
}

/* eslint-disable-next-line */
Expand Down
8 changes: 4 additions & 4 deletions containers/RepoViewer/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from 'react'

import { TYPE, EVENT, ERR } from '@constant'
import { asyncSuit, buildLog, errRescue } from '@utils'
import { githubApi } from '@services'
import { githubAPI } from '@services'

import S from './schema'

Expand Down Expand Up @@ -62,12 +62,12 @@ const DataSolver = [
log('should sync repo: ', store.viewingData)
const { id, ownerName, title } = store.viewingData

githubApi
githubAPI
.searchRepo(ownerName, title)
.then(res =>
sr71$.mutate(S.updateRepo, { id, ...githubApi.transformRepo(res) })
sr71$.mutate(S.updateRepo, { id, ...githubAPI.transformRepo(res) })
)
.catch(e => store.handleError(githubApi.parseError(e)))
.catch(e => store.handleError(githubAPI.parseError(e)))
},
},
]
Expand Down
6 changes: 3 additions & 3 deletions containers/WikiThread/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect } from 'react'

import { TYPE, EVENT, ERR, THREAD } from '@constant'
import { asyncSuit, buildLog, errRescue, BStore, nilOrEmpty } from '@utils'
import { githubApi } from '@services'
import { githubAPI } from '@services'

import S from './schema'

Expand Down Expand Up @@ -43,14 +43,14 @@ export const syncWikiFromGithub = () => {
return store.mark({ showSyncWarning: true })
}

githubApi
githubAPI
.searchWiki(store.curCommunity.raw)
.then(res => {
if (!res || R.startsWith('404', res))
return store.mark({ curView: TYPE.NOT_FOUND })
syncWiki(res)
})
.catch(e => store.handleError(githubApi.parseError(e)))
.catch(e => store.handleError(githubAPI.parseError(e)))
}

export const addContributor = user => {
Expand Down
1 change: 0 additions & 1 deletion package-docker.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"draft-js-mention-plugin": "3.1.3",
"draft-js-plugins-editor": "2.1.1",
"express": "^4.16.4",
"fetch-jsonp": "^1.1.3",
"glob": "^7.1.2",
"graphql": "14.3.1",
"graphql-request": "^1.6.0",
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"draft-js-mention-plugin": "3.1.3",
"draft-js-plugins-editor": "2.1.1",
"express": "^4.16.4",
"fetch-jsonp": "^1.1.3",
"glob": "^7.1.2",
"graphql": "14.4.0",
"graphql-request": "^1.6.0",
Expand Down
31 changes: 22 additions & 9 deletions services/github_api/client.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
import fetchJsonp from 'fetch-jsonp'
import fetch from 'isomorphic-fetch'

import { BStore, makeGithubExplore } from '@utils'
import { graphqlEndpoint } from './config'

const token = BStore.get('github_token')

const v3EndpointOpt = {
headers: { Authorization: `token ${token}` },
const rawOptions = {
headers: {
Authorization: `token ${token}`,
Accept: 'application/vnd.github.VERSION.raw',
'Content-Type': 'application/vnd.github.VERSION.raw',
},
}

const jsonOptions = {
headers: {
Authorization: `token ${token}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
}

// graphql client
export const graphqlClient = makeGithubExplore(graphqlEndpoint, token)
// jsonp client
export const restpClient = api =>
fetchJsonp(`${api}`, v3EndpointOpt).then(r => r.json())
// rest client
/* fetch(`${api}`, v3EndpointOpt).then(r => r.json()) */
export const restClient = api => fetch(`${api}`).then(r => r.text())

export const restClient = (api, fmt = 'json') => {
if (fmt === 'json') {
return fetch(`${api}`, jsonOptions).then(r => r.json())
}

return fetch(`${api}`, rawOptions).then(r => r.text())
}
4 changes: 2 additions & 2 deletions services/github_api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { searchUserPromise, ransformUser } from './user_search'
import { searchWikiPromise } from './wiki_search'
import { searchCheatsheeetPromise } from './cheatsheet_search'

const githubApi = {
const githubAPI = {
// search repo
searchRepo: (owner, name) => searchRepoPromise(owner, name),
transformRepo: res => transformRepo(res),
Expand All @@ -34,4 +34,4 @@ const githubApi = {
},
}

export default githubApi
export default githubAPI
24 changes: 12 additions & 12 deletions services/github_api/repo_search.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
import R from 'ramda'
import { timeout } from 'promise-timeout'

import { ISSUE_ADDR } from '@config'

import { TIMEOUT_SEC, restEndpoint } from './config'
import { graphqlClient, restpClient } from './client'
import { graphqlClient, restClient } from './client'

import S from './schema'

const baseInfoQuery = (owner, name) =>
graphqlClient.request(S.repository, { owner, name })
const baseInfoQuery = (owner, name) => {
return graphqlClient.request(S.repository, { owner, name })
}

const contributorsQuery = (owner, name) => {
const path = 'contributors?page=1&per_page=8'
const api = `${restEndpoint}/repos/${owner}/${name}/${path}`

return restpClient(`${api}`)
return restClient(`${api}`)
}

const readmeQuery = (owner, name) => {
return restClient(`${restEndpoint}/repos/${owner}/${name}/readme`, 'raw')
}

export const searchRepoPromise = (owner, name) =>
Promise.all([
timeout(baseInfoQuery(owner, name), TIMEOUT_SEC),
timeout(contributorsQuery(owner, name), TIMEOUT_SEC),
timeout(readmeQuery(owner, name), TIMEOUT_SEC),
])

const getRelaseTag = releases => {
Expand All @@ -37,7 +41,8 @@ const getLicense = value => {
// transform to match our model
export const transformRepo = res => {
const baseInfoRes = res[0].repository
const contributorsRes = res[1].data
const contributorsRes = res[1]
const readme = res[2]
const contributors = []

R.forEach(user => {
Expand All @@ -61,14 +66,9 @@ export const transformRepo = res => {
licenseInfo,
homepageUrl,
releases,
object,
primaryLanguage,
} = baseInfoRes

const readme = object
? object.text
: `同步错误: 目前只同步源仓库中的 README.md 文件,如果源仓库中为 README.MD / readme.md / readme.MD 等格式可能会导致该错误。 如果是其他原因,[恳请提交 issue](${ISSUE_ADDR}/new)`

return {
title: name,
ownerName: owner.login,
Expand Down
6 changes: 1 addition & 5 deletions services/github_api/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const user = `
}
}
`

const repository = `
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
Expand All @@ -22,11 +23,6 @@ const repository = `
licenseInfo {
key
}
object(expression: "master:README.md") {
... on Blob {
text
}
}
forkCount
stargazers {
totalCount
Expand Down
2 changes: 1 addition & 1 deletion services/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as githubApi } from './github_api'
export { default as githubAPI } from './github_api'
export { default as sentry } from './sentry'
2 changes: 1 addition & 1 deletion utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export {
atomizeValues,
} from './graphql_helper'

// export { default as githubApi } from './github_api'
// export { default as githubAPI } from './github_api'

export {
parseURL,
Expand Down
Loading