Add auth0 management code

This commit is contained in:
Saurabh Misra 2023-12-10 17:26:30 -08:00
parent 4d744d5f80
commit 81502395ec
5 changed files with 43 additions and 32 deletions

16
js/cf-api/auth0-utils.ts Normal file
View file

@ -0,0 +1,16 @@
import { ManagementClient } from "auth0"
export async function userNickname(userId: string): Promise<string | null> {
const m = new ManagementClient({
domain: process.env.AUTH0_ISSUER_BASE_URL || "",
clientId: process.env.AUTH0_CLIENT_ID_MANAGEMENT || "",
clientSecret: process.env.AUTH0_CLIENT_SECRET_MANAGEMENT || "",
})
try {
const user = await m.users.get({ id: userId, fields: "nickname" })
return user.data?.nickname
} catch (error) {
console.log("Error getting user nickname:", error)
return null
}
}

View file

@ -1,24 +0,0 @@
import fetch, { type RequestInit, type Response } from "node-fetch"
// TODO use node-auth0 sdk for this
export async function userNickname(userId: string): Promise<string | null> {
const url = new URL(`${process.env.AUTH0_ISSUER_BASE_URL}/api/v2/users/${userId}`)
const params = new URLSearchParams({
fields: "nickname",
include_fields: "true",
})
url.search = params.toString()
const options: RequestInit = {
method: "GET",
headers: {
authorization: `Bearer ${process.env.AUTH0_MANAGEMENT_API_TOKEN}`,
},
}
const response: Response = await fetch(url, options)
if (response.ok) {
const user: { nickname } = await response.json()
return user.nickname
} else return null
}

View file

@ -8,7 +8,7 @@ import { createPullRequestFromDiffContents } from "./create-pr-from-diffcontents
import { githubApp, webhookApiPath } from "./github-app"
import functions from "@codeflash-ai/common/token-functions"
import dotenv from "dotenv"
import { userNickname } from "./auth0"
import { userNickname } from "./auth0-utils"
const userForAPIKey = functions.userForAPIKey

View file

@ -14,6 +14,7 @@
"@azure/keyvault-secrets": "^4.7.0",
"@codeflash-ai/common": "1.0.1",
"@prisma/client": "^5.6.0",
"auth0": "^4.2.0",
"body-parser": "^1.20.2",
"code-suggester": "^4.3.3",
"dotenv": "^16.3.1",

View file

@ -8,23 +8,41 @@ def main():
root = pathlib.Path(__file__).parent.parent.parent.resolve()
test_root = root / "code_to_optimize" / "tests" / "pytest"
print("cwd", root)
command = ["python", "codeflash/main.py", "--file", "code_to_optimize/bubble_sort.py", "--function", "sorter",
"--test-root", str(test_root), "--root", str(root)]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
cwd=str(root), env=os.environ.copy())
command = [
"python",
"codeflash/main.py",
"--file",
"code_to_optimize/bubble_sort.py",
"--function",
"sorter",
"--test-root",
str(test_root),
"--root",
str(root),
]
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
cwd=str(root),
env=os.environ.copy(),
)
output = []
for line in process.stdout:
print(line, end='') # Print each line in real-time
print(line, end="") # Print each line in real-time
output.append(line) # Store each line in the output variable
return_code = process.wait()
stdout = ''.join(output)
stdout = "".join(output)
assert return_code == 0, f"The codeflash command returned exit code {return_code} instead of 0"
m = re.search(r"Performance went up by (\d+\.\d+)x", stdout)
assert m, "Failed to find performance improvement at all"
improvement = float(m.group(1))
assert 30000 < improvement < 120000, "Performance improvement was not in the expected range"
assert (
30000 < improvement < 120000
), f"Performance improvement was not in the expected range, got {improvement}"
if __name__ == "__main__":