Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Presigned url #3724

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
intial clean up 🧹
  • Loading branch information
RulaKhaled committed Jul 7, 2022
commit eabe5802aa1fd953b13328db5e30dfe9fbaa04a6
3 changes: 3 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ const defaultConfig = {
SENTRY_ENVIRONMENT: 'staging',
}

const INTERNAL_API = `${defaultConfig.API_URL}/internal`

function removeReactAppPrefix(obj) {
// in .env file, the variable must start with REACT_APP_
// to be injected in the application, so we remove that
@@ -16,6 +18,7 @@ function removeReactAppPrefix(obj) {

const config = {
...defaultConfig,
INTERNAL_API,
...removeReactAppPrefix(process.env),
...window.configEnv,
}
12 changes: 4 additions & 8 deletions src/services/uploadPresignedUrl/hooks.js
Original file line number Diff line number Diff line change
@@ -3,17 +3,13 @@ import { useParams } from 'react-router-dom'

import Api from 'shared/api'


function fetchUploadPresignedUrl({ provider, path }) {
return Api.get({ path, provider }, { useUploadPath: true })
return Api.get({ path, provider, isUploadPath: true })
}

export function useUploadPresignedUrl({ path }) {
const { provider } = useParams()
return useQuery(
['uploadPresignedUrl', provider],
() => {
return fetchUploadPresignedUrl({ provider, path })
}
)
return useQuery(['uploadPresignedUrl', provider], () => {
return fetchUploadPresignedUrl({ provider, path })
})
}
9 changes: 4 additions & 5 deletions src/shared/api/api.js
Original file line number Diff line number Diff line change
@@ -13,9 +13,9 @@ function _fetch({
body,
provider = 'gh',
extraHeaders = {},
useUploadPath,
isUploadPath,
}) {
const uri = generatePath({ path, query, useUploadPath })
const uri = generatePath({ path, query, isUploadPath })
const headers = {
Accept: 'application/json',
'Content-Type': 'application/json; charset=utf-8',
@@ -31,7 +31,7 @@ function _fetch({
}).then(async (res) => {
let data = null
try {
data = camelizeKeys(await res.json())
data = camelizeKeys(await res.json())
} catch {
// nothing to do, body can be empty
}
@@ -46,11 +46,10 @@ function _fetch({
}

function prefillMethod(method) {
return (config, useUploadPath) =>
return (config) =>
_fetch({
...config,
method,
useUploadPath,
})
}

14 changes: 9 additions & 5 deletions src/shared/api/api.test.js
Original file line number Diff line number Diff line change
@@ -26,7 +26,10 @@ const userData = {
],
}

const mockedPresignedUrl = {presignedUrl: "http://minio:9000/archive/v4/raw/2022-06-23/942173DE95CBF167C5683F40B7DB34C0/ee3ecad424e67419d6c4531540f1ef5df045ff12/919ccc6d-7972-4895-b289-f2d569683a17.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=codecov-default-key%2F20220705%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220705T101702Z&X-Amz-Expires=10&X-Amz-SignedHeaders=host&X-Amz-Signature=8846492d85f62187493cbff3631ec7f0ccf2d355f768eecf294f0572cf758e4c"}
const mockedPresignedUrl = {
presignedUrl:
'http://minio:9000/archive/v4/raw/2022-06-23/942173DE95CBF167C5683F40B7DB34C0/ee3ecad424e67419d6c4531540f1ef5df045ff12/919ccc6d-7972-4895-b289-f2d569683a17.txt?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=codecov-default-key%2F20220705%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220705T101702Z&X-Amz-Expires=10&X-Amz-SignedHeaders=host&X-Amz-Signature=8846492d85f62187493cbff3631ec7f0ccf2d355f768eecf294f0572cf758e4c',
}

const server = setupServer(
rest.get('/internal/test', (req, res, ctx) => {
@@ -130,10 +133,11 @@ describe('when calling an endpoint with a token', () => {

describe('when using a get request with upload path', () => {
beforeEach(() => {
return Api.get(
{ path: '/upload', provider: 'gh' },
{ useUploadPath: true }
).then((data) => {
return Api.get({
path: '/upload',
provider: 'gh',
isUploadPath: true,
}).then((data) => {
result = data
})
})
6 changes: 2 additions & 4 deletions src/shared/api/helpers.js
Original file line number Diff line number Diff line change
@@ -13,10 +13,8 @@ export const ProviderCookieKeyMapping = {
bitbucket: 'bitbucket-token',
}

export function generatePath({ path, query, useUploadPath = false }) {
const baseUrl = useUploadPath
? config.API_URL
: `${config.API_URL}/internal`
export function generatePath({ path, query, isUploadPath = false }) {
const baseUrl = isUploadPath ? config.API_URL : config.INTERNAL_API
const queryString = qs.stringify(snakeifyKeys(query), {
arrayFormat: 'repeat',
})
4 changes: 2 additions & 2 deletions src/shared/api/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -20,8 +20,8 @@ describe('generatePath', () => {
).toStrictEqual(`${config.API_URL}/internal/epic?rocket=league&fort=nite`)
})

it('generates a upload report path when use useUploadPath', () => {
expect(generatePath({ path: '/upload', useUploadPath: true })).toStrictEqual(
it('generates a upload report path when use isUploadPath', () => {
expect(generatePath({ path: '/upload', isUploadPath: true })).toStrictEqual(
`${config.API_URL}/upload`
)
})