forked from BrianCArnold/RequireUserApproval
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converted to typescript, need to updates the 'any's.
- Loading branch information
1 parent
9dd806d
commit b205214
Showing
9 changed files
with
446 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
"use strict"; | ||
const LOCAL_FILE_MISSING = 'Local file missing'; | ||
|
||
module.exports = { | ||
LOCAL_FILE_MISSING, | ||
LOCAL_FILE_MISSING, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = __importDefault(require("@actions/core")); | ||
const github_1 = __importDefault(require("@actions/github")); | ||
require("lodash/partition"); | ||
const yaml_1 = __importDefault(require("yaml")); | ||
function fetch_config() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const context = get_context(); | ||
const octokit = get_octokit(); | ||
const config_path = get_config_path(); | ||
const { data: response_body } = yield octokit.repos.getContent({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
path: config_path, | ||
ref: context.ref, | ||
}); | ||
return yaml_1.default.parse(response_body.content); | ||
}); | ||
} | ||
function get_reviews() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const context = get_context(); | ||
const octokit = get_octokit(); | ||
let reviewsResult = yield octokit.pulls.listReviews({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
pull_number: context.payload.pull_request.number | ||
}); | ||
return reviewsResult.data; | ||
}); | ||
} | ||
/* Private */ | ||
let octokit_cache; | ||
let context_cache; | ||
let token_cache; | ||
let config_path_cache; | ||
function get_context() { | ||
return context_cache || (context_cache = github_1.default.context); | ||
} | ||
function get_token() { | ||
return token_cache || (token_cache = core_1.default.getInput('token')); | ||
} | ||
function get_config_path() { | ||
return config_path_cache || (config_path_cache = core_1.default.getInput('config')); | ||
} | ||
function get_octokit() { | ||
const token = get_token(); | ||
return octokit_cache = github_1.default.getOctokit(token); | ||
} | ||
function clear_cache() { | ||
context_cache = undefined; | ||
token_cache = undefined; | ||
config_path_cache = undefined; | ||
octokit_cache = undefined; | ||
} | ||
exports.default = { | ||
fetch_config, | ||
get_reviews, | ||
clear_cache, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const core_1 = __importDefault(require("@actions/core")); | ||
const github_1 = __importDefault(require("./github")); | ||
function run() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
core_1.default.info('Fetching configuration file from input "config"...'); | ||
let config; | ||
try { | ||
config = yield github_1.default.fetch_config(); | ||
} | ||
catch (error) { | ||
if (error.status === 404) { | ||
core_1.default.warning('No configuration file is found in the base branch; terminating the process'); | ||
return; | ||
} | ||
throw error; | ||
} | ||
let reviews = yield github_1.default.get_reviews(); | ||
let requirementCounts = {}; | ||
let requirementMembers = {}; | ||
for (let req in config.groups) { | ||
requirementCounts[req] = config.groups[req].required; | ||
requirementMembers[req] = config.groups[req].members; | ||
} | ||
let processedReviewers = []; | ||
for (let i = 0; i < reviews.length; i++) { | ||
let review = reviews[i]; | ||
let userName = review.user.login; | ||
if (!processedReviewers.includes(userName)) { | ||
processedReviewers.push(userName); | ||
for (let req in requirementMembers) { | ||
if (requirementMembers[req].includes(userName)) { | ||
requirementCounts[req]--; | ||
} | ||
} | ||
} | ||
} | ||
for (let req in requirementCounts) { | ||
if (requirementCounts[req] > 0) { | ||
core_1.default.setFailed('Missing one or more required approvers.'); | ||
} | ||
} | ||
}); | ||
} | ||
module.exports = { | ||
run, | ||
}; | ||
// Run the action if it's not running in an automated testing environment | ||
if (process.env.NODE_ENV !== 'automated-testing') { | ||
run().catch((error) => core_1.default.setFailed(error)); | ||
} |
Oops, something went wrong.