Skip to content

Commit

Permalink
Build dist/index.js for #2 (#3)
Browse files Browse the repository at this point in the history
* Feature/add options (#1)

* add 'add' action and ghBaseUrl

* use env for base url

* update nodejs ver 12 to 16

* update README.md
  • Loading branch information
afrocandy authored May 15, 2023
1 parent 02e83a2 commit 8857267
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ This action will set / delete Branch Protection rules on specified branches of G
```
- name: Run Branch Protection
uses: venh/branch-protection@v1.0.0
uses: venh/branch-protection@v1.1.0
with:
token: '${{ secrets.GITHUB_ADMIN_ACCESS_TOKEN }}'
org: Name-Of-Your-GitHub-Organization
Expand Down
65 changes: 31 additions & 34 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3100,7 +3100,7 @@ var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const core = __nccwpck_require__(186);
const { request } = __nccwpck_require__(234);
const { request: orgRequest } = __nccwpck_require__(234);
const fs = __nccwpck_require__(747)

var excludedReposPath = '';
Expand All @@ -3113,29 +3113,43 @@ async function run() {
excludedReposPath = core.getInput("excludedReposPath");
includedReposPath = core.getInput("includedReposPath");
const action = core.getInput("action");
const limit = 100;
const canDeleteProtection = action == 'set' || action == 'delete';
const canSetProtection = action == 'set' || action == 'add';

var rulesObj;
var branches;
try {
if(!fs.existsSync(rulesPath)){
throw "Rules JSON is missing."
}

const request = orgRequest.defaults({
baseUrl: process.env.GITHUB_API_URL || 'https://api.github.com',
headers: {
authorization: "token " + token,
},
})
const rules = fs.readFileSync(rulesPath, {encoding:'utf8', flag:'r'});
rulesObj = JSON.parse(rules);
keys = Object.keys(rulesObj);
var repos = await getFinalRepos(token, orgName);
var repos = await getFinalRepos(request, orgName);
for (let i = 0; i < repos.length; i++) {
branches = await getBranches(token, repos[i], keys);
branches = await getBranches(request, repos[i], keys);
for (let j = 0; j < branches.length; j++) {
if(branches[j].protected){
if (!canDeleteProtection) {
console.log("Skip Branch Protection for " + branches[j].name + " branch of " + repos[i]);
continue;
}

console.log("Deleting Branch Protection for " + branches[j].name + " branch of " + repos[i]);
core.debug("Deleting Branch Protection for " + branches[j].name + " branch of " + repos[i]);
await deleteProtection(token, repos[i], branches[j].name);
await deleteProtection(request, repos[i], branches[j].name);
}
if(action == "set"){
if(canSetProtection){
console.log("Setting Branch Protection for " + branches[j].name + " branch of " + repos[i]);
core.debug("Setting Branch Protection for " + branches[j].name + " branch of " + repos[i]);
await setProtection(token, repos[i], branches[j].name, rulesObj[branches[j].name] )
await setProtection(request, repos[i], branches[j].name, rulesObj[branches[j].name] )
}
}
}
Expand All @@ -3146,7 +3160,7 @@ async function run() {
}
}

async function setProtection(token, repoName, branchName, ruleData){
async function setProtection(request, repoName, branchName, ruleData){
const url = "/repos/" + repoName + "/branches/" + branchName + "/protection"
if(ruleData == ""){
ruleData = {
Expand All @@ -3159,9 +3173,6 @@ async function setProtection(token, repoName, branchName, ruleData){
}
try {
const result = await request("PUT " + url, {
headers: {
authorization: "token " + token,
},
data: ruleData
});
//console.log(result.data);
Expand All @@ -3173,14 +3184,10 @@ async function setProtection(token, repoName, branchName, ruleData){
}
}

async function deleteProtection(token, repoName, branchName){
async function deleteProtection(request, repoName, branchName){
const url = "/repos/" + repoName + "/branches/" + branchName + "/protection"
try{
const result = await request("DELETE " + url, {
headers: {
authorization: "token " + token,
}
});
const result = await request("DELETE " + url);
if(result.status != 204){
throw "Exception occured during Delete Protection";
}
Expand All @@ -3191,15 +3198,11 @@ async function deleteProtection(token, repoName, branchName){
}
}

async function getBranches(token, repoName, branchNames){
async function getBranches(request, repoName, branchNames){
branchInfoArr = [];
const url = "/repos/" + repoName + "/branches"
try {
const result = await request("GET " + url, {
headers: {
authorization: "token " + token,
}
});
const result = await request("GET " + url);
branchData = result.data;
for (let j = 0; j < branchData.length; j++) {
const element = branchData[j];
Expand All @@ -3215,13 +3218,10 @@ async function getBranches(token, repoName, branchNames){
return branchInfoArr;
}

async function getRepoCount(token, orgName){
async function getRepoCount(request, orgName){
repoCnt = 0;
try {
const result = await request("GET /orgs/{org}/repos", {
headers: {
authorization: "token " + token,
},
org: orgName,
per_page:1,
type: "all"
Expand All @@ -3241,13 +3241,10 @@ function getPageCount(itemCount, limit){
return pageCount;
}

async function getPagedRepos(token, orgName, pageNum, limit){
async function getPagedRepos(request, orgName, pageNum, limit){
var repos = [];
try {
const result = await request("GET /orgs/{org}/repos", {
headers: {
authorization: "token " + token,
},
org: orgName,
per_page:limit,
type: "all",
Expand All @@ -3264,7 +3261,7 @@ async function getPagedRepos(token, orgName, pageNum, limit){
return repos;
}

async function getFinalRepos(token, orgName){
async function getFinalRepos(request, orgName){
repos = [];
includedRepos = [];
limit = 100;
Expand All @@ -3276,12 +3273,12 @@ async function getFinalRepos(token, orgName){
}
return includedRepos;
}
repoCount = await getRepoCount(token, orgName);
repoCount = await getRepoCount(request, orgName);
pageCnt = getPageCount(repoCount, limit);
excludedRepos = getReposFromFile(excludedReposPath);
for (let i = 0; i < pageCnt; i++) {
i = i + 1;
pagedRepos = await getPagedRepos(token, orgName, i, limit);
pagedRepos = await getPagedRepos(request, orgName, i, limit);
for (let j = 0; j < pagedRepos.length; j++) {
repoShortName = pagedRepos[j].replace(orgName + "/","");
if(!excludedRepos.includes(repoShortName)) {
Expand Down

0 comments on commit 8857267

Please sign in to comment.