Свести данные JSON для json2csv - PullRequest
0 голосов
/ 12 марта 2019

У меня есть данные JSON

{
    "url": "https://api.github.com/repos/UserName/SalesforcePOC/issues/3",
    "repository_url": "https://api.github.com/repos/UserName/SalesforcePOC",
    "labels_url": "https://api.github.com/repos/UserName/SalesforcePOC/issues/3/labels{/name}",
    "comments_url": "https://api.github.com/repos/UserName/SalesforcePOC/issues/3/comments",
    "events_url": "https://api.github.com/repos/UserName/SalesforcePOC/issues/3/events",
    "html_url": "https://github.com/UserName/SalesforcePOC/issues/3",
    "id": 417232567,
    "node_id": "MDU6SXNzdWU0MTcyMzI1Njc=",
    "number": 3,
    "title": "CEAS-345",
    "user": {
      "login": "UserNameG",
      "id": 47857828,
      "node_id": "MDQ6VXNlcjQ3ODU3ODI4",
      "avatar_url": "https://avatars3.githubusercontent.com/u/47857828?v=4",
      "gravatar_id": "",
      "url": "https://api.github.com/users/UserNameG",
      "html_url": "https://github.com/UserNameG",
      "followers_url": "https://api.github.com/users/UserNameG/followers",
      "following_url": "https://api.github.com/users/UserNameG/following{/other_user}",
      "gists_url": "https://api.github.com/users/UserNameG/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/UserNameG/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/UserNameG/subscriptions",
      "organizations_url": "https://api.github.com/users/UserNameG/orgs",
      "repos_url": "https://api.github.com/users/UserNameG/repos",
      "events_url": "https://api.github.com/users/UserNameG/events{/privacy}",
      "received_events_url": "https://api.github.com/users/UserNameG/received_events",
      "type": "User",
      "site_admin": false
    },
    "labels": [
      {
        "id": 1257724245,
        "node_id": "MDU6TGFiZWwxMjU3NzI0MjQ1",
        "url": "https://api.github.com/repos/UserName/SalesforcePOC/labels/bug",
        "name": "bug",
        "color": "d73a4a",
        "default": true
      },
      {
        "id": 1257724247,
        "node_id": "MDU6TGFiZWwxMjU3NzI0MjQ3",
        "url": "https://api.github.com/repos/UserName/SalesforcePOC/labels/duplicate",
        "name": "duplicate",
        "color": "cfd3d7",
        "default": true
      }
    ],
    "state": "open",
    "locked": false,
    "assignee": null,
    "assignees": [

    ],
    "milestone": null,
    "comments": 2,
    "created_at": "2019-03-05T10:43:50Z",
    "updated_at": "2019-03-06T08:25:18Z",
    "closed_at": null,
    "author_association": "COLLABORATOR",
    "body": "Remove\r\nhttps://github.com/UserName/SalesforcePOC/blob/9b0b6587cef4fabde769ab4633ec8b916b95aa0c/src/destructiveChangesPost.xml#L5-L8"
  },

Мне нужны данные в файле .csv, где каждая проблема занимает 1 столбец, и если в каждой проблеме есть несколько меток, как в приведенном выше коде «Дубликат» и «Ошибка», они должны быть в следующем столбце в отдельной ячейке (разделены через запятую или новую строку в той же ячейке Excel).

Я использую json2csv для получения данных в файле .csv

const graphql = require("graphql-request");
const json2csv = require("json2csv");
const fs = require("fs");
const argv = require("minimist")(process.argv.slice(2));
const fileName = argv.filename || "issues.csv";

const fields = [

    {
        value: "node.number",
        label: "Issue Number"
    },
    {
        value: "node.title",
        label: "Issue title"
    },
    {
        value: "node.author.login",
        label: "Issue Author/Code Reviewer"
    },
    {
        value: "node.state",
        label: "Issue Status"
    },

    {
        value: "node.milestone.id",
        label: "Team/Tower"
    },
    {
        value: "node.labels.edges.node.name",
        label: "label"
    },
    {
        value: "node.bodyText",
        label: "Issue Description"
    },
    {
        value: "node.comments.edges.node.bodyText",
        label: "comment_body"
    },
    {
        value: "node.comments.edges.node.author.login",
        label: "comment_author"
    },
    {
        value: "node.comments.edges.node.createdAt",
        label: "comment_createdAt"
    },

    {
        value: "node.url",
        label: "url"
    }
];

const client = new graphql.GraphQLClient("https://api.github.com/graphql", {
    headers: {
        Authorization: `Bearer ${argv.token}`
    }
});

const query = `{
  repository(owner: "${argv.owner}", name: "${argv.repo}") {
    issues(last: 100) {
      edges {
        node {
          title
          number
          state
          author {
            login
          }
          bodyText
          url
          milestone {
            id
          }
          labels(first: 10) {
            edges {
              node {
                name
              }
            }
          }
          comments(last: 3) {
            edges {
              node {
                author {
                  avatarUrl
                  login
                  resourcePath
                  url
                }
                bodyText
                createdAt
              }
            }
          }
        }
      }
    }
  }
}`;

function writeCsvFile(csv) {
    fs.writeFile(fileName, csv, function (err) {
        if (err) throw err;
        console.log(`${fileName} file saved`);
    });
}

client
    .request(query)
    .then(data => {
        const json2csvConfig = {
            data: data.repository.issues.edges,
            fields: fields,
            includeEmptyRows: true,
            unwindPath: ["node.labels.edges", "node.comments.edges"]
        };

        json2csv(json2csvConfig, (err, csvString) => {
            writeCsvFile(csvString.replace(/[‘’]/g, `'`).replace(/[“”]/g, '"'));
        });
    })
    .catch(error => console.error(error));

Как изменить раскрутку, чтобы показывать данные для меток в одной ячейке?

Спасибо

...