PyGithub icon indicating copy to clipboard operation
PyGithub copied to clipboard

How can you retrieve the description of a pull request associated with a commit?

Open atulkpatel opened this issue 2 years ago • 6 comments

I've tried the following;

commit = org.get_repo("my-repo").get_commit(sha="c2144da718afdbc343115e995f585d6d58c7ec6bd")
print(commit.author)
print(commit.committer)
print(commit.last_modified)

Which returns the details of the commit, however, can not find how to fetch the commit message. I thought it might be;

print(commit.get_comments())

But that doesn't retrieve the commit message.

Does anyone know if it's possible?

thanks in advance!

atulkpatel avatar Jun 17 '22 19:06 atulkpatel

@atulkpatel you can try this

print(commit.commit.message)

Ref: https://pygithub.readthedocs.io/en/latest/github_objects/GitCommit.html#github.GitCommit.GitCommit

hpsony94 avatar Sep 14 '22 09:09 hpsony94

Is there a way we can fetch commit description as well. commit.commit.message gives commit title but looking if there's a way to get description as well.

munisameera avatar Jan 25 '24 21:01 munisameera

Are you sure the commit your are looking at has a multi-line message? The first line is used as the title, all lines are the message.

Here is an example:

import github
gh = github.Github()
repo = gh.get_repo("PyGithub/PyGithub")
commit = repo.get_commit("e47c153bad20306add8585d36b89e71610ce01df")
print(commit.commit.message)

Provides the multi-line commit message: e47c153bad20306add8585d36b89e71610ce01df

Add missing branch protection fields (#2873)

Co-authored-by: jodelasur <[email protected]>
Co-authored-by: Terence Ho <[email protected]>
Co-authored-by: harry-unlikelyai <[email protected]>

EnricoMi avatar Jan 26 '24 09:01 EnricoMi

Sorry, I meant PR description. To elaborate my use case, I am looking for the Jira Issue ID in the PR associated with a commit sha. My team follows this process of adding the Jira ID in the PR description (and not in the commit message), so I was wondering if there is way to get the PR number from commit sha using PyGithub

munisameera avatar Jan 26 '24 16:01 munisameera

Try Commit.get_pulls to get associated pull requests, then PullRequest.body:

import github
gh = github.Github()
repo = gh.get_repo("PyGithub/PyGithub")
commit = repo.get_commit("e47c153bad20306add8585d36b89e71610ce01df")
for pull in commit.get_pulls():
    print(pull.body)

EnricoMi avatar Jan 27 '24 21:01 EnricoMi

Great, that woirks! thank you!

munisameera avatar Jan 28 '24 16:01 munisameera