|
1 | 1 | import browser from "webextension-polyfill"; |
2 | | -import { MessageType } from "src/types"; |
| 2 | +import { |
| 3 | + FileCoverageReportResponse, |
| 4 | + FileMetadata, |
| 5 | + MessageType, |
| 6 | +} from "src/types"; |
| 7 | + |
| 8 | +export async function getMetadata(url: string): Promise<FileMetadata> { |
| 9 | + const response = await fetch(url).then((response) => response.json()); |
| 10 | + return { |
| 11 | + owner: response.payload.repo.ownerLogin, |
| 12 | + repo: response.payload.repo.name, |
| 13 | + path: response.payload.path, |
| 14 | + commit: response.payload.refInfo.currentOid, |
| 15 | + }; |
| 16 | +} |
3 | 17 |
|
4 | | -export async function getFlags(url: { |
5 | | - [key: string]: string; |
6 | | -}): Promise<string[]> { |
| 18 | +export async function getFlags(metadata: FileMetadata): Promise<string[]> { |
7 | 19 | const payload = { |
8 | 20 | service: "github", |
9 | | - owner: url.owner, |
10 | | - repo: url.repo, |
| 21 | + owner: metadata.owner, |
| 22 | + repo: metadata.repo, |
11 | 23 | }; |
12 | 24 |
|
13 | | - const flagsResponse = await browser.runtime.sendMessage({ |
| 25 | + const response = await browser.runtime.sendMessage({ |
14 | 26 | type: MessageType.FETCH_FLAGS_LIST, |
15 | 27 | payload, |
16 | 28 | referrer: window.location.href, |
17 | 29 | }); |
18 | 30 |
|
19 | | - const flags = flagsResponse.ok ? flagsResponse.data.results : []; |
| 31 | + const flags = response.ok ? response.data.results : []; |
20 | 32 |
|
21 | 33 | return flags.map((f: any) => f.flag_name); |
22 | 34 | } |
23 | 35 |
|
24 | | -export async function getComponents(url: { |
25 | | - [key: string]: string; |
26 | | -}): Promise<string[]> { |
| 36 | +export async function getComponents(metadata: FileMetadata): Promise<string[]> { |
27 | 37 | const payload = { |
28 | 38 | service: "github", |
29 | | - owner: url.owner, |
30 | | - repo: url.repo, |
| 39 | + owner: metadata.owner, |
| 40 | + repo: metadata.repo, |
31 | 41 | }; |
32 | 42 |
|
33 | | - const componentsResponse = await browser.runtime.sendMessage({ |
| 43 | + const response = await browser.runtime.sendMessage({ |
34 | 44 | type: MessageType.FETCH_COMPONENTS_LIST, |
35 | 45 | payload, |
36 | 46 | referrer: window.location.href, |
37 | 47 | }); |
38 | 48 |
|
39 | | - const components = componentsResponse.ok ? componentsResponse.data : []; |
| 49 | + const components = response.ok ? response.data : []; |
40 | 50 |
|
41 | 51 | return components.map((c: any) => c.component_id); |
42 | 52 | } |
43 | 53 |
|
44 | 54 | export async function getCommitReport( |
45 | | - url: { [key: string]: string }, |
| 55 | + metadata: FileMetadata, |
46 | 56 | flag: string | undefined, |
47 | 57 | component_id: string | undefined |
48 | | -) { |
49 | | - const commonPayload = { |
| 58 | +): Promise<FileCoverageReportResponse> { |
| 59 | + const payload = { |
50 | 60 | service: "github", |
51 | | - owner: url.owner, |
52 | | - repo: url.repo, |
53 | | - path: url.path, |
| 61 | + owner: metadata.owner, |
| 62 | + repo: metadata.repo, |
| 63 | + path: metadata.path, |
| 64 | + sha: metadata.commit, |
54 | 65 | flag, |
55 | 66 | component_id, |
56 | 67 | }; |
57 | 68 |
|
58 | | - // TODO: check if codecov can figure out whether branch or sha |
59 | | - const shaResponse = await browser.runtime.sendMessage({ |
60 | | - type: MessageType.FETCH_COMMIT_REPORT, |
61 | | - payload: { |
62 | | - ...commonPayload, |
63 | | - sha: url.ref, |
64 | | - }, |
65 | | - referrer: window.location.href, |
66 | | - }); |
67 | | - |
68 | | - if (shaResponse.ok) { |
69 | | - return shaResponse.data; |
70 | | - } |
71 | | - |
72 | | - const branchResponse = await browser.runtime.sendMessage({ |
| 69 | + const response = await browser.runtime.sendMessage({ |
73 | 70 | type: MessageType.FETCH_COMMIT_REPORT, |
74 | | - payload: { |
75 | | - ...commonPayload, |
76 | | - branch: url.ref, |
77 | | - }, |
| 71 | + payload, |
78 | 72 | referrer: window.location.href, |
79 | 73 | }); |
80 | 74 |
|
81 | | - return branchResponse.data; |
| 75 | + return response.data; |
82 | 76 | } |
83 | 77 |
|
84 | 78 | export async function getPRReport(url: any) { |
|
0 commit comments