-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
const FormData = require('form-data'); | ||
|
||
const { | ||
CI_DOMAIN, | ||
CI_TYPES, | ||
CI_TYPES_KEYS | ||
} = require('./ci_type_parser'); | ||
|
||
const CI_CRUMB_URL = `https://${CI_DOMAIN}/crumbIssuer/api/json`; | ||
const CI_PR_NAME = CI_TYPES.get(CI_TYPES_KEYS.PR).jobName; | ||
const CI_PR_URL = `https://${CI_DOMAIN}/job/${CI_PR_NAME}/build`; | ||
|
||
class RunPRJob { | ||
constructor(cli, request, owner, repo, prid) { | ||
this.cli = cli; | ||
this.request = request; | ||
this.owner = owner; | ||
this.repo = repo; | ||
this.prid = prid; | ||
} | ||
|
||
async getCrumb() { | ||
try { | ||
const { crumb } = await this.request.json(CI_CRUMB_URL); | ||
return crumb; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
get payload() { | ||
const payload = new FormData(); | ||
payload.append('json', JSON.stringify({ | ||
parameter: [ | ||
{ name: 'CERTIFY_SAFE', value: 'on' }, | ||
{ name: 'TARGET_GITHUB_ORG', value: this.owner }, | ||
{ name: 'TARGET_REPO_NAME', value: this.repo }, | ||
{ name: 'PR_ID', value: this.prid }, | ||
{ name: 'REBASE_ONTO', value: '<pr base branch>' }, | ||
{ name: 'DESCRIPTION_SETTER_DESCRIPTION', value: '' } | ||
] | ||
})); | ||
return payload; | ||
} | ||
|
||
async start() { | ||
const { cli } = this; | ||
cli.startSpinner('Validating Jenkins credentials'); | ||
const crumb = await this.getCrumb(); | ||
|
||
if (crumb === false) { | ||
cli.stopSpinner('Jenkins credentials invalid', | ||
this.cli.SPINNER_STATUS.FAILED); | ||
return false; | ||
} | ||
cli.stopSpinner('Jenkins credentials valid'); | ||
|
||
try { | ||
cli.startSpinner('Starting PR CI job'); | ||
await this.request.text(CI_PR_URL, { | ||
method: 'POST', | ||
headers: { | ||
'Jenkins-Crumb': crumb | ||
}, | ||
body: this.payload | ||
}); | ||
cli.stopSpinner('PR CI job successfully started'); | ||
} catch (err) { | ||
cli.stopSpinner('Failed to start CI', this.cli.SPINNER_STATUS.FAILED); | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
module.exports = { RunPRJob }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters