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: attach PR information as context to logs #4660

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Changes from all commits
Commits
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
35 changes: 30 additions & 5 deletions packages/merge-on-green/src/merge-on-green.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,30 @@ handler.cleanDatastoreTable = async function cleanDatastoreTable(
}
};

/**
* Returns a new child logger with added PR contexst.
* @param {GCFLogger} logger The base logger
* @param {DatastorePR} watchedPR The pull request
* @returns {GCFLogger} New logger with added context
*/
function addPullRequestLoggerContext(
logger: GCFLogger,
watchedPR: DatastorePR
): GCFLogger {
// deep copy base logger bindings
const bindings = JSON.parse(JSON.stringify(logger.getBindings()));
if (!bindings.trigger) {
bindings.trigger = {};
}
bindings.trigger.trigger_source_repo = {
owner: watchedPR.owner,
repo_name: watchedPR.repo,
url: `https://github.com/${watchedPR.owner}/${watchedPR.repo}`,
};
bindings.pull_request_url = watchedPR.url;
return logger.child(bindings);
}

/**
* Calls the main MOG logic, either deletes or keeps that PR in the Datastore table
* @param watchedPRs array of watched PRs
Expand All @@ -433,7 +457,8 @@ handler.checkPRMergeability = async function checkPRMergeability(
const work = watchedPRs.splice(0, WORKER_SIZE);
await Promise.all(
work.map(async wp => {
logger.info(`checking ${wp.url}, ${wp.installationId}`);
const prLogger = addPullRequestLoggerContext(logger, wp);
prLogger.info(`checking ${wp.url}, ${wp.installationId}`);
try {
const remove = await mergeOnGreen(
wp.owner,
Expand All @@ -445,10 +470,10 @@ handler.checkPRMergeability = async function checkPRMergeability(
wp.label,
wp.author,
octokit,
logger
prLogger
);
if (remove || wp.state === 'stop') {
await handler.removePR(wp.url, logger);
await handler.removePR(wp.url, prLogger);
try {
await handler.cleanUpPullRequest(
wp.owner,
Expand All @@ -459,15 +484,15 @@ handler.checkPRMergeability = async function checkPRMergeability(
octokit
);
} catch (err) {
logger.warn(
prLogger.warn(
`Failed to delete reaction and label on ${wp.owner}/${wp.repo}/${wp.number}`
);
}
}
} catch (e) {
const err = e as Error;
err.message = `Error in merge-on-green: \n\n${err.message}`;
logger.error(err);
prLogger.error(err);
}
})
);
Expand Down