8.2.0 • Published 1 month ago

screwdriver-scm-base v8.2.0

Weekly downloads
55
License
BSD-3-Clause
Repository
github
Last release
1 month ago

Scm Base

Version Downloads Build Status Open Issues License

Base class for defining the behavior between Screwdriver and source control management (SCM) systems

Usage

npm install screwdriver-scm-base

Interface

This is a promise based interface for interacting with a source control management system

configure

The configure function takes in an object and resets the configuration values

addWebhook

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.scmUriStringSCM URI to add the webhook to (e.g., "github.com:8888:branchName"
config.tokenStringAccess token for SCM
config.webhookUrlStringThe URL to use for webhook notifications
config.scmContextString(optional) The name of scm context

Expected Outcome

Update the repository with the desired webhook configuration.

Expected Promise response

  1. Resolves when the webhook is correctly attached to the repository
  2. Rejects when the repository was unable to be updated with the webhook configuration

addDeployKey

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.checkoutUrlStringCheckout url for a repo to parse
config.tokenStringAccess token for SCM
config.scmContextString(optional) The name of scm context

Expected Outcome

Add deploy public key counterpart to the repository.

Expected Promise response

  1. Resolves when the deploy key is successfully generated and added
  2. Rejects when the deploy key fails to generate or add

autoDeployKeyGenerationEnabled

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.scmContextString(optional) The name of scm context

Expected Outcome

Returns whether auto deploy key generation is enabled or not.

Expected Promise response

  1. Resolves to true/false corresponding to the flag status

parseUrl

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.checkoutUrlStringCheckout url for a repo to parse
config.rootDirString(optional) Root directory where source code lives (ex: src/app/component)
config.scmContextString(optional) The name of scm context
config.tokenStringAccess token for scm

Expected Outcome

An scmUri (ex: github.com:1234:branchName, where 1234 is a repo ID number), which will be a unique identifier for the repo and branch in Screwdriver.

Expected Promise response

  1. Resolve with an scm uri for the repository (e.g.: github.com:12345:master or github.com:12345:master:src/app/component)
  2. Reject if not able to parse url

parseHook

Required parameters:

ParameterTypeDescription
headersObjectThe request headers associated with the webhook payload
payloadObjectThe webhook payload received from the SCM service

Expected Outcome

A key-map of data related to the received payload in the form of:

{
    action: 'opened',   // can be 'opened', 'reopened', 'closed', or 'synchronized' for type 'pr'; 'push' for type 'repo'
    branch: 'mynewbranch',
    checkoutUrl: 'https://batman@bitbucket.org/batman/test.git',
    hookId: '81e6bd80-9a2c-11e6-939d-beaa5d9adaf3', // webhook event uuid
    lastCommitMessage: 'This is the last commit message', // get a message of the last one from commits object
    prNum: 3,
    prRef: 'pull/3/merge',
    prSource: 'fork', // If type is 'pr', prSource is 'fork' or 'branch'
    scmContext: 'github:github.com',
    sha: '9ff49b2d1437567cad2b5fed7a0706472131e927',
    type: 'pr',         // can be 'pr' or 'repo'
    username: 'robin'  // should be the actor/creator of the webhook event (not necessarily the author)
}

Expected Promise response

  1. Resolve with a parsed hook object
  2. Reject if not able to parse hook

getChangedFiles

Required parameters:

ParameterTypeDescription
configObjectYesConfiguration Object
config.typeStringThe type of action from Git (can be 'pr' or 'repo')
config.payloadObjectThe webhook payload received from the SCM service
config.tokenStringAccess token for scm

Expected Outcome

An array of file paths that were changed:

['README.md', 'folder/screwdriver.yaml'] // array of changed files

Expected Promise response

  1. Resolve with an array of files
  2. Reject if not able to parse hook

getCheckoutCommand

Required parameters:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.branchStringYesPipeline branch
config.hostStringYesScm host (ex: github.com)
config.manifestStringNoRepo Manifest URL
config.orgStringYesScm org (ex: screwdriver-cd)
config.prRefStringNoPR branch or reference
config.repoStringYesScm repo (ex: guide)
config.rootDirStringNoRoot directory where source code lives (ex: src/app/component)
config.shaStringYesScm sha
config.scmContextStringNoThe name of scm context

Expected Outcome

Checkout command in the form of:

{
    name: 'sd-checkout-code', // must be 'sd-checkout-code' exactly
    command: 'git clone https://github.com/screwdriver-cd/guide'
}

Expected Promise response

  1. Resolve with a checkout command object for the repository
  2. Reject if not able to get checkout command

getSetupCommand

Internally calls getCheckoutCommand to get just the checkout command for a build, given a pipeline model and build configuration.

Required Parameters:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.pipelinePipelineModelYesPipeline model
config.jobObjectYesJob config with repoManifest annotation
config.buildObjectYesBuild config with sha and possibly prRef

decorateUrl

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.scmUriStringScm uri (ex: github.com:1234:branchName)
config.tokenStringAccess token for scm
config.scmContextString(optional) The name of scm context

Expected Outcome

Decorated url in the form of:

{
    url: 'https://github.com/screwdriver-cd/scm-base',
    name: 'screwdriver-cd/scm-base',
    branch: 'branchName'
}

Expected Promise response

  1. Resolve with a decorated url object for the repository
  2. Reject if not able to get decorate url

decorateCommit

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.scmUriStringScm uri (ex: github.com:1234:branchName)
config.shaStringCommit sha to decorate
config.tokenStringAccess token for scm
config.scmContextString(optional) The name of scm context

Expected Outcome

Decorated commit in the form of:

{
    url: 'https://github.com/screwdriver-cd/scm-base/commit/5c3b2cc64ee4bdab73e44c394ad1f92208441411',
    message: 'Use screwdriver to publish',
    author: {
        url: 'https://github.com/d2lam',
        name: 'Dao Lam',
        username: 'd2lam',
        avatar: 'https://avatars3.githubusercontent.com/u/3401924?v=3&s=400'
    }
}

Expected Promise response

  1. Resolve with a decorate commit object for the repository
  2. Reject if not able to decorate commit

decorateAuthor

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.tokenStringAccess token for scm
config.usernameStringAuthor to decorate
config.scmContextString(optional) The name of scm context

Expected Outcome

Decorated author in the form of:

{
    url: 'https://github.com/d2lam',
    name: 'Dao Lam',
    username: 'd2lam',
    avatar: 'https://avatars3.githubusercontent.com/u/3401924?v=3&s=400'
}

Expected Promise response

  1. Resolve with a decorate author object for the repository
  2. Reject if not able to decorate author

getPermissions

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.scmUriStringThe scm uri to get permissions on (ex: github.com:1234:branchName)
config.tokenStringAccess token for scm
config.scmContextString(optional) The name of scm context

Expected Outcome

Permissions for a given token on a repository in the form of:

{
    admin: true,
    push: true,
    pull: true
}

Expected Promise response

  1. Resolve with a permissions object for the repository
  2. Reject if not able to get permissions

getOrgPermissions

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.organizationStringThe scm organization to get permissions on (ex: screwdriver-cd)
config.usernameStringThe user to get permissions on (ex: foo)
config.tokenStringAccess token for scm
config.scmContextString(optional) The name of scm context

Expected Outcome

Permissions for a given user on a organization in the form of:

{
    admin: false,
    member: true
}

Expected Promise response

  1. Resolve with a permissions object for the organization
  2. Reject if not able to get permissions

getCommitSha

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.scmUriStringThe scm uri (ex: github.com:1234:branchName)
config.tokenStringAccess token for scm
config.scmContextString(optional) The name of scm context

Expected Outcome

The commit sha for a given branch on a repository.

Expected Promise response

  1. Resolve with a commit sha string for the given scmUri
  2. Reject if not able to get a sha

getCommitRefSha

Required parameters:

ParameterTypeDescription
configObjectConfiguration Object
config.tokenStringAccess token for scm
config.ownerStringOwner of target repository
config.repoStringTarget repository
config.refStringReference of the commit
config.scmContextString(optional) The name of scm context

Expected Outcome

The commit sha for a ref on a repository.

Expected Promise response

  1. Resolve with a commit sha string for the given owner, repo and ref
  2. Reject if not able to get a sha

updateCommitStatus

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.buildStatusStringYesThe screwdriver build status to translate into scm commit status
config.contextStringNoThe status context
config.descriptionStringNoThe status description
config.jobNameStringNoOptional name of the job that finished
config.pipelineIdNumberNoThe pipeline id
config.scmContextStringNoThe name of scm context
config.scmUriStringYesThe scm uri (ex: github.com:1234:branchName)
config.shaStringYesThe scm sha to update a status for
config.tokenStringYesAccess token for scm
config.urlStringNoThe target url for setting up details

Expected Outcome

Update the commit status for a given repository and sha.

Expected Promise Response

  1. Resolve when the commit status was updated
  2. Reject if the commit status fails to update

getFile

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.pathStringYesThe path to the file on scm to read
config.refStringNoThe reference to the scm repo, could be a branch or sha
config.scmUriStringYesThe scm uri (ex: github.com:1234:branchName)
config.tokenStringYesAccess token for scm
config.scmContextStringNoThe name of scm context

Expected Outcome

The contents of the file at path in the repository

Expected Promise Response

  1. Resolve with the contents of path
  2. Reject if the path cannot be downloaded, decoded, or is not a file

getBellConfiguration

Expected Outcome

A configuration that can be passed to the bell OAuth module to authenticate users.

Expected Promise Response

  1. Resolve with a valid bell configuration

getOpenedPRs

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.scmUriStringYesThe scm uri (ex: github.com:1234:branchName)
config.tokenStringYesAccess token for scm
config.scmContextStringNoThe name of scm context

Expected Outcome

The list of objects consist of PR names and refs (either a branch or a sha) for the pipeline. For example:

[{
  name: 'PR-5',
  ref: '73675432e1288f67332af3ecd0155cf455af1492'
}, {
  name: 'PR-6',
  ref: 'dfbbc032fa331a95ee5107d1f16e9ff5f7c9d2fa'
}]

Expected Promise Response

  1. Resolve with the list of objects consists of PR names and refs
  2. Reject if the input or output is not valid

getPrInfo

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.scmUriStringYesThe scm uri (ex: github.com:1234:branchName)
config.tokenStringYesAccess token for scm
config.prNumIntegerYesThe PR number used to fetch the PR
config.scmContextStringNoThe name of scm context

Expected Outcome

The object consists of PR name, sha, ref, and url for the pipeline.

Expected Promise Response

  1. Resolve with the object consists of PR name, sha, ref, and url
  2. Reject if the input or output is not valid

addPrComment

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.scmUriStringYesThe scm uri (ex: github.com:1234:branchName)
config.tokenStringYesAccess token for scm
config.prNumIntegerYesThe PR number used to fetch the PR
config.commentStringYesThe PR comment
config.scmContextStringNoThe name of scm context

Expected Outcome

The object consisting of PR comment ID, create time, and username.

Expected Promise Response

  1. Resolve with the object consists of PR comment ID, create time, and username
  2. Reject if the input or output is not valid

getScmContexts

No parameters are required.

Expected Outcome

The array of scm context names (e.g. github:github.com, gitlab:my-gitlab)

Expected Response

  1. The array of scm context names

getScmContext

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.hostnameStringYesThe 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:

ParameterTypeDescription
headersObjectThe request headers associated with the webhook payload
payloadObjectThe webhook payload received from the SCM service

Expected Outcome

The received webhook is available or not as boolean.

Expected Promise Response

  1. Resolve with the received webhook is available or not.
  2. Reject if the input or output is not valid

getBranchList

The parameters required are:

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.scmUriStringYesThe scm uri (ex: github.com:1234:branchName)
config.tokenStringYesAccess token for scm

Expected Outcome

The array of objects consisting of branch names.

Expected Promise Response

  1. Resolve with an array of objects consisting of branch names.

getDisplayName (overriding needs only the case of scm-router)

The parameters required are:

ParameterTypeDescription
scmContextStringThe name of scm context

Expected Outcome

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:

ParameterTypeDescription
scmContextStringThe name of scm context

Expected Outcome

Read-only SCM config

Expected Response

  1. Read-only SCM config

openPr

ParameterTypeRequiredDescription
configObjectYesConfiguration Object
config.checkoutUrlStringYesCheckout url for a repo
config.tokenStringYesAccess token for scm
config.titleStringYesPull request title
config.messageStringYesPull request message

Expected Outcome

An object containing information of new pull request

Expected Response

  1. Pull request object

isEnterpriseUser

Required parameters:

ParameterTypeDescription
configObjectThe config object
config.tokenStringThe github token to interact with the graphql api
config.slugStringThe github enterprise slug
config.loginStringThe github user's login name

Expected Outcome

  1. Returns True or False

Extending

To make use of the validation functions, the functions to override are:

  1. _addWebhook
  2. _addDeployKey
  3. _autoDeployKeyGenerationEnabled
  4. _parseUrl
  5. _parseHook
  6. _getChangedFiles
  7. _getCheckoutCommand
  8. _decorateUrl
  9. _decorateCommit
  10. _decorateAuthor
  11. _getPermissions
  12. _getOrgPermissions
  13. _getCommitSha
  14. _addPrComment
  15. _updateCommitStatus
  16. _getFile
  17. _getOpenedPRs
  18. _getBellConfiguration
  19. _getPrInfo
  20. stats 
  21. _getScmContexts
  22. _getScmContext
  23. _canHandleWebhook 
  24. _getBranchList
  25. _openPr
  26. getDisplayName (overriding needs only the case of scm-router)
  27. getReadOnlyInfo (overriding needs only the case of scm-router)
  28. _isEnterpriseUser
class MyScm extends ScmBase {
    // Implement the interface
    _getFile(config) {
        // do stuff here to lookup scmUri
        return Promise.resolve('these are contents that are gotten')
    }
}

const scm = new MyScm({});
scm.getFile({
    scmUri: 'github.com:12345:master',
    path: 'screwdriver.yaml',
    token: 'abcdefg'
}).then(data => {
    // do something...
});

Testing

npm test

License

Code licensed under the BSD 3-Clause license. See LICENSE file for terms.

8.2.0

1 month ago

8.1.2

2 months ago

8.1.1

5 months ago

8.1.0

12 months ago

8.0.0

1 year ago

7.4.0

2 years ago

7.3.0

3 years ago

7.2.1

3 years ago

7.2.0

3 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

4 years ago

7.1.0

4 years ago

7.0.0

4 years ago

6.1.1

4 years ago

6.1.0

4 years ago

6.0.0

4 years ago

5.3.3

4 years ago

5.3.2

4 years ago

5.3.1

4 years ago

5.3.0

4 years ago

5.2.5

4 years ago

5.2.4

5 years ago

5.2.3

5 years ago

5.2.2

5 years ago

5.2.1

5 years ago

5.2.0

5 years ago

5.1.2

5 years ago

5.1.1

5 years ago

5.1.0

5 years ago

5.0.0

5 years ago

4.4.3

6 years ago

4.4.2

6 years ago

4.4.1

6 years ago

4.4.0

6 years ago

4.3.0

6 years ago

4.2.1

6 years ago

4.2.0

6 years ago

4.1.0

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.0.3

6 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.7.2

7 years ago

2.7.1

7 years ago

2.7.0

7 years ago

2.6.1

7 years ago

2.6.0

7 years ago

2.5.2

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.5

7 years ago

2.4.4

7 years ago

2.4.3

7 years ago

2.4.2

7 years ago

2.4.1

7 years ago

2.4.0

7 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.3

8 years ago

2.1.2

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.11

8 years ago

2.0.10

8 years ago

2.0.9

8 years ago

2.0.8

8 years ago

2.0.7

8 years ago

2.0.6

8 years ago

2.0.5

8 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.0.1

8 years ago