gitlab4j / gitlab4j-api

GitLab4J API (gitlab4j-api) provides a full featured Java client library for working with GitLab repositories via the GitLab REST API
MIT License
1.07k stars 459 forks source link

'reporter' level access #166

Closed ricky001 closed 6 years ago

ricky001 commented 6 years ago

How to know whether the login user has 'reporter' level access for each project

gmessner commented 6 years ago

@ricky001 I would iterate over the projects the user is a member of and call the getMember method as follows:

User currentUser = gitLabApi.getUserApi().getCurrentUser();
Integer userId = currentUser.getId();
Pager<Project> projectPager = gitLabApi.getProjectApi().getMemberProjects(50);
while(projectPager.hasNext()) {
    for (Project project : projectPager.next()) {
        Member member = gitLabApi.getProjectApi().getMember(project.getId(), userId);
        AccessLevel accessLevel = member.getAccessLevel();
        if (accessLevel != null && accessLevel.toValue() >= AccessLevel.REPORTER.toValue()) {
                System.out.println(currentUser.getUsername() + " has REPORTER access to " + project.getName());
        }
    }
}
gmessner commented 6 years ago

@ricky001 Were you able to get this to work?

ricky001 commented 6 years ago

@gmessner the above code is not working, below is the error coming. even i am having reporter level access to that project

ricky.c has REPORTER access to demo ricky.c has REPORTER access to Demo ricky.c has REPORTER access to Demo Exception in thread "main" org.gitlab4j.api.GitLabApiException: 404 Not found at org.gitlab4j.api.AbstractApi.validate(AbstractApi.java:322) at org.gitlab4j.api.AbstractApi.get(AbstractApi.java:65) at org.gitlab4j.api.ProjectApi.getMember(ProjectApi.java:1045) at org.samsung.com.experiment.main(experiment.java:48)

gmessner commented 6 years ago

There is not enough information above to make any determination. I'm also wondering why you have the same project name repeated twice. To rule out a problem with the Pager can you try just the first 100 projects using:

User currentUser = gitLabApi.getUserApi().getCurrentUser();
Integer userId = currentUser.getId();
List<Project> projects =  gitLabApi.getProjectApi().getMemberProjects(1, 100);
for (Project project : projects) {
    System.out.println("Getting member info, userId=" + userId + ", project=" + project.getName());
    Member member = gitLabApi.getProjectApi().getMember(project.getId(), userId);
    AccessLevel accessLevel = member.getAccessLevel();
    if (accessLevel != null && accessLevel.toValue() >= AccessLevel.REPORTER.toValue()) {
        System.out.println(currentUser.getUsername() + " has REPORTER access to " + project.getName());
    }
}
ricky001 commented 6 years ago

@gmessner the project name repeated twice because it is from different user, but i am having reporter level access but it is giving 404 not found error

gmessner commented 6 years ago

A 404 error indicates that the member you are trying to get from a project is not a member of the project.

You should consider using the following, since it will not throw an exception if the user is not a member:

public Optional<Member> getOptionalMember(Integer projectId, Integer userId)
ricky001 commented 6 years ago

I am part of the project with reporter level access but project visibility access is internal. Any effect of visibility

gmessner commented 6 years ago

I don't believe so. The best way to track down these issues is to try the offending user/member pair in something like Postman. Remember, all that GitLab4J is doing is making REST calls and returning the results. And as indicated, the 404 error implies that the userId being used in the getMember() call is not a member of the project.

ricky001 commented 6 years ago

How can i prove that after being part of project with reporter level access, it is showing 404 not found error

gmessner commented 6 years ago

I don't quite understand your question, from your previous questions, it seems that you are doing this as one user (the logged in user) but trying to do a getMember() on a user other than the currently logged in user.

gmessner commented 6 years ago

Without seeing your actual code I cannot help much here.

ricky001 commented 6 years ago

public class Demo {

public static void main(String[] args) throws GitLabApiException {
    // TODO Auto-generated method stub
    GitLabApi gitLabApi = new GitLabApi(ApiVersion.V4,"url", "H-Zrp_dMKmKIZUYcfoSz");
    User currentUser = gitLabApi.getUserApi().getCurrentUser();
    Integer userId = currentUser.getId();
    Pager<Project> projectPager = gitLabApi.getProjectApi().getMemberProjects(50);
    List<Project> projects =  gitLabApi.getProjectApi().getMemberProjects(1, 100);
    for (Project project : projects) {
        System.out.println("Getting member info, userId=" + userId + ", project=" + project.getNameWithNamespace());
        Member member = gitLabApi.getProjectApi().getMember(project.getId(), userId);
        AccessLevel accessLevel = member.getAccessLevel();
        if (accessLevel != null && accessLevel.toValue() >= AccessLevel.REPORTER.toValue()) {
            System.out.println(currentUser.getUsername() + " has REPORTER access to " + project.getName());
        }
    }
}

}

gmessner commented 6 years ago

Reading the documentation further at: https://docs.gitlab.com/ee/api/members.html#get-a-member-of-a-group-or-project

You can only do a get member if the authenticated user is the owner of the project. From the documentation:

Attribute Type Required Description
id integer/string yes The ID or URL-encoded path of the project or group owned by the authenticated user
user_id integer yes The user ID of the member

This might explain your 404 error

gmessner commented 6 years ago

@ricky001 Can this issue be closed, or do you still need assistance?