Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improved server, security and commands #529

Merged
merged 2 commits into from
Dec 20, 2023

Conversation

lukefx
Copy link
Contributor

@lukefx lukefx commented Dec 17, 2023

I improved the Bitbucket server to enable all commands like review, ask and so on... plus I added the missing webhook HMAC security check.

Signed-off-by: Luca Simone <info@lucasimone.info>
@mrT23 mrT23 requested a review from okotek December 17, 2023 16:44
@mrT23
Copy link
Collaborator

mrT23 commented Dec 17, 2023

@okotek
can you take a look ?

Copy link
Contributor

@okotek okotek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contributions, please see the comments

log_context["api_url"] = url
with get_logger().contextualize(**log_context):
background_tasks.add_task(PRAgent().handle_request, url, body)


@router.post("/webhook")
@router.post("/")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the webhook endpoint to /?

Copy link
Contributor Author

@lukefx lukefx Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because on the configuration page in bitbucket, there is a Test connection button, if the webhook has the same get and post URL you can see if the call succeeds or not otherwise this test will always fail.

An example, it succeeds because the URL supports get and post
image


router = APIRouter()


def handle_request(background_tasks: BackgroundTasks, url: str, body: str, log_context: dict):
def handle_request(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you change the parenthesis styling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black... I linted and formatted with black

pr_agent/servers/bitbucket_server_webhook.py Outdated Show resolved Hide resolved
if webhook_secret:
body_bytes = await request.body()
signature_header = request.headers.get("x-hub-signature", None)
verify_signature(body_bytes, webhook_secret, signature_header)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you verify that it works as expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it works... the HMAC method is the same as GitHub just the header has a slightly different name (without -256)

elif data["eventKey"] == "pr:comment:added":
body = data["comment"]["text"]
else:
return JSONResponse(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should return 200 in this case, this is not an error, unless you know differently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we can, but you have to configure which events you want to be pushed as webhook. If you push some events that are not supported it could be a bad request, or could be a 200 and who cares or, could also be a 501 not implemented which makes also sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking before in some cases, after a certain threshold of errors the webhook is disabled.

app.include_router(router)
uvicorn.run(app, host="0.0.0.0", port=3000)
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", "3000")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change this to BITBUCKET_SERVER.PORT, use get_settings().get(), add to configuration file

Copy link
Contributor Author

@lukefx lukefx Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's like this because some cloud providers uses an environment variable PORT to bind the ingress... also in bitbucket_app.py is done like this:

https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/servers/bitbucket_app.py#L162

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't complain on my own code :(

@okotek okotek merged commit ccb1169 into Codium-ai:main Dec 20, 2023
2 checks passed
yochail pushed a commit to yochail/pr-agent that referenced this pull request Feb 11, 2024
…ements

feat: Improved server, security and commands
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants