C# 的 Jira Api

问题描述 投票:0回答:1

我正在尝试创建一个 .net 应用程序来获取项目(例如 DevProject)中存在的所有 jira 票证。 我正在尝试使用 jira api,但我要么收到空响应,要么收到 404 错误。我确定我错过了一些东西,但我不确定是什么。

这是我公司的 baseurl 的 jira 帐户,我正在使用的是“https://mycompany.atlassian.net” 并且 restUrl 是“/rest/api/3/issue/devissue”

这是一个简单的代码片段

HttpWebResponse response = null;
HttpWebRequest request = WebRequest.Create(baseurl+restUrl) as HttpWebRequest;
request.Method = "GET";
request.Accept = "application/json";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Basic " + GetEncodedCredentials("myUsename", "password"));

string responseContent;
using (response = request.GetResponse() as HttpWebResponse)
{
  StreamReader reader = new StreamReader(response.GetResponseStream());
  responseContent = reader.ReadToEnd();
}

在密码部分我也尝试添加 api 令牌,但没有成功。

我希望它返回问题或任何内容,以检查我是否以正确的方式调用 api,或者我是否遗漏了某些内容。 任何建议都会有帮助。

我也使用我公司的 jira 域创建了一个 API 令牌。

c# asp.net jira-rest-api
1个回答
0
投票

这是基于他们的 API 文档的快速实现。

async void Main()
{
    string jiraBaseUrl = "YOUR_BASE_URL";
    string issueKey = "YOUR_TICKET_KEY"; // Replace with your JIRA issue key
    string email = "YOUR_EMAIL_TIED_TO_API_TOKEN";
    string apiToken = "YOUR_API_TOKEN"; // Replace with your JIRA API token

    using (HttpClient client = new HttpClient())
    {
        client.BaseAddress = new Uri(jiraBaseUrl);
        var base64Credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes($"{email}:{apiToken}"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Credentials);

        HttpResponseMessage response = await client.GetAsync($"/rest/api/3/issue/{issueKey}");

        if (response.IsSuccessStatusCode)
        {
            string json = await response.Content.ReadAsStringAsync();
            Root issue = JsonConvert.DeserializeObject<Root>(json);
            //json.Dump();
            issue.Dump();
            issue.Fields.Content.Dump();
            string formattedDescription = FormatDescription(issue.Content);
            Console.WriteLine(formattedDescription);
            //var issue = JsonSerializer.Deserialize<Issue>(json);
            //Console.WriteLine($"Issue Key: {issue.Key}");
            //Console.WriteLine($"Summary: {issue.Fields.Summary}");
            //Console.WriteLine($"Description: {issue.Fields.Description}");
        }
        else
        {
            Console.WriteLine($"Error: {response.StatusCode}");
            string errorMessage = await response.Content.ReadAsStringAsync();
            Console.WriteLine(errorMessage);
        }
    }
}
public static string FormatDescription(List<Content> contentList)
{
    if (contentList == null || contentList.Count == 0)
        return string.Empty;

    return string.Join("\n\n", contentList.SelectMany(content => content.Contents.Select(ParseContentItem)));
}


public static string ParseContent(Content content)
{
    if (content == null || content.Contents == null)
        return string.Empty;

    return string.Join("", content.Contents.Select(ParseContentItem));
}

public static string ParseContentItem(ContentItem contentItem)
{
    if (contentItem == null)
        return string.Empty;

    string text = contentItem.Text ?? "";

    if (contentItem.Attrs != null && !string.IsNullOrEmpty(contentItem.Attrs.Text))
    {
        text = contentItem.Attrs.Text;
    }

    return text;
}

public class AvatarUrls
{
    [JsonProperty("48x48")]
    public string _48x48 { get; set; }

    [JsonProperty("24x24")]
    public string _24x24 { get; set; }

    [JsonProperty("16x16")]
    public string _16x16 { get; set; }

    [JsonProperty("32x32")]
    public string _32x32 { get; set; }
}

public class AccountType
{
    public string AccountId { get; set; }
    public string EmailAddress { get; set; }
    public AvatarUrls AvatarUrls { get; set; }
    public string DisplayName { get; set; }
    public bool Active { get; set; }
    public string TimeZone { get; set; }
    public string AccountTypes { get; set; }
}

public class Customfield10993
{
    public string Type { get; set; }
    public int Version { get; set; }
    public List<Content> Content { get; set; }
}

public class Content
{
    public string Type { get; set; }
    public List<ContentItem> Contents { get; set; }
}

public class ContentItem
{
    public string Type { get; set; }
    public string Text { get; set; }
    public MentionAttrs Attrs { get; set; }
    public List<Mark> Marks { get; set; }
}

public class MentionAttrs
{
    public string Id { get; set; }
    public string Text { get; set; }
    public string AccessLevel { get; set; }
}

public class Mark
{
    public string Type { get; set; }
    public MarkAttrs Attrs { get; set; }
}

public class MarkAttrs
{
    public string Href { get; set; }
}

public class Issuelinks
{
    public string Id { get; set; }
    public string Self { get; set; }
    public LinkType Type { get; set; }
    public InwardIssue InwardIssue { get; set; }
}

public class LinkType
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Inward { get; set; }
    public string Outward { get; set; }
    public string Self { get; set; }
}

public class InwardIssue
{
    public string Id { get; set; }
    public string Key { get; set; }
    public string Self { get; set; }
    public Fields Fields { get; set; }
}

public class Fields
{
    public string Summary { get; set; }
    public Status Status { get; set; }
    public Priority Priority { get; set; }
    public Issuetype Issuetype { get; set; }
    public AccountType Assignee { get; set; }
    public List<Content> Content { get; set; } 
}

public class Status
{
    public string Self { get; set; }
    public string Description { get; set; }
    public string IconUrl { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }
    public StatusCategory StatusCategory { get; set; }
}

public class StatusCategory
{
    public string Self { get; set; }
    public int Id { get; set; }
    public string Key { get; set; }
    public string ColorName { get; set; }
    public string Name { get; set; }
}

public class Priority
{
    public string Self { get; set; }
    public string IconUrl { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }
}

public class Issuetype
{
    public string Self { get; set; }
    public string Id { get; set; }
    public string Description { get; set; }
    public string IconUrl { get; set; }
    public string Name { get; set; }
    public bool Subtask { get; set; }
    public int AvatarId { get; set; }
    public int HierarchyLevel { get; set; }
}

public class Comment
{
    public string Self { get; set; }
    public string Id { get; set; }
    public AccountType Author { get; set; }
    public Body Body { get; set; }
    public AccountType UpdateAuthor { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }
    public bool JsdPublic { get; set; }
}

public class Body
{
    public int Version { get; set; }
    public string Type { get; set; }
    public List<Content> Content { get; set; }
}

public class CommentContainer
{
    public List<Comment> Comments { get; set; }
    public string Self { get; set; }
    public int MaxResults { get; set; }
    public int Total { get; set; }
    public int StartAt { get; set; }
}

public class Root
{
    public string Expand { get; set; }
    public string Id { get; set; }
    public string Self { get; set; }
    public string Key { get; set; }
    public Fields Fields { get; set; }
    public List<Content> Content { get; set; } // Add this property
}
© www.soinside.com 2019 - 2024. All rights reserved.