Created
November 30, 2023 13:50
-
-
Save wuriyanto48/b07341bf8a7bce1d2f56191c64f3f387 to your computer and use it in GitHub Desktop.
(Python) Gitlab REST API get commit by user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import ujson as json | |
header ={ | |
'PRIVATE-TOKEN': 'your_token' | |
} | |
def get_commits_by_user(project_id, email, since, until, branch): | |
json_loads_of_commit = [] | |
params = { | |
"since": since, | |
"until": until, | |
"ref_name": branch | |
} | |
url_p = "https://gitlab.mydomain.com/api/v4/projects/%s/repository/commits" % project_id | |
r = requests.get(url_p, params=params, headers=header) | |
print(r.url) | |
c = 0 | |
while r.status_code == 200: | |
jsLoad = json.loads(r.content) | |
newDate = jsLoad[-1]["committed_date"] | |
if (params["until"] == newDate): | |
break | |
user_commits = [] | |
for cm in jsLoad: | |
if cm["author_email"] == email: | |
user_commits.append(cm) | |
c += 1 | |
json_loads_of_commit.append(user_commits) | |
params["until"] = newDate | |
params["per_page"] = 100 | |
r = requests.get(url_p, params, headers=header) | |
print(len(json_loads_of_commit)) | |
print(json.dumps(json_loads_of_commit)) | |
if __name__ == '__main__': | |
since = "2023-11-01T00:00:42.000+01:00" | |
until = "2023-12-30T00:00:42.000+01:00" | |
get_commits_by_user('20000', '[email protected]', since, until, "development") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment