Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

read unchanged file from head with pre-receive hook

Jens Bergman
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 16, 2018

So I want to have a file e.g. "whitelisted-files.txt" which contains the file-names jenkins are allowed to change. I´m trying to create a pre-receive hook that reads the latest version of "whitelisted-files.txt" from the branch that jenkins are pushing to. After reading I will compare the file-names to the files changed in the commit. To be clear, I don´t want to read the files being committed. 

 

Is that possible? I guess there is no file-path where the file lies since it can exist in multiple branches and multiple versions in each branch? All help appreciated :)

1 answer

1 accepted

0 votes
Answer accepted
Jens Bergman
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 16, 2018

I solved it myself:
ByteArrayOutputStream out = new ByteArrayOutputStream();
contentService.streamFile(repository, branch, pathToFile, (s) -> out);
try {
String content = out.toString(StandardCharsets.UTF_8.toString());
System.out.println("content: " + content);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

Where contentService is initalized from the constructor and repository is from the RepositoryHookContext-class. https://docs.atlassian.com/DAC/javadoc/stash/3.11.3/api/reference/com/atlassian/stash/content/ContentService.html#streamFile%28com.atlassian.stash.repository.Repository,%20java.lang.String,%20java.lang.String,%20com.atlassian.stash.util.PageRequest,%20boolean,%20com.atlassian.stash.content.FileContentCallback%29 

 

Hopefully the answer can be useful for someone :) 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events