首页 > 解决方案 > 在一个简单的节点/反应应用程序中获取 Gitlab API 数据

问题描述

基本上,我很难将数据从 Gitlab 拉到一个简单的节点/反应应用程序。

我从 Gitlab API 文档获得了这个 CURL API 链接:

curl --header "PRIVATE-TOKEN: S3x4***RJ" "https://git.mywebsite.com/api/v4/projects/1212/boards"

当我在终端中使用它时,很好地为我提供了我需要的所有数据:

  {
    "id": 111,
    "name": "Jimmy",
    "username": "JSmith",
    "state": "active",
    "avatar_url": "https://git.mywebsite.com/uploads/-/system/user/avatar/111/avatar.png",
    "web_url": "https://git.mywebsite.com/JSmith"
  },
  {
    "id": 223,
    "name": "Billy Dean",
    "username": "BDean",
    "state": "active",
    "avatar_url": "https://git.mywebsite.com/uploads/-/system/user/avatar/223/avatar.png",
    "web_url": "https://git.mywebsite.com/BDean"
  }

等等

但是,我的 .js 应该如何使用这个 CURL API 实现,以便获取包含“id”或“state”数据之类的变量?

我尝试使用https://curl.trillworks.com/#node转换此 CURL

像这样实现后:

import React from 'react'
import {PageHeader} from "antd";
import * as util from "util"; // All Resources

const api = new Gitlab({
    host: 'https://git.mywebsite.com',
    token: 'S3x4***_hXxRJ',
});

// Listing users
let users = api.Users

// Or using Promise-Then notation
api.Projects.all().then((projects) => {
    console.log(projects);
});
console.log(util.inspect(users, false, null));
alert(util.inspect(users, false, null));


class AgileMetrics extends React.Component {


    componentDidMount() {
    }

    render() {
        return (
            <div>
                <PageHeader
                    className="site-page-header"
                    title="Metrics"
                    subTitle="Get an overview on the Metrics here..."
                />
            </div>
        );
    }

}

export default Metrics;

...我得到的只是我的连接信息:

在此处输入图像描述

我还尝试了Gitbreaker库,在他们的示例中使用它。也许使用他们的例子,我错过了添加其他东西,这对有经验的人来说是显而易见的。

标签: node.jsreactjscurlgitlab-api

解决方案


看起来您只分配users给 Gitlab 包中的 users api 函数。并假设您的utils函数提取了您想要的数据idstate- 因为它只是分配了原始函数,所以它正在渲染它。

也许是这样的?

// https://github.com/jdalrymple/gitbeaker#readme
import { Gitlab, Projects, Users } from '@gitbeaker/browser'; 
// .. add other imports

// initialize the api with credentials
const api = new Gitlab({
    host: 'https://git.mywebsite.com',
    token: 'S3x4***_hXxRJ',
});

class AgileMetrics extends React.Component {
    async componentDidMount() {
      const projects = await Projects.all()
      const users = await Users.all()

      // process `users` with `utils.inspect`
      console.log(users)
    }

    render() {
        return (
            <div>
                <PageHeader
                    className="site-page-header"
                    title="Metrics"
                    subTitle="Get an overview on the Metrics here..."
                />
            </div>
        );
    }

}

推荐阅读