Skip to content

Commit

Permalink
feat: protect labels
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Apr 3, 2019
1 parent 7ff2078 commit a0b24e1
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions dist/context/repoContext.d.ts
Expand Up @@ -3,6 +3,7 @@ import { LabelResponse, Labels } from './initRepoLabels';
import { TeamContext } from './teamContext';
interface RepoContextWithoutTeamContext<GroupNames extends string> {
labels: Labels;
protectedLabelIds: LabelResponse['id'][];
hasNeedsReview: (labels: LabelResponse[]) => boolean;
hasRequestedReview: (labels: LabelResponse[]) => boolean;
hasChangesRequestedReview: (labels: LabelResponse[]) => boolean;
Expand Down
2 changes: 1 addition & 1 deletion dist/context/repoContext.d.ts.map

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

22 changes: 19 additions & 3 deletions dist/index-node10-dev.cjs.js

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

2 changes: 1 addition & 1 deletion dist/index-node10-dev.cjs.js.map

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions dist/index-node10.cjs.js

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

2 changes: 1 addition & 1 deletion dist/index-node10.cjs.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pr-handlers/checkrunCompleted.d.ts.map

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

2 changes: 1 addition & 1 deletion dist/pr-handlers/labelsChanged.d.ts.map

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

2 changes: 1 addition & 1 deletion src/context/initRepoLabels.ts
Expand Up @@ -57,7 +57,7 @@ export const initRepoLabels = async <GroupNames extends string>(
} else if (
existingLabel.name !== labelConfig.name ||
existingLabel.color !== labelColor // ||
// TODO: description is always undefined
// TODO: description is never updated
// existingLabel.description !== description
) {
context.log.info('Needs to update label', {
Expand Down
6 changes: 6 additions & 0 deletions src/context/repoContext.ts
Expand Up @@ -8,6 +8,7 @@ import { obtainTeamContext, TeamContext } from './teamContext';

interface RepoContextWithoutTeamContext<GroupNames extends string> {
labels: Labels;
protectedLabelIds: LabelResponse['id'][];

hasNeedsReview: (labels: LabelResponse[]) => boolean;
hasRequestedReview: (labels: LabelResponse[]) => boolean;
Expand Down Expand Up @@ -89,6 +90,11 @@ async function initRepoContext<GroupNames extends string>(

return Object.assign(repoContext, {
labels,
protectedLabelIds: [
...requestedReviewLabelIds,
...changesRequestedLabelIds,
...approvedReviewLabelIds,
],
hasNeedsReview,
hasRequestedReview,
hasChangesRequestedReview,
Expand Down
4 changes: 0 additions & 4 deletions src/pr-handlers/checkrunCompleted.ts
Expand Up @@ -8,10 +8,6 @@ export default (app: Application) => {
createHandlerPullRequestsChange(
(context) => context.payload.check_run.pull_requests,
async (context, repoContext) => {
console.log(
'check_run.completed',
context.payload.check_run.pull_requests,
);
await Promise.all(
context.payload.check_run.pull_requests.map((pr) =>
context.github.pulls
Expand Down
16 changes: 15 additions & 1 deletion src/pr-handlers/labelsChanged.ts
Expand Up @@ -11,11 +11,25 @@ export default (app: Application) => {
if (sender.type === 'Bot') return;

await handlerPullRequestChange(context, async (repoContext) => {
const label = context.payload.label;
if (repoContext.protectedLabelIds.includes(label.id)) {
if (context.payload.action === 'labeled') {
await context.github.issues.removeLabel(
context.issue({ name: label.name }),
);
} else {
await context.github.issues.addLabels(
context.issue({ labels: [label.name] }),
);
}
return;
}

await updateStatusCheckFromLabels(context, repoContext);

if (
context.payload.action === 'labeled' &&
context.payload.label.id ===
label.id ===
(repoContext.labels['merge/automerge'] &&
repoContext.labels['merge/automerge'].id)
) {
Expand Down

0 comments on commit a0b24e1

Please sign in to comment.