Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fetch = require("node-fetch");
- const prompt = require("prompt");
- const puppeteer = require("puppeteer");
- const createToken = require("github-create-token");
- // Tools used to create the .csv with the datas that we recuperate from the several git projects (name of projects and number of issues)
- const fs = require("fs");
- const csvWriter = require("csv-write-stream");
- const writer = csvWriter({ headers: ["PROJECT", "ISSUES"] });
- writer.pipe(fs.createWriteStream("issues.csv"));
- // Urls used to recuperate the name of the projects on waffle.
- const urls = {
- login:
- "https://github.com/login?client_id=b68f04bdff231220a275&return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Db68f04bdff231220a275%26redirect_uri%3Dhttps%253A%252F%252Fwaffle.io%252Fgithub-callback%26response_type%3Dcode%26scope%3Dread%253Aorg%252Cuser%253Aemail%252Crepo%26state%3DyLWJntH29IyHBDMZFgcoPom4",
- projectsPage:
- "https://waffle.io/inextensodigital/emeabridge.eu/settings/sources"
- };
- // Same use as the urls above.
- const selectors = {
- login: "#login_field",
- password: "#password",
- submit: '[name="commit"]',
- openMenu: ".buttons > li > a",
- goToWaffleInextensodigital: ".button.private.js-private-login",
- projects:
- ".well.project-source.settings-list-item.ng-scope.ng-isolate-scope > div > span",
- errorConnectionMessage: ".flash.flash-full.flash-error"
- };
- /*
- Access token permitting the authentication to the account with the following mail : 'vincent.mazel@etu.univ-amu.fr'.
- However, this token shouldn't be here and should be generated each type the program is launched or stocked in a database for a security question.
- Solution --> stocker dans une base de données. Quand on lance le programme, on rentre id et password. On va check dans la bd pour voir si ya un token.
- Si oui c'est cool, sinon on en crée un tj avec le même nom (genre graphql_dashboard). Et hop, le tour est joué : pas de compte ni de token en dur dans le code.
- */
- let accessToken = "5ba612b28a01387c9e6f81cf2740b4b92b370844"; // a35c660cf1a14131057e7047384908c924c6b5aa
- // Options used for the generation of a github token.
- let opts = {
- username: "",
- password: "",
- scopes: ["repo"],
- note: "dashboard_token"
- };
- // Labels of the several issues that we recuperate.
- const labels = [
- '"ideas"',
- '"backlog"',
- '"priorisation"',
- '"definition"',
- '"proposal"',
- '"acceptance"',
- '"in progress"'
- ];
- // A variable that will be useful later when we'll have to make a graphql request to recuperate the number of issues done.
- let query = `query {`;
- // The configuration of the prompt.
- const schema = {
- properties: {
- email: {
- description: "Enter your email",
- type: "string",
- required: true
- },
- password: {
- description: "Enter your password",
- type: "string",
- required: true,
- hidden: true,
- replace: "*"
- }
- }
- };
- /*
- Asking here the email and password in order to connect on waffle and recuperate the name of
- the several ongoing projects.
- */
- prompt.start();
- console.log(
- "\n" +
- "Enter your github email and password to automaticaly recuperate the projects on waffle : " +
- "\n"
- );
- prompt.get(schema, function(err, result) {
- (async () => {
- const browser = await puppeteer.launch({
- headless: false,
- args: [
- "--no-sandbox",
- "--disable-setuid-sandbox",
- "--disable-dev-shm-usage"
- ]
- });
- // Init page
- const page = await browser.newPage();
- await page.setViewport({ width: 1200, height: 900 });
- await page.goto(urls.login, {
- waitUntil: "networkidle0"
- });
- await page.type(selectors.login, "vincent.mazel@etu.univ-amu.fr"); // result.email
- await page.type(selectors.password, "newyorkciti17"); // result.password
- await page.click(selectors.submit);
- await page.waitForNavigation({ waitUntil: "networkidle0" });
- await page.waitFor(2000);
- /* if (
- await page.evaluate(async s => {
- console.log(
- document.body.contains(
- document.querySelector(s.errorConnectionMessage)
- )
- );
- return document.body.contains(
- document.querySelector(s.errorConnectionMessage)
- );
- }, selectors)
- ) */
- if (
- await page.evaluate(async s => {
- console.log(
- document.body.contains(
- document.querySelector(s.errorConnectionMessage)
- )
- );
- return document.body.contains(
- document.querySelector(s.errorConnectionMessage)
- );
- }, selectors)
- ) {
- console.log("\n" + "Wrong username or password, be careful !" + "\n");
- } else {
- console.log("1");
- await page.waitForSelector(selectors.openMenu);
- console.log("2");
- await page.click(selectors.openMenu);
- console.log("3");
- await page.waitForSelector(selectors.goToWaffleInextensodigital);
- await page.waitFor(3000);
- await page.click(selectors.goToWaffleInextensodigital);
- await page.waitForNavigation({ waitUntil: "networkidle2" });
- await page.goto(urls.projectsPage, { waitUntil: "networkidle2" });
- await page.waitFor(1500);
- // Recuperation of the name of the several ongoing projects.
- const projects = await page.evaluate(async s => {
- const projectsName = [];
- const projectsReq = document.querySelectorAll(s.projects);
- projectsReq.forEach(function(thisArg) {
- projectsName.push(
- thisArg.innerHTML.substring("inextensodigital/".length)
- );
- });
- return projectsName;
- }, selectors);
- // Projects are recuperated, closing of the browser.
- await browser.close();
- // Generation of a github token.
- /* opts.username = result.email;
- opts.password = result.password;
- createToken(opts, clbk);
- function clbk(error, results, info) {
- if (info) {
- console.error(info);
- }
- if (error) {
- throw new Error(error.message);
- }
- console.log("Token : " + results.token);
- accessToken = results.token;
- } */
- /*
- Generation of the graphql request using the query variable initialized earlier.
- However, it's quite important to notice here that this is quite a dirty way to recuperate our issues.
- Indeed, factorizing code by using fragments and variables would have been better, but I didn't manage to do it,
- so this solution is for the moment the best one that I can give.
- */
- for (let i = 1; i < projects.length; i += 1) {
- query +=
- `repo` +
- i +
- `: repository(owner:"inextensodigital", name:"` +
- projects[i] +
- `") {
- name
- issues(labels:[` +
- labels +
- `], first:100, states:[OPEN]) {
- edges {
- node {
- title
- }
- }
- }
- }`;
- }
- query += `}`;
- // Sending the graphql request.
- fetch("https://api.github.com/graphql", {
- method: "POST",
- body: JSON.stringify({ query }),
- headers: {
- Authorization: `Bearer ${accessToken}`
- }
- })
- .then(res => res.text())
- .then(body => {
- const jsonObj = JSON.parse(body);
- let tabNameNbIssues = [];
- let nbIssues = 0;
- Object.entries(jsonObj.data).forEach(([key, value]) => {
- nbIssues += value.issues.edges.length;
- // tabNameNbIssues[value.name] = value.issues.edges.length;
- writer.write([value.name, value.issues.edges.length]);
- tabNameNbIssues.push({
- projectName: value.name,
- nbIssues: value.issues.edges.length
- });
- });
- writer.end();
- console.log("\n" + "Number total of issues : " + nbIssues + "\n");
- })
- .catch(error => console.error(error));
- }
- })();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement