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

Force waiting for automated merge #40

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
a10160a
Force waiting for automated merge
eduard-bagdasaryan Oct 28, 2020
4b6db52
Adjusted documentation and a couple of fixes
eduard-bagdasaryan Oct 29, 2020
459ed6f
Fixed an image
eduard-bagdasaryan Oct 29, 2020
3c01486
Polished documentation
eduard-bagdasaryan Oct 29, 2020
28113c5
Improved automated merge status reporting
eduard-bagdasaryan Nov 2, 2020
3cfb8ba
Automated merge status description now covers each PR state
eduard-bagdasaryan Nov 3, 2020
bb28a22
Limit SHA printing for automated merge status to two cases
eduard-bagdasaryan Nov 4, 2020
8002def
Fixed _debugString() and some polishing
eduard-bagdasaryan Nov 6, 2020
fda84b0
Refactored automated merge statuses
eduard-bagdasaryan Jan 24, 2022
ab290cc
Distinguish CI-reported status checks from bot-reported ones
eduard-bagdasaryan Jan 27, 2022
d9ccf2c
Refactored CerivedStatusChecks to cover check for a specific scope
eduard-bagdasaryan Jan 31, 2022
5ecd6fd
Adjusted PrProblem messages to have similar style
eduard-bagdasaryan Feb 2, 2022
05b40c3
Documentation adjustments and a couple of fixes
eduard-bagdasaryan Feb 2, 2022
882e936
Removed footnotes from the documentation
eduard-bagdasaryan Feb 2, 2022
ad34328
Bug fixing
eduard-bagdasaryan Feb 4, 2022
374f0b6
Code cleanup
eduard-bagdasaryan Feb 7, 2022
8dff72c
Added an XXX
eduard-bagdasaryan Feb 7, 2022
d4da690
Refactored and adjusted documentation
eduard-bagdasaryan Feb 10, 2022
4262e17
Fixed a couple of bugs and added debugging for each 'phase'
eduard-bagdasaryan Feb 11, 2022
12bd7d8
Removed M-abandoned-staging-checks label
eduard-bagdasaryan Feb 11, 2022
03e483d
Removed a stale XXX
eduard-bagdasaryan Feb 11, 2022
bd6e9ad
Fixed a couple of branch XXX
eduard-bagdasaryan Feb 11, 2022
0c7d0c9
Require that a PrProblem can occur only when statuses are known
eduard-bagdasaryan Feb 15, 2022
7ee8757
Refactored and simplified DerivedStatusChecks
eduard-bagdasaryan Feb 18, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ are satisfied:
text displayed by GitHub for these so called "mergeable" PRs varies
depending on whether the changes are approved and whether the PR
branch is out of date with its target branch.
* All the _required_ checks have succeeded on the PR branch:
* If _all_ checks have succeeded, then GitHub says "All checks have
passed" next to a green check mark:
![](./docs/images/all_passed.png)
* All the _required_ checks have succeeded on the PR branch.
* _all_ checks, except for the special-purpose "automated merge status"
test (see below) have succeeded:
![](./docs/images/merge_automatically_status.png)
* If any _optional_ checks have failed, then GitHub will show "Some
checks were not successful" message:
![](./docs/images/required_passed.png)
Expand Down Expand Up @@ -112,6 +112,14 @@ If a PR processing step fails for PR-specific reasons (e.g., a CI test
failure), then the bot moves on to the next pull request, labeling the
failed PR if/as needed (see below for PR labels).

## Automated merge status

The bot adds this required status automatically to PR and staging commit.
During PR lifecycle the status is "pending", thus preventing the manual
merge button on the GitHub PR page from becoming green. Anubis uses this
mechanism to forbid manual merges, which result in wrong commit messages
and missed staging checks. The bot satisfies this check just before
merging (for the staging commit) and just after merging (for PR).

## Pull request labels

Expand Down Expand Up @@ -298,6 +306,7 @@ All configuration fields are required.
*voting_delay_max* | The maximum merging age of a PR that has fewer than `config::sufficient_approvals` votes. The PR age string should comply with [timestring](https://github.com/mike182uk/timestring) parser. | "10d"
*staging_checks*| The expected number of CI tests executed against the staging branch. | 2
*approval_url*| The URL associated with an approval status test description. | ""
*automated_merge_url*| The URL associated with an automated merge status test description. | ""
*logger_params* | A JSON-formatted parameter list for the [Bunyan](https://github.com/trentm/node-bunyan) logging library [constructor](https://github.com/trentm/node-bunyan#constructor-api). | <pre>{<br> "name": "anubis",<br> "streams": [ ... ]<br>}</pre>

TODO: Merge all three "mutually exclusive" boolean `*_run` options into one `run_mode` option accepting on of four mode names, including "production". Document individual string values in a separate table (here).
Expand Down
Binary file added docs/images/merge_automatically_status.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/required_passed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ConfigOptions {
this._stagingChecks = conf.staging_checks;
this._loggerParams = conf.logger_params;
this._approvalUrl = conf.approval_url;
this._automatedMergeUrl = conf.automated_merge_url;

// unused
this._githubUserNoreplyEmail = null;
Expand Down Expand Up @@ -112,6 +113,15 @@ class ConfigOptions {
approvalContext() { return "PR approval"; }

copiedDescriptionSuffix() { return " (copied from PR by Anubis)"; }

// an URL of the description of the automated merge test status
automatedMergeStatusUrl() { return this._automatedMergeUrl; }

// the 'context name' of the automated merge test status
automatedMergeStatusContext() { return "Merge automatically"; }
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
automatedMergeStatusContext() { return "Merge automatically"; }
automatedMergeStatusContext() { return "Automated merge"; }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


// whether the bot will create the automated merge test statuses for PR and staged commit
manageAutomatedMergeStatus() { return this.automatedMergeStatusUrl().length > 0; }
}

const configFile = process.argv.length > 2 ? process.argv[2] : './config.json';
Expand Down
103 changes: 88 additions & 15 deletions src/MergeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,16 @@ class StatusChecks
assert(expectedStatusCount !== undefined);
assert(expectedStatusCount !== null);
assert(context);
this.expectedStatusCount = expectedStatusCount;
this._expectedStatusCount = expectedStatusCount;
this.context = context;
this.requiredStatuses = [];
this._requiredStatuses = [];
this.optionalStatuses = [];
}

addRequiredStatus(requiredStatus) {
assert(requiredStatus);
assert(!this.hasStatus(requiredStatus.context));
this.requiredStatuses.push(requiredStatus);
this._requiredStatuses.push(requiredStatus);
}

addOptionalStatus(optionalStatus) {
Expand All @@ -179,19 +179,19 @@ class StatusChecks
}

hasStatus(context) {
return this.requiredStatuses.some(el => el.context.trim() === context.trim()) ||
return this._requiredStatuses.some(el => el.context.trim() === context.trim()) ||
this.optionalStatuses.some(el => el.context.trim() === context.trim());
}

hasApprovalStatus(approval) {
return this.requiredStatuses.some(el =>
return this._requiredStatuses.some(el =>
el.context.trim() === Config.approvalContext() &&
el.state === approval.state &&
el.description === approval.description);
}

setApprovalStatus(approval) {
this.requiredStatuses = this.requiredStatuses.filter(st => st.context !== Config.approvalContext());
this._requiredStatuses = this._requiredStatuses.filter(st => st.context !== Config.approvalContext());
let raw = {
state: approval.state,
target_url: Config.approvalUrl(),
Expand All @@ -201,10 +201,40 @@ class StatusChecks
this.addRequiredStatus(new StatusCheck(raw));
}

hasAutomatedMergeStatus(state, description) {
return this._requiredStatuses.some(el =>
el.context.trim() === Config.automatedMergeStatusContext() &&
el.state === state &&
el.description === description);
Copy link
Contributor

Choose a reason for hiding this comment

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

If possible, reorder to place faster and more selective conditions first:

Suggested change
el.context.trim() === Config.automatedMergeStatusContext() &&
el.state === state &&
el.description === description);
el.description === description &&
el.state === state &&
el.context.trim() === Config.automatedMergeStatusContext();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

setAutomatedMergeStatus(state, description) {
this._requiredStatuses = this._requiredStatuses.filter(st => st.context !== Config.automatedMergeStatusContext());
let raw = {
state: state,
target_url: Config.automatedMergeStatusUrl(),
description: description,
context: Config.automatedMergeStatusContext()
};
this.addRequiredStatus(new StatusCheck(raw));
}

// all required statuses except the 'automated merge test' status
requiredStatuses() {
return this._requiredStatuses.filter(st => st.context !== Config.automatedMergeStatusContext());
}

// the 'expected status count' except the 'automated merge test' status
expectedStatusCount() {
const hasAutomated = this._requiredStatuses.some(el => el.context.trim() === Config.automatedMergeStatusContext());
return hasAutomated ? this._expectedStatusCount - 1 : this._expectedStatusCount;
}

// no more required status changes or additions are expected
final() {
return (this.requiredStatuses.length >= this.expectedStatusCount) &&
this.requiredStatuses.every(check => !check.pending());
const required = this.requiredStatuses();
return (required.length >= this.expectedStatusCount()) &&
required.every(check => !check.pending());
}

// something went wrong with at least one of the required status checks
Expand All @@ -215,18 +245,18 @@ class StatusChecks
// Whether at least one of the required status checks failed.
// Ignores checks with the given context, when searching.
_failedExcept(context) {
const filteredChecks = this.requiredStatuses.filter(st => context ? st.context !== context : true);
const filteredChecks = this.requiredStatuses().filter(st => context ? st.context !== context : true);
return filteredChecks.some(check => check.failed());
}

// the results are final and all checks were a success
succeeded() {
return this.final() && this.requiredStatuses.every(check => check.success());
return this.final() && this.requiredStatuses().every(check => check.success());
}

toString() {
let combinedStatus = "context: " + this.context + " expected/required/optional: " + this.expectedStatusCount + "/" +
this.requiredStatuses.length + "/" + this.optionalStatuses.length + ", combined: ";
let combinedStatus = "context: " + this.context + " expected/required/optional: " + this.expectedStatusCount() + "/" +
this.requiredStatuses().length + "/" + this.optionalStatuses.length + ", combined: ";
if (this.failed())
combinedStatus += "failure";
else if (this.succeeded())
Expand All @@ -237,7 +267,7 @@ class StatusChecks
combinedStatus += "to-be-determined";

let requiredDetail = "";
for (let st of this.requiredStatuses) {
for (let st of this.requiredStatuses()) {
if (requiredDetail !== "")
requiredDetail += ", ";
requiredDetail += st.context + ": " + st.state;
Expand Down Expand Up @@ -599,6 +629,41 @@ class PullRequest {
await GH.createStatus(sha, this._approval.state, Config.approvalUrl(), this._approval.description, Config.approvalContext());
}

async _setAutomatedMergeStatuses() {
if (!Config.manageAutomatedMergeStatus())
return;

const state = this._prState.merged() ? "success" : "pending";
Copy link
Contributor

Choose a reason for hiding this comment

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

As we have discussed, this will become pending/success/failure.

const description = this._automatedMergeStatusDescription();

if (!this._prStatuses.hasAutomatedMergeStatus(state, description)) {
await this._createAutomatedMergeStatus(this._prHeadSha(), state);
this._prStatuses.setAutomatedMergeStatus(state, description);
}

await this._setAutomatedMergeStatusForStaged(state);
}

async _setAutomatedMergeStatusForStaged(state) {
const description = this._automatedMergeStatusDescription();
Copy link
Contributor

Choose a reason for hiding this comment

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

Please declare closer to the first use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

if (!Config.manageAutomatedMergeStatus())
return;

if (this._stagedStatuses && !this._stagedStatuses.hasAutomatedMergeStatus(state, description)) {
await this._createAutomatedMergeStatus(this._stagedSha(), state);
this._stagedStatuses.setAutomatedMergeStatus(state, description);
}
}

async _createAutomatedMergeStatus(sha, state) {
if (this._dryRun("creating automated merge status"))
return;
await GH.createStatus(sha, state, Config.automatedMergeStatusUrl(),
this._automatedMergeStatusDescription(), Config.automatedMergeStatusContext());
}

_automatedMergeStatusDescription() { return this._prState.toString(); }
Copy link
Contributor

Choose a reason for hiding this comment

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

As we have discussed, the automated merge status check description should be more detailed. We should also avoid internal/undocumented terminology like "brewing". Here are a few rough suggestions:

  • success (M-merged) and success (will be merged) -- see another change request for details.

  • failure (M-failed-foo) -- where M-failed-foo is a PR label that corresponds to a "problematic" condition "foo" that requires a human action to correct (e.g., M-failed-description). On a logical level, the set of such labels/conditions includes M-failed-review. There is no such label (yet?); we can use PR approval text instead. There can be multiple active M-failed-foo labels (at least in theory). If that can happen in the current code, to avoid noise and to simplify implementation, I would probably list just one label (e.g., the first encountered label or whichever is easier to implement).

  • pending (M-waiting-foo) -- where M-waiting-foo is a PR label that corresponds to a "positive" condition "foo" that Anubis is waiting for (e.g., M-waiting-staging-checks). On a logical level (at least), the set of such labels/conditions includes M-waiting-objections and M-waiting-approvals that are documented but not yet set; we can use their names (because they are documented) or just use PR approval text instead of those two label names (because they are not implemented). My weak recommendation is to do the latter. I do not think there can be multiple active M-waiting-foo labels (today), but please correct me if I am wrong.

When multiple states apply, the state listed higher (above) wins.

For any M-{waiting,failed}-staging-checks label inside the status check description, it would be nice to add a staged commit SHA: pending (M-waiting-staging-checks for SHA) and failure (M-failed-staging-checks for SHA).

Similarly, it would be nice to add SHA to the visible success status -- success (will be merged as SHA) and success (M-merged as SHA). AFAICT, success (will be merged as SHA) will be visible (at least for a while) if the final merge step fails so having SHA here would be useful. If the success (M-merged as SHA) status is never actually visible (except in the merged commit itself), then adding SHA to that specific status would be kind of pointless and, hence, is not necessary. On the other hand, automatic PR closure may fail or a human can reopen an auto-closed request, making even the "final" success status visible, right?


async _getRequiredContexts() {
if (this._requiredContextsCache)
return this._requiredContextsCache;
Expand Down Expand Up @@ -639,7 +704,9 @@ class PullRequest {
// returns filled StatusChecks object
async _getStagingStatuses() {
const combinedStagingStatuses = await GH.getStatuses(this._stagedSha());
const genuineStatuses = combinedStagingStatuses.statuses.filter(st => !st.description.endsWith(Config.copiedDescriptionSuffix()));
const genuineStatuses = combinedStagingStatuses.statuses.filter(st =>
!st.description.endsWith(Config.copiedDescriptionSuffix()) &&
st.context !== Config.automatedMergeStatusContext());
assert(genuineStatuses.length <= Config.stagingChecks());
let statusChecks = new StatusChecks(Config.stagingChecks(), "Staging");
// all genuine checks are 'required'
Expand Down Expand Up @@ -763,6 +830,8 @@ class PullRequest {

assert(!this._prState.merged());

await this._setAutomatedMergeStatuses();

this._messageValid = this._prMessageValid();
this._log("messageValid: " + this._messageValid);

Expand Down Expand Up @@ -792,6 +861,8 @@ class PullRequest {

assert(this._prState.merged());

await this._setAutomatedMergeStatuses();

// Clear any positive labels (there should be no negatives here)
// because Config.mergedLabel() set below already implies that all
// intermediate processing steps have succeeded.
Expand Down Expand Up @@ -1076,7 +1147,7 @@ class PullRequest {
this._log("_supplyStagingWithPrRequired: skip existing " + requiredContext);
continue;
}
const requiredPrStatus = this._prStatuses.requiredStatuses.find(el => el.context.trim() === requiredContext.trim());
const requiredPrStatus = this._prStatuses.requiredStatuses().find(el => el.context.trim() === requiredContext.trim());
assert(requiredPrStatus);
assert(!requiredPrStatus.description.endsWith(Config.copiedDescriptionSuffix()));

Expand Down Expand Up @@ -1166,6 +1237,8 @@ class PullRequest {

assert(!this._stagingBanned);

await this._setAutomatedMergeStatusForStaged("success");
Copy link
Contributor

Choose a reason for hiding this comment

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

Since we are setting this a bit prematurely/optimistically, it would be nice to make this one success (will merge SHA) and then, if the status may become visible anywhere after we successfully merge (as discussed in another change request), change that to success (M-merged as SHA).


try {
await GH.updateReference(this._prBaseBranchPath(), this._stagedSha(), false);
} catch (e) {
Expand Down