Getting Github individual file contributors - python

I am planning to build a plug-in for Sphinx documentation system plug-in which shows the names and Github profile links of the persons who have contributed to the documentation page.
Github has this feature internally
Is it possible to get Github profile links of the file contributors through Github API? Note that commiter emails are not enough, one must be able to map them to a Github user profile link. Also note that I don't want all repository contributors - just individual file contributors.
If this is not possible then what kind of alternative methods (private API, scraping) you could suggest to extract this information from Github?

First, you can show the commits for a given file:
https://api.github.com/repos/:owner/:repo/commits?path=PATH_TO_FILE
For instance:
https://api.github.com/repos/git/git/commits?path=README
Second, that JSON response does, in the author section, contain an url filed named 'html_url' to the GitHub profile:
"author": {
"login": "gitster",
"id": 54884,
"avatar_url": "https://0.gravatar.com/avatar/750680c9dcc7d0be3ca83464a0da49d8?d=https%3A%2F%2Fidenticons.github.com%2Ff8e73a1fe6b3a5565851969c2cb234a7.png",
"gravatar_id": "750680c9dcc7d0be3ca83464a0da49d8",
"url": "https://api.github.com/users/gitster",
"html_url": "https://github.com/gitster", <==========
"followers_url": "https://api.github.com/users/gitster/followers",
"following_url": "https://api.github.com/users/gitster/following{/other_user}",
"gists_url": "https://api.github.com/users/gitster/gists{/gist_id}",
"starred_url": "https://api.github.com/users/gitster/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/gitster/subscriptions",
"organizations_url": "https://api.github.com/users/gitster/orgs",
"repos_url": "https://api.github.com/users/gitster/repos",
"events_url": "https://api.github.com/users/gitster/events{/privacy}",
"received_events_url": "https://api.github.com/users/gitster/received_events",
"type": "User"
},
So you shouldn't need to scrape any web page here.
Here is a very crude jsfiddle to illustrate that, based on the javascript extract:
var url = "https://api.github.com/repos/git/git/commits?path=" + filename
$.getJSON(url, function(data) {
var twitterList = $("<ul />");
$.each(data, function(index, item) {
if(item.author) {
$("<li />", {
"text": item.author.html_url
}).appendTo(twitterList);
}
});

Using GraphQL API v4, you can use :
{
repository(owner: "torvalds", name: "linux") {
object(expression: "master") {
... on Commit {
history(first: 100, path: "MAINTAINERS") {
nodes {
author {
email
name
user {
email
name
avatarUrl
login
url
}
}
}
}
}
}
}
}
Try it in the explorer
Using curl & jq to have a list of the first 100 contributors of this file without duplicates :
TOKEN=<YOUR_TOKEN>
OWNER=torvalds
REPO=linux
BRANCH=master
FILEPATH=MAINTAINERS
curl -s -H "Authorization: token $TOKEN" \
-H "Content-Type:application/json" \
-d '{
"query": "{repository(owner: \"'"$OWNER"'\", name: \"'"$REPO"'\") {object(expression: \"'"$BRANCH"'\") { ... on Commit { history(first: 100, path: \"'"$FILEPATH"'\") { nodes { author { email name user { email name avatarUrl login url}}}}}}}}"
}' https://api.github.com/graphql | \
jq '[.data.repository.object.history.nodes[].author| {name,email}]|unique'

Why do you need to use Github API for that? You can just clone the package and use git log:
git log --format=format:%an path/to/file ver1..ver2 |sort |uniq

Until and unless it is not necessary to interact with GITHUB API directly one can get the list of contributors by cloning the repo down and then getting into the cloned directory and then getting the list from the github log file using shortlog command
import os
import commands
cmd = "git shortlog -s -n"
os.chdir("C:\Users\DhruvOhri\Documents\COMP 6411\pygithub3-0.3")
os.system("git clone https://github.com/poise/python.git")
os.chdir("/home/d/d_ohri/Desktop/python")
output = commands.getoutput(cmd)
print(output)
raw_input("press enter to continue")
There is one more way to list contributors in case one wants to use GITHUB API, we can use pytgithub3 wrapper to interact with GITHUB API and get list of contributors as follows using list_contributors:
from pytgithub3.services.repo import Repo
r = Repo()
r.lis_contributors(user='userid/author',repo='repo name')
for page in r:
for result in page:
print result

Related

How to add permission can_get_all_acc_detail to an account of existing blockchain network on hyperledger-iroha?

I want to add account, which has some information readable by all users. According to documentation the user needs to have permissions can_get_all_acc_detail. So I'm trying to add those with creating new role:
tx = self.iroha.transaction([
self.iroha.command('CreateRole', role_name='info', permissions=[primitive_pb2.can_get_all_acc_detail])
])
tx = IrohaCrypto.sign_transaction(tx, account_private_key)
net.send_tx(tx)
Unfortunately after sending transaction I see status:
status_name:ENOUGH_SIGNATURES_COLLECTED, status_code:9, error_code:0(OK)
But then it is taking 5 minutes until timeout.
I've notices that transaction json has different way of embedding permissions than in generic block:
payload {
reduced_payload {
commands {
create_role {
role_name: "info_account"
permissions: can_get_all_acc_detail
}
}
creator_account_id: "admin#example"
created_time: 1589408498074
quorum: 1
}
}
signatures {
public_key: "92f9f9e10ce34905636faff41404913802dfce9cd8c00e7879e8a72085309f4f"
signature: "568b69348aa0e9360ea1293efd895233cb5a211409067776a36e6647b973280d2d0d97a9146144b9894faeca572d240988976f0ed224c858664e76416a138901"
}
In compare in genesis.block it is:
{
"createRole": {
"roleName": "money_creator",
"permissions": [
"can_add_asset_qty",
"can_create_asset",
"can_receive",
"can_transfer"
]
}
},
I'm using iroha version 1.1.3 (but also tested on 1.1.1), python iroha sdh version is 0.0.5.5.
does the account you used to execute the 'Create Role' command have the "can_create_role" permission?

GitHub API: Enable Push Restrictions for branch

I am trying to disable and enable branch protections for a GitHub project in a Python script using the GitHub API (Version 2.11). More specifically I want to remove all push restrictions from a branch and later enable them with specific teams.
Replacing/adding existing team restrictions works via
PUT/POST /repos/:owner/:repo/branches/:branch/protection/restrictions/teams
And removing push restrictions also works like a charm using
DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions
But, if I remove the push restrictions, I have found no way how to enable it back again to add specific teams. If I try to add or replace teams, the message says 'Push restrictions not enabled'.
So how can I enable the checkbox Restrict who can push to this branch to add teams in a script? See screenshot for the desired outcome: Push Restrictions
The API documentation just presents me the options Get restrictions of protected branch and Remove restrictions of protected branch.
What I tried so far:
Just removing all teams without removing the restrictions does not work, because then nobody is able to push.
Sending PUT/POST to /repos/:owner/:repo/branches/:branch/protection/restrictions gives a 404.
Right now I have no other way than clicking the checkbox manually, then adding and replacing works via API.
Check Update branch protection section of Github API Rest :
PUT /repos/:owner/:repo/branches/:branch/protection
Using bash & curl :
ownerWithRepo="MyOrg/my-repo"
branch="master"
curl -X PUT \
-H 'Accept: application/vnd.github.luke-cage-preview+json' \
-H 'Authorization: Token YourToken' \
-d '{
"restrictions": {
"users": [
"bertrandmartel"
],
"teams": [
"my-team"
]
},
"required_status_checks": null,
"enforce_admins": null,
"required_pull_request_reviews": null
}' "https://api.github.com/repos/$ownerWithRepo/branches/$branch/protection"
Note that setting null to one of those fields will disable(uncheck) the feature
In python :
import requests
repo = 'MyOrg/my-repo'
branch = 'master'
access_token = 'YourToken'
r = requests.put(
'https://api.github.com/repos/{0}/branches/{1}/protection'.format(repo, branch),
headers = {
'Accept': 'application/vnd.github.luke-cage-preview+json',
'Authorization': 'Token {0}'.format(access_token)
},
json = {
"restrictions": {
"users": [
"bertrandmartel"
],
"teams": [
"my-team"
]
},
"required_status_checks": None,
"enforce_admins": None,
"required_pull_request_reviews": None
}
)
print(r.status_code)
print(r.json())
I am trying to add a teams to the protected branch.
Can I know whether my-team is
slug
name
should it include the owner of the team as well.
"teams": [
"my-team"
]
Thanks in advance for helping out.

Can't link new repositories to projects using Bitbucket's API

I'm building a shell application that allows my teammates to start new projects by running a few commands. It should be able to create a new project and a new repository inside that project.
Although I'm specifying the project key/uuid when creating a new repository, it doesn't work. What I'm expecting is a success message with the details for the new repository. Most of the time, this is what I get:
{"type": "error", "error": {"message": "string indices must be integers", "id": "ef4c2b1b49c74c7fbd557679a5dd0e58"}}
or the repository goes to the first project created for that team (which is the default behaviour when no project key/uuid is specified, according to Bitbucket's API documentation).
So I'm guessing there's something in between my request & their code receiving it? Because it looks like they're not even getting the request data.
# Setup Request Body
rb = {
"scm": "git",
"project": {
"key": "PROJECT_KEY_OR_UUID"
}
}
# Setup URL
url = "https://api.bitbucket.org/2.0/repositories/TEAM_NAME/REPOSITORY_NAME"
# Request
r = requests.post(url, data=rb)
In the code from the api docs you'll notice that the Content-Type header is "application/json".
$ curl -X POST -H "Content-Type: application/json" -d '{
"scm": "git",
"project": {
"key": "MARS"
}
}' https://api.bitbucket.org/2.0/repositories/teamsinspace/hablanding
In your code you're passing your data in the data parameter, which creates an "application/x-www-form-urlencoded" Content-Type header, and urlencodes your post data.
Instead, you should use the json parameter.
rb = {
"scm": "git",
"project": {
"key": "PROJECT_KEY_OR_UUID"
}
}
url = "https://api.bitbucket.org/2.0/repositories/TEAM_NAME/REPOSITORY_NAME"
r = requests.post(url, json=rb)

Get the emails of the stargazers for a repo in github using python

Actually trying to get the
users(stargazers) for a repo in github by using the github api
Emails of all the stargazers
So for the first point i had tried the below command
curl -i https://api.github.com/repos/my_username/my_repo_name/stargazers
result
I had got results in the form of json dict something like below
[
{
"login": "gazer_name",
"id": 17xxx,
"avatar_url": .........,
"gravatar_id": "....",
.......
.......
"type": "User"
}
{
"login": "hello_man",
"id": 18xxx,
"avatar_url": .........,
"gravatar_id": "....",
.......
.......
"type": "User"
}
......
......
]
So now according to the second point, i want to get the emails of all the users ?
so how can we do that with github api ? whether we need to login for sure in order to get the emails ?
Because we can get the user email by using the command GET /user/emails as described here
That means something like curl -u "my_user_name" https://api.github.com/user/emails only after logging in
And how to use GET /user/emails exactly because when i entered in to terminal as it is the result is nothing, but when i used authentication with curl like above its displaying emails, which indicates that login is required inorder to get user emails
So how can we get the emails of the stargazers ?

Retrieving most recent commit revision number from a github project

How do you retrieve the revision number for the most recent commit of a github project?
The APIv3 docs are a little vague, and only provide a partially URL that doesn't seem to work.
e.g. Does /repos/:user/:repo/commits correspond to https://www.github.com/repos/:user/:repo/commits? OR something else like https://www.github.com/api/v2/json/repos/:user/:repo/commits? Neither work for any combination of user and repo.
In git, you can only ask for the current commit on some given branch. Here's an example:
$ wget -q -O - https://api.github.com/repos/smarnach/pyexiftool/git/refs/heads/master
{
"ref": "refs/heads/master",
"url": "https://api.github.com/repos/smarnach/pyexiftool/git/refs/heads/master",
"object": {
"type": "commit",
"url": "https://api.github.com/repos/smarnach/pyexiftool/git/commits/7be4b9bb680521369f2ae3310b1f6de5d14d1f8b",
"sha": "7be4b9bb680521369f2ae3310b1f6de5d14d1f8b"
}
}

Categories

Resources