How to get the modified file name with PullRequest

Hi All,

I given a pull request from one branch to other with one reviewer and reviewer approved it and he merged it.

In this context.

Here I able to get ll the pullrequest deatails i.e., ID,Title,Author,Reviewers,Source,Destination but

I need to capture the modified file name. i.e.,

How to get this file name. here .. I'm using RepositoryMergeRequestCheck, RepositorySettingsValidator in my class..

Thanks in advance

Anil

5 answers

Any information for this please..

Still looking for this...any suggestions..

Any idea for this , how to get the commited file name...

0 vote

Hi Anil,

I think you're looking for PullRequestService.streamChanges. That allows you to get all modified files, including the type of change (add, copy, rename, delete, modify).

Cheers,

Michael

Hi Michael ,

Thanks for the reply I'l implement this and let you know.

Same like this while commiting the file to my repository.

Example: git commit -m "some text" test.txt.

So in above case how to get the file name i.e., test.txt with some hook. pre/post hook.

Thanks

Anil

Stash pre/post hooks won't run until you push your changes to Stash. They won't run when you commit in your local clone.

In a pre/post hook (on the Stash server), you'll receive a list of RefChange objects, one for each branch that was updated in the push. If you want to get a list of changed paths for the branch in question, you can use CommitService.streamChanges

Hi Michael,

Thanks for the suggestion.

In My postReceiveHook I did like this

public void postReceive(RepositoryHookContext context, Collection<RefChange> refChanges){

for (RefChange refChange : refChanges) {

ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder( context.getRepository()).exclude(refChange.getFromHash()) .include(refChange.getToHash()).build();

final Page<Changeset> cs = commitService.getChangesetsBetween( request, PAGE_REQUEST);

for (Changeset changeset : cs.getValues()){

ChangesRequest changeReq = new ChangesRequest.Builder(context.getRepository(),changeset.getId()).build();

//Initialized this commitService in constructor

commitService.streamChanges(changeReq, new ChangeCallback() {

@Override

public void onStart() throws IOException {}

@Override

public void onEnd(boolean arg0) throws IOException {}

@Override

public boolean onChange(Change change) throws IOException {

//change.getPath().getName() , Here getting the filePath

call_method_to_update_to_ticket_portals(filePath);

return false;

}

});

}

Test1: Single file commit is worked.

$git commit -m "Ticket123 is fixed" test1.txt

$git push origin master

Test2: In my cloned repository, I have two files which were modified and committed.

$git commit -m "Ticket123 is fixed" test1.txt

$git commit -m "Ticket123 is fixed" test2.txt

$git push origin master

Here I'm getting the filePath name test1.txt only, but I should get both the filenames.

I suspect the problem with the above code of,

ChangesRequest changeReq = new

ChangesRequest.Builder(context.getRepository(),changeset.getId()).build();

Here, how can I get all the local committed files.

How to use untilId and sinceId above.

I tried,

ChangesRequest changeReq = newChangesRequest.Builder(context.getRepository(),

changeset.getId()).sinceId(refChange.fromHash()).build();

then also its showing me only one file name.

~Anil

Any suggestion for above..

Anil,

In your code example, you're doing two loops: one to iterate over the commits and the second loop to iterate over the changes for each commit. With your example commits, you should be seeing test1.txt on the first commit and test2.txt on the second commit. Perhaps you haven't debugged all the way through the first loop?

You don't need to loop over the commits though. You can directly call streamChanges, which will be more efficient anyway:

public void postReceive(RepositoryHookContext context, Collection&lt;RefChange&gt; refChanges){
    
    for (RefChange refChange : refChanges) {

        ChangesRequest changeReq = new ChangesRequest.Builder( context.getRepository(), refChange.getToHash())
            .sinceId(refChange.getFromHash())
            .build();

        commitService.streamChanges(changeReq, yourCallbackHere);
}

Hi Michael,

I'm looping the commits because, Earlier I'm checking the commits with valid ticket (checking this in my pre-hook) and based on the ticket number I'm updating the ticket portals with commi_message,commit_id,updaed_date...etc., (ticket portals updation I'm doing in post-hook)

Here I want to update the file name so I used the streamChanges..

I think with your suggestion,

ChangesRequest changeReq = new ChangesRequest.Builder( context.getRepository(), refChange.getToHash())
.sinceId(refChange.getFromHash())
.build();
commitService.streamChanges(changeReq, yourCallbackHere);
I can get the all my required commit related detalis now. I'l check now.
And I'm using the call back like this, Is it ok?..

commitService.streamChanges(changeReq, new ChangeCallback() {

@Override

public void onStart() throws IOException {}

@Override

public void onEnd(boolean arg0) throws IOException {}

@Override

public boolean onChange(Change change) throws IOException {

//change.getPath().getName() , Here getting the filePath

call_method_to_update_to_ticket_portals(filePath);

return false;

}

});

}

I debugged it before the callback got called one time. Though I have made modifications in many files in a single Commit.
My doubt is new ChangeCallback() { } is right way to use the call back or not...

Thanks for the suggestion. I'm debugging again for my issue.
Regards
Anil

I'd probably split things a bit differently. I'd use a pre-hook to detect whether your commits have valid ticket numbers. I'd then use a post-hook to update the tickets portal. After all, you won't know for sure that the push has been accepted until the post-hook is called. Even if _your_ pre-hook doesn't reject the push, another hook might and you'd end up with inconsistent state in your tickets portal?

Michael

In fact I'm also doing the same.

I'm validating the ticket validation in commited message with my pre-hook. If not It will reject.

In post-hook I'm trying to update to portals. Inorder to get the ticket number I traverse through the commits in loop.

But, with commmitService#streamChanges( request, mycallback)..

here my call back calling only once..But i'm having mutiple changes(more files) in single commit. how to get all the files details..

Anil

Hi Michael,

Observed that my callback not calling as per the changes.

What I'm doing now to read the commits inorder to get the ticket number to which it should update and get all the changes (All files) is :

In My post hook:

postReceive(){

for (RefChange refChange : refChanges) {

ChangesetsBetweenRequest request = new ChangesetsBetweenRequest.Builder( context.getRepository()).exclude(refChange.getFromHash()) .include(refChange.getToHash()).build();

final Page<Changeset> csComm = commitService.getChangesetsBetween( request, PAGE_REQUEST);

for (Changeset changeset : csComm .getValues()){

//FETCHING COMMIT RELATED DETAILS HERE

}

ChangesRequest changeReq = new ChangesRequest.Builder(context.getRepository(),refChange.getToHash()).sinceId(refChange.getFromHash()).build();

final Page<Change> csChange = commitService.getChanges(changeReq, PAGE_REQUEST);

for (Change change : csChange .getValues()) {

//Fetching all the files here, change.getPath().getName());

}

}//post receive end

Is it fine. I checked it it given me all the files which I changed but I suspect the efficiency.

Anil

Now I able to get the file path with commitservice, The problem is in my callback's onChange() method I did return false.

public boolean onChange(Change change) throws IOException {

//change.getPath().getName() , Here getting the filePath

call_method_to_update_to_ticket_portals(filePath);

return false;

}

Below thread helped me to correct it.

https://answers.atlassian.com/questions/295383/check-changed-files-in-pull-request

Thanks all

Anil

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Confluence

Color tables for a shiny Confluence page

...; ## Developed by: Alana Fernando ## Shared with love ## @param style:title=style type|type=enum|required=true|desc=Choose a style.|enumValues=Style1,Style2,Style3,Style4,Style5 ## @param alignment:title...

143 views 8 11
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you