Skip to content

Commit

Permalink
Fix #205: Fails CI when a don't merge label get added on a pull reque…
Browse files Browse the repository at this point in the history
…st. (#264)
  • Loading branch information
jameesjohn authored May 20, 2021
1 parent 2d02f32 commit 65f1717
Show file tree
Hide file tree
Showing 7 changed files with 119,681 additions and 121,307 deletions.
7 changes: 7 additions & 0 deletions actions/src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const { context } = require('@actions/github');
const issueLabelsModule = require('./issues/checkIssueLabels');
const claCheckGithubActionModule = require('./pull_requests/claCheck');
const constants = require('../../constants');
const PRLabelsModule = require('./pull_requests/labelCheck');

module.exports = {
async dispatch(event, action) {
Expand All @@ -42,6 +43,12 @@ module.exports = {
core.info('cla check triggered');
await claCheckGithubActionModule.claCheckGithubAction();
break;
case constants.prLabelCheck:
await PRLabelsModule.checkLabels();
break;
case constants.dontMergeLabelCheck:
await PRLabelsModule.checkUnLabeled();
break;
}
}
}
Expand Down
84 changes: 84 additions & 0 deletions actions/src/pull_requests/labelCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2021 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
* @fileoverview File to handle checks when a pull request is labeled.
*/

const core = require('@actions/core');
const { context, GitHub } = require('@actions/github');
const DONT_MERGE_LABEL_PREFIX = "PR: don't merge";

const checkLabels = async () => {
core.info('Checking newly added label...');
const token = core.getInput('repo-token');
const label = context.payload.label;
const octokit = new GitHub(token);

if (label.name.startsWith(DONT_MERGE_LABEL_PREFIX)) {
await handleDontMergeLabel(octokit, label.name);
}
};

/**
* Handles cases when a good first issue gets added by a non whitelisted user.
*
* @param {import('@actions/github').GitHub} octokit
*/
const handleDontMergeLabel = async (octokit, label) => {
core.setFailed(
'This PR should not be merged because it has a ' + label + ' label.'
);
};

/**
* Handles cases when a good first issue gets added by a non whitelisted user.
*
* @param {import('@actions/github').GitHub} octokit
*/
const handleDontMergeLabelRemoved = async(octokit) => {
const {data: pullRequest} = await octokit.pulls.get({
pull_number: context.payload.pull_request.number,
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
});

const labelNames = pullRequest.labels.map(label => label.name);

const dontMergeLabel = labelNames.find(
label => label.startsWith(DONT_MERGE_LABEL_PREFIX)
);

if (dontMergeLabel) {
await handleDontMergeLabel(octokit, dontMergeLabel);
} else {
core.info("This PR does not contain a PR don't merge label");
}
};

const checkUnLabeled = async () => {
core.info('Checking newly removed label...');
const token = core.getInput('repo-token');
const label = context.payload.label;
const octokit = new GitHub(token);

if (label.name.startsWith(DONT_MERGE_LABEL_PREFIX)) {
await handleDontMergeLabelRemoved(octokit, label.name);
}
};

module.exports = {
checkLabels,
checkUnLabeled,
};
Loading

0 comments on commit 65f1717

Please sign in to comment.