Skip to content

Commit

Permalink
Merge pull request #35 from product-os/kyle/remove-probot
Browse files Browse the repository at this point in the history
Switch to ES modules and use octokit directly
  • Loading branch information
flowzone-app[bot] authored Aug 23, 2024
2 parents 5d7e745 + e65ef97 commit c69ceb8
Show file tree
Hide file tree
Showing 3 changed files with 322 additions and 3,262 deletions.
51 changes: 11 additions & 40 deletions index.js → index.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#!/usr/bin/env node
require("dotenv").config();
const { Probot, ProbotOctokit } = require("probot");
const { throttling } = require("@octokit/plugin-throttling");
const yaml = require("yaml");
const fs = require("fs");
const path = require("path");
import dotenv from "dotenv";
dotenv.config();
import { Octokit } from "octokit";
import yaml from "yaml";
import fs from "fs";
import path from "path";

// Load the GitHub Personal Access Token and organization name from environment variables
const token = process.env.GITHUB_TOKEN;
const org = process.env.ORG_NAME;
const specificRepo = process.env.SPECIFIC_REPO; // Optionally set this to run on a single repo
const reposDir = path.join(process.cwd(), ".github", "repos");
const orgSettingsPath = path.join(process.cwd(), ".github", "settings.yml");
const packageSettingsPath = path.join(__dirname, "assets", "settings.yml");

// https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/creating-rulesets-for-a-repository#using-fnmatch-syntax
const protectedBranches = [
Expand All @@ -28,36 +26,10 @@ const protectedBranches = [
}
];

// Apply throttling plugin to Octokit
const ThrottledOctokit = ProbotOctokit.plugin(throttling);

// Initialize a Probot instance with a custom Octokit class
const probot = new Probot({
appId: 12345, // Dummy App ID, not used in PAT authentication
githubToken: token,
Octokit: ThrottledOctokit.defaults({
auth: `token ${token}`,
throttle: {
onRateLimit: (retryAfter, options) => {
console.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
if (options.request.retryCount === 0) {
// only retries once
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onSecondaryRateLimit: (_, options) => {
console.warn(
`Secondary rate limit hit for request ${options.method} ${options.url}`
);
},
},
}),
});
// Initialize Octokit with the GitHub token
const octokit = new Octokit({ auth: token });

async function processRepository(octokit, repoName) {
async function processRepository(repoName) {
let rulesets = [];
let branches = [];

Expand Down Expand Up @@ -256,14 +228,13 @@ async function processRepository(octokit, repoName) {
}

async function fetchRepoSettings() {
const octokit = await probot.auth();
if (!fs.existsSync(reposDir)) {
fs.mkdirSync(reposDir);
}

if (specificRepo) {
// Process a specific repository
await processRepository(octokit, specificRepo);
await processRepository(specificRepo);
} else {
// Process all repositories in the organization
for await (const response of octokit.paginate.iterator(
Expand All @@ -274,7 +245,7 @@ async function fetchRepoSettings() {
}
)) {
for (const repo of response.data) {
await processRepository(octokit, repo.name);
await processRepository(repo.name);
}
}
}
Expand Down
Loading

0 comments on commit c69ceb8

Please sign in to comment.