Skip to content

Commit

Permalink
feat(security): ManageSecurityHandler
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Nov 21, 2023
1 parent 90fa1f8 commit 26a99e5
Show file tree
Hide file tree
Showing 53 changed files with 3,731 additions and 743 deletions.
4 changes: 4 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ component_management:
name: queries
paths:
- src/queries/*.ts
- component_id: security
name: security
paths:
- src/subdomains/security/**/*.ts
- component_id: teams
name: teams
paths:
Expand Down
1 change: 1 addition & 0 deletions .commitlintrc.cts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const config: UserConfig = {
'octokit',
'queries',
'runner',
'security',
'teams',
'users'
])
Expand Down
140 changes: 140 additions & 0 deletions __fixtures__/api.github.com/repos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
[
{
"allow_forking": true,
"archive_url": "https://api.github.com/repos/flex-development/repostructure-action/{archive_format}{/ref}",
"archived": false,
"assignees_url": "https://api.github.com/repos/flex-development/repostructure-action/assignees{/user}",
"blobs_url": "https://api.github.com/repos/flex-development/repostructure-action/git/blobs{/sha}",
"branches_url": "https://api.github.com/repos/flex-development/repostructure-action/branches{/branch}",
"clone_url": "https://github.com/flex-development/repostructure-action.git",
"collaborators_url": "https://api.github.com/repos/flex-development/repostructure-action/collaborators{/collaborator}",
"comments_url": "https://api.github.com/repos/flex-development/repostructure-action/comments{/number}",
"commits_url": "https://api.github.com/repos/flex-development/repostructure-action/commits{/sha}",
"compare_url": "https://api.github.com/repos/flex-development/repostructure-action/compare/{base}...{head}",
"contents_url": "https://api.github.com/repos/flex-development/repostructure-action/contents/{+path}",
"contributors_url": "https://api.github.com/repos/flex-development/repostructure-action/contributors",
"created_at": "2023-03-07T00:38:54Z",
"default_branch": "main",
"deployments_url": "https://api.github.com/repos/flex-development/repostructure-action/deployments",
"description": "Repository infrastructure as code for GitHub Actions",
"disabled": false,
"downloads_url": "https://api.github.com/repos/flex-development/repostructure-action/downloads",
"events_url": "https://api.github.com/repos/flex-development/repostructure-action/events",
"fork": false,
"forks": 0,
"forks_count": 0,
"forks_url": "https://api.github.com/repos/flex-development/repostructure-action/forks",
"full_name": "flex-development/repostructure-action",
"git_commits_url": "https://api.github.com/repos/flex-development/repostructure-action/git/commits{/sha}",
"git_refs_url": "https://api.github.com/repos/flex-development/repostructure-action/git/refs{/sha}",
"git_tags_url": "https://api.github.com/repos/flex-development/repostructure-action/git/tags{/sha}",
"git_url": "git://github.com/flex-development/repostructure-action.git",
"has_discussions": true,
"has_downloads": true,
"has_issues": true,
"has_pages": false,
"has_projects": true,
"has_wiki": false,
"homepage": "https://github.com/flex-development/rice-action",
"hooks_url": "https://api.github.com/repos/flex-development/repostructure-action/hooks",
"html_url": "https://github.com/flex-development/repostructure-action",
"id": 610531283,
"is_template": false,
"issue_comment_url": "https://api.github.com/repos/flex-development/repostructure-action/issues/comments{/number}",
"issue_events_url": "https://api.github.com/repos/flex-development/repostructure-action/issues/events{/number}",
"issues_url": "https://api.github.com/repos/flex-development/repostructure-action/issues{/number}",
"keys_url": "https://api.github.com/repos/flex-development/repostructure-action/keys{/key_id}",
"labels_url": "https://api.github.com/repos/flex-development/repostructure-action/labels{/name}",
"language": "TypeScript",
"languages_url": "https://api.github.com/repos/flex-development/repostructure-action/languages",
"license": {
"key": "bsd-3-clause",
"name": "BSD 3-Clause \"New\" or \"Revised\" License",
"node_id": "MDc6TGljZW5zZTU=",
"spdx_id": "BSD-3-Clause",
"url": "https://api.github.com/licenses/bsd-3-clause"
},
"merges_url": "https://api.github.com/repos/flex-development/repostructure-action/merges",
"milestones_url": "https://api.github.com/repos/flex-development/repostructure-action/milestones{/number}",
"mirror_url": null,
"name": "repostructure-action",
"network_count": 0,
"node_id": "R_kgDOJGP30w",
"notifications_url": "https://api.github.com/repos/flex-development/repostructure-action/notifications{?since,all,participating}",
"open_issues": 9,
"open_issues_count": 9,
"organization": {
"avatar_url": "https://avatars.githubusercontent.com/u/48739091?v=4",
"events_url": "https://api.github.com/users/flex-development/events{/privacy}",
"followers_url": "https://api.github.com/users/flex-development/followers",
"following_url": "https://api.github.com/users/flex-development/following{/other_user}",
"gists_url": "https://api.github.com/users/flex-development/gists{/gist_id}",
"gravatar_id": "",
"html_url": "https://github.com/flex-development",
"id": 48739091,
"login": "flex-development",
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQ4NzM5MDkx",
"organizations_url": "https://api.github.com/users/flex-development/orgs",
"received_events_url": "https://api.github.com/users/flex-development/received_events",
"repos_url": "https://api.github.com/users/flex-development/repos",
"site_admin": false,
"starred_url": "https://api.github.com/users/flex-development/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/flex-development/subscriptions",
"type": "Organization",
"url": "https://api.github.com/users/flex-development"
},
"owner": {
"avatar_url": "https://avatars.githubusercontent.com/u/48739091?v=4",
"events_url": "https://api.github.com/users/flex-development/events{/privacy}",
"followers_url": "https://api.github.com/users/flex-development/followers",
"following_url": "https://api.github.com/users/flex-development/following{/other_user}",
"gists_url": "https://api.github.com/users/flex-development/gists{/gist_id}",
"gravatar_id": "",
"html_url": "https://github.com/flex-development",
"id": 48739091,
"login": "flex-development",
"node_id": "MDEyOk9yZ2FuaXphdGlvbjQ4NzM5MDkx",
"organizations_url": "https://api.github.com/users/flex-development/orgs",
"received_events_url": "https://api.github.com/users/flex-development/received_events",
"repos_url": "https://api.github.com/users/flex-development/repos",
"site_admin": false,
"starred_url": "https://api.github.com/users/flex-development/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/flex-development/subscriptions",
"type": "Organization",
"url": "https://api.github.com/users/flex-development"
},
"private": false,
"pulls_url": "https://api.github.com/repos/flex-development/repostructure-action/pulls{/number}",
"pushed_at": "2023-11-20T12:06:10Z",
"releases_url": "https://api.github.com/repos/flex-development/repostructure-action/releases{/id}",
"size": 5953,
"ssh_url": "[email protected]:flex-development/repostructure-action.git",
"stargazers_count": 1,
"stargazers_url": "https://api.github.com/repos/flex-development/repostructure-action/stargazers",
"statuses_url": "https://api.github.com/repos/flex-development/repostructure-action/statuses/{sha}",
"subscribers_count": 3,
"subscribers_url": "https://api.github.com/repos/flex-development/repostructure-action/subscribers",
"subscription_url": "https://api.github.com/repos/flex-development/repostructure-action/subscription",
"svn_url": "https://github.com/flex-development/repostructure-action",
"tags_url": "https://api.github.com/repos/flex-development/repostructure-action/tags",
"teams_url": "https://api.github.com/repos/flex-development/repostructure-action/teams",
"temp_clone_token": null,
"topics": [
"gitops",
"iac",
"infrastructure",
"infrastructure-as-code",
"infrastructure-management",
"repository-infrastructure",
"repository-settings",
"typescript"
],
"trees_url": "https://api.github.com/repos/flex-development/repostructure-action/git/trees{/sha}",
"updated_at": "2023-11-08T04:41:06Z",
"url": "https://api.github.com/repos/flex-development/repostructure-action",
"visibility": "public",
"watchers": 1,
"watchers_count": 1,
"web_commit_signoff_required": true
}
]
9 changes: 4 additions & 5 deletions __fixtures__/octokit.provider.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
* @module fixtures/OctokitProvider
*/

import * as github from '@actions/github'
import Octokit from '#src/octokit/models/octokit.model'
import type { ValueProvider } from '@nestjs/common'
import { Octokit } from '@octokit/core'
import { paginateGraphql } from '@octokit/plugin-paginate-graphql'
import INPUT_API from './input-api.fixture'
import INPUT_TOKEN from './input-token.fixture'

Expand All @@ -17,7 +15,8 @@ import INPUT_TOKEN from './input-token.fixture'
*/
const OctokitProvider: ValueProvider<Octokit> = {
provide: Octokit,
useValue: github.getOctokit(INPUT_TOKEN, {
useValue: new Octokit({
auth: INPUT_TOKEN,
baseUrl: INPUT_API,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
Expand All @@ -27,7 +26,7 @@ const OctokitProvider: ValueProvider<Octokit> = {
request: {
fetch: async (info: RequestInfo, opts: RequestInit) => fetch(info, opts)
}
}, paginateGraphql)
})
}

export default OctokitProvider
47 changes: 45 additions & 2 deletions __tests__/setup/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import apps from '#fixtures/api.github.com/apps.json' assert { type: 'json' }
import root from '#fixtures/api.github.com/graphql.json' assert { type: 'json' }
import repos from '#fixtures/api.github.com/repos.json' assert { type: 'json' }
import CLIENT_MUTATION_ID from '#fixtures/client-mutation-id.fixture'
import type BranchProtection from '#src/branches/types/branch-protection'
import type Environment from '#src/environments/types/environment'
Expand All @@ -13,7 +14,7 @@ import type UpdateLabelInput from '#src/labels/commands/update.command'
import type Label from '#src/labels/types/label'
import type Team from '#src/teams/types/team'
import type User from '#src/users/types/user'
import type { OctokitData, OctokitParameters } from '#tests/types'
import type { OctokitBody, OctokitData, OctokitParameters } from '#tests/types'
import connection from '#tests/utils/connection'
import {
assign,
Expand Down Expand Up @@ -42,7 +43,7 @@ import {
graphql as executeGraphql,
type ExecutionResult
} from 'graphql'
import { HttpResponse, graphql, http } from 'msw'
import { HttpResponse, graphql, http, type StrictResponse } from 'msw'
import { setupServer, type SetupServer } from 'msw/node'
import schema from './graphql/schema'

Expand All @@ -55,6 +56,13 @@ import schema from './graphql/schema'
* @const {SetupServer} server
*/
const server: SetupServer = setupServer(
http.delete<
OctokitParameters<'GET /repos/{owner}/{repo}'>,
EmptyObject,
null
>(/(?:security-fixes|vulnerability-(?:alerts|reporting))$/, () => {
return <StrictResponse<null>>new HttpResponse(null, { status: 204 })
}),
http.get<
OctokitParameters<'GET /apps/{app_slug}'>,
EmptyObject,
Expand Down Expand Up @@ -82,6 +90,41 @@ const server: SetupServer = setupServer(

return HttpResponse.json(app)
}),
http.patch<
OctokitParameters<'GET /repos/{owner}/{repo}'>,
OctokitBody<'PATCH', '/repos/{owner}/{repo}'>,
OctokitData<'PATCH /repos/{owner}/{repo}'>
>(/\/repos\/(?<owner>[\w-]+)\/(?<repo>[\w-]+)$/, ({ params }) => {
/**
* Repository to update.
*
* @const {Optional<typeof repos[number]>} repo
*/
const repo: Optional<typeof repos[number]> = repos.find(r => {
return r.owner.login === params.owner && r.name === params.repo
})

// return error response if repo was not found
if (!repo) {
return HttpResponse.json({
documentation_url:
'https://docs.github.com/rest/repos/repos#get-a-repository',
message: 'Not Found'
}, {
status: 404,
statusText: 'Not Found'
})
}

return HttpResponse.json(repo)
}),
http.put<
OctokitParameters<'GET /repos/{owner}/{repo}'>,
EmptyObject,
null
>(/(?:security-fixes|vulnerability-(?:alerts|reporting))$/, () => {
return <StrictResponse<null>>new HttpResponse(null, { status: 204 })
}),
graphql.link(/\/graphql$/).operation<ExecutionResult>(async ({
operationName,
query,
Expand Down
1 change: 1 addition & 0 deletions __tests__/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
* @module tests/types
*/

export type { default as OctokitBody } from './octokit-body'
export type { default as OctokitData } from './octokit-data'
export type { default as OctokitParameters } from './octokit-parameters'
24 changes: 24 additions & 0 deletions __tests__/types/octokit-body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file Test Types - OctokitBody
* @module tests/types/OctokitBody
*/

import type { Get, OrUppercase, Stringify } from '@flex-development/tutils'
import type { paths } from '@octokit/openapi-types'

/**
* GitHub REST API body helper.
*
* @template R - REST API endpoint
* @template M - REST API method
*/
type OctokitBody<
M extends OrUppercase<Stringify<keyof paths[R]>>,
R extends keyof paths
> = Get<
Get<paths[R], Lowercase<Stringify<M>>>,
'requestBody.content.application/json',
never
>

export type { OctokitBody as default }
7 changes: 5 additions & 2 deletions __tests__/types/octokit-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @module tests/types/OctokitData
*/

import type { EmptyObject, IfNever } from '@flex-development/tutils'
import type { Endpoints } from '@octokit/types'

/**
Expand All @@ -11,7 +12,9 @@ import type { Endpoints } from '@octokit/types'
* @template R - REST API endpoint
*/
type OctokitData<R extends keyof Endpoints> =
| Endpoints[R]['response']['data']
| Record<'documentation_url' | 'message', string>
Endpoints[R]['response']['data'] extends infer J // dprint-ignore-next
? | IfNever<J, EmptyObject, J>
| Record<'documentation_url' | 'message', string>
: never

export type { OctokitData as default }
Loading

0 comments on commit 26a99e5

Please sign in to comment.