andygrunwald / go-jira

Go client library for Atlassian Jira
https://pkg.go.dev/github.com/andygrunwald/go-jira?tab=doc
MIT License
1.47k stars 468 forks source link

Problem with example create #69

Open ypsman opened 7 years ago

ypsman commented 7 years ago

Im trying to Connect with the Example to Jira and creating an Issue. Getting this as Response {"errorMessages":[],"errors":{"project":"project is required"}} panic: runtime error: invalid memory address or nil pointer dereference

go version go1.6.2 linux/amd64 Jira 7.3 & 7.1

Im new to golang so a little hint what to do would be nice :)

andygrunwald commented 7 years ago

Hey @ypsman, can you please post your complete code here? Without the source code it is hard to follow. Thanks.

ypsman commented 7 years ago

import (
    "fmt"

    "github.com/andygrunwald/go-jira"
)

func main() {
    jiraClient, err := jira.NewClient(nil, "https://myjira.de/")
    if err != nil {
        panic(err)
    }

    res, err := jiraClient.Authentication.AcquireSessionCookie("APIuser", "pass")
    if err != nil || res == false {
        fmt.Printf("Result: %v\n", res)
        panic(err)
    }

    i := jira.Issue{
        Fields: &jira.IssueFields{
            Assignee: &jira.User{
                Name: "myname",
            },
            Reporter: &jira.User{
                Name: "APIuser",
            },
            Description: "Test Issue",
            Type: jira.IssueType{
                ID: "60",
            },
            Project: jira.Project{
                Name: "APITEST",
            },
            Summary: "Just a demo issue",
        },
    }

    issue, _, err := jiraClient.Issue.Create(&i)

    if err != nil {
        panic(err)
    }

    fmt.Printf("%s: %+v\n", issue.Key, issue.Fields.Summary)
}

its just like the Example. I used it in another Project, that didnt work, so i tryd it in a single File. Search for Issues Works.

andygrunwald commented 7 years ago

Does the Project APITEST exists?

ypsman commented 7 years ago

Yes, the Fields are correct, its work with a Pyton script.

andygrunwald commented 7 years ago

Can you may post the Python script as well? With this i might be able to find the difference and maybe we hit a bug.

ypsman commented 7 years ago

Its just a simple script with the Jira Module(python3) I cant post it complete, but heres the class i use for.

from jira import JIRA
from jira.exceptions import JIRAError
class JiraAPI:
    def __init__(self):
        try:
            self.api = JIRA(jiraserver, basic_auth=(jiraauth))
        except JIRAError as e:
            print("Exit Code: " + e.status_code)
            sys.exit()

def ticket_create(self):
    self.ticket = {
                    'project' : self.Project,
                    'summary' : self.ticket_summary,
                    'issuetype' : { 'name' : 'Aufgabe' },
                    'description' : self.description,
                    "assignee": { "name": self.assignee }
                }
        JiraAPI().issue_create(self.ticket)

def issue_create(self, myTicket):
        self.api.create_issue(fields=myTicket)

btw, its a german Jira Version, hope this Helps.

ypsman commented 7 years ago

Hi @andygrunwald,

any news, or just a working example for me:) ty

yageek commented 7 years ago

@andygrunwald I also encountered this issue.

If you take a look to the official documentation of JIRA to create an issue (https://developer.atlassian.com/jiradev/jira-apis/jira-rest-apis/jira-rest-api-tutorials/jira-rest-api-example-create-issue), you may have to use something like:

i := jira.Issue{
        Fields: &jira.IssueFields{
            Reporter: &jira.User{
                Name: "username",
            },
            Description: "Description",
            Type: jira.IssueType{
                Name: "Bug",
            },
            Project: jira.Project{
                Key:"PROJECTKEY",
            },
            Summary: "Some summary.",
        },
    }
ypsman commented 7 years ago

@yageek thanks Now it works, just replaced Name with Key for the Project, also IssuType needs Name and not ID.

Thank you:)

andygrunwald commented 7 years ago

Thanks for helping here @yageek! @ypsman Would you mind to fix the example with a PR?

PS: Sorry to come so late to the game.

ypsman commented 7 years ago

@andygrunwald Sure :)

74

andygrunwald commented 7 years ago

Merged :) Thank you all

vbisbest commented 3 years ago

I have a similar issue with Reporter. Using any but the AccountID as the value fails to create the ticket with: "{\"errorMessages\":[],\"errors\":{\"reporter\":\"Reporter is required.\"}}"

Ideas?

tuckyapps commented 3 years ago

@andygrunwald Hey, great project by the way! I am having the same issue as @vbisbest with the reporter, is there any fix coming soon? Thanks!

benjivesterby commented 3 years ago

@tuckyapps this is an open source project and those of us maintainers work on these issues as we're able. You're welcome to submit a PR to update the example so that it is correct and I'll review it. Otherwise, if you need production support you're welcome to reach out to the maintainers and see if they're available for contracted support.

manuelbcd commented 3 years ago

@vbisbest @tuckyapps would you be so kind to specify jira version and type? Maybe I can bear a hand here Best.

andygrunwald commented 3 years ago

Hey @tuckyapps and @vbisbest, as @benjivesterby wrote, we, as maintainers, do not have the time that we would love to spend on this project, due to our work, family, and other obligations. We would ask you and others to provide an understanding of this.

What would be helpful are a few information about your setup, like:

If you need support in a timely manner incl. support, please reach out to me. I am able to offer contractual support including feature and bug development for this.

PS: Thanks @benjivesterby and @manuelbcd for your support! PPS: Reopening, because it seems that this is still an issue.

tuckyapps commented 3 years ago

Hi @andygrunwald @manuelbcd @benjivesterby ,

Thanks again, didn't mean it in a bad way.

I am not using the library because only to solve a specific need and I decided to go for Jira API this time.

In case it helps, this is the code I am using that is working:

// createJiraIssue is the function used to create a Jira issue.
func createJiraIssue(jiraNotification *notifications.Notification) error {
    jiraSite := jiraNotification.JiraSite
    jiraLoginEmail := jiraNotification.JiraLoginEmail
    jiraLoginToken := jiraNotification.JiraToken

    vulnDescription := jiraNotification.VulnerabilityDescription
    vulnPOC := jiraNotification.VulnerabilityProofOfConcept
    vulnCriticality := jiraNotification.VulnerabilityCriticalityDescription
    vulnSolution := jiraNotification.VulnerabilitySolution

    jiraIssue := JiraIssue{}
    jiraIssue.Fields.Project.Key = jiraNotification.JiraProjectKey
    jiraIssue.Fields.Summary = jiraNotification.VulnerabilityName
    jiraIssue.Fields.IssueType.Name = "Bug"
    jiraIssue.Fields.JiraPriority.Name = jiraNotification.VulnerabilityCriticality
    jiraIssue.Fields.Description = jiraNotification.VulnerabilityDescription
    jiraIssue.Fields.Labels = []string{"vulnerability", "strike.sh"}

    client := resty.New()
    resp, err := client.R().SetBasicAuth(jiraLoginEmail, jiraLoginToken).
        SetHeader("Content-Type", "application/json").
        SetBody(jiraIssue).
        Post(fmt.Sprintf("%s/rest/api/2/issue", jiraSite))

    if err != nil || resp.StatusCode() != http.StatusOK && resp.StatusCode() != http.StatusCreated {
        return fmt.Errorf("error creating Jira issue, status code '%v', err '%s', message '%s'", resp.StatusCode(), err, resp)
    }

    return nil
}

Again, great project, thank you for helping the community.

Cheers,

Santi

manuelbcd commented 3 years ago

Let me shed some light on this (if I understood the problem properly).

@tuckyapps, @vbisbest you said you are unable to create issues when specifying reporter, using any username but accountID

That is right since Jira cloud abandoned usernames (please have a look to official article https://confluence.atlassian.com/cloud/blog/2018/06/say-goodbye-to-usernames-in-atlassian-cloud )

In short: Jira cloud API is accepting only accountIDs in user-related issue fields. It is not a go-jira library bug.

I hope it will help you.

@andygrunwald , @benjivesterby : then the question is... do you want to change the examples to specify accountID instead of username since username is no longer accepted by jira-cloud? The accountID is compatible with both on-prem (old/new) and cloud versions. I would be more than happy to prepare and send that PR

tuckyapps commented 3 years ago

@manuelbcd Wow, thank you for your fast response!

Thank you for providing the article. I'm wondering however, why does it works using the API then?

Doesn't make sense, right?

manuelbcd commented 3 years ago

Hey @tuckyapps. Sorry, maybe I did not express myself properly.

username field is not deprecated, it is a valid field within user structures in both Jira server and Jira cloud. However, you can't avoid accoundID in Jira cloud anymore (since 2008 due to GDPR compliance). That means you need to specify at least accountID... that makes redundant to specify also username, since accountID is enough and it is valid for both Cloud and Server versions.

Did I clarify your question? If not, please tell it to me, I'm gad to discuss and learn

Best.

tuckyapps commented 3 years ago

Hi @manuelbcd,

Now I get what you said :)

However, still wondering why it works using the API without the account id.

Best,

Santi

manuelbcd commented 3 years ago

It will work with Jira Server API but not with Jira Cloud API.

If you are communicating with Jira Server API, you will be able to refer to users using only username field.

If you are communicating with Jira Cloud API, you can't refer to users using usernema, you need to use accountID.

But both (Server and Cloud) accepts accountID.

Best.

tuckyapps commented 3 years ago

Hmm then I don’t get it, as I am using Jira cloud 🤔

manuelbcd commented 3 years ago

If Jira Cloud is allowing you to create issues by specifying username without accountID it is probably because your project has a default assignee and reporter :)

Please perform the folllowing test:
Having user-A as default assignee and user-B as default reporter, try to create an issue indicanting username from user-C (and avoid accountID). If I'm right, this issue will be created ignoring user-C