maxtepkeev / python-redmine

Python Redmine is a library for communicating with a Redmine project management application
Other
541 stars 120 forks source link

Cant Set priority_id #323

Closed caffeineaddiction closed 1 year ago

caffeineaddiction commented 1 year ago

Documentation here: https://python-redmine.com/resources/issue.html says that I should be able to either:

.create_issue() with named arguments like:

newissue = self.__redmineClient__.create_issue(
  project_id=project_id,
  subject=issue_title,
  tracker_id=tracker_id,
  description=issue_description,
  priority_id=priority_id,
  assigned_to_id=user_id
)

but gives the following error:

  File "/app/plugins/redminer.py", line 98, in __handle_view_submission__
    newissue = self.__redmineClient__.create_issue(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: RedmineClient.create_issue() got an unexpected keyword argument 'priority_id'

and if I instead try to use newissue.update() like:

newissue.update(newissue.id, priority_id=priority_id)

I get the following error:

 File "/app/plugins/redminer.py", line 108, in __handle_view_submission__
    newissue.update(newissue.id, priority_id=priority_id)
    ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/redminelib/resources/standard.py", line 158, in __getattr__
    return super().__getattr__(attr)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/redminelib/resources/base.py", line 204, in __getattr__
    raise exceptions.ResourceAttrError
redminelib.exceptions.ResourceAttrError: Resource doesn't have the requested attribute

current redmine setup:

Environment:
  Redmine version                5.0.4.stable
  Ruby version                   3.1.3-p185 (2022-11-24) [x86_64-linux]
  Rails version                  6.1.7
  Environment                    production
  Database adapter               Mysql2
  Mailer queue                   ActiveJob::QueueAdapters::AsyncAdapter
  Mailer delivery                smtp
Redmine settings:
  Redmine theme                  Default
SCM:
  Subversion                     1.14.1
  Mercurial                      5.6.1
  Bazaar                         3.1.0
  Git                            2.30.2
  Filesystem                     
Redmine plugins:
  no plugin installed
Python 3.7.7
pip 19.2.3
python-redmine 2.4.0
maxtepkeev commented 1 year ago

The code you're showing is yours and not from the python-redmine library, so I assume the bug is there and not in the library, because we don't hardcode any Redmine fields in methods and it accepts basically everything.

I suggest you try the vanilla python-redmine code from the docs first and check that it works and only then try to create any custom wrappers around it, i.e. do the following first:

from redminelib import Redmine

redmine = Redmine('https://XXX.com', username='XXX', password='XXX')

issue = redmine.issue.create(
  project_id=project_id,
  subject=issue_title,
  tracker_id=tracker_id,
  description=issue_description,
  priority_id=priority_id,
  assigned_to_id=user_id
)

print(list(issue))
caffeineaddiction commented 1 year ago

Pretty sure I am not wrapping anything. I have a class SlackApp where the redmine client is set like:

from redmine import RedmineClient
from redmine import SlackModalForm

class SlackApp:
  def __init__(self, config, app, handler):
    # ... snip ...
    self.__redmineClient__ = RedmineClient(self.__redmine_url__, self.redmine_api_key, self.redmine_cert_path)

from there I am making the api call as shown in the orig post:

newissue = self.__redmineClient__.create_issue(
  project_id=project_id,
  subject=issue_title,
  tracker_id=tracker_id,
  description=issue_description,
  priority_id=priority_id,
  assigned_to_id=user_id
)

this happens in a diff part of the SlackApp class on return from a form modal but that shouldn't matter.

Further, I took the exact same block of code and implemented it via the raw Redmine REST API and it worked as intended:

  def __handle_view_submission__(self, ack, body, logger):
    ack()

    view = body.get("view")
    state = view.get("state")
    values = state.get("values")

    # Extract the input values
    project_id = values["project"]["ssb_redmine_project_select_action"]["selected_option"]["value"]
    tracker_id = 1  # Severity ID (1: Bug, 2: Feature, etc.)
    user_id = values["assignee"]["ssb_redmine_assignee_select"]["selected_option"]["value"]
    issue_title = values["subject"]["ssb_redmine_subject_input"]["value"]
    form_issue_description = values["description"]["ssb_redmine_description_input"]["value"]
    priority_id = values["priority"]["ssb_redmine_priority_select"]["selected_option"]["value"]

    # trigger_metadata = json.loads(body["view"]["private_metadata"])
    trigger_metadata = self.__get_shortcut_trigger_metadata__(body)
    channel_id = trigger_metadata["channel_id"]
    slack_user_id = trigger_metadata["user_id"]

    issue_description = self.__format_issue_description__(form_issue_description, trigger_metadata)

    issue_data = {
      "issue": {
        "project_id": project_id,
        "subject": issue_title,
        "tracker_id": tracker_id,
        "description": issue_description,
        "priority_id": priority_id,
        "assigned_to_id": user_id
      }
    }

    headers = {
      "Content-Type": "application/json",
      "X-Redmine-API-Key": self.redmine_api_key
    }

    try:
      response = requests.post(
        f"{self.__redmine_url__}/issues.json",
        headers=headers,
        data=json.dumps(issue_data),
        verify=False
      )
      response.raise_for_status()  # Raises a HTTPError if the status is 4xx, 5xx

      newissue = response.json()["issue"]

not sure if the issue is with my understanding of your api, or your implementation of the api ... but their is a disconnect somewhere.

maxtepkeev commented 1 year ago

Have you checked that the code I showed you works ?

I don't know where you got that SlackApp class from, but the method create_issue doesn't exist in python-redmine library, so it's a wrapper around library's code for sure and the problem is there and not in the library.

maxtepkeev commented 1 year ago

Ah wait, I think I know where this problem comes from, it should be

self.__redmineClient__.issue.create(
  project_id=project_id,
  subject=issue_title,
  tracker_id=tracker_id,
  description=issue_description,
  priority_id=priority_id,
  assigned_to_id=user_id
)

Does that work for you ?

caffeineaddiction commented 1 year ago

nm, your correct.

Implemented somewhere else in the codebase I inherited:

class RedmineClient:
  def create_issue(self, project_id, tracker_id, subject, description, assigned_to_id):
    issue = self.redmine.issue.new()
    issue.project_id = project_id
    issue.tracker_id = tracker_id
    issue.subject = subject
    issue.description = description
    issue.assigned_to_id = assigned_to_id
    issue.save()
    return issue

Sorry for the trouble