Skip to content

Commit

Permalink
force PR update (#4)
Browse files Browse the repository at this point in the history
* update Pr number

* helps to checkout myself

* all dispatch to work

* update dist

* need awaits

* add some debug

* test forEach

* see what is in object

* see length

* try some deconstructing

* fix some bad variables

* dont need to map on data anymore

* try to find error

* fix mapping issue add some feedback

* reduce approvals

* move to imports instead of rquires

* fix addcomment

* update label

* output update

* remove workflow_dispatch
  • Loading branch information
j-fulbright authored Jul 21, 2023
1 parent 188f064 commit 911c622
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 71 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"parserOptions": {
"ecmaVersion": "latest"
"ecmaVersion": "latest",
"sourceType": "module"
},
"env": {
"es6": true
"es6": true
}
}
8 changes: 5 additions & 3 deletions .github/workflows/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ jobs:
isApprovedByCommiters: ${{ steps.label-when-approved-by-commiters.outputs.isApproved }}
isApprovedByAnyone: ${{ steps.label-when-approved-by-anyone.outputs.isApproved }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Label when approved by developers
uses: ./
id: label-when-approved-by-developers
Expand All @@ -18,6 +20,6 @@ jobs:
label: "approved"
require_committers_approval: "true"
remove_label_when_approval_missing: "true"
comment: "PR approved by 2 developers, ready to merge"
numOfApprovals: 2
pullRequestNumber: 3
comment: "PR approved by developers, ready to merge"
numOfApprovals: 1
pullRequestNumber: 4
128 changes: 93 additions & 35 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9784,16 +9784,62 @@ module.exports = JSON.parse('[[[0,44],"disallowed_STD3_valid"],[[45,46],"valid"]
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __nccwpck_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => (module['default']) :
/******/ () => (module);
/******/ __nccwpck_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __nccwpck_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __nccwpck_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/compat */
/******/
/******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/";
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
// This entry need to be wrapped in an IIFE because it need to be in strict mode.
(() => {
const core = __nccwpck_require__(2186);
const github = __nccwpck_require__(5438);
"use strict";
__nccwpck_require__.r(__webpack_exports__);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(2186);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(5438);
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__nccwpck_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_1__);



function getRequiredEnv(key) {
const value = process.env[key];
Expand All @@ -9805,12 +9851,12 @@ function getRequiredEnv(key) {
}

function verboseOutput(name, value) {
core.info(`Setting output: ${name}: ${value}`);
core.setOutput(name, value);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`Setting output: ${name}: ${value}`);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setOutput)(name, value);
}

async function setLabel(octokit, owner, repo, pullRequestNumber, label) {
core.info(`Setting label "${label}"`);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`Setting label "${label}"`);
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/labels', {
owner,
repo,
Expand All @@ -9819,8 +9865,18 @@ async function setLabel(octokit, owner, repo, pullRequestNumber, label) {
});
}

async function addComment(octokit, owner, repo, pullRequestNumber, comment) {
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`Adding comment "${comment}"`);
await octokit.request('POST /repos/{owner}/{repo}/issues/{issue_number}/comments', {
owner,
repo,
issue_number: pullRequestNumber,
body: comment
});
}

async function removeLabel(octokit, owner, repo, pullRequestNumber, label) {
core.info(`Removing label "${label}"`);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`Removing label "${label}"`);
await octokit.request('DELETE /repos/{owner}/{repo}/issues/{issue_number}/labels/{name}', {
owner,
repo,
Expand All @@ -9845,9 +9901,9 @@ async function processReviews(reviews, committers, requireCommittersApproval, nu
}
}

core.info('Reviews:');
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)('Reviews:');
for (const user in reviewStates) {
core.info(`\t${user}: ${reviewStates[user].toLowerCase()}`);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`\t${user}: ${reviewStates[user].toLowerCase()}`);
}

for (const user in reviewStates) {
Expand All @@ -9873,17 +9929,17 @@ async function processReviews(reviews, committers, requireCommittersApproval, nu
}

async function getReviews(octokit, owner, repo, pullRequestNumber, requireCommittersApproval) {
const reviews = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
const { data: reviews } = await octokit.request('GET /repos/{owner}/{repo}/pulls/{pull_number}/reviews', {
owner,
repo,
pull_number: pullRequestNumber
});
const reviewers = reviews ? reviews.map((review) => review.user.login) : [];
const reviewers = reviews && reviews.length > 0 ? reviews.map((review) => review.user.login) : [];
const reviewersAlreadyChecked = [];
const committers = [];

if (requireCommittersApproval) {
core.info('Checking reviewers permissions');
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`\nChecking reviewers permissions`);
for (const reviewer of reviewers) {
if (!reviewersAlreadyChecked.includes(reviewer)) {
const r = await octokit.request('GET /repos/{owner}/{repo}/collaborators/{username}/permission', {
Expand All @@ -9895,26 +9951,26 @@ async function getReviews(octokit, owner, repo, pullRequestNumber, requireCommit
if (r.data.permission === 'admin' || r.data.permission === 'write') {
committers.push(reviewer);
}
core.info(`\t${reviewer}: ${r.data.permission}`);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(`\t${reviewer}: ${r.data.permission}`);
reviewersAlreadyChecked.push(reviewer);
}
}
}

return { reviews, reviewers, committers };
return { reviews, committers };
}

async function run() {
const token = core.getInput('token', { required: true });
const userLabel = core.getInput('label') || 'not set';
const requireCommittersApproval = core.getInput('require_committers_approval') === 'true';
const removeLabelWhenApprovalMissing = core.getInput('remove_label_when_approval_missing') === 'true';
const comment = core.getInput('comment') || '';
const pullRequestNumberInput = core.getInput('pullRequestNumber') || 'not set';
const numOfApprovals = parseInt(core.getInput('numOfApprovals') || 1);

const octokit = github.getOctokit(token);
const context = github.context;
const token = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('token', { required: true });
const userLabel = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('label') || 'not set';
const requireCommittersApproval = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('require_committers_approval') === 'true';
const removeLabelWhenApprovalMissing = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('remove_label_when_approval_missing') === 'true';
const comment = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('comment') || '';
const pullRequestNumberInput = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('pullRequestNumber') || 'not set';
const numOfApprovals = parseInt((0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)('numOfApprovals') || 1);

const octokit = (0,_actions_github__WEBPACK_IMPORTED_MODULE_1__.getOctokit)(token);
const context = _actions_github__WEBPACK_IMPORTED_MODULE_1__.context;
const repository = getRequiredEnv('GITHUB_REPOSITORY');
const eventName = getRequiredEnv('GITHUB_EVENT_NAME');
const [owner, repo] = repository.split('/');
Expand All @@ -9923,13 +9979,13 @@ async function run() {

//
try {
core.info(
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)(
`\n############### Set Label When Approved Begin ##################\n` +
`label: "${userLabel}"\n` +
`requireCommittersApproval: ${requireCommittersApproval}\n` +
`comment: ${comment}\n` +
`pullRequestNumber: ${pullRequestNumberInput}` +
`numOfApprovals: ${numOfApprovals}`
`pullRequestNumber: ${pullRequestNumberInput}\n` +
`numOfApprovals: ${numOfApprovals}\n`
);

// Workflow event setup
Expand All @@ -9940,7 +9996,7 @@ async function run() {
}
} else if (eventName === 'workflow_run') {
if (pullRequestNumberInput === 'not set') {
core.warning(
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.warning)(
`If action is triggered by "workflow_run" then input "pullRequestNumber" is required.\n` +
`It might be missing because the pull request might have been already merged or a fixup pushed to` +
`the PR branch. None of the outputs will be set as we cannot find the right PR.`
Expand All @@ -9963,10 +10019,12 @@ async function run() {
});

// Get the labels
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)('Grabbing labels');
const labelNames = pullRequest.labels.map((label) => label.name);

// Get the reviews
const { reviews, reviewers, committers } = getReviews(
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)('Grabbing reviews');
const { reviews, committers } = await getReviews(
octokit,
owner,
repo,
Expand All @@ -9975,7 +10033,7 @@ async function run() {
);

// Check if the PR is approved
const isApproved = processReviews(reviews, committers, requireCommittersApproval, numOfApprovals);
const isApproved = await processReviews(reviews, committers, requireCommittersApproval, numOfApprovals);

// Add or remove the label
let shouldLabelBeSet = false;
Expand All @@ -9996,16 +10054,16 @@ async function run() {

// Set outputs
verboseOutput('isApproved', String(isApproved));
verboseOutput('labelSet', String(isLabelShouldBeSet));
verboseOutput('labelRemoved', String(isLabelShouldBeRemoved));
verboseOutput('shouldLabelBeSet', String(shouldLabelBeSet));
verboseOutput('shouldLabelBeRemoved', String(shouldLabelBeRemoved));
} catch (error) {
core.setFailed(error.message);
(0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed)(error.message);
}
}

run()
.then(() => core.info('\n############### Set Label When Approved End ##################\n'))
.catch((e) => core.setFailed(e.message));
.then(() => (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.info)('\n############### Set Label When Approved End ##################\n'))
.catch((e) => (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed)(e.message));

})();

Expand Down
Loading

0 comments on commit 911c622

Please sign in to comment.