go-jira icon indicating copy to clipboard operation
go-jira copied to clipboard

Problem with example create

Open ypsman opened this issue 8 years ago • 25 comments

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 :)

ypsman avatar Apr 17 '17 10:04 ypsman

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

andygrunwald avatar Apr 17 '17 11:04 andygrunwald


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.

ypsman avatar Apr 17 '17 12:04 ypsman

Does the Project APITEST exists?

andygrunwald avatar Apr 18 '17 07:04 andygrunwald

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

ypsman avatar Apr 18 '17 08:04 ypsman

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.

andygrunwald avatar Apr 21 '17 18:04 andygrunwald

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 avatar Apr 21 '17 18:04 ypsman

Hi @andygrunwald,

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

ypsman avatar May 04 '17 11:05 ypsman

@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.",
		},
	}

yageek avatar May 11 '17 15:05 yageek

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

Thank you:)

ypsman avatar May 11 '17 18:05 ypsman

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.

andygrunwald avatar May 11 '17 18:05 andygrunwald

@andygrunwald Sure :) #74

ypsman avatar May 11 '17 18:05 ypsman

Merged :) Thank you all

andygrunwald avatar May 12 '17 14:05 andygrunwald

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?

vbisbest avatar Feb 11 '21 20:02 vbisbest

@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!

tuckyapps avatar Apr 13 '21 04:04 tuckyapps

@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.

benjivesterby avatar Apr 14 '21 09:04 benjivesterby

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

manuelbcd avatar Apr 16 '21 12:04 manuelbcd

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:

  • go-jira version (git tag or sha):
  • Go version (go version):
  • Jira version:
  • Jira type (cloud or on-premise):
  • Involved Jira plugins:
  • Operating System and version:

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.

andygrunwald avatar Apr 16 '21 18:04 andygrunwald

Hi @andygrunwald @manuelbcd @benjivesterby ,

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

  • go-jira version was the last release.
  • Jira is Cloud.
  • System is Mac OS Big Sur.

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

tuckyapps avatar Apr 16 '21 23:04 tuckyapps

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

manuelbcd avatar Apr 17 '21 08:04 manuelbcd

@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?

tuckyapps avatar Apr 17 '21 15:04 tuckyapps

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.

manuelbcd avatar Apr 19 '21 07:04 manuelbcd

Hi @manuelbcd,

Now I get what you said :)

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

Best,

Santi

tuckyapps avatar Apr 19 '21 14:04 tuckyapps

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.

manuelbcd avatar Apr 19 '21 14:04 manuelbcd

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

tuckyapps avatar Apr 19 '21 15:04 tuckyapps

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

manuelbcd avatar Apr 19 '21 15:04 manuelbcd