Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sha1 step to hash files #32

Merged
merged 3 commits into from
Oct 9, 2017
Merged

Add support for sha1 step to hash files #32

merged 3 commits into from
Oct 9, 2017

Conversation

emanuelez
Copy link
Member

This can be useful under many circumstances, in particular when building docker images containing some kind of cache for a package manager.

@emanuelez
Copy link
Member Author

I guess this plugins is maintained by Cloudbees employees so I imagine it must be reviewed by them as well. So...

cc @reviewbybees

@ghost
Copy link

ghost commented Oct 5, 2017

This pull request originates from a CloudBees employee. At CloudBees, we require that all pull requests be reviewed by other CloudBees employees before we seek to have the change accepted. If you want to learn more about our process please see this explanation.

return filePath.act(new ComputeSha1());
}

private class ComputeSha1 implements FilePath.FileCallable<String> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use MasterToSlaveFileCallable instead.

private class ComputeSha1 implements FilePath.FileCallable<String> {
@Override
public String invoke(File file, VirtualChannel virtualChannel) throws IOException, InterruptedException {
HashFunction hf = Hashing.sha1();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get a bit nervous when I see google libraries used in Jenkins code :) back-compat issues, remoting problems and what not that has been happening in the past.
Might not be an issue here, but isn't there a perfectly good api for this in the standard library?

@Override
public String invoke(File file, VirtualChannel virtualChannel) throws IOException, InterruptedException {
HashFunction hf = Hashing.sha1();
HashCode hc = hf.newHasher().putBytes(Files.toByteArray(file)).hash();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should check that the file exists and is a file before trying to read it and provide a usefull error message if it isn't.

/*
* The MIT License (MIT)
*
* Copyright (c) 2017 CloudBees Inc.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I miss a mail welcoming you to CloudBees :D if not then you can't claim copyright for anyone but yourself or the entity you are doing the work for ;)

/**
* Tests {@link TouchStep}.
*
* @author Robert Sandell &lt;rsandell@cloudbees.com&gt;.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I wrote this

return null;
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will just end up in the agent log where no one will find it. Probably better to just let the exception pass through all the way up to run since it should happen very rarely.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature of invoke only allows IOException and InterruptedException. Should I wrap NoSuchAlgorithmException with one of those or into a runtime exception?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, wrapping it in an IOException would be acceptable I guess :)

@emanuelez
Copy link
Member Author

Better?

try {
return sha1(file);
} catch (NoSuchAlgorithmException e) {
throw new IOException(e.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well it's cyustomary to provide the original exception as the cause of the new one, but I'll allow it :)

@rsandell rsandell merged commit 55da91e into master Oct 9, 2017
@rsandell rsandell deleted the sha1-step branch October 9, 2017 09:35
@rsandell
Copy link
Member

rsandell commented Oct 9, 2017

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants