Skip to content

Commit

Permalink
Merge branch 'ivana/codebase-discovery-changes-chores' of https://git…
Browse files Browse the repository at this point in the history
…hub.com/grafana/github-datasource into ivana/codebase-discovery-changes-chores
  • Loading branch information
ivanahuckova committed Jun 20, 2024
2 parents ba0b525 + 90fbea3 commit 758732a
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 59 deletions.
1 change: 0 additions & 1 deletion pkg/github/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ func (d *Datasource) HandleVulnerabilitiesQuery(ctx context.Context, query *mode
opt := models.ListVulnerabilitiesOptions{
Repository: query.Repository,
Owner: query.Owner,
Query: query.Options.Query,
}

return GetAllVulnerabilities(ctx, d.client, opt)
Expand Down
3 changes: 3 additions & 0 deletions pkg/github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Issue struct {
Title string
ClosedAt githubv4.DateTime
CreatedAt githubv4.DateTime
UpdatedAt githubv4.DateTime
Closed bool
Author struct {
models.User `graphql:"... on User"`
Expand All @@ -40,6 +41,7 @@ func (c Issues) Frames() data.Frames {
data.NewField("closed", nil, []bool{}),
data.NewField("created_at", nil, []time.Time{}),
data.NewField("closed_at", nil, []*time.Time{}),
data.NewField("updated_at", nil, []time.Time{}),
)

for _, v := range c {
Expand All @@ -58,6 +60,7 @@ func (c Issues) Frames() data.Frames {
v.Closed,
v.CreatedAt.Time,
closedAt,
v.UpdatedAt.Time,
)
}

Expand Down
9 changes: 9 additions & 0 deletions pkg/github/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func TestIssuesDataframe(t *testing.T) {
CreatedAt: githubv4.DateTime{
Time: createdAt,
},
UpdatedAt: githubv4.DateTime{
Time: createdAt,
},
Closed: false,
Author: struct {
models.User "graphql:\"... on User\""
Expand Down Expand Up @@ -87,6 +90,9 @@ func TestIssuesDataframe(t *testing.T) {
CreatedAt: githubv4.DateTime{
Time: createdAt,
},
UpdatedAt: githubv4.DateTime{
Time: createdAt.Add(time.Hour * 6),
},
Closed: true,
Author: struct {
models.User "graphql:\"... on User\""
Expand Down Expand Up @@ -125,6 +131,9 @@ func TestIssuesDataframe(t *testing.T) {
CreatedAt: githubv4.DateTime{
Time: createdAt,
},
UpdatedAt: githubv4.DateTime{
Time: createdAt,
},
Closed: false,
Author: struct {
models.User "graphql:\"... on User\""
Expand Down
32 changes: 22 additions & 10 deletions pkg/github/testdata/issues.golden.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//
// Frame[0]
// Name: issues
// Dimensions: 8 Fields by 3 Rows
// +----------------+----------------+----------------------+-----------------+---------------+--------------+---------------------------------+---------------------------------+
// | Name: title | Name: author | Name: author_company | Name: repo | Name: number | Name: closed | Name: created_at | Name: closed_at |
// | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: |
// | Type: []string | Type: []string | Type: []string | Type: []string | Type: []int64 | Type: []bool | Type: []time.Time | Type: []*time.Time |
// +----------------+----------------+----------------------+-----------------+---------------+--------------+---------------------------------+---------------------------------+
// | Issue #1 | firstUser | ACME Corp | grafana/grafana | 1 | false | 2020-08-25 16:21:56 +0000 +0000 | null |
// | Issue #2 | secondUser | ACME Corp | grafana/grafana | 2 | true | 2020-08-25 16:21:56 +0000 +0000 | 2020-08-25 22:21:56 +0000 +0000 |
// | Issue #3 | firstUser | ACME Corp | grafana/grafana | 3 | false | 2020-08-25 16:21:56 +0000 +0000 | null |
// +----------------+----------------+----------------------+-----------------+---------------+--------------+---------------------------------+---------------------------------+
// Dimensions: 9 Fields by 3 Rows
// +----------------+----------------+----------------------+-----------------+---------------+--------------+---------------------------------+---------------------------------+---------------------------------+
// | Name: title | Name: author | Name: author_company | Name: repo | Name: number | Name: closed | Name: created_at | Name: closed_at | Name: updated_at |
// | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: |
// | Type: []string | Type: []string | Type: []string | Type: []string | Type: []int64 | Type: []bool | Type: []time.Time | Type: []*time.Time | Type: []time.Time |
// +----------------+----------------+----------------------+-----------------+---------------+--------------+---------------------------------+---------------------------------+---------------------------------+
// | Issue #1 | firstUser | ACME Corp | grafana/grafana | 1 | false | 2020-08-25 16:21:56 +0000 +0000 | null | 2020-08-25 16:21:56 +0000 +0000 |
// | Issue #2 | secondUser | ACME Corp | grafana/grafana | 2 | true | 2020-08-25 16:21:56 +0000 +0000 | 2020-08-25 22:21:56 +0000 +0000 | 2020-08-25 22:21:56 +0000 +0000 |
// | Issue #3 | firstUser | ACME Corp | grafana/grafana | 3 | false | 2020-08-25 16:21:56 +0000 +0000 | null | 2020-08-25 16:21:56 +0000 +0000 |
// +----------------+----------------+----------------------+-----------------+---------------+--------------+---------------------------------+---------------------------------+---------------------------------+
//
//
// 🌟 This was machine generated. Do not edit. 🌟
Expand Down Expand Up @@ -78,6 +78,13 @@
"frame": "time.Time",
"nullable": true
}
},
{
"name": "updated_at",
"type": "time",
"typeInfo": {
"frame": "time.Time"
}
}
]
},
Expand Down Expand Up @@ -122,6 +129,11 @@
null,
1598394116000,
null
],
[
1598372516000,
1598394116000,
1598372516000
]
]
}
Expand Down
14 changes: 1 addition & 13 deletions pkg/github/vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,7 @@ import (
"github.com/shurcooL/githubv4"
)

// QueryListVulnerabilities lists all labels in a repository
//
// {
// repository(name: "grafana", owner: "grafana") {
// labels(first: 100) {
// nodes {
// color
// description
// name
// }
// }
// }
// }
// QueryListVulnerabilities lists all vulnerability alerts in a repository
//
// {
// repository(name: "repo-name", owner: "repo-owner") {
Expand Down
5 changes: 3 additions & 2 deletions pkg/github/workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ func GetWorkflows(ctx context.Context, client models.Client, opts models.ListWor
}

// Fetch this many workflows per page because this API endpoint does not allow filtering by time.
// It's unlikely a repository will have more workflows than this.
data, _, err := client.ListWorkflows(ctx, opts.Owner, opts.Repository, &github.ListOptions{Page: 0, PerPage: 1000})
// 100 is the maximum number of workflows that can be retrieved per request as specified in the GitHub API documentation.
// Also, it's unlikely a repository will have more workflows than this.
data, _, err := client.ListWorkflows(ctx, opts.Owner, opts.Repository, &github.ListOptions{Page: 1, PerPage: 100})
if err != nil {
return nil, fmt.Errorf("listing workflows: opts=%+v %w", opts, err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/models/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ const (
IssueCreatedAt IssueTimeField = iota
// IssueClosedAt is used when filtering when an Issue was closed
IssueClosedAt
// IssueUpdatedAt is used when filtering when an Issue was updated (last time)
IssueUpdatedAt
)

func (d IssueTimeField) String() string {
return [...]string{"created", "closed"}[d]
return [...]string{"created", "closed", "updated"}[d]
}

// ListIssuesOptions provides options when retrieving issues
Expand Down
3 changes: 0 additions & 3 deletions pkg/models/vulnerabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,4 @@ type ListVulnerabilitiesOptions struct {

// Owner is the owner of the repository (ex: grafana)
Owner string `json:"owner"`

// Query searches x by name and description
Query string `json:"query"`
}
3 changes: 3 additions & 0 deletions src/components/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ export const components = {
Number: {
input: 'Query editor number',
},
Issues: {
timeFieldInput: 'Query editor issues time field',
},
},
AnnotationEditor: {
container: 'Annotation editor container',
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export enum PullRequestTimeField {
export enum IssueTimeField {
CreatedAt,
ClosedAt,
UpdatedAt,
}

export enum WorkflowsTimeField {
Expand Down
2 changes: 1 addition & 1 deletion src/views/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const queryEditors: {
},
[QueryType.Vulnerabilities]: {
component: (props: Props, onChange: (val: any) => void) => (
<QueryEditorVulnerabilities {...(props.query.options || {})} onChange={onChange} />
<QueryEditorVulnerabilities {...(props.query.options || {})} />
),
},
[QueryType.Projects]: {
Expand Down
19 changes: 19 additions & 0 deletions src/views/QueryEditorIssues.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import QueryEditorIssues from './QueryEditorIssues';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { components } from 'components/selectors';

describe('QueryEditorIssues', () => {
it('should have CreatedAt, ClosedAt and UpdatedAt time field option', async () => {
const props = {
onChange: jest.fn(),
};
render(<QueryEditorIssues {...props} />);
expect(screen.getByText('Time Field')).toBeInTheDocument();
userEvent.click(screen.getByTestId(components.QueryEditor.Issues.timeFieldInput));
expect(await screen.findByText('CreatedAt')).toBeInTheDocument();
expect(await screen.findByText('ClosedAt')).toBeInTheDocument();
expect(await screen.findByText('UpdatedAt')).toBeInTheDocument();
});
});
2 changes: 2 additions & 0 deletions src/views/QueryEditorIssues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Input, Select, InlineField } from '@grafana/ui';
import { SelectableValue } from '@grafana/data';
import { IssuesOptions, IssueTimeField } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';
import { components } from 'components/selectors';

interface Props extends IssuesOptions {
onChange: (value: IssuesOptions) => void;
Expand Down Expand Up @@ -58,6 +59,7 @@ const QueryEditorIssues = (props: Props) => {
tooltip="The time field to filter on the time range"
>
<Select
data-testid={components.QueryEditor.Issues.timeFieldInput}
width={RightColumnWidth}
options={timeFieldOptions}
value={props.timeField || defaultTimeField}
Expand Down
27 changes: 2 additions & 25 deletions src/views/QueryEditorVulnerabilities.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
import React, { useState } from 'react';
import { Input, InlineField } from '@grafana/ui';

import { LabelsOptions } from '../types';
import { RightColumnWidth, LeftColumnWidth } from './QueryEditor';

interface Props extends LabelsOptions {
onChange: (value: LabelsOptions) => void;
}

const QueryEditorVulnerabilities = (props: Props) => {
const [query, setQuery] = useState<string>(props.query || '');
return (
<>
<InlineField labelWidth={LeftColumnWidth * 2} label="Query (optional)">
<Input
width={RightColumnWidth}
value={query}
onChange={(el) => setQuery(el.currentTarget.value)}
onBlur={(el) => props.onChange({ ...props, query: el.currentTarget.value })}
/>
</InlineField>
</>
);
};
import React from 'react';

const QueryEditorVulnerabilities = () => <></>;
export default QueryEditorVulnerabilities;
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8685,9 +8685,9 @@ write-file-atomic@^4.0.2:
signal-exit "^3.0.7"

ws@^8.11.0:
version "8.17.0"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.0.tgz#d145d18eca2ed25aaf791a183903f7be5e295fea"
integrity sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==
version "8.17.1"
resolved "https://registry.yarnpkg.com/ws/-/ws-8.17.1.tgz#9293da530bb548febc95371d90f9c878727d919b"
integrity sha512-6XQFvXTkbfUOZOKKILFG1PDK2NDQs4azKQl26T0YS5CxqWLgXajbPZ+h4gZekJyRqFU8pvnbAbbs/3TgRPy+GQ==

xdg-basedir@^4.0.0:
version "4.0.0"
Expand Down

0 comments on commit 758732a

Please sign in to comment.