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

Add a simulate page #455

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

Add a simulate page #455

wants to merge 3 commits into from

Conversation

ashmeer7
Copy link

@ashmeer7 ashmeer7 commented Jul 29, 2022

This allows a user to:

  • Input a branch that has a policy file. The simulate page will evaluate
    that policy file on the given PR
  • In put a user to simulate a fake approval for. The simulate page will
    evaluate the PR as if the user had submitted an approval
  • Or both!

Abstracted away the majority of functionality into a BaseDetails
handler. This allows consumers to submit a policy and a user to override
the defaults done by the Details handler. The Details page still exists
since it currently sends the information back to GitHub.

Here is a sample of the simulate page on a test repository:
Screen Shot 2022-07-28 at 6 30 14 PM

This allows a user to:
- Input a branch that has a policy file. The simulate page will evaluate
  that policy file on the given PR
- In put a user to simulate a fake approval for. The simulate page will
  evaluate the PR as if the user had submitted an approval
- Or both!

Abstracted away the majority of functionality into  a BaseDetails
handler. This allows consumers to submit a policy and a user to override
the defaults done by the Details handler. The Details page still exists
since it currently sends the information back to GitHub.
@ashmeer7 ashmeer7 requested a review from bluekeyes July 29, 2022 00:11
@bluekeyes
Copy link
Member

Could you add a screenshot of what the simulate form and the button look like to the PR description?

This makes it possible to handle errors similar to how it was done
before
Copy link
Member

@bluekeyes bluekeyes left a comment

Choose a reason for hiding this comment

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

Sorry about the delay reviewing this. I think we'll want to make some improvements to the form UI, but I haven't had a chance to pull this down and mess around with the HTML and styles yet, so I don't have specific suggestions right now.

let simulatePath = currentPath.replace("details", "simulate")
document.location = simulatePath
}
document.getElementById(buttonId).addEventListener("click", redirectToSimulate)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this needs to be a JS function. We should be able to use a link that we style to look like a button, where the target URL is rendered as part of the page.

Comment on lines +15 to +16
prefillParam(BRANCH)
prefillParam(USERNAME)
Copy link
Member

Choose a reason for hiding this comment

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

Instead of hardcoding specific fields here, it might be nice to make this a bit more generic. One idea is to add a data-queryparam attribute to all of the input tags that need to be pre-filled. Then the JS can use querySelectorAll to find all of them, read the value of of the attribute to get the parameter name, and then do the logic you have here to set the value to the right thing.

Comment on lines +236 to +240
func (b *Base) EvaluateConfig(ctx context.Context, prctx pull.Context, client *github.Client, evaluator common.Evaluator, fc *FetchedConfig) common.Result {
result := evaluator.Evaluate(ctx, prctx)

return result
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we need this function since it only calls a function on an argument with no other processing.

Copy link
Author

Choose a reason for hiding this comment

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

I went with this approach to contrast with EvaluateFetchedConfig.

This one does the evaluating portion (and any processing if required) while EvaluateFetchedConfig evaluates and posts the results. This way, the simulate page can display what is happening without messing with the status of the PR.

logger := zerolog.Ctx(ctx)
statusDescription := result.StatusDescription
Copy link
Member

Choose a reason for hiding this comment

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

Any reason this moved up here instead of staying where it was?

Copy link
Author

Choose a reason for hiding this comment

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

It made it easier to understand what was going on IMO

return result
}

func (b *Base) PostEvaluatedResults(ctx context.Context, prctx pull.Context, client *github.Client, evaluator common.Evaluator, fc *FetchedConfig, result common.Result) (common.Result, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove evaluator as an argument here since it is not used

DetailsBase
}

type fakePrContext struct {
Copy link
Member

Choose a reason for hiding this comment

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

nit: let's call this SimulatePullContext or something like that

return reviews, err
}

func new(ctx pull.Context) *fakePrContext {
Copy link
Member

Choose a reason for hiding this comment

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

nit: calling this new shadows a builtin Go function; NewSimulatePullContext or something like that would be better

@@ -1,12 +1,14 @@
{{/* templatetree:extends page.html.tmpl */}}
{{/* templatetree:extends base-details.html.tmpl */}}
{{define "title"}}{{.PullRequest.GetBase.GetRepo.GetFullName}}#{{.PullRequest.GetNumber}} - Details | PolicyBot{{end}}
Copy link
Member

Choose a reason for hiding this comment

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

This is now duplicated with base-details.html.tmpl, as is body-class and some other templates. If we're going to extend a template, let's either remove these overrides or remove the definitions from the parent.

{{end}}
{{define "footer"}}
<footer class="bg-white shadow-sm w-full z-10">
<button id="details-to-simulate" class="bg-blue3 hover:bg-blue5 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" type="button">Simulate changes on this PR</button>
Copy link
Member

Choose a reason for hiding this comment

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

As mentioned on the JS file, this should be a regular link that we style to look like a button if you want

<div class="bg-light-gray3 border-b border-gray3">
<div class="pl-5 pt-5">
<form class="w-full max-w-md" method="get">
<div class="md:flex md:items-center mb-6">
Copy link
Member

Choose a reason for hiding this comment

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

As a general rule, I've tried to avoid the media query based classes (md: prefix) in favor of things that naturally scale and adjust as the screen size changes. I'd probably see how this works with the un-prefixed classes and then adjust from there.

@bluekeyes
Copy link
Member

#476 is going to conflict with this, but it should make it much easier to evaluate a PR without posting status checks.

@ashmeer7
Copy link
Author

Addressed some PR comments and fixed what I could fix without getting a whole test setup. I'll be back with more questions soon enough.

@elliot-graebert-skydio
Copy link

elliot-graebert-skydio commented Aug 12, 2023

@bluekeyes this came up when deploying Policy-Bot. Could we get this merged? Use case was simply trying to test out a new policy on a branch, and not knowing the right path to do so.

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