Get only the collaborators, not the owners of the ORG of the github repo

1.6k views Asked by At

I have Github Organization with the owners Owner1 and Owner2 and members Member1 and Member2. Now I created a new repository in this Organization and added two users as collaborators (user1 and user2) of this repo.

Now the result of postman with the below GET request will result in the following:

Owner1
Owner2
user1
user2

POSTMAN request : https://<github-host>/api/v3/repos/<my-gihub-org>/<my-github-repo>/collaborators?per_page=100

But I am interested in listing only the collaborators i.e., user1 and user2; Not the owners.

Could someone please help me here..!

2

There are 2 answers

0
mnestorov On BEST ANSWER

You can pass the affiliation parameter, in order to filter out any collaborators that directly work on a certain project.

curl -H "Authorization: <bearer KEY>" \
     -H "Accept: application/vnd.github.v3+json" \
     -XGET https://api.github.com/repos/:org/:repo/collaborators\?affiliation\=direct

This should return just the people who work on a project, and not both collaborators and owners.

GitHub states that:

affiliation | string | query
Filter collaborators returned by their affiliation. Can be one of:

  • outside: All outside collaborators of an organization-owned repository.
  • direct: All collaborators with permissions to an organization-owned repository, regardless of organization membership status.
  • all: All collaborators the authenticated user can see.

If it so happens that an owner is also a direct collaborator to this repo, then you have to do another request, where you check which person is the owner, and then filter them out from the collaborators request.


curl -H "Authorization: <bearer KEY>" \
     -H "Accept: application/vnd.github.v3+json" \
     -XGET https://api.github.com/orgs/:org/members\?role\=admin

With this request, you can check which are the owners of an organization.


If you fancy using the new graphql feature, then you can query for

{
  organization(login: "org_name") {
    id
    name
    repository(name: "repo_name") {
      collaborators(affiliation: DIRECT, first: N) {
        edges {
          permission
          node {
            name
          }
        }
      }
    }
  }
}

3
PDHide On

A owner is a collaborator by default so it will always return the owner , you can check for users with admin flag off if that is what you meant by owner

I am not sure how a repository could have two owners

Use the latest git api:

https://api.github.com/repos/{owner}/{repo}/collaborators

you can use below pre-requesst script in postman to print only non owners:

const postRequest = {
    url: ' https://api.github.com/orgs/<org>/members\role=admin',
    method: 'GET',
    header: {
        'Content-Type': 'application/json',
        'Accept': 'application/vnd.github.v3+json'
    },
    auth: {
        "type": "basic",
        "basic": [
            { "key": "username", "value": "<yourusername>" },
            { "key": "password", "value": "<yourtoken>" }
        ]
    }

};
pm.sendRequest(postRequest, (error, responseOwner) => {
    if (error) {
        console.log("THe errror is : " + JSON.stringify(error))
    } else {
        for (let i of pm.response.json()) {
            let found = 0;
            for (let j of responseOwner.json()) {
                i.login === j.login ? found = 1 : null
            }
            found === 1 ? null : console.log(i)
        }

    }
});