Skip to content

Commit

Permalink
Fix #214: Introduce Github action to perform CLA Check (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
gp201 authored Apr 9, 2021
1 parent e053814 commit 3743e0e
Show file tree
Hide file tree
Showing 8 changed files with 343,602 additions and 23,821 deletions.
4 changes: 4 additions & 0 deletions actions/src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
const core = require('@actions/core');
const { context } = require('@actions/github');
const issueLabelsModule = require('./issues/checkIssueLabels');
const claCheckGithubActionModule = require('./pull_requests/claCheck');
const constants = require('../../constants');

module.exports = {
Expand All @@ -36,6 +37,9 @@ module.exports = {
case constants.issuesLabelCheck:
await issueLabelsModule.checkLabels();
break;
case constants.claCheckGithubAction:
await claCheckGithubActionModule.claCheckGithubAction();
break;
}
}
}
Expand Down
120 changes: 120 additions & 0 deletions actions/src/pull_requests/claCheck.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// 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 check if PR Author has signed the CLA
*/

const core = require('@actions/core');
const { context, GitHub } = require('@actions/github');
const { google } = require('googleapis');

/**
* Create an OAuth2 client with the given credentials, and then execute the
* given claCheck function.
*/
const authorize = async function() {
try {
const CREDENTIALS = JSON.parse(process.env.SHEETS_CRED);
const SHEETS_TOKEN = JSON.parse(process.env.SHEETS_TOKEN);
// eslint-disable-next-line camelcase
const { client_secret, client_id, redirect_uris } = CREDENTIALS.installed;
var oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
oAuth2Client.credentials = SHEETS_TOKEN;
return oAuth2Client;
} catch (err) {
core.setFailed('Auth failure: ' + err);
}
};

const generateOutput = async (hasClaSigned) => {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const LINK_RESULT = 'here'.link(
'https://github.com/oppia/oppia/wiki/' +
'Contributing-code-to-Oppia#setting-things-up'
);
const octokit = new GitHub(GITHUB_TOKEN);
const PR_NUMBER = context.payload.pull_request.number;
const PR_AUTHOR = context.payload.pull_request.user.login;

let comment = '';
if (!hasClaSigned) {
comment = ('Hi! @' +
PR_AUTHOR +
' Welcome to Oppia! Could you please ' +
'follow the instructions ' + LINK_RESULT +
" and sign the CLA Sheet to get started? You'll need to do " +
'this before we can accept your PR. Thanks!');
core.info('Commenting in PR...');

await octokit.issues.createComment(
{
body: comment,
issue_number: PR_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
}
);
core.setFailed(PR_AUTHOR + ' has not signed the CLA');
} else {
core.info(`${PR_AUTHOR} has signed the CLA`);
}
};


/**
* Checks if the PR Author has signed the CLA Sheet.
* @param {google.auth.OAuth2} auth The authenticated Google OAuth client.
*/
const checkSheet = async (auth) => {
const PR_AUTHOR = context.payload.pull_request.user.login;
const sheets = google.sheets({ version: 'v4', auth });
const SPREADSHEET_ID = process.env.SPREADSHEET_ID;
await sheets.spreadsheets.values.get(
{
spreadsheetId: SPREADSHEET_ID,
range: 'Usernames!A:A',
},
async (err, res) => {
if (err) {
core.setFailed('The API returned an error: ' + err);
}
const rows = res.data.values;
const flatRows = [].concat.apply([], rows);
if (!rows || rows.length === 0) {
core.setFailed('No data found.');
} else {
core.info(`Checking if ${PR_AUTHOR} has signed the CLA`);
const hasUserSignedCla = flatRows.includes(PR_AUTHOR);
await generateOutput(hasUserSignedCla);
}
}
);
};

const claCheckGithubAction = async () => {
// Authorize a client with the loaded credentials.
const auth = await authorize();

// Call the sheets API with the authorized client.
await checkSheet(auth);
};

module.exports = {
claCheckGithubAction,
};
Loading

0 comments on commit 3743e0e

Please sign in to comment.