Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jun 18, 2024
1 parent c0815ed commit 9f350d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
3 changes: 1 addition & 2 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import process from 'process'

// --- Initialization ------------------------------------------------------------------------------------------------

log.debug({appId: config.githubAppAuth.appId}, 'GitHub app')
const GITHUB_ACTIONS_ACCESS_MANAGER = await accessTokenManager(config.githubAppAuth)
const GITHUB_ACTIONS_ACCESS_MANAGER = await accessTokenManager(config)

// --- Server Setup --------------------------------------------------------------------------------------------------
export const app = new Hono<{ Variables: { log: Logger, id: string } }>()
Expand Down
22 changes: 12 additions & 10 deletions server/src/github-actions-access-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ import {Status} from './common/http-utils.js'
import {components} from '@octokit/openapi-types'
import {createAppAuth} from '@octokit/auth-app'
import limit from 'p-limit'
import {config} from './config.js'
import log from './logger.js'
import type {
RestEndpointMethodTypes,
} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types'

/**
* GitHub Access Manager
* @param appAuth - GitHub App authentication
* @param options - options
* @returns access token manager
*/
export async function accessTokenManager(appAuth: {
appId: string,
privateKey: string,
}) {
const GITHUB_APP_CLIENT = new Octokit({authStrategy: createAppAuth, auth: appAuth})
export async function accessTokenManager(options: {
githubAppAuth: { appId: string, privateKey: string, },
accessPolicyLocation: {
owner: { repo: string, path: string,},
repo: { path: string}
}}) {
log.debug({appId: options.githubAppAuth.appId}, 'GitHub app')
const GITHUB_APP_CLIENT = new Octokit({authStrategy: createAppAuth, auth: options.githubAppAuth})
const GITHUB_APP = await GITHUB_APP_CLIENT.apps.getAuthenticated()
.then((res) => res.data!)

Expand Down Expand Up @@ -106,8 +108,8 @@ export async function accessTokenManager(appAuth: {

// --- load owner access policy ----------------------------------------------------------------------------------
const ownerAccessPolicy = await getOwnerAccessPolicy(appInstallationClient, {
owner: tokenRequest.owner, repo: config.accessPolicyLocation.owner.repo,
path: config.accessPolicyLocation.owner.path,
owner: tokenRequest.owner, repo: options.accessPolicyLocation.owner.repo,
path: options.accessPolicyLocation.owner.path,
strict: false, // ignore invalid access policy entries
})
log.debug({ownerAccessPolicy}, `${tokenRequest.owner} access policy:`)
Expand Down Expand Up @@ -196,7 +198,7 @@ export async function accessTokenManager(appAuth: {
tokenRequest.repositories.map((repo) => limitRepoPermissionRequests(async () => {
const repoAccessPolicy = await getRepoAccessPolicy(appInstallationClient, {
owner: tokenRequest.owner, repo,
path: config.accessPolicyLocation.repo.path,
path: options.accessPolicyLocation.repo.path,
strict: false, // ignore invalid access policy entries
})
log.debug({repoAccessPolicy}, `${tokenRequest.owner}/${repo} access policy`)
Expand Down

0 comments on commit 9f350d3

Please sign in to comment.