Skip to content

Commit 3611b1f

Browse files
committed
Add comments and simplify some JSX
Use `ActionMenu.Anchor` instead of `ActionMenu.Button`. The theming styles are not correct. Will work on that next.
1 parent 6c376d8 commit 3611b1f

File tree

8 files changed

+68
-60
lines changed

8 files changed

+68
-60
lines changed

extensions/ql-vscode/src/authentication.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ export class Credentials {
3535
return c;
3636
}
3737

38-
static async initializeOverride(overrideToken: string) {
38+
/**
39+
* Initializes an instance of credentials with an octokit instance using
40+
* a token from the user's GitHub account. This method is meant to be
41+
* used non-interactive environments such as tests.
42+
*
43+
* @param overrideToken The GitHub token to use for authentication.
44+
* @returns An instance of credentials.
45+
*/
46+
static async initializeWithToken(overrideToken: string) {
3947
const c = new Credentials();
4048
c.octokit = await c.createOctokit(false, overrideToken);
4149
return c;

extensions/ql-vscode/src/remote-queries/remote-queries-interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ export class RemoteQueriesInterfaceManager {
307307
resultCount: analysisResult.resultCount,
308308
downloadLink: analysisResult.downloadLink,
309309
fileSize: this.formatFileSize(analysisResult.fileSizeInBytes),
310-
starCount: analysisResult.starCount,
310+
starCount: analysisResult.starCount
311311
}));
312312
}
313313
}

extensions/ql-vscode/src/remote-queries/remote-queries-manager.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,12 @@ export class RemoteQueriesManager extends DisposableObject {
181181
results => this.interfaceManager.setAnalysisResults(results, queryResult.queryId));
182182
}
183183

184-
private mapQueryResult(executionEndTime: number, resultIndex: RemoteQueryResultIndex, queryId: string): RemoteQueryResult {
184+
private mapQueryResult(
185+
executionEndTime: number,
186+
resultIndex: RemoteQueryResultIndex,
187+
queryId: string,
188+
stargazers: Record<string, number>
189+
): RemoteQueryResult {
185190
const analysisSummaries = resultIndex.successes.map(item => ({
186191
nwo: item.nwo,
187192
databaseSha: item.sha || 'HEAD',
@@ -192,6 +197,7 @@ export class RemoteQueriesManager extends DisposableObject {
192197
urlPath: `${resultIndex.artifactsUrlPath}/${item.artifactId}`,
193198
innerFilePath: item.sarifFileSize ? 'results.sarif' : 'results.bqrs',
194199
queryId,
200+
starCount: stargazers[item.nwo]
195201
} as DownloadLink
196202
}));
197203
const analysisFailures = resultIndex.failures.map(item => ({
@@ -278,9 +284,8 @@ export class RemoteQueriesManager extends DisposableObject {
278284
queryItem.completed = true;
279285
queryItem.status = QueryStatus.Completed;
280286
queryItem.failureReason = undefined;
281-
const queryResult = this.mapQueryResult(executionEndTime, resultIndex, queryItem.queryId);
282-
283-
await this.addStargazers(resultIndex, credentials, queryResult);
287+
const stargazers = await this.getStargazersCount(resultIndex, credentials);
288+
const queryResult = this.mapQueryResult(executionEndTime, resultIndex, queryItem.queryId, stargazers);
284289

285290
await this.storeJsonFile(queryItem, 'query-result.json', queryResult);
286291

@@ -304,10 +309,9 @@ export class RemoteQueriesManager extends DisposableObject {
304309
}
305310
}
306311

307-
private async addStargazers(resultIndex: RemoteQueryResultIndex, credentials: Credentials, queryResult: RemoteQueryResult) {
312+
private async getStargazersCount(resultIndex: RemoteQueryResultIndex, credentials: Credentials) {
308313
const nwos = resultIndex.successes.map(s => s.nwo);
309-
const stargazers = await getStargazers(credentials, nwos);
310-
queryResult.analysisSummaries.forEach(analysis => analysis.starCount = stargazers[analysis.nwo]);
314+
return await getStargazers(credentials, nwos);
311315
}
312316

313317
// Pulled from the analysis results manager, so that we can get access to

extensions/ql-vscode/src/remote-queries/view/RemoteQueries.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ const Failures = (queryResult: RemoteQueryResult) => {
128128
const SummaryTitleWithResults = ({
129129
queryResult,
130130
analysesResults,
131-
sort, setSort
131+
sort,
132+
setSort
132133
}: {
133134
queryResult: RemoteQueryResult,
134135
analysesResults: AnalysisResults[],
@@ -145,7 +146,6 @@ const SummaryTitleWithResults = ({
145146
text="Download all"
146147
onClick={() => downloadAllAnalysesResults(queryResult)} />
147148
}
148-
149149
<SortRepoFilter
150150
sort={sort}
151151
setSort={setSort}

extensions/ql-vscode/src/remote-queries/view/SortRepoFilter.tsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import * as React from 'react';
22
import { FilterIcon } from '@primer/octicons-react';
3-
import { ActionList, ActionMenu } from '@primer/react';
3+
import { ActionList, ActionMenu, IconButton } from '@primer/react';
4+
import styled from 'styled-components';
5+
6+
const SortWrapper = styled.span`
7+
flex-grow: 2;
8+
text-align: right;
9+
margin-right: 0;
10+
`;
411

512
export type Sort = 'name' | 'stars' | 'results';
6-
type SortBy = { name: string, sort: Sort }[];
713
type Props = {
814
sort: Sort;
915
setSort: (sort: Sort) => void;
@@ -15,13 +21,13 @@ type Sortable = {
1521
resultCount?: number;
1622
};
1723

18-
const sortBy: SortBy = [
24+
const sortBy = [
1925
{ name: 'Sort by Name', sort: 'name' },
2026
{ name: 'Sort by Results', sort: 'results' },
2127
{ name: 'Sort by Stars', sort: 'stars' },
2228
];
2329

24-
export function sorter(sort: Sort) {
30+
export function sorter(sort: Sort): (left: Sortable, right: Sortable) => number {
2531
// stars and results are highest to lowest
2632
// name is alphabetical
2733
return (left: Sortable, right: Sortable) => {
@@ -43,25 +49,29 @@ export function sorter(sort: Sort) {
4349
};
4450
}
4551

52+
// FIXME These styles are not correct. Need to figure out
53+
// why the theme is not being applied to the ActionMenu
4654
const SortRepoFilter = ({ sort, setSort }: Props) => {
47-
return <span className="vscode-codeql__analysis-sorter">
55+
return <SortWrapper>
4856
<ActionMenu>
49-
<ActionMenu.Button
50-
className="vscode-codeql__analysis-sort-dropdown"
51-
aria-label="Sort results"
52-
leadingIcon={FilterIcon}
53-
trailingIcon="" />
54-
<ActionMenu.Overlay width="medium">
57+
<ActionMenu.Anchor>
58+
<IconButton icon={FilterIcon} variant="invisible" aria-label="Sort results" />
59+
</ActionMenu.Anchor>
60+
61+
<ActionMenu.Overlay width="small" anchorSide="outside-bottom">
5562
<ActionList selectionVariant="single">
5663
{sortBy.map((type, index) => (
57-
<ActionList.Item key={index} selected={type.sort === sort} onSelect={() => setSort(type.sort)}>
64+
<ActionList.Item
65+
key={index}
66+
selected={type.sort === sort} onSelect={() => setSort(type.sort as Sort)}
67+
>
5868
{type.name}
5969
</ActionList.Item>
6070
))}
6171
</ActionList>
6272
</ActionMenu.Overlay>
6373
</ActionMenu>
64-
</span>;
74+
</SortWrapper>;
6575

6676
};
6777

extensions/ql-vscode/src/remote-queries/view/StarCount.tsx

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
11
import * as React from 'react';
22
import { StarIcon } from '@primer/octicons-react';
3+
import styled from 'styled-components';
4+
5+
const Star = styled.span`
6+
flex-grow: 2;
7+
text-align: right;
8+
margin-right: 0;
9+
`;
10+
11+
const Count = styled.span`
12+
text-align: left;
13+
width: 2em;
14+
margin-left: 0.5em;
15+
`;
316

417
type Props = { starCount?: number };
518

619
const StarCount = ({ starCount }: Props) => (
720
Number.isFinite(starCount) ? (
821
<>
9-
<span className="vscode-codeql__analysis-star">
22+
<Star>
1023
<StarIcon size={16} />
11-
</span>
12-
<span className='vscode-codeql__analysis-count'>
24+
</Star>
25+
<Count>
1326
{displayStars(starCount!)}
14-
</span>
27+
</Count>
1528
</>
1629
) : (
17-
<>
18-
<span className="vscode-codeql__analysis-star">
19-
{/* empty */}
20-
</span>
21-
<span className='vscode-codeql__analysis-count'>
22-
{/* empty */}
23-
</span>
24-
</>
30+
<></>
2531
)
2632
);
2733

extensions/ql-vscode/src/remote-queries/view/remoteQueries.css

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,6 @@
3030
padding-right: 0.1em;
3131
}
3232

33-
.vscode-codeql__analysis-sorter,
34-
.vscode-codeql__analysis-star {
35-
flex-grow: 2;
36-
text-align: right;
37-
margin-right: 0;
38-
}
39-
40-
.vscode-codeql__analysis-count {
41-
text-align: left;
42-
width: 2em;
43-
margin-left: 0.5em;
44-
}
45-
4633
.vscode-codeql__expand-button {
4734
background: none;
4835
color: var(--vscode-textLink-foreground);
@@ -52,13 +39,6 @@
5239
font-size: x-small;
5340
}
5441

55-
button.vscode-codeql__analysis-sort-dropdown {
56-
display: inline-grid;
57-
background-color: var(--vscode-editor-background);
58-
border: none;
59-
padding: 0.5em;
60-
}
61-
6242
.vscode-codeql__analysis-failure {
6343
margin: 0;
6444
font-family: ui-monospace, SFMono-Regular, SF Mono, Menlo, Consolas,

extensions/ql-vscode/src/vscode-tests/no-workspace/remote-queries/gh-actions-api-client.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { fail } from 'assert';
22
import { expect } from 'chai';
33
import * as sinon from 'sinon';
44
import { Credentials } from '../../../authentication';
5-
import { cancelRemoteQuery, getStargazers } from '../../../remote-queries/gh-actions-api-client';
5+
import { cancelRemoteQuery, getStargazers as getStargazersCount } from '../../../remote-queries/gh-actions-api-client';
66
import { RemoteQuery } from '../../../remote-queries/remote-query';
77

88
describe('gh-actions-api-client mock responses', () => {
@@ -55,13 +55,13 @@ describe('gh-actions-api-client mock responses', () => {
5555
describe('gh-actions-api-client real responses', function() {
5656
this.timeout(10000);
5757

58-
it('should get the stargazers for projects', async () => {
58+
it('should get the stargazers for repos', async () => {
5959
if (skip()) {
6060
return;
6161
}
6262

63-
const credentials = await Credentials.initializeOverride(process.env.VSCODE_CODEQL_GITHUB_TOKEN!);
64-
const stargazers = await getStargazers(credentials, [
63+
const credentials = await Credentials.initializeWithToken(process.env.VSCODE_CODEQL_GITHUB_TOKEN!);
64+
const stargazers = await getStargazersCount(credentials, [
6565
'github/codeql',
6666
'github/vscode-codeql',
6767
'rails/rails',

0 commit comments

Comments
 (0)