Skip to content
This repository was archived by the owner on Nov 4, 2020. It is now read-only.

Had trouble with setting options for createProject #15

Closed
samrocketman opened this issue Sep 12, 2013 · 6 comments
Closed

Had trouble with setting options for createProject #15

samrocketman opened this issue Sep 12, 2013 · 6 comments

Comments

@samrocketman
Copy link
Contributor

gitlabhq/gitlab-recipes#126

I was having trouble with setting new options for git.createProject. I was passing options to the function as booleans like True and I even tried str(True). I found that the API is expecting a number such as 0 or 1 e.g. public=1. I resolved that issue by writing str(int(True)).

Not sure if you care to modify anything as it wasn't really a problem with your project. But it might confuse developers trying to develop against the API with python-gitlab. Perhaps it would be useful to add a documentation example for git.createProject where it is setting some options changing defaults.

@Itxaka
Copy link
Member

Itxaka commented Sep 13, 2013

Hi there! thanks for your feedback!

Im not sure I understand your problem, going by the documentation http://python-gitlab.readthedocs.org/ all the args to createProject() are strings so a simple:

git.createProject("new project", public="1")

Should work, I think.

Can you explain it further?

Im firing up a VM to install gitlab 6 and try the method to see if I can find the issue you are mentioning, will report back ASAP.

Cheers!
Itxaka.

@samrocketman
Copy link
Contributor Author

Sure, I'm using optparse for argument handling in a python 2.x script. I'm setting options in the script like --wiki and --issues.

My optparse looks like the following:

parser = OptionParser()
parser.add_option("--issues",dest="issues",action="store_true",default=False)
parser.add_option("--wall",dest="wall",action="store_true",default=False)
parser.add_option("--merge",dest="merge",action="store_true",default=False)
parser.add_option("--wiki",dest="wiki",action="store_true",default=False)
parser.add_option("--snippets",dest="snippets",action="store_true",default=False)
parser.add_option("--public",dest="public",action="store_true",default=False)
parser.add_option("--create",dest="create",action="store_true",default=False)
parser.add_option("--delete",dest="delete",action="store_true",default=False)
parser.add_option("--desc",dest="desc",metavar="DESC",default=False)
(options,args) = parser.parse_args()

So I was creating a project like.....

new_project=git.createProject(pname,description=options.desc,issues_enabled=options.issues,wall_enabled=options.wall,merge_requests_enabled=options.merge,wiki_enabled=options.wiki,snippets_enabled=options.snippets,public=options.public)

Since I was passing a boolean it would be set as True. Booleans didn't work so I converted to a string e.g. str(options.merge) to get merge_requests_enabled="True". Converting to a string didn't work so I first converted it to an integer and then to a string and it finally worked. e.g. str(int(options.merge)) to get merge_requests_enabled="1". With that working I changed my new_project command to the following.

new_project=git.createProject(pname,description=options.desc,issues_enabled=str(int(options.issues)),wall_enabled=str(int(options.wall)),merge_requests_enabled=str(int(options.merge)),wiki_enabled=str(int(options.wiki)),snippets_enabled=str(int(options.snippets)),public=str(int(options.public)))

@samrocketman
Copy link
Contributor Author

https://github.com/Itxaka/python-gitlab/blob/master/gitlab/__init__.py#L267-L272

You could check for typing in your code and then auto convert it for the user passing the argument. e.g.

import types
def convertType(val):
  if type(val) == types.BooleanType:
    val = str(int(val))
  elif type(val) == types.StringType and val == "True":
    val = str(int(bool(val)))

Utilize it like so...

    data = {"name": name, "description": convertType(description),
            "default_branch": convertType(default_branch),
            "issues_enabled": convertType(issues_enabled), 
            "wall_enabled": convertType(wall_enabled),
            "merge_requests_enabled": convertType(merge_requests_enabled),
            "wiki_enabled": convertType(wiki_enabled),
            "snippets_enabled": convertType(snippets_enabled)}

And do the same for the public value. But again it's not that big of a deal. If you add it to your documentation I think it should be enough and note that booleans don't work in your documentation. Sometimes it's better to just document it. However you want to handle it is fine with me.

@Itxaka
Copy link
Member

Itxaka commented Sep 14, 2013

Hi there,

I was thinking maybe instead of using all args as strings, change them to int, at least for values that only have those options. I mean, it makes no sense tu use strings when the value is gonna be 1 or 0/true or false, like in the case of public or xxxID

what do you think?

@samrocketman
Copy link
Contributor Author

I didn't know that it would straight up handle int. Yeah I'd say converting to just an int is better if requests can handle that.

Itxaka added a commit that referenced this issue Sep 16, 2013
@Itxaka
Copy link
Member

Itxaka commented Sep 16, 2013

I tried to change it, let me know if it works now :)

Itxaka added a commit that referenced this issue Sep 16, 2013
changed some args, could fix Issue #15
@Itxaka Itxaka closed this as completed Sep 28, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants