How to get contents of file in pull request from pygithub? - github

I'm not able to find how to get contents of file present in pull request on github. Is there any way to do this using pygithub?
For repository, we can do this using
contents = repo.get_contents(filename)
However, I did not find such api for pull request object. Is there any way to do this?

I have found a nice workaround to this. We can not get contents from File objects, however, it is possible to get them from repository, at any version. I did this like following :
This works irrespective of PR is open/closed/merged.
(1) Grab commits from PR.
(2) Grab files from commits.
(3) Use Repository object to get the contents of file at reference corresponding the sha of commit in PR.
Example :
github = Github(login_or_token=my_github_token)
repo = github.get_repo(repo_name, lazy=False)
# Grab PR
pull_number = 20
pr = repo.get_pull(pull_number)
commits = pr.get_commits()
for commit in commits:
files = commit.files
for file in files:
filename = file.filename
contents = repo.get_contents(filename, ref=commit.sha).decoded_content
# Now do whatever you want to do with contents :)

Take a look at this: https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files
Have not tried, but pygithub does have a method for this called get_files to use this API call: https://pygithub.readthedocs.io/en/latest/github_objects/PullRequest.html#github.PullRequest.PullRequest.get_files
EDIT: Using requests:
import requests
username = 'astrochun'
repo = 'Zcalbase_gal'
pr = 84
response = requests.get(f"https://api.github.com/repos/{username}/{repo}/pulls/{pr}/files")
dict0 = response.json()
pr_files = [elem['filename'] for elem in dict0]

Related

how can I pass filelist of a build to mailnotifier in BuildBot?

I use Perforce as CVS and plan to send mail when builds finish. In a buildstep, I can use self.build.allFiles() to get the filelist but I couldn't figure out a way to do so in the mailnotifier.
github issue
You can use the DataConnector class to achieve that.
changes = yield self.master.data.get("builds", buildid, "changes")
files = []
for c in changes:
files.extend(c['files'])

how to get a list of commits from refChanges in Atlassian Stash Pre Receive Repository Hook

Im trying to write a stash plugin that will iterate through the commits in a change set pushed to stash in a Pre Receive Repository Hook.
The API passes a Collection of refChange in the onReceive method.
public boolean onReceive(RepositoryHookContext context, Collection<RefChange> refChanges, HookResponse hookResponse)
if I make 3 commits then push I get one RefChange which looks like this
refId = refs/heads/master
fromHash = ded3e4583653f14892cc3e8a898ba74ee75e1a58 // First Commit in change set
toHash = ae017dcdadf7ca69617fb05f6905cccfe2aa4229 // Most recent commit
type = "UPDATE"
Id like to get a collection of all the commits so that I can get all the commit messages.
I'm looking at com.atlassian.stash.commit.CommitService getCommit and getCommits. I think I need to getCommitsBetween but can't quite figure out how to crate the GetCommitsBetween parameter needed from the RefChange I have.
Am I even heading down the right path here?
Even though the CommitsBetweenRequest page on the Atlassian Stash API documentation is one of the few pages with an explanation, it took some trial and error to figure this out. GetCommitsBetween works but here's the trick...
Set the commitsBetweenBuilder.exclude to the starting commit in the change set and commitsBetweenBuilder.include to the ending commit hash.
CommitsBetweenRequest.Builder commitsBetweenBuilder = new CommitsBetweenRequest.Builder(context.getRepository() );
commitsBetweenBuilder.exclude(refChange.getFromHash()); //Starting with
commitsBetweenBuilder.include(refChange.getToHash()); // ending with
PageRequest pageRequest = new PageRequestImpl(0,6);
Page<Commit> commits = commitService.getCommitsBetween(commitsBetweenBuilder.build(), pageRequest);
//TODO: handle Pages
for (Commit commit : commits.getValues()) {
hookResponse.out().println("Message = " + commit.getMessage() + "\n");
}
I wasn't able to get the dependency injection working for the CommitService. Spring for some reason wasn't able to find it, when trying to run in locally ???
I did getting it working using the component locator.
CommitService commitService = ComponentLocator.getComponent(CommitService.class);

Commit a whole folder with modified files in it with SharpSvn.

I am using SharpSvn in my C# project. I am having a folder with some text files in it and another folders with subfolders in it. I am adding the folder under version control and commit it. So far, so good. This is my code:
using (SvnClient client = new SvnClient())
{
SvnCommitArgs ca = new SvnCommitArgs();
SvnStatusArgs sa = new SvnStatusArgs();
sa.Depth = SvnDepth.Empty;
Collection<SvnStatusEventArgs> statuses;
client.GetStatus(pathsConfigurator.FranchisePath, sa, out statuses);
if (statuses.Count == 1)
{
if (!statuses[0].Versioned)
{
client.Add(pathsConfigurator.FranchisePath);
ca.LogMessage = "Added";
client.Commit(pathsConfigurator.FranchisePath, ca);
}
else if (statuses[0].Modified)
{
ca.LogMessage = "Modified";
client.Commit(pathsConfigurator.FranchisePath, ca);
}
}
}
I make some changes to one of the text files and then run the code againg. The modification is not committed. This condition is false:
if (statuses.Count == 1)
and all the logic in the if block does not execute.
I have not written this logic and cannot quite get this lines of code:
client.GetStatus(pathsConfigurator.FranchisePath, sa, out statuses);
if (statuses.Count == 1) {}
I went on the oficial site of the API but couldn`t find documentation about this.
Can someone that is more familiar with this API tell what these two lines do ?
What changes need to be done to this code so if some of the contents of the pathsConfigurator.FranchisePath folder are changed, the whole folder with the changes to be commited. Any suggestions with working example will be greatly appreciated.
Committing one directory with everything inside is pretty easy: Just call commit on that directory.
The default Depth of commit is SvnDepth.Infinity so that would work directly. You can set additional options by providing a SvnCommitArgs object to SvnClient.Commit()

sharpsvn search repository commits

Using sharpsvn I want to search all of the comments on the commited files in our svn server for specific strings.
but i cannot get this to work. I have tried the following but it does not like my target path.
Error: Revision type requires a working copy path, not a URL
SvnRevisionRange range = new SvnRevisionRange(SvnRevision.Working, SvnRevision.Zero);
string targetPath = "http://********:81/svn/";
Collection<SvnLogEventArgs> items;
SvnLogArgs args = new SvnLogArgs { Range = range };
client.GetLog(new Uri(targetPath), args, out items);
any ideas would be great
This is because you used:
new SvnRevisionRange(SvnRevision.Working, SvnRevision.Zero)
It can't determine the revision number of your working copy when you just pass a URL.
Try SvnRevision.Head to get the latest version in the repository instead

How can I Diff a Svn Repository using SharpSvn

My question is quite simple and with the SharpSvn Api, it should be easy as well. Here what I did:
path = "c:\project";
using (SvnLookClient client = new SvnLookClient())
{
SvnLookOrigin o = new SvnLookOrigin(path);
Collection<SvnChangedEventArgs> changeList;
client.GetChanged(o, out changeList); // <-- Exception
}
and when I call the GetChanged, I get an exception:
Can't open file 'c:\project\format': The system cannot find the file specified.
So, Maybe there is something I'm missing? Or maybe it's not the right way to do find out the list of files and folders that were modified in the local repository?
Thanks in advance.
The SvnLookClient class in SharpSvn is the equivalent to the 'svnlook' console application. It is a low level tool that enables repository hooks to look into specific transactions of a repository using direct file access.
You probably want to use the SvnClient class to look at a WorkingCopy and most likely its Status() or in some cases simpler GetStatus() function to see what changed.
The path that the SvnLookOrigin constructor wants is actually:
path = "c:\project\.svn\";
That is, it wants that special ".svn" directory not just the root of where the source is checked out to.
Although you probably do want to listen to Bert and do something like:
path = "c:\project";
using (SvnLookClient client = new SvnLookClient())
{
SvnLookOrigin o = new SvnLookOrigin(path);
Collection<SvnChangedEventArgs> changeList;
client.GetStatus(o, out changeList); // Should now return the differences between this working copy and the remote status.
}