Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to get the modified file name with PullRequest

anil kumar April 7, 2014

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

0 votes
anil kumar May 25, 2014

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

0 votes
Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 18, 2014

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

anil kumar May 19, 2014

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

Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 19, 2014

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

anil kumar May 21, 2014

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

anil kumar May 22, 2014

Any suggestion for above..

Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 22, 2014

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);
}

anil kumar May 22, 2014

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

Michael Heemskerk
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 22, 2014

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

anil kumar May 22, 2014

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

anil kumar May 22, 2014

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

0 votes
anil kumar May 18, 2014

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

0 votes
anil kumar April 10, 2014

Still looking for this...any suggestions..

0 votes
anil kumar April 9, 2014

Any information for this please..

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events