Skip to content

Commit

Permalink
feat(2437): Add method to get read-only flag (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Jun 11, 2021
1 parent 53191fb commit 559cbb4
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 39 deletions.
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,24 @@ The object consisting of PR comment ID, create time, and username.
No parameters are required.

#### Expected Outcome
The array of scm context name (e.g. [github:github.com, gitlab:my-gitlab])
The array of scm context names (e.g. [github:github.com, gitlab:my-gitlab])

#### Expected Response
1. The array of scm context name
1. The array of scm context names

### getScmContext
The parameters required are:

| Parameter | Type | Required | Description |
| :------------- | :---- | :------- | :-------------|
| config | Object | Yes | Configuration Object |
| config.hostname | String | Yes | The scm host name (ex: `github.com`) |

#### Expected Outcome
The matching scm context name string (e.g. github:github.com)

#### Expected Response
1. The matching scm context name

### canHandleWebhook
The parameters required are:
Expand Down Expand Up @@ -508,6 +522,19 @@ The display name of scm context
#### Expected Response
1. The display name of scm context

### getReadOnlyInfo (overriding needs only the case of `scm-router`)
The parameters required are:

| Parameter | Type | Description |
| :------------- | :---- | :-------------|
| scmContext | String | The name of scm context |

#### Expected Outcome
Read-only SCM config

#### Expected Response
1. Read-only SCM config

### openPr
| Parameter | Type | Required | Description |
| :------------- | :---- | :------- | :-------------|
Expand Down Expand Up @@ -546,11 +573,14 @@ To make use of the validation functions, the functions to override are:
1. `_getBellConfiguration`
1. `_getPrInfo`
1. `stats` 
1. `_getScmContexts` 
1. `_getScmContexts`
1. `_getScmContext`
1. `_canHandleWebhook` 
1. `_getBranchList`
1. `_openPr`
1. `getDisplayName` (overriding needs only the case of `scm-router`
1. `getDisplayName` (overriding needs only the case of `scm-router`)
1. `getReadOnlyInfo` (overriding needs only the case of `scm-router`


```js
class MyScm extends ScmBase {
Expand Down
95 changes: 77 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

/* eslint-disable no-underscore-dangle */
const Hoek = require('@hapi/hoek');
const Joi = require('joi');
const dataSchema = require('screwdriver-data-schema');
const { getAnnotations } = require('./lib/helper');
Expand Down Expand Up @@ -40,6 +41,24 @@ class ScmBase {
this.config = config;
}

/**
* Set token correctly if is read-only SCM
* @param {Object} config
* @return {Object} Config with proper token
*/
getConfig(config) {
const newConfig = config;
const { accessToken, enabled } = Hoek.reach(this.config, 'readOnly', { default: {} });

if (newConfig && enabled && accessToken) {
newConfig.token = accessToken;

return newConfig;
}

return newConfig;
}

/**
* Adds the Screwdriver webhook to the SCM repository
*
Expand All @@ -55,7 +74,7 @@ class ScmBase {
*/
addWebhook(config) {
return validate(config, dataSchema.plugins.scm.addWebhook)
.then(() => this._addWebhook(config));
.then(() => this._addWebhook(this.getConfig(config)));
}

_addWebhook() {
Expand Down Expand Up @@ -99,7 +118,7 @@ class ScmBase {
*/
addDeployKey(config) {
return validate(config, dataSchema.plugins.scm.addDeployKey)
.then(() => this._addDeployKey(config));
.then(() => this._addDeployKey(this.getConfig(config)));
}

_addDeployKey() {
Expand All @@ -118,7 +137,7 @@ class ScmBase {
*/
parseUrl(config) {
return validate(config, dataSchema.plugins.scm.parseUrl)
.then(validUrl => this._parseUrl(validUrl))
.then(validUrl => this._parseUrl(this.getConfig(validUrl)))
.then(uri => validate(uri, dataSchema.models.pipeline.base.extract('scmUri')));
}

Expand Down Expand Up @@ -154,7 +173,7 @@ class ScmBase {
*/
getChangedFiles(config) {
return validate(config, dataSchema.plugins.scm.getChangedFilesInput)
.then(validInput => this._getChangedFiles(validInput))
.then(validInput => this._getChangedFiles(this.getConfig(validInput)))
.then(changedFiles => validate(changedFiles,
dataSchema.plugins.scm.getChangedFilesOutput));
}
Expand Down Expand Up @@ -235,6 +254,7 @@ class ScmBase {
checkoutConfig.commitBranch = o.build.baseBranch;
}

// Set parentConfig info
if (o.configPipeline) {
const parentConfig = { sha: o.configPipelineSha };

Expand Down Expand Up @@ -264,7 +284,7 @@ class ScmBase {
*/
decorateUrl(config) {
return validate(config, dataSchema.plugins.scm.decorateUrl)
.then(validUrl => this._decorateUrl(validUrl))
.then(validUrl => this._decorateUrl(this.getConfig(validUrl)))
.then(decoratedUrl => validate(decoratedUrl, dataSchema.core.scm.repo));
}

Expand All @@ -284,7 +304,7 @@ class ScmBase {
*/
decorateCommit(config) {
return validate(config, dataSchema.plugins.scm.decorateCommit)
.then(validCommit => this._decorateCommit(validCommit))
.then(validCommit => this._decorateCommit(this.getConfig(validCommit)))
.then(decoratedCommit => validate(decoratedCommit, dataSchema.core.scm.commit));
}

Expand All @@ -303,7 +323,7 @@ class ScmBase {
*/
decorateAuthor(config) {
return validate(config, dataSchema.plugins.scm.decorateAuthor)
.then(validAuthor => this._decorateAuthor(validAuthor))
.then(validAuthor => this._decorateAuthor(this.getConfig(validAuthor)))
.then(decoratedAuthor => validate(decoratedAuthor, dataSchema.core.scm.user));
}

Expand All @@ -322,7 +342,17 @@ class ScmBase {
*/
getPermissions(config) {
return validate(config, dataSchema.plugins.scm.getPermissions)
.then(validConfig => this._getPermissions(validConfig));
.then((validConfig) => {
if (Hoek.reach(this.config, 'readOnly.enabled')) {
return Promise.resolve({
admin: true,
push: true,
pull: true
});
}

return this._getPermissions(validConfig);
});
}

_getPermissions() {
Expand All @@ -341,7 +371,7 @@ class ScmBase {
*/
getOrgPermissions(config) {
return validate(config, dataSchema.plugins.scm.getOrgPermissions)
.then(validConfig => this._getOrgPermissions(validConfig));
.then(validConfig => this._getOrgPermissions(this.getConfig(validConfig)));
}

_getOrgPermissions() {
Expand All @@ -360,7 +390,7 @@ class ScmBase {
*/
getCommitSha(config) {
return validate(config, dataSchema.plugins.scm.getCommitSha)
.then(validConfig => this._getCommitSha(validConfig));
.then(validConfig => this._getCommitSha(this.getConfig(validConfig)));
}

_getCommitSha() {
Expand All @@ -380,7 +410,7 @@ class ScmBase {
*/
getCommitRefSha(config) {
return validate(config, dataSchema.plugins.scm.getCommitRefSha)
.then(validConfig => this._getCommitRefSha(validConfig));
.then(validConfig => this._getCommitRefSha(this.getConfig(validConfig)));
}

_getCommitRefSha() {
Expand All @@ -405,7 +435,7 @@ class ScmBase {
*/
updateCommitStatus(config) {
return validate(config, dataSchema.plugins.scm.updateCommitStatus)
.then(validConfig => this._updateCommitStatus(validConfig));
.then(validConfig => this._updateCommitStatus(this.getConfig(validConfig)));
}

_updateCommitStatus() {
Expand All @@ -424,7 +454,7 @@ class ScmBase {
*/
getFile(config) {
return validate(config, dataSchema.plugins.scm.getFile)
.then(validConfig => this._getFile(validConfig));
.then(validConfig => this._getFile(this.getConfig(validConfig)));
}

_getFile() {
Expand All @@ -442,7 +472,7 @@ class ScmBase {
*/
getOpenedPRs(config) {
return validate(config, dataSchema.plugins.scm.getCommitSha) // includes scmUri, token and scmContext
.then(validConfig => this._getOpenedPRs(validConfig))
.then(validConfig => this._getOpenedPRs(this.getConfig(validConfig)))
.then(jobList =>
validate(jobList, Joi.array().items(
Joi.object().keys({
Expand Down Expand Up @@ -487,7 +517,7 @@ class ScmBase {
*/
getPrInfo(config) {
return validate(config, dataSchema.plugins.scm.getCommitSha) // includes scmUri, token and scmContext
.then(validConfig => this._getPrInfo(validConfig))
.then(validConfig => this._getPrInfo(this.getConfig(validConfig)))
.then(pr => validate(pr, Joi.object().keys({
name: dataSchema.models.job.base.extract('name').required(),
sha: dataSchema.models.build.base.extract('sha').required(),
Expand Down Expand Up @@ -521,7 +551,7 @@ class ScmBase {
*/
addPrComment(config) {
return validate(config, dataSchema.plugins.scm.addPrComment) // includes scmUri, token and scmContext
.then(validConfig => this._addPrComment(validConfig))
.then(validConfig => this._addPrComment(this.getConfig(validConfig)))
.then(prComment => validate(prComment, Joi.alternatives().try(
Joi.object().keys({
commentId: dataSchema.models.job.base.extract('id').required(),
Expand Down Expand Up @@ -565,6 +595,26 @@ class ScmBase {
throw new Error('Not implemented');
}

/**
* Get a scm context matching given hostname
* @method getScmContext
* @param {Object} config
* @param {String} config.hostname Scm hostname (e.g. github.com or GHE.com)
* @return {String} Returns scm context (e.g. github:github.com
* or github:GHE.com)
*/
getScmContext(config) {
const result = this._getScmContext(config);
const schema = dataSchema.models.pipeline.base.extract('scmContext').required();
const validateResult = schema.validate(result);

return validateResult.error || result;
}

_getScmContext() {
throw new Error('Not implemented');
}

/**
* Determine a scm module can handle the received webhook
* @method canHandleWebhook
Expand All @@ -589,6 +639,15 @@ class ScmBase {
return this.config.displayName || '';
}

/**
* Get readOnly object
* @method getReadOnlyInfo
* @return {Object}
*/
getReadOnlyInfo() {
return this.config.readOnly || {};
}

/**
* Get the branch list related to the repository
* @method getBranchList
Expand All @@ -600,7 +659,7 @@ class ScmBase {
*/
getBranchList(config) {
return validate(config, dataSchema.plugins.scm.getBranchList)
.then(() => this._getBranchList(config));
.then(() => this._getBranchList(this.getConfig(config)));
}

_getBranchList() {
Expand All @@ -622,7 +681,7 @@ class ScmBase {
*/
openPr(config) {
return validate(config, dataSchema.plugins.scm.openPr)
.then(() => this._openPr(config));
.then(() => this._openPr(this.getConfig(config)));
}

_openPr() {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"nyc": "^15.0.0"
},
"dependencies": {
"@hapi/hoek": "^9.1.0",
"joi": "^17.2.0",
"screwdriver-data-schema": "^21.0.0"
"screwdriver-data-schema": "^21.3.1"
}
}
Loading

0 comments on commit 559cbb4

Please sign in to comment.