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

Check for branch protection before raising a PR #824

Closed
wants to merge 8 commits into from

Conversation

NovemberTang
Copy link
Contributor

@NovemberTang NovemberTang commented Feb 27, 2024

What does this change?

Any part of the service catalogue using createPrAndAddToProject will now check that the repo has sufficient branch protections before raising a PR.

Why?

These PRs need to be reviewed by teams as they have been raised by a bot. We make requests to GitHub to get this information. We can't rely on repocop to provide this information for two reasons:

  1. Branch protection data could be out of date, especially if the job has failed recently
  2. CloudQuery does not collect data about rulesets or custom properties

How has it been verified?

This has been tested on PROD. #823 was generated as a result, as the repo does not have branch protection, but does have the ruleset applied as a consequence of its production_status property

@NovemberTang NovemberTang marked this pull request as ready for review February 27, 2024 11:12
@NovemberTang NovemberTang requested review from a team as code owners February 27, 2024 11:12
);
}
} else {
console.log(`Testing generation of ${fileName} for ${repoName}`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this now exists as logPr()


console.debug(allResults.data);

const result = allResults.data.find(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

repos that contain the custom property production_status : production have a branch protection ruleset applied to them automatically

Comment on lines +108 to +110
const hasProtection =
protection.data.enabled === true &&
!!protection.data.required_pull_request_reviews;
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 to not get this from the database? There's a protected column in the github_repository_branches table.

Copy link
Contributor Author

@NovemberTang NovemberTang Feb 27, 2024

Choose a reason for hiding this comment

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

Defence in depth. It's possible that this is overkill but my reasoning is:

Ideally whoever is invoking this function would have made sure that the repo they're referring to has branch protection, but there's no guarantee of that. There's also no guarantee that the branch protection they have includes a review requirement. Basically, I'm trying to trust repocop as little as possible. (FWIW when repocop starts sending messages to this lambda I do plan to have it filter out unprotected repos, but someone using this a year from now might forget).

An alternative approach could be establishing a requirement to bundle prisma into any lambda that uses this function, and calling out to the DB at runtime, but I think that'll increase the setup complexity, increase the size of the zip 100-1000x, make inline lambda editing impossible, and is a bit tricky to document, so may not be worth it to grab two pieces of information.

Another (minor) point is that it's possible that a repo's branch protection has been changed between CQ running, and this lambda being invoked, and this code handles that edge case.

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I'm not quite sure I understand how we're expecting this lambda to work... Are you suggesting it's manually invoked by a person every time? If so, I'd suggest we don't need AWS infrastructure here; a script that we run locally would simplify things.

I'd suggest we move to a setup where the lambda is invoked either on a schedule, or on an event (e.g. the CloudQuery task completing). This removes human error, and answers:

a repo's branch protection has been changed between CQ running, and this lambda being invoked

If the lambda does need to be invoked by a human from time to time, could we script it? The script would perform the pre-flight checks of ensuring branch protection is enabled, etc.

make inline lambda editing impossible

I'd challenge the use-case for this. Editing the lambda function in place removes any audit trail!

As I said though, I'm not sure I understand the role/context of this lambda - happy to chat about it.

repoName: string,
): Promise<boolean> {
const allResults = await octokit.request(
'GET /repos/{owner}/{repo}/properties/values',
Copy link
Member

Choose a reason for hiding this comment

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

Similar to above, any reason to not get this from the database? There's a custom_properties field in the github_repositories table.

prTitle: string,
prBody: string,
) {
console.log(`Testing generation of ${fileName} for ${repoName}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this line make sense for all uses of logPr?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that it did. Where do you think it doesn't make sense?

@NovemberTang
Copy link
Contributor Author

Reasons to close:
We're not currently doing anything dangerous. This PR is anticipating future "dangerous" PRs that we might raise, but we can avoid that by just not raising them.

To keep functions relatively single-purpose, it's probably best for the protection check to happen somewhere else.

@akash1810 akash1810 deleted the nt/require-branch-protection branch May 13, 2024 14:12
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.

3 participants