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(trusted): add optional comment configuration #1682

Merged
merged 3 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions packages/trusted-contribution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ options:
| Name | Description | Type | Default |
|----- | ----------- | ---- | ------- |
| `trustedContributors` | List of user login names that are considered trusted | `string[]` | `['renovate-bot', 'release-please[bot]', 'gcf-merge-on-green[bot]']` |
| `annotations` | The list of annotation objects to leave the on the PR | `object` | `{ type: 'label'; text: 'kokoro:force-run' }` |
| `annotation.type` | Configure the bot to either comment on the PR or add a label | `comment`|`label` | `label` |
| `annotation.text` | The label text or comment text to be left on the PR | `string` | `kokoro:force-run`

## Deployment and Permissions

Expand Down
147 changes: 147 additions & 0 deletions packages/trusted-contribution/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/trusted-contribution/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@
"devDependencies": {
"@types/mocha": "^8.0.0",
"@types/node": "^14.0.22",
"@types/sinon": "^10.0.0",
"c8": "^7.2.0",
"cross-env": "^7.0.2",
"gts": "^3.0.0",
"mocha": "^8.0.1",
"nock": "^13.0.2",
"sinon": "^10.0.0",
"typescript": "^4.0.0"
},
"engines": {
Expand Down
35 changes: 30 additions & 5 deletions packages/trusted-contribution/src/trusted-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
import {Probot} from 'probot';
import {logger} from 'gcf-utils';

interface Annotation {
type: 'comment' | 'label';
text: string;
}

interface ConfigurationOptions {
trustedContributors?: string[];
annotations?: Annotation[];
}

const WELL_KNOWN_CONFIGURATION_FILE = 'trusted-contribution.yml';
Expand All @@ -29,6 +35,12 @@ const DEFAULT_TRUSTED_CONTRIBUTORS = [
'yoshi-code-bot',
'gcf-owl-bot[bot]',
];
const DEFAULT_ANNOTATIONS: Annotation[] = [
{
type: 'label',
text: 'kokoro:force-run',
},
];

function isTrustedContribution(
config: ConfigurationOptions,
Expand Down Expand Up @@ -66,11 +78,24 @@ export = (app: Probot) => {
remoteConfiguration = remoteConfiguration! || {};
// TODO: add additional verification that only dependency version changes occurred.
if (isTrustedContribution(remoteConfiguration, PR_AUTHOR)) {
const issuesAddLabelsParams = context.repo({
issue_number: context.payload.pull_request.number,
labels: ['kokoro:force-run'],
});
await context.octokit.issues.addLabels(issuesAddLabelsParams);
const annotations =
remoteConfiguration.annotations || DEFAULT_ANNOTATIONS;
for (const annotation of annotations) {
if (annotation.type === 'label') {
const issuesAddLabelsParams = context.repo({
issue_number: context.payload.pull_request.number,
labels: [annotation.text],
});
await context.octokit.issues.addLabels(issuesAddLabelsParams);
} else if (annotation.type === 'comment') {
await context.octokit.issues.createComment({
issue_number: context.payload.pull_request.number,
body: annotation.text,
owner: context.repo().owner,
repo: context.repo().repo,
});
}
}
}
}
);
Expand Down
3 changes: 3 additions & 0 deletions packages/trusted-contribution/test/fixtures/gcbrun.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
annotations:
- type: comment
text: "/gcbrun"
80 changes: 74 additions & 6 deletions packages/trusted-contribution/test/trusted-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {describe, it, beforeEach} from 'mocha';
import {describe, it, afterEach, beforeEach} from 'mocha';
import assert from 'assert';
import {resolve} from 'path';
// eslint-disable-next-line node/no-extraneous-import
import {Probot, createProbot, ProbotOctokit} from 'probot';
import nock from 'nock';
import sinon from 'sinon';
import * as fs from 'fs';
import {logger} from 'gcf-utils';

import myProbotApp from '../src/trusted-contribution';

nock.disableNetConnect();

const fixturesPath = resolve(__dirname, '../../test/fixtures');

// TODO: stop disabling warn once the following upstream patch is landed:
// https://github.com/probot/probot/pull/926
global.console.warn = () => {};

describe('TrustedContributionTestRunner', () => {
let probot: Probot;
let requests: nock.Scope;
Expand All @@ -47,6 +46,11 @@ describe('TrustedContributionTestRunner', () => {
requests = nock('https://api.github.com');
});

afterEach(() => {
nock.cleanAll();
sinon.restore();
});

describe('without configuration file', () => {
beforeEach(() => {
requests = requests
Expand All @@ -55,7 +59,6 @@ describe('TrustedContributionTestRunner', () => {
)
.reply(404)
.get(
// FIXME(#68): why is this necessary?
'/repos/chingor13/.github/contents/.github%2Ftrusted-contribution.yml'
)
.reply(404);
Expand Down Expand Up @@ -524,4 +527,69 @@ describe('TrustedContributionTestRunner', () => {
});
});
});

it('should add a comment if configured with annotations', async () => {
requests
.get(
'/repos/chingor13/google-auth-library-java/contents/.github%2Ftrusted-contribution.yml'
)
.replyWithFile(200, './test/fixtures/gcbrun.yml')
.post(
'/repos/chingor13/google-auth-library-java/issues/3/comments',
() => true
)
.reply(200);

await probot.receive({
name: 'pull_request',
payload: {
action: 'opened',
pull_request: {
number: 3,
user: {
login: 'renovate-bot',
},
},
repository: {
name: 'google-auth-library-java',
owner: {
login: 'chingor13',
},
},
},
id: 'abc123',
});
requests.done();
});

it('should log an error if the config cannot be fetched', async () => {
requests = requests
.get(
'/repos/chingor13/google-auth-library-java/contents/.github%2Ftrusted-contribution.yml'
)
.reply(500);
const errorStub = sinon.stub(logger, 'error');

await probot.receive({
name: 'pull_request',
payload: {
action: 'opened',
pull_request: {
number: 3,
user: {
login: 'not-real',
},
},
repository: {
name: 'google-auth-library-java',
owner: {
login: 'chingor13',
},
},
},
id: 'abc123',
});
assert.ok(errorStub.calledOnce);
requests.done();
});
});