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

🏖💧 remove branch from sizes lookup #39

Merged
merged 1 commit into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
🏖💧 remove branch from sizes lookup
  • Loading branch information
Glitch (size-plugin-bot) committed Oct 18, 2019
commit c620fa3f9fa6d001419606f34f54bfcedcca994e
7 changes: 4 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const MAX_RETRY = 20;
const RETRY_INTERVAL = 30 * 1000;

const SIZE_STORE_ENDPOINT = process.env.SIZE_STORE_ENDPOINT || 'https://size-store.now.sh';
const SIZE_STORE_ENDPOINT =
process.env.SIZE_STORE_ENDPOINT || "https://size-store.now.sh";

const BOT = 'size-plugin[bot]';
const BOT = "size-plugin[bot]";

const STAR_REPO_MESSAGE = `

Expand All @@ -17,5 +18,5 @@ module.exports = {
RETRY_INTERVAL,
SIZE_STORE_ENDPOINT,
STAR_REPO_MESSAGE,
BOT,
BOT
};
44 changes: 23 additions & 21 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/* eslint-disable camelcase */
/* eslint-disable no-console */

const axios = require('axios');
const { toMap, getBotConfig } = require('./utils/utils');
const { SIZE_STORE_ENDPOINT } = require('./config');
const { decorateComment, decorateHeading } = require('./utils/template');
const { fetchWithRetry } = require('./utils/api');
const { isPullRequestOpenedByMe } = require('./utils/github');
const axios = require("axios");
const { toMap, getBotConfig } = require("./utils/utils");
const { SIZE_STORE_ENDPOINT } = require("./config");
const { decorateComment, decorateHeading } = require("./utils/template");
const { fetchWithRetry } = require("./utils/api");
const { isPullRequestOpenedByMe } = require("./utils/github");

const url = `${SIZE_STORE_ENDPOINT}/diff`;

Expand All @@ -19,7 +19,7 @@ async function commentPullRequest(context, message) {
function sizeCommentTemplate(item) {
const {
filename,
diff: { files },
diff: { files }
} = item;
return `
${decorateHeading(filename, files)}
Expand All @@ -42,7 +42,7 @@ function combinedCommentMessageTemplate(items, sha) {
(agg, item) => `${agg}
${sizeCommentTemplate(item)}
`,
'',
""
);

return `
Expand All @@ -58,55 +58,57 @@ async function get(context) {
const {
pull_request: {
number: pull_request_number,
head: { ref: branch, sha },
user,
},
head: { sha },
user
}
} = context.payload;
const {
repository: { full_name },
repository: { full_name }
} = context.payload;
const repo = full_name.toLowerCase();
if (!isPullRequestOpenedByMe(user)) {
const config = await getBotConfig(context);
const sizefilepaths = config['size-files'].map(filename => ({
const sizefilepaths = config["size-files"].map(filename => ({
filename,
commented: false,
commented: false
}));

await fetchWithRetry(() => {
const params = {
repo,
branch,
sha,
pull_request_number,
pull_request_number
};
return axios.get(url, { params }).then(({ data }) => {
const values = Object.values(data);
if (sizefilepaths.length > 1) {
const sizeFileNameMap = toMap(
sizefilepaths.filter(item => !item.commented),
'filename',
"filename"
);
const sizeMap = toMap(values, 'filename');
const sizeMap = toMap(values, "filename");
let counter = 0;
for (const filename of Object.keys(sizeFileNameMap)) {
if (sizeMap[filename]) {
const message = commentMessageTemplate(sizeMap[filename], sha);
commentPullRequest(context, message).then(
console.log,
console.error,
console.error
);
sizeFileNameMap[filename].commented = true;
counter += 1;
}
}
if (counter !== Object.values(sizeFileNameMap).length) {
console.log(Object.values(sizeFileNameMap));
throw Error('waiting for all file sizes');
throw Error("waiting for all file sizes");
}
} else {
const message = combinedCommentMessageTemplate(values, sha);
commentPullRequest(context, message).then(console.log, console.error);
commentPullRequest(context, message).then(
console.log,
console.error
);
}
});
});
Expand Down
16 changes: 10 additions & 6 deletions src/events.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
/* eslint-disable no-console */

const diff = require('./diff');
const size = require('./size');
const diff = require("./diff");
const size = require("./size");

function register(app) {
app.on(['push'], async (context) => {
app.on(["push"], async context => {
setTimeout(size.get, 0, context);
});
app.on(
['pull_request.opened', 'pull_request.synchronize', 'pull_request.reopened'],
async (context) => {
[
"pull_request.opened",
"pull_request.synchronize",
"pull_request.reopened"
],
async context => {
setTimeout(diff.get, 0, context);
},
}
);
}
module.exports = { register };
26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
/* eslint-disable no-console */

const { register } = require('./events');
const keepAlive = require('./utils/keep-alive');
const { register } = require("./events");
const keepAlive = require("./utils/keep-alive");

module.exports = (app) => {
console.log('Yay, the app was loaded!');
const router = app.route('/size-plugin');
module.exports = app => {
console.log("Yay, the app was loaded!");
const router = app.route("/size-plugin");
router.use(
keepAlive({
path: '/size-plugin/_keepalive',
path: "/size-plugin/_keepalive",
handler(req, res, next, options) {
if (req.path === options.path.replace('/size-plugin', '')) {
if (req.path === options.path.replace("/size-plugin", "")) {
res.send("I'm alive");
} else {
next();
}
},
}),
}
})
);
router.get('/ping', (req, res) => {
res.send('pong!!');
router.get("/ping", (req, res) => {
res.send("pong!!");
});
router.get('/', (req, res) => {
res.send('Hello Dolores !!');
router.get("/", (req, res) => {
res.send("Hello Dolores !!");
});
register(app);
};
Loading