Skip to content

Commit

Permalink
Added robin actions (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
proksh authored Aug 30, 2024
1 parent 398c367 commit ae2da37
Show file tree
Hide file tree
Showing 4 changed files with 436 additions and 213 deletions.
196 changes: 132 additions & 64 deletions ApiClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch, { FetchError, fileFromSync, FormData } from 'node-fetch';
import fetch, { fileFromSync, FormData } from 'node-fetch'

export enum BenchmarkStatus {
PENDING = 'PENDING',
Expand All @@ -9,33 +9,57 @@ export enum BenchmarkStatus {
WARNING = 'WARNING',
}

export type UploadRequest = {
export type CloudUploadRequest = {
benchmarkName?: string
repoOwner?: string
repoName?: string
pullRequestId?: string
branch?: string,
commitSha?: string,
env?: { [key: string]: string },
agent: string,
androidApiLevel?: number,
iOSVersion?: number,
includeTags: string[],
excludeTags: string[],
appBinaryId?: string,
deviceLocale?: string,
branch?: string
commitSha?: string
env?: { [key: string]: string }
agent: string
androidApiLevel?: number
iOSVersion?: number
includeTags: string[]
excludeTags: string[]
appBinaryId?: string
deviceLocale?: string
}

export type RobinUploadRequest = {
projectId: string
repoOwner?: string
repoName?: string
pullRequestId?: string
branch?: string
commitSha?: string
env?: { [key: string]: string }
agent: string
androidApiLevel?: number
iOSVersion?: number
includeTags: string[]
excludeTags: string[]
appBinaryId?: string
deviceLocale?: string
}

// irrelevant data has been factored out from this model
export type UploadResponse = {
uploadId: string,
teamId: string,
targetId: string,
uploadId: string
teamId: string
targetId: string
appBinaryId: string
}

export type RobinUploadResponse = {
uploadId: string
orgId: string
appId: string
appBinaryId: string
}

export class UploadStatusError {
constructor(public status: number, public text: string) { }
constructor(public status: number, public text: string) {}
}

export enum CancellationReason {
Expand All @@ -46,89 +70,133 @@ export enum CancellationReason {
}

export type Flow = {
name: string,
status: BenchmarkStatus,
errors?: string[],
name: string
status: BenchmarkStatus
errors?: string[]
cancellationReason?: CancellationReason
}

export type UploadStatusResponse = {
uploadId: string,
status: BenchmarkStatus,
completed: boolean,
uploadId: string
status: BenchmarkStatus
completed: boolean
flows: Flow[]
}

export default class ApiClient {

constructor(
private apiKey: string,
private apiUrl: string
) { }
private apiUrl: string,
private projectId?: string
) {}

async uploadRequest(
request: UploadRequest,
async cloudUploadRequest(
request: CloudUploadRequest,
appFile: string | null,
workspaceZip: string | null,
mappingFile: string | null,
mappingFile: string | null
): Promise<UploadResponse> {
const formData = new FormData()

formData.set('request', JSON.stringify(request))
if (appFile) {
formData.set(
'app_binary',
fileFromSync(appFile)
)
formData.set('app_binary', fileFromSync(appFile))
}

if (workspaceZip) {
formData.set(
'workspace',
fileFromSync(workspaceZip)
);
formData.set('workspace', fileFromSync(workspaceZip))
}

if (mappingFile) {
formData.set(
'mapping',
fileFromSync(mappingFile)
)
formData.set('mapping', fileFromSync(mappingFile))
}

formData.set('request', JSON.stringify(request))
const res = await fetch(`${this.apiUrl}/v2/upload`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
Authorization: `Bearer ${this.apiKey}`,
},
body: formData
});
body: formData,
})
if (!res.ok) {
const body = await res.text();
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`);
const body = await res.text()
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`)
}
return await res.json() as UploadResponse;
return (await res.json()) as UploadResponse
}

async getUploadStatus(
uploadId: string,
): Promise<UploadStatusResponse> {
const res = await fetch(`${this.apiUrl}/v2/upload/${uploadId}/status?includeErrors=true`, {
method: 'GET',
async robinUploadRequest(
request: RobinUploadRequest,
appFile: string | null,
workspaceZip: string | null,
mappingFile: string | null
): Promise<RobinUploadResponse> {
const formData = new FormData()

if (appFile) {
formData.set('app_binary', fileFromSync(appFile))
}
if (workspaceZip) {
formData.set('workspace', fileFromSync(workspaceZip))
}
if (mappingFile) {
formData.set('mapping', fileFromSync(mappingFile))
}
formData.set('request', JSON.stringify(request))

const res = await fetch(`${this.apiUrl}/runMaestroTest`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.apiKey}`,
Authorization: `Bearer ${this.apiKey}`,
},
});
body: formData,
})
if (!res.ok) {
const body = await res.text();
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`);
const body = await res.text()
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`)
}
return (await res.json()) as RobinUploadResponse
}

if (res.status >= 400) {
const text = await res.text();
Promise.reject(new UploadStatusError(res.status, text));
async getUploadStatus(uploadId: string): Promise<UploadStatusResponse> {
// If Project Id exist - Hit robin
if (!!this.projectId) {
const res = await fetch(`${this.apiUrl}/upload/${uploadId}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
})
if (!res.ok) {
const body = await res.text()
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`)
}

if (res.status >= 400) {
const text = await res.text()
Promise.reject(new UploadStatusError(res.status, text))
}

return (await res.json()) as UploadStatusResponse
}
// Else if no project id - Hit Cloud
else {
const res = await fetch(
`${this.apiUrl}/v2/upload/${uploadId}/status?includeErrors=true`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
}
)
if (!res.ok) {
const body = await res.text()
throw new Error(`Request to ${res.url} failed (${res.status}): ${body}`)
}

return await res.json() as UploadStatusResponse;
if (res.status >= 400) {
const text = await res.text()
Promise.reject(new UploadStatusError(res.status, text))
}

return (await res.json()) as UploadStatusResponse
}
}
}
Loading

0 comments on commit ae2da37

Please sign in to comment.