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

fix(merge-on-green): add cleanup cron job, fix blocking issue for removing #1112

Merged
merged 21 commits into from Nov 13, 2020
Merged

fix(merge-on-green): add cleanup cron job, fix blocking issue for removing #1112

merged 21 commits into from Nov 13, 2020

Conversation

sofisl
Copy link
Contributor

@sofisl sofisl commented Nov 12, 2020

See java-bigtable/498, and this issue.

I did some further digging into this multiple commenting issue, that did not seem to be solved by #1091. I think the real issue here is when people add an 'automerge' label and then either remove that label, close, or merge that PR soon after. I think a race condition is happening where the PR is not yet recorded in the Datastore table when the PR is closed or removed, making so that it doesn't actually get removed.

I don't know how to solve this problem categorically. What I ended up doing is creating a clean-up cron job that runs every 30 minutes. Every 30 minutes, if the cron job is the 'cleanup' job, the app will go through the PRs in the Datastore table and clean up ones that are either closed, merged, or do not have an automerge label on them anymore.

@sofisl sofisl requested a review from a team November 12, 2020 08:31
@google-cla google-cla bot added the cla: yes This human has signed the Contributor License Agreement. label Nov 12, 2020
Copy link
Contributor

@tbpg tbpg left a comment

Choose a reason for hiding this comment

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

Design question: could we do this cleanup "check" before doing anything with a PR in the normal loop/cron? That way, there won't be a delay between the problematic state and when it gets fixed by the next cleanup cron iteration.

Also, I saw the multiple-comment behavior on #1111, which doesn't seem like the same issue?

@@ -148,6 +148,59 @@ handler.cleanUpPullRequest = async function cleanUpPullRequest(
.catch(logger.error);
};

/**
* Check if PR has been merged, closed, or unlabeled, then remove
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing end of sentence?

@sofisl
Copy link
Contributor Author

sofisl commented Nov 12, 2020

Thanks for the review @tbpg!

Design question: could we do this cleanup "check" before doing anything with a PR in the normal loop/cron? That way, there won't be a delay between the problematic state and when it gets fixed by the next cleanup cron iteration.

I think what you're asking is if we can check to see if the PR is closed/merged/unlabeled before doing something in the regular workflow. This was the previous implementation of the bot, but I changed it in #1023 because checking this info was causing us to run into rate limits very quickly. Now, we wait for the unlabeled/closed/merged event to update the Datastore table. But, now we're running into a separate issue on whether or not the PR is in the table before it gets deleted if it gets added and deleted in the table too quickly.

But, maybe we run the cleanup cron job every 15 minutes, instead of every 30? It is an expensive task to do, but it might be worth to address these issues. I'll leave that suggestion up to you and/or anyone else who has thoughts on it :)

Also, I saw the multiple-comment behavior on #1111, which doesn't seem like the same issue?

Funny you mention that PR, it is actually the same issue! I was testing it last night. I quickly added and removed the automerge label.

Screen Shot 2020-11-12 at 12 19 47 PM

If you see there, MOG actually ended up merging that PR, even though it didn't have an automerge label on it. I am not exactly clear on what causes the multiple commenting issue, but I do know that this fix should solve it, since that PR shouldn't have even been in the Datastore table to begin with.

@@ -26,7 +26,7 @@ import Ajv from 'ajv';
import yaml from 'js-yaml';
import {PullsListFilesResponseData} from '@octokit/types';

export const configFileName = 'sync-repo-settings.yaml';
export const configFileName = 'sync-repo-settings.yml';
Copy link
Contributor

Choose a reason for hiding this comment

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

There seem to be some changes unrelated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oof, thanks for the heads up. Need to resync to main branch.

@sofisl sofisl requested a review from tbpg November 12, 2020 22:19
@sofisl sofisl requested a review from tmatsuo November 12, 2020 22:59
@@ -340,12 +340,6 @@ async function updateRepoTeams(
logger.info(`Update team access for ${repo}`);
const [owner, name] = repo.split('/');

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this also an irrelevant change?

assert(removePRStub.called);
});

it('deletes a PR if label is not found when cleaning up repository', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

awesome to see the increased coverage 👍

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

The logic in schedule.repository is getting pretty expansive, perhaps we could split it into two helpers that get called depending on the type of payload:

runCleanUp/runMergeOnGreen or something like that?

@@ -210,15 +297,19 @@ function handler(app: Application) {
github as any
);
if (remove || wp.state === 'stop') {
await handler.removePR(wp.url);
try {
Copy link
Contributor

Choose a reason for hiding this comment

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

spacing looks a little weird here, might need to run npm run fix.

@@ -188,6 +252,29 @@ function handler(app: Application) {
//open issue for a fix: https://github.com/octokit/webhooks.js/issues/277
app.on('schedule.repository' as any, async context => {
const watchedPRs = await handler.listPRs();
if (context.payload.cleanUp === true) {
while (watchedPRs.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I might be tempted to pull this into a helper function.

@sofisl sofisl requested review from bcoe and tmatsuo November 12, 2020 23:53
@tmatsuo
Copy link
Contributor

tmatsuo commented Nov 13, 2020

This is kind of meta comment. Take this a grain of solt.

The PR title should be something like:

fix(merge-on-green): add cleanup cron job
  1. Maybe we should use package name in the paren
  2. More concrete title is better in my opinion

@sofisl sofisl changed the title fix: multiple commenting issue fix(merge-on-green): add cleanup cron job Nov 13, 2020
@sofisl sofisl changed the title fix(merge-on-green): add cleanup cron job fix(merge-on-green): add cleanup cron job, fix blocking issue for removing Nov 13, 2020
Copy link
Contributor

@tbpg tbpg left a comment

Choose a reason for hiding this comment

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

A few small comments. Otherwise, LGTM.

})
).data;
} catch (err) {
pr = undefined;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this return early rather than continue unnecessarily?

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 might leave this just in case the github API is unavailable for some reason, but we can confirm by checking to see if a label is on the PR in the next function.

label.name === MERGE_ON_GREEN_LABEL_SECURE
);

if (pr?.merged === true || pr?.state === 'closed' || !foundLabel) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (pr?.merged === true || pr?.state === 'closed' || !foundLabel) {
if (pr?.merged || pr?.state === 'closed' || !foundLabel) {

packages/merge-on-green/src/merge-on-green.ts Outdated Show resolved Hide resolved
@sofisl sofisl merged commit 7a9ac98 into googleapis:master Nov 13, 2020
gcf-owl-bot bot added a commit that referenced this pull request Jun 9, 2021
VERSION is used in @google-cloud/cloud-rad to publish ref docs for
a particular version. Pass VERSION in via Stubby or Fusion.
Source-Link: googleapis/synthtool@740366b
Post-Processor: gcr.io/repo-automation-bots/owlbot-nodejs:latest@sha256:bbdd52de226c00df3356cdf25460397b429ab49552becca645adbc412f6a4ed5
gcf-merge-on-green bot pushed a commit that referenced this pull request Jun 9, 2021
VERSION is used in @google-cloud/cloud-rad to publish ref docs for
a particular version. Pass VERSION in via Stubby or Fusion.
Source-Link: googleapis/synthtool@740366b
Post-Processor: gcr.io/repo-automation-bots/owlbot-nodejs:latest@sha256:bbdd52de226c00df3356cdf25460397b429ab49552becca645adbc412f6a4ed5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants