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

Digger minimal node client backbone #1

Merged
merged 9 commits into from Dec 5, 2016
Merged

Conversation

wtrocki
Copy link
Contributor

@wtrocki wtrocki commented Dec 3, 2016

Minimal example for digger node.js client
Supported methods:

  • login
  • job creating
  • triggering build
  • fetching logs

Review @aliok @matzew

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 3, 2016

module.exports = (auth, jobArgs, callback) => {
var options = authHelper.createJenkinsOptions(auth.url, auth.user, auth.password);
var jenkins = require('jenkins')(options);
var fileLocation = path.join(__dirname, '..', '..', 'templates', 'standard.xml');
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need a Jenkins XML / template file here ?

Copy link
Contributor Author

@wtrocki wtrocki Dec 5, 2016

Choose a reason for hiding this comment

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

That's just plain template to create job. We using jenkinsfiles but we would need some simple xml file to create job that would have only giturl and branch configured. Rest would come from Jenkinsfile itself.

I investigated Jenkinfile's automatic job creation and it's really powerful, but for our internal needs we would need to create job explicitly.

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

I am getting this, locally w/ the client:

➜  digger-node git:(31eba05) ✗ node ./bin/digkins.js login https://jenkins-digger.osm3.feedhenry.net
Username: admin
Password: ****************
bin/digkins.js login <url> [user] [password]

Options:
  --version   Show version number                                      [boolean]
  -h, --help  Show help                                                [boolean]

auth.createJenkinsOptions is not a function

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

node --version

v6.9.1

@wtrocki do I need different node version ?

Create jenkins job for git repository with Jenkinsfile
digkins job create <name> [repository] [branch]

Setup jenkins credetials and login into jenkins

Choose a reason for hiding this comment

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

credetials -> credentials

name: jobArgs.name,
xml: jobXML
}
jenkins.job.create(options, function (err, data) {

Choose a reason for hiding this comment

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

I'd suggest using arrow function instead, for consistency

*/
module.exports = (auth, jobArgs, callback) => {
var options = authHelper.createJenkinsOptions(auth.url, auth.user, auth.password);
var jenkins = require('jenkins')(options);

Choose a reason for hiding this comment

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

IMHO I would avoid using require anywhere but at the top of the file so that you can easily identify what modules this file depends on.

callback(err, data);
});
} else {
callback("Standard template is missing!");

Choose a reason for hiding this comment

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

Returning a promise instead of using a callback would increase readability and simplicity. I strongly recommend using them from the beginning, specially when you can use node built-in promises.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@josemigallas Let's not start flame wars here.
We had team wide discussions about how we going to use ES2016 and what rules we should have. All of this is here: https://github.com/fheng/best_practice
I'm going to add eslint support soon.

Choose a reason for hiding this comment

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

No flame wars at all, just my humble suggestion, don't get me wrong. I read already that repo and it's said nothing about the preferred use of callback/Promise. However, from the last discussion I understood that Promises are preferred.

@@ -0,0 +1,14 @@
var auth = require("../util/auth")

// Retrieve jenkins server information

Choose a reason for hiding this comment

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

Just an observation: the other doc is written with block format (/** ... */) but here is line format (// ...)

@@ -0,0 +1,7 @@
exports.command = 'job <command>'
exports.desc = 'Manage jenkins jobs'
exports.builder = function (yargs) {

Choose a reason for hiding this comment

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

Again, here is a clearer example that the code cleanness would benefit from using arrow functions.

exports.command = 'build <job>'
exports.describe = 'Trigger build for Jenkins job'
exports.builder = (yargs) => {
return yargs.count('verbose').alias('v', 'verbose');

Choose a reason for hiding this comment

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

IMO this would looks utterly better on a single line without return.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That should be part of the javascript styling tool - something like eslint. Going to add this soon. Any manual styling are hard to enforce and I do not want to make changes for this now until we would have automatic process in place that would fail the build.

Copy link

Choose a reason for hiding this comment

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

What I mean is that it should be
yargs => yargs.count('verbose').alias('v', 'verbose');

}
return;
}
logger.info(`Job ${argv.name} created!`);

Choose a reason for hiding this comment

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

This indentation is one step further making it less readable.

@@ -0,0 +1,14 @@
var authUtil = require("../util/auth")

// Retrieve jenkins server information

Choose a reason for hiding this comment

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

Here the doc is written in single line format (// ...) but in the other files is with block format (/** ... */) I suggest using always the block format.

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 5, 2016

@matzew

No shrinkwrap used because I'm using fixed versions in package.json. So we should not have shrinkwrap here and possibly no shrinkwrap in any other projects.

Good point about node version. I would mark that we support node>=4 in package.json

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

Using this GH project:
https://github.com/wtrocki/helloworld-android-gradle

returns in this error:

node ./bin/digkins.js log matzew-android 94
## Starting streaming build 1 log
Started by user Jenkins Admin
Cloning the remote Git repository
Cloning repository https://github.com/wtrocki/helloworld-android-gradle
 > git init /var/lib/jenkins/jobs/matzew-android/workspace@script # timeout=10
Fetching upstream changes from https://github.com/wtrocki/helloworld-android-gradle
 > git --version # timeout=10
 > git fetch --tags --progress https://github.com/wtrocki/helloworld-android-gradle +refs/heads/*:refs/remotes/origin/*
 > git config remote.origin.url https://github.com/wtrocki/helloworld-android-gradle # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/wtrocki/helloworld-android-gradle # timeout=10
Fetching upstream changes from https://github.com/wtrocki/helloworld-android-gradle
 > git fetch --tags --progress https://github.com/wtrocki/helloworld-android-gradle +refs/heads/*:refs/remotes/origin/*
Seen branch in repository origin/Android-N
Seen branch in repository origin/FH-v3.10.0
Seen branch in repository origin/FH-v3.10.1
Seen branch in repository origin/FH-v3.11.0
Seen branch in repository origin/FH-v3.11.1
Seen branch in repository origin/FH-v3.12.0
Seen branch in repository origin/FH-v3.13.0
Seen branch in repository origin/FH-v3.13.1
Seen branch in repository origin/FH-v3.14.0
Seen branch in repository origin/FH-v3.15.0
Seen branch in repository origin/FH-v3.7.0
Seen branch in repository origin/FH-v3.8.0
Seen branch in repository origin/FH-v3.8.1
Seen branch in repository origin/FH-v3.9.0
Seen branch in repository origin/FH-v3.9.1
Seen branch in repository origin/RHMAP-3243_Forms-App-PDF-CSV-Export
Seen branch in repository origin/master
Seen branch in repository origin/revert-16-Android-N
Seen branch in repository origin/update
Seen 19 remote branches
Multiple candidate revisions
Checking out Revision 51406d8f429d4c9f31057314284273c0ab02955c (origin/Android-N)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 51406d8f429d4c9f31057314284273c0ab02955c
First time build. Skipping changelog.
ERROR: /var/lib/jenkins/jobs/matzew-android/workspace@script/Jenkinsfile not found
Finished: FAILURE

So, I could fetch the log file, with the node client (yay), but there is something wrong w/ the client ?

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 5, 2016

@matzew - Tested, it works fine.

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

COOL

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

@josemigallas can you test this too, before we merge it ?

Feel free to test against OSM3, shared cluster

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

I trriggered a build, and did fetch the logs:

 node ./bin/digkins.js log matzew-android 97

First time: worked good

Now, running that above comment a second time, to fetch the logfile again, I am getting:

error: Cannot fetch build log for matzew-android

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

@wtrocki Not sure I get what's the number (97 in this case) means.

the build job on Jenkins itself is #3:
https://jenkins-digger.osm3.feedhenry.net/job/matzew-android/3/

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 5, 2016

@matzew When triggering build it's placed in the queue (there is no build number initially because build may wait a little for possible executors). 97 is just jenkins queue number that would allow you to query your request all the time. Later when your build would start you get this '#3' number and you can use it as well, but 97 number is easyer from the api point of view because you do not need to pull jenkins until job would be scheduled.

@matzew
Copy link
Contributor

matzew commented Dec 5, 2016

node ./bin/digkins.js log matzew-android 3 gives me no log as well

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 5, 2016

@matzew Only queue items are supported for the moment. I'm going to investigate this once I would deal with other comments. Thanks for really good review guys!

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 5, 2016

@matzew I replicated problem with logging and it looks like issue with jenkins. Some kind of variation of: https://issues.jenkins-ci.org/browse/JENKINS-27256. Jenkins removes queue item after a while, which is against their documentation. Going to check if the most recent versions have this as well and if we cannot fix it then I would migrate to different api. This looks like problem that can be resolved on jenkins rather than on client.

I have resolved all the comments. Going to merge that now as we definitely going to rewrite this after we would support different platforms.

@wtrocki wtrocki merged commit 48a0558 into aerogear:master Dec 5, 2016
@wtrocki wtrocki deleted the backbone branch December 5, 2016 18:46
@aliok
Copy link
Member

aliok commented Dec 6, 2016

@wtrocki number 97 in @matzew 's case is not related to the job. So, the call could be

node ./bin/digkins.js log 103

or

node ./bin/digkins.js log matzew-android 3

Fetching logs didn't work in my trial as well.

@wtrocki
Copy link
Contributor Author

wtrocki commented Dec 6, 2016

@aliok This was just client backbone and I explained wht this fails and how we going to resolve it. Looks like queue item is disappearing shortly after work is build, so we would need to wait until work would be queued to build (this can take time depending on other jobs running) and then use job number to retrieve job details. We can clearly inspire here by other clients like nestor:
https://github.com/cliffano/nestor

@@ -0,0 +1,5 @@
const Configstore = require('configstore');
Copy link
Member

Choose a reason for hiding this comment

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

+1 on configstore. didn't know that one, looks great.

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.

None yet

4 participants