Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a separate branch instead of gbp-balances.toml, create separate files #27

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8d0571e
Add configuration field
Mothblocks Mar 18, 2021
c97ef26
Use an empty branch instead of master
Mothblocks Mar 18, 2021
fea2c86
Remove commented out code
Mothblocks Mar 18, 2021
d79385e
Update gbp.yml
Mothblocks Mar 18, 2021
0e7b7ff
Stupid way of solving this
Mothblocks Mar 18, 2021
ea986cf
Update dist
Mothblocks Mar 18, 2021
d601e10
Update node build
Mothblocks Mar 18, 2021
3a3da8a
Temporary isMaintainer block
Mothblocks Mar 18, 2021
8d70def
Change block to not be detected as unreachable code
Mothblocks Mar 18, 2021
ec22df7
Update dist
Mothblocks Mar 18, 2021
0752fb9
Update dist
Mothblocks Mar 18, 2021
c4b30cc
Remove branch creation
Mothblocks Mar 18, 2021
a464eef
Update dist
Mothblocks Mar 18, 2021
247e58c
Remove branch creation, it doesn't work
Mothblocks Mar 18, 2021
0db2163
Remove unused argument
Mothblocks Mar 18, 2021
47d078c
Update dist
Mothblocks Mar 18, 2021
68c98aa
Base64
Mothblocks Mar 18, 2021
b97a279
Update dist
Mothblocks Mar 18, 2021
255b92b
Other way around
Mothblocks Mar 18, 2021
0240eb3
Update dist
Mothblocks Mar 18, 2021
b87a446
sha
Mothblocks Mar 18, 2021
7ad2ea9
Update dist
Mothblocks Mar 18, 2021
27a56eb
sha, again
Mothblocks Mar 18, 2021
78c2a6b
Update dist
Mothblocks Mar 18, 2021
d4f542d
Use branch
Mothblocks Mar 18, 2021
c8b44fa
Update dist
Mothblocks Mar 18, 2021
0aac8f7
Separate errors
Mothblocks Mar 18, 2021
b987e82
Update dist
Mothblocks Mar 18, 2021
2d21ddb
Fix error
Mothblocks Mar 18, 2021
c9419b3
Update dist
Mothblocks Mar 18, 2021
668565d
B64 -> ASCII
Mothblocks Mar 18, 2021
50ef2cb
Update dist
Mothblocks Mar 18, 2021
0dac3a3
Remove isMaintainer patch
Mothblocks Mar 18, 2021
9f10a6b
Update dist
Mothblocks Mar 18, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/gbp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Pull GBP branch
run: |
git fetch origin gbp-balances || true

# Make sure it's loaded
git checkout gbp-balances || true

# Go back to master
git checkout master
- name: GBP action
uses: ./
with:
Expand Down
162 changes: 68 additions & 94 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13149,11 +13149,10 @@ Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.merged = void 0;
const core = __importStar(__nccwpck_require__(3923));
const github = __importStar(__nccwpck_require__(5873));
const toml = __importStar(__nccwpck_require__(8830));
const isMaintainer_1 = __nccwpck_require__(7470);
const points = __importStar(__nccwpck_require__(5474));
function merged(configuration) {
var _a, _b, _c, _d, _e, _f;
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
return __awaiter(this, void 0, void 0, function* () {
const pullRequest = github.context.payload.pull_request;
if (pullRequest === undefined) {
Expand All @@ -13166,8 +13165,8 @@ function merged(configuration) {
const labels = pullRequest.labels;
const labelNames = labels.map((label) => label.name);
const user = pullRequest.user;
const balanceSheet = yield points.readBalanceFile();
const oldBalance = (balanceSheet && points.readBalances(balanceSheet)[user.id]) || 0;
const octokit = github.getOctokit(core.getInput("token"));
const oldBalance = (yield points.readBalanceOf(octokit, (_b = (_a = github.context.payload.repository) === null || _a === void 0 ? void 0 : _a.owner) === null || _b === void 0 ? void 0 : _b.login, (_c = github.context.payload.repository) === null || _c === void 0 ? void 0 : _c.name, configuration.branch, user.id)) || 0;
let balance;
let pointsReceived = 0;
if (configuration.reset_label !== undefined &&
Expand All @@ -13181,30 +13180,7 @@ function merged(configuration) {
return;
}
}
const newOutput = points.setBalance(balanceSheet, user, balance);
try {
toml.parse(newOutput);
}
catch (_g) {
return Promise.reject(`setBalance resulted in invalid output: ${newOutput}`);
}
const octokit = github.getOctokit(core.getInput("token"));
const fileContentsParams = {
owner: (_b = (_a = github.context.payload.repository) === null || _a === void 0 ? void 0 : _a.owner) === null || _b === void 0 ? void 0 : _b.login,
repo: (_c = github.context.payload.repository) === null || _c === void 0 ? void 0 : _c.name,
path: ".github/gbp-balances.toml",
};
const sha = yield octokit.repos
.getContent(fileContentsParams)
.then((contents) => {
const data = contents.data;
return Array.isArray(data) ? undefined : data.sha;
})
.catch(() => {
// Most likely 404
return undefined;
});
yield octokit.repos.createOrUpdateFileContents(Object.assign(Object.assign({}, fileContentsParams), { message: `Updating GBP from PR #${pullRequest.number} [ci skip]`, content: Buffer.from(newOutput, "binary").toString("base64"), sha }));
yield points.writeBalanceOf(octokit, configuration.branch, (_e = (_d = github.context.payload.repository) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.login, (_f = github.context.payload.repository) === null || _f === void 0 ? void 0 : _f.name, `Updating GBP from PR #${pullRequest.number} [ci skip]`, user.id, balance);
if (yield isMaintainer_1.isMaintainer(octokit, configuration.maintainer_team_slug, github.context.payload, user)) {
core.info("Author is maintainer");
return;
Expand All @@ -13224,8 +13200,8 @@ function merged(configuration) {
}
if (comment !== undefined) {
yield octokit.issues.createComment({
owner: (_e = (_d = github.context.payload.repository) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.login,
repo: (_f = github.context.payload.repository) === null || _f === void 0 ? void 0 : _f.name,
owner: (_h = (_g = github.context.payload.repository) === null || _g === void 0 ? void 0 : _g.owner) === null || _h === void 0 ? void 0 : _h.login,
repo: (_j = github.context.payload.repository) === null || _j === void 0 ? void 0 : _j.name,
issue_number: pullRequest.number,
body: comment,
});
Expand Down Expand Up @@ -13277,7 +13253,7 @@ const github = __importStar(__nccwpck_require__(5873));
const isMaintainer_1 = __nccwpck_require__(7470);
const points = __importStar(__nccwpck_require__(5474));
function opened(configuration) {
var _a, _b, _c;
var _a, _b, _c, _d, _e, _f;
return __awaiter(this, void 0, void 0, function* () {
const pullRequest = github.context.payload.pull_request;
if (pullRequest === undefined) {
Expand All @@ -13289,15 +13265,14 @@ function opened(configuration) {
core.info("Author is maintainer");
return;
}
const balanceSheet = yield points.readBalanceFile();
const userBalance = (balanceSheet && points.readBalances(balanceSheet)[user.id]) || 0;
const userBalance = (yield points.readBalanceOf(octokit, (_b = (_a = github.context.payload.repository) === null || _a === void 0 ? void 0 : _a.owner) === null || _b === void 0 ? void 0 : _b.login, (_c = github.context.payload.repository) === null || _c === void 0 ? void 0 : _c.name, configuration.branch, user.id)) || 0;
const labels = pullRequest.labels;
const labelNames = labels.map((label) => label.name);
const pointsReceived = points.getPointsFromLabels(configuration, labelNames);
if (userBalance < 0 && pointsReceived <= 0) {
yield octokit.issues.createComment({
owner: (_b = (_a = github.context.payload.repository) === null || _a === void 0 ? void 0 : _a.owner) === null || _b === void 0 ? void 0 : _b.login,
repo: (_c = github.context.payload.repository) === null || _c === void 0 ? void 0 : _c.name,
owner: (_e = (_d = github.context.payload.repository) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.login,
repo: (_f = github.context.payload.repository) === null || _f === void 0 ? void 0 : _f.name,
issue_number: pullRequest.number,
body: `You currently have a negative Fix/Feature pull request delta of ${userBalance}. ` +
"Maintainers may close this PR at will. Fixing issues or improving the codebase " +
Expand Down Expand Up @@ -13350,8 +13325,11 @@ const fs_1 = __nccwpck_require__(5747);
const Either_1 = __nccwpck_require__(7945);
const t = __importStar(__nccwpck_require__(4478));
const toml = __importStar(__nccwpck_require__(8830));
const BLACKLISTED_NAMES = new Set(["master", "main"]);
const DEFAULT_BRANCH = "gbp-balances";
const configurationSchema = t.intersection([
t.partial({
branch: t.string,
no_balance_label: t.string,
reset_label: t.string,
}),
Expand All @@ -13363,7 +13341,13 @@ function parseConfig(configurationText) {
const valueEither = configurationSchema.decode(toml.parse(configurationText));
if (Either_1.isRight(valueEither)) {
const value = valueEither.right;
return Object.assign(Object.assign({}, value), { points: new Map(Object.entries(value.points)) });
if (value.branch !== undefined && BLACKLISTED_NAMES.has(value.branch)) {
throw `${value.branch} is a blacklisted name, as it is certainly not what you want. \
The branches gbp-action uses should be used EXCLUSIVELY for gbp-actions. \
The action will wipe the branch COMPLETELY, and use it just to store balances. \
`;
}
return Object.assign(Object.assign({ branch: DEFAULT_BRANCH }, value), { points: new Map(Object.entries(value.points)) });
}
else {
throw valueEither.left;
Expand Down Expand Up @@ -13536,15 +13520,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.setBalance = exports.readBalances = exports.readBalanceFile = exports.getPointsFromLabels = exports.HEADER = void 0;
const fs_1 = __nccwpck_require__(5747);
const Either_1 = __nccwpck_require__(7945);
const t = __importStar(__nccwpck_require__(4478));
const toml = __importStar(__nccwpck_require__(8830));
exports.HEADER = "# This file is @generated by the GBP actions. " +
"If you edit this, preserve the format, and ensure IDs are sorted in numerical order.\n";
/// User IDs -> balances
const validateBalances = t.record(t.string, t.number);
exports.writeBalanceOf = exports.readBalanceOf = exports.getPointsFromLabels = void 0;
const path = __importStar(__nccwpck_require__(5622));
const POINTS_DIRECTORY = "points";
function getPointsFromLabels(configuration, labels) {
let points = 0;
for (const label of labels) {
Expand All @@ -13559,62 +13537,58 @@ function getPointsFromLabels(configuration, labels) {
return points;
}
exports.getPointsFromLabels = getPointsFromLabels;
function getUserId(line) {
const userId = parseInt(line.split(" ")[0], 10);
if (Number.isNaN(userId)) {
return undefined;
}
return userId;
function readBalanceOf(octokit, owner, repo, branch, id) {
return __awaiter(this, void 0, void 0, function* () {
const content = yield octokit.repos
.getContent({
owner,
repo,
path: path.join(POINTS_DIRECTORY, `${id}.txt`),
ref: branch,
})
.catch(() => undefined);
if (content === undefined) {
return 0;
}
const data = content.data;
const points = parseInt(Buffer.from(data.content, "base64").toString("ascii"), 10);
if (Number.isNaN(points)) {
return Promise.reject(`Points is somehow NaN: ${data.content}`);
}
return points;
});
}
function readBalanceFile() {
exports.readBalanceOf = readBalanceOf;
function writeBalanceOf(octokit, branch, owner, repo, message, userId, points) {
return __awaiter(this, void 0, void 0, function* () {
return fs_1.promises
.open("./.github/gbp-balances.toml", "r")
.then((file) => file.readFile({
encoding: "utf-8",
}))
const filePath = path.join(POINTS_DIRECTORY, `${userId}.txt`);
const sha = yield octokit.repos
.getContent({
owner,
repo,
path: filePath,
ref: branch,
})
.then((contents) => {
const data = contents.data;
return Array.isArray(data) ? undefined : data.sha;
})
.catch(() => {
// Most likely 404
return undefined;
});
yield octokit.repos.createOrUpdateFileContents({
owner,
repo,
branch,
message,
content: Buffer.from(points.toString(), "binary").toString("base64"),
path: filePath,
sha,
});
});
}
exports.readBalanceFile = readBalanceFile;
function readBalances(text) {
const balancesEither = validateBalances.decode(toml.parse(text));
if (Either_1.isRight(balancesEither)) {
return balancesEither.right;
}
else {
throw balancesEither.left;
}
}
exports.readBalances = readBalances;
function setBalance(tomlOutput, user, newBalance) {
const balanceLine = `${user.id} = ${newBalance} # ${user.login}`;
if (tomlOutput === undefined) {
return exports.HEADER + balanceLine;
}
const replaceRegex = new RegExp(`${user.id} = .*`, "gm");
const newOutput = tomlOutput.replace(replaceRegex, balanceLine);
if (newOutput !== tomlOutput) {
return newOutput;
}
// Brand new name, find where it is in order
const lines = tomlOutput.split("\n");
for (const [lineNumber, line] of lines.entries()) {
const userId = getUserId(line);
if (userId === undefined) {
continue;
}
if (user.id < userId) {
const linesUpdated = [...lines];
linesUpdated.splice(lineNumber, 0, balanceLine);
return linesUpdated.join("\n");
}
}
return `${tomlOutput}\n${balanceLine}`;
}
exports.setBalance = setBalance;
exports.writeBalanceOf = writeBalanceOf;


/***/ }),
Expand Down
54 changes: 18 additions & 36 deletions src/actions/merged.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ export async function merged(configuration: Configuration) {
const labelNames = labels.map((label) => label.name)
const user: GithubUser = pullRequest.user

const balanceSheet = await points.readBalanceFile()
const octokit = github.getOctokit(core.getInput("token"))

const oldBalance =
(balanceSheet && points.readBalances(balanceSheet)[user.id]) || 0
(await points.readBalanceOf(
octokit,
github.context.payload.repository?.owner?.login!,
github.context.payload.repository?.name!,
configuration.branch,
user.id,
)) || 0

let balance
let pointsReceived = 0
Expand All @@ -45,40 +52,15 @@ export async function merged(configuration: Configuration) {
}
}

const newOutput = points.setBalance(balanceSheet, user, balance)
try {
toml.parse(newOutput)
} catch {
return Promise.reject(
`setBalance resulted in invalid output: ${newOutput}`,
)
}

const octokit = github.getOctokit(core.getInput("token"))

const fileContentsParams = {
owner: github.context.payload.repository?.owner?.login!,
repo: github.context.payload.repository?.name!,
path: ".github/gbp-balances.toml",
}

const sha = await octokit.repos
.getContent(fileContentsParams)
.then((contents) => {
const data = contents.data
return Array.isArray(data) ? undefined : data.sha
})
.catch(() => {
// Most likely 404
return undefined
})

await octokit.repos.createOrUpdateFileContents({
...fileContentsParams,
message: `Updating GBP from PR #${pullRequest.number} [ci skip]`,
content: Buffer.from(newOutput, "binary").toString("base64"),
sha,
})
await points.writeBalanceOf(
octokit,
configuration.branch,
github.context.payload.repository?.owner?.login!,
github.context.payload.repository?.name!,
`Updating GBP from PR #${pullRequest.number} [ci skip]`,
user.id,
balance,
)

if (
await isMaintainer(
Expand Down
9 changes: 7 additions & 2 deletions src/actions/opened.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ export async function opened(configuration: Configuration) {
return
}

const balanceSheet = await points.readBalanceFile()
const userBalance =
(balanceSheet && points.readBalances(balanceSheet)[user.id]) || 0
(await points.readBalanceOf(
octokit,
github.context.payload.repository?.owner?.login!,
github.context.payload.repository?.name!,
configuration.branch,
user.id,
)) || 0

const labels: GithubLabel[] = pullRequest.labels
const labelNames = labels.map((label) => label.name)
Expand Down
13 changes: 13 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { isRight } from "fp-ts/lib/Either"
import * as t from "io-ts"
import * as toml from "toml"

const BLACKLISTED_NAMES = new Set(["master", "main"])
const DEFAULT_BRANCH = "gbp-balances"

export type Configuration = {
maintainer_team_slug?: string
no_balance_label?: string
reset_label?: string
branch: string

points: Map<string, number>
}

const configurationSchema = t.intersection([
t.partial({
branch: t.string,
no_balance_label: t.string,
reset_label: t.string,
}),
Expand All @@ -30,7 +35,15 @@ export function parseConfig(configurationText: string): Configuration {
if (isRight(valueEither)) {
const value = valueEither.right

if (value.branch !== undefined && BLACKLISTED_NAMES.has(value.branch)) {
throw `${value.branch} is a blacklisted name, as it is certainly not what you want. \
The branches gbp-action uses should be used EXCLUSIVELY for gbp-actions. \
The action will wipe the branch COMPLETELY, and use it just to store balances. \
`
}

return {
branch: DEFAULT_BRANCH,
...value,
points: new Map(Object.entries(value.points)),
}
Expand Down
Loading