需要使用 JIRA REST API 和 NodeJS 从 JIRA 获取 project/issues

Need to get project/issues from JIRA using JIRA REST API with NodeJS

我正在构建一个 NodeJS 应用程序,我想使用 JIRA 提供的 REST API 从 JIRA 获取 project/issues。我的 jira 在某些服务器上是 运行 ('http://example.com:8080/secure/Dashboard.jspa'),我可以使用 BASIC AUTH 从 POSTMAN 使用 REST API 来获取所有类型的数据,但是当我尝试使用 REST 登录 JIRA 时API 和 NodeJS,我得到了一些回应,但我无法理解我将如何使用该信息来调用其他 API。

我正在做的是,我将用户名和密码作为命令行参数传递,然后发送这些凭据以登录到 JIRA。然后我将使用 'node-fetch' 包从 REST API 获取信息。

下面是我的代码:

const fetch = require("node-fetch");
const yargs = require("yargs");
var JiraClient = require("jira-connector");
var request = require("request");

const jiraBaseUrl = "http://example.com:8080/secure/Dashboard.jspa";
const loginUrl = "auth/1/session";

const username = yargs.argv.u;
const password = yargs.argv.p;
const projectName = yargs.argv.n;

var headers = {
  "Content-Type": "application/json"
};

var options = {
  url: "http://example.com:8080/rest/api/2/issue/createmeta",
  headers: headers,
  auth: {
    user: username,
    pass: password
  }
};

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
}

request(options, callback);

有人可以告诉我我做错了什么,或者我需要如何处理我获得的数据才能使用其他 API,例如 ('http://example.com:8080/rest/api/2/issue/10008')?或者我在登录时做错了什么?

我已阅读 JIRA 网站上的文档,但未能正确理解。

如果您查看 Jira Rest API documentationrest/api/2/issue/createmeta 是获取创建问题元数据的终点。它 "returns details of projects, issue types within projects, and, when requested, the create screen fields for each issue type for the user. " 这个数据应该是巨大的,因为它 returns 所有项目的详细信息,以及项目中的所有问题类型。

如果您想使用其他 API,只需将 url 更改为具有正确端点 (documentation) 的适当 url 并按照文档进行操作作为正文数据发送。

这是获取一个问题的详细信息的示例: 把你要获取的issueIdOrKey放在括号里

var options = {
   method: 'GET',
   url: 'http://example.com:8080/rest/api/latest/issue/{issueIdOrKey}', 
   auth: { username: username, password: password },
   headers: {
      'Accept': 'application/json'
   }
};

request(options, function (error, response, body) {
   if (error) throw new Error(error);
   console.log(
      'Response: ' + response.statusCode + ' ' + response.statusMessage
   );
   console.log(body); //this would log all the info (in json) of the issue 
   // you can use a online json parser to look at this information in a formatted way

});