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

feat: post processors can specify new PR title and body #4774

Merged
merged 4 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions packages/owl-bot/cloud-build/update-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ steps:
dir: '${_PR_REPOSITORY}'
env:
- 'DEFAULT_BRANCH=$_DEFAULT_BRANCH'
- 'NEW_PULL_REQUEST_TEXT_PATH=/workspace/new_pull_request_text.txt'

# Install credentials *after* running the container.
- name: 'bash'
Expand Down Expand Up @@ -91,6 +92,9 @@ steps:
- ${_PR}
- --github-token
- ${_GITHUB_TOKEN}
- --new-pull-request-text-path
- /workspace/new_pull_request_text.txt

dir: ${_PR_REPOSITORY}
env:
- 'HOME=/workspace'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ import * as proc from 'child_process';
import path = require('path');
import {githubRepoFromOwnerSlashName} from '../../github-repo';
import {hasGitChanges} from '../../git-utils';
import * as fs from 'fs';
import {resplit, WithRegenerateCheckbox} from '../../create-pr';

interface Args {
'dest-repo': string;
pr: number;
'github-token': string;
'repo-path': string;
'new-pull-request-text-path': string;
}

export const commitPostProcessorUpdateCommand: yargs.CommandModule<{}, Args> = {
Expand Down Expand Up @@ -64,6 +67,12 @@ export const commitPostProcessorUpdateCommand: yargs.CommandModule<{}, Args> = {
type: 'string',
demand: true,
})
.option('new-pull-request-text-path', {
describe:
'Path to a text file containing the new pull request title and body.',
type: 'string',
default: '',
})
.option('repo-path', {
describe: 'Local path to the repository',
type: 'string',
Expand Down Expand Up @@ -95,11 +104,12 @@ export async function commitPostProcessorUpdate(args: Args): Promise<void> {
// Check if the ignore label has been added during the post-processing.
// If so, do not push changes.
console.log(`Retrieving PR info for ${repo}`);
const {data: prData} = await octokit.pulls.get({
const prOwnerRepoPullNumber = {
owner: repo.owner,
repo: repo.repo,
pull_number: args.pr,
});
};
const {data: prData} = await octokit.pulls.get(prOwnerRepoPullNumber);
console.log(`Retrieved PR info for ${repo}`);

if (prData.labels.find(label => label.name === OWL_BOT_IGNORE)) {
Expand Down Expand Up @@ -138,6 +148,14 @@ export async function commitPostProcessorUpdate(args: Args): Promise<void> {
cmd('git pull --no-verify', {cwd: repoDir});
// Push changes back to origin.
cmd('git push --no-verify', {cwd: repoDir});

// Update the PR title and body if new ones were provided.
const text_path = args['new-pull-request-text-path'];
if (text_path && fs.existsSync(text_path)) {
const text = fs.readFileSync(text_path).toString();
const prContent = resplit(text, WithRegenerateCheckbox.No);
await octokit.pulls.update({...prOwnerRepoPullNumber, ...prContent});
}
}

export function commitOwlbotUpdate(repoDir: string) {
Expand Down
31 changes: 30 additions & 1 deletion packages/owl-bot/test/commit-post-processor-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {copyTagFrom} from '../src/copy-code';
import sinon from 'sinon';
import nock from 'nock';
import {OWL_BOT_IGNORE, OWLBOT_RUN_LABEL} from '../src/labels';
import * as fs from 'fs';

export function makeOrigin(logger = console): string {
const cmd = newCmd(logger);
Expand Down Expand Up @@ -104,6 +105,7 @@ describe('commitPostProcessorUpdate', () => {
pr,
'github-token': gitHubToken,
'repo-path': repoPath,
'new-pull-request-text-path': '',
};
}

Expand Down Expand Up @@ -177,21 +179,48 @@ describe('commitPostProcessorUpdate', () => {
assert.match(log, /Updates from OwlBot/);
});

it('adds commit when .OwlBot.yaml contains no flag', async () => {
function makeRepoWithPendingCommit(): {origin: string; clone: string} {
const origin = makeOrigin();
const clone = cloneRepo(origin);
makeDirTree(clone, [`${yamlPath}:deep-remove-regex:\n - /pasta.txt`]);
cmd(`git add ${yamlPath}`, {cwd: clone});
const copyTag = copyTagFrom(yamlPath, 'abc123');
cmd(`git commit -m "Copy-Tag: ${copyTag}"`, {cwd: clone});
makeDirTree(clone, ['a.txt:The post processor ran.']);
return {origin, clone};
}

it('adds commit when .OwlBot.yaml contains no flag', async () => {
const {origin, clone} = makeRepoWithPendingCommit();
await commitPostProcessorUpdate(prepareArgs(clone));
const log = cmd('git log --format=%B main', {cwd: origin}).toString(
'utf-8'
);
assert.match(log, /Updates from OwlBot/);
});

it('updates pr title and body', async () => {
const {origin, clone} = makeRepoWithPendingCommit();
const args = prepareArgs(clone);
args['new-pull-request-text-path'] = tmp.fileSync().name;
fs.writeFileSync(
args['new-pull-request-text-path'],
'updated title\n\nUpdated body.'
);
// commitPostProcessorUpdate() should issue a PATCH request to update the PR.
nock('https://api.github.com')
SurferJeffAtGoogle marked this conversation as resolved.
Show resolved Hide resolved
.patch(`/repos/${destRepo}/pulls/${pr}`, {
title: 'updated title',
body: 'Updated body.',
})
.reply(204);
await commitPostProcessorUpdate(args);
const log = cmd('git log --format=%B main', {cwd: origin}).toString(
'utf-8'
);
assert.match(log, /Updates from OwlBot/);
});

it('squashes commit when .OwlBot.yaml contains flag', async () => {
const origin = makeOrigin();
const clone = cloneRepo(origin);
Expand Down