Skip to content

Commit bbb6f10

Browse files
authored
Add new GitHub API client with functions for new MRVA flow (#1527)
1 parent 0476815 commit bbb6f10

4 files changed

Lines changed: 203 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Credentials } from '../../authentication';
2+
import { OctokitResponse } from '@octokit/types/dist-types';
3+
import { VariantAnalysisSubmission } from '../shared/variant-analysis';
4+
import {
5+
VariantAnalysis,
6+
VariantAnalysisRepoTask,
7+
VariantAnalysisSubmissionRequest
8+
} from './variant-analysis';
9+
10+
export async function submitVariantAnalysis(
11+
credentials: Credentials,
12+
submissionDetails: VariantAnalysisSubmission
13+
): Promise<VariantAnalysis> {
14+
const octokit = await credentials.getOctokit();
15+
16+
const { actionRepoRef, query, databases, controllerRepoId } = submissionDetails;
17+
18+
const data: VariantAnalysisSubmissionRequest = {
19+
action_repo_ref: actionRepoRef,
20+
language: query.language,
21+
query_pack: query.pack,
22+
repositories: databases.repositories,
23+
repository_lists: databases.repositoryLists,
24+
repository_owners: databases.repositoryOwners,
25+
};
26+
27+
const response: OctokitResponse<VariantAnalysis> = await octokit.request(
28+
'POST /repositories/:controllerRepoId/code-scanning/codeql/variant-analyses',
29+
{
30+
controllerRepoId,
31+
data
32+
}
33+
);
34+
35+
return response.data;
36+
}
37+
38+
export async function getVariantAnalysis(
39+
credentials: Credentials,
40+
controllerRepoId: number,
41+
variantAnalysisId: number
42+
): Promise<VariantAnalysis> {
43+
const octokit = await credentials.getOctokit();
44+
45+
const response: OctokitResponse<VariantAnalysis> = await octokit.request(
46+
'GET /repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId',
47+
{
48+
controllerRepoId,
49+
variantAnalysisId
50+
}
51+
);
52+
53+
return response.data;
54+
}
55+
56+
export async function getVariantAnalysisRepo(
57+
credentials: Credentials,
58+
controllerRepoId: number,
59+
variantAnalysisId: number,
60+
repoId: number
61+
): Promise<VariantAnalysisRepoTask> {
62+
const octokit = await credentials.getOctokit();
63+
64+
const response: OctokitResponse<VariantAnalysisRepoTask> = await octokit.request(
65+
'GET /repositories/:controllerRepoId/code-scanning/codeql/variant-analyses/:variantAnalysisId/repositories/:repoId',
66+
{
67+
controllerRepoId,
68+
variantAnalysisId,
69+
repoId
70+
}
71+
);
72+
73+
return response.data;
74+
}
75+
76+
export async function getRepositoryIdFromNwo(
77+
credentials: Credentials,
78+
owner: string,
79+
repo: string
80+
): Promise<number> {
81+
const octokit = await credentials.getOctokit();
82+
83+
const response = await octokit.rest.repos.get({ owner, repo });
84+
return response.data.id;
85+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Defines basic information about a repository.
3+
*
4+
* Different parts of the API may return different subsets of information
5+
* about a repository, but this model represents the very basic information
6+
* that will always be available.
7+
*/
8+
export interface Repository {
9+
id: number,
10+
name: string,
11+
full_name: string,
12+
private: boolean,
13+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { Repository } from './repository';
2+
3+
export interface VariantAnalysisSubmissionRequest {
4+
action_repo_ref: string,
5+
language: VariantAnalysisQueryLanguage,
6+
query_pack: string,
7+
repositories?: string[],
8+
repository_lists?: string[],
9+
repository_owners?: string[]
10+
}
11+
12+
export type VariantAnalysisQueryLanguage =
13+
| 'csharp'
14+
| 'cpp'
15+
| 'go'
16+
| 'java'
17+
| 'javascript'
18+
| 'python'
19+
| 'ruby';
20+
21+
export interface VariantAnalysis {
22+
id: number,
23+
controller_repo: Repository,
24+
actor_id: number,
25+
query_language: VariantAnalysisQueryLanguage,
26+
query_pack_url: string,
27+
status: VariantAnalysisStatus,
28+
actions_workflow_run_id?: number,
29+
failure_reason?: VariantAnalysisFailureReason,
30+
scanned_repositories?: VariantAnalysisScannedRepository[],
31+
skipped_repositories?: VariantAnalysisSkippedRepositories
32+
}
33+
34+
export type VariantAnalysisStatus =
35+
| 'in_progress'
36+
| 'completed';
37+
38+
export type VariantAnalysisFailureReason =
39+
| 'no_repos_queried'
40+
| 'internal_error';
41+
42+
export type VariantAnalysisRepoStatus =
43+
| 'pending'
44+
| 'in_progress'
45+
| 'succeeded'
46+
| 'failed'
47+
| 'canceled'
48+
| 'timed_out';
49+
50+
export interface VariantAnalysisScannedRepository {
51+
repository: Repository,
52+
analysis_status: VariantAnalysisRepoStatus,
53+
result_count?: number,
54+
artifact_size_in_bytes?: number,
55+
failure_message?: string
56+
}
57+
58+
export interface VariantAnalysisSkippedRepositoryGroup {
59+
repository_count: number,
60+
repositories: Array<{
61+
id?: number,
62+
full_name: string
63+
}>
64+
}
65+
66+
export interface VariantAnalysisRepoTask {
67+
repository: Repository,
68+
analysis_status: VariantAnalysisRepoStatus,
69+
artifact_size_in_bytes?: number,
70+
result_count?: number,
71+
failure_message?: string,
72+
database_commit_sha?: string,
73+
source_location_prefix?: string,
74+
artifact_url?: string
75+
}
76+
77+
export interface VariantAnalysisSkippedRepositories {
78+
access_mismatch_repos: VariantAnalysisSkippedRepositoryGroup,
79+
not_found_repos: VariantAnalysisSkippedRepositoryGroup,
80+
no_codeql_db_repos: VariantAnalysisSkippedRepositoryGroup,
81+
over_limit_repos: VariantAnalysisSkippedRepositoryGroup
82+
}

extensions/ql-vscode/src/remote-queries/shared/variant-analysis.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,26 @@ export interface VariantAnalysisSkippedRepositoryGroup {
7272
fullName: string
7373
}>
7474
}
75+
76+
/**
77+
* Captures information needed to submit a variant
78+
* analysis for processing.
79+
*/
80+
export interface VariantAnalysisSubmission {
81+
startTime: number,
82+
controllerRepoId: number,
83+
actionRepoRef: string,
84+
query: {
85+
name: string,
86+
filePath: string,
87+
language: VariantAnalysisQueryLanguage,
88+
89+
// Base64 encoded query pack.
90+
pack: string,
91+
},
92+
databases: {
93+
repositories?: string[],
94+
repositoryLists?: string[],
95+
repositoryOwners?: string[],
96+
}
97+
}

0 commit comments

Comments
 (0)