Skip to content

Commit

Permalink
feat(823): Add support for release and tag trigger (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
tk3fftk authored and tkyi committed Mar 8, 2019
1 parent dbdb57e commit 5530085
Show file tree
Hide file tree
Showing 4 changed files with 444 additions and 7 deletions.
74 changes: 71 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class GithubScm extends Scm {
let action = 'createHook';
const params = {
active: true,
events: ['push', 'pull_request'],
events: ['push', 'pull_request', 'create', 'release'],
owner: config.scmInfo.owner,
repo: config.scmInfo.repo,
name: 'web',
Expand Down Expand Up @@ -631,6 +631,35 @@ class GithubScm extends Scm {
}
}

/**
* Get a commit sha from a reference
* @async getCommitRefSha
* @param {Object} config
* @param {String} config.token The token used to authenticate to the SCM
* @param {String} config.owner The owner of the target repository
* @param {String} config.repo The target repository name
* @param {String} config.ref The reference which we want
* @return {Promise} Resolves to the commit sha
*/
async _getCommitRefSha(config) {
try {
const commit = await this.breaker.runCommand({
action: 'getCommitRefSha',
token: config.token,
params: {
owner: config.owner,
repo: config.repo,
ref: config.ref
}
});

return commit.data.sha;
} catch (err) {
winston.error('Failed to getCommitRefSha: ', err);
throw err;
}
}

/**
* Update the commit status for a given repo and sha
* @async _updateCommitStatus
Expand Down Expand Up @@ -975,6 +1004,7 @@ class GithubScm extends Scm {
const baseSource = hoek.reach(webhookPayload, 'pull_request.base.repo.id');
const headSource = hoek.reach(webhookPayload, 'pull_request.head.repo.id');
const prSource = baseSource === headSource ? 'branch' : 'fork';
const ref = `pull/${prNum}/merge`;

// Possible actions
// "opened", "closed", "reopened", "synchronize",
Expand All @@ -993,7 +1023,8 @@ class GithubScm extends Scm {
checkoutUrl,
prNum,
prTitle,
prRef: `pull/${prNum}/merge`,
prRef: ref,
ref,
prSource,
sha: hoek.reach(webhookPayload, 'pull_request.head.sha'),
type: 'pr',
Expand All @@ -1012,9 +1043,46 @@ class GithubScm extends Scm {
username: hoek.reach(webhookPayload, 'sender.login'),
lastCommitMessage: hoek.reach(webhookPayload, 'head_commit.message') || '',
hookId,
scmContext: scmContexts[0]
scmContext: scmContexts[0],
ref: hoek.reach(webhookPayload, 'ref')
};
case 'release': {
return {
action: 'release',
branch: hoek.reach(webhookPayload, 'release.target_commitish'),
checkoutUrl,
type: 'repo',
username: hoek.reach(webhookPayload, 'sender.login'),
hookId,
scmContext: scmContexts[0],
ref: hoek.reach(webhookPayload, 'release.tag_name')
};
}
case 'create': {
const refType = hoek.reach(webhookPayload, 'ref_type');

if (refType !== 'tag') {
winston.info('%s event of %s is not available yet in scm-github plugin',
type, refType);

return null;
}

return {
action: 'tag',
branch: hoek.reach(webhookPayload, 'master_branch'),
checkoutUrl,
type: 'repo',
username: hoek.reach(webhookPayload, 'sender.login'),
hookId,
scmContext: scmContexts[0],
ref: hoek.reach(webhookPayload, 'ref')
};
}

default:
winston.info('%s event is not available yet in scm-github plugin', type);

return null;
}
}
Expand Down
157 changes: 157 additions & 0 deletions test/data/github.release.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
{
"action": "published",
"release": {
"url": "https://api.github.com/repos/Codertocat/Hello-World/releases/11248810",
"assets_url": "https://api.github.com/repos/Codertocat/Hello-World/releases/11248810/assets",
"upload_url": "https://uploads.github.com/repos/Codertocat/Hello-World/releases/11248810/assets{?name,label}",
"html_url": "https://github.com/Codertocat/Hello-World/releases/tag/0.0.1",
"id": 11248810,
"node_id": "MDc6UmVsZWFzZTExMjQ4ODEw",
"tag_name": "0.0.1",
"target_commitish": "master",
"name": null,
"draft": false,
"author": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
},
"prerelease": false,
"created_at": "2018-05-30T20:18:05Z",
"published_at": "2018-05-30T20:18:44Z",
"assets": [

],
"tarball_url": "https://api.github.com/repos/Codertocat/Hello-World/tarball/0.0.1",
"zipball_url": "https://api.github.com/repos/Codertocat/Hello-World/zipball/0.0.1",
"body": null
},
"repository": {
"id": 135493233,
"node_id": "MDEwOlJlcG9zaXRvcnkxMzU0OTMyMzM=",
"name": "Hello-World",
"full_name": "Codertocat/Hello-World",
"owner": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
},
"private": false,
"html_url": "https://github.com/Codertocat/Hello-World",
"description": null,
"fork": false,
"url": "https://api.github.com/repos/Codertocat/Hello-World",
"forks_url": "https://api.github.com/repos/Codertocat/Hello-World/forks",
"keys_url": "https://api.github.com/repos/Codertocat/Hello-World/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/Codertocat/Hello-World/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/Codertocat/Hello-World/teams",
"hooks_url": "https://api.github.com/repos/Codertocat/Hello-World/hooks",
"issue_events_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/events{/number}",
"events_url": "https://api.github.com/repos/Codertocat/Hello-World/events",
"assignees_url": "https://api.github.com/repos/Codertocat/Hello-World/assignees{/user}",
"branches_url": "https://api.github.com/repos/Codertocat/Hello-World/branches{/branch}",
"tags_url": "https://api.github.com/repos/Codertocat/Hello-World/tags",
"blobs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/Codertocat/Hello-World/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/Codertocat/Hello-World/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/Codertocat/Hello-World/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/Codertocat/Hello-World/statuses/{sha}",
"languages_url": "https://api.github.com/repos/Codertocat/Hello-World/languages",
"stargazers_url": "https://api.github.com/repos/Codertocat/Hello-World/stargazers",
"contributors_url": "https://api.github.com/repos/Codertocat/Hello-World/contributors",
"subscribers_url": "https://api.github.com/repos/Codertocat/Hello-World/subscribers",
"subscription_url": "https://api.github.com/repos/Codertocat/Hello-World/subscription",
"commits_url": "https://api.github.com/repos/Codertocat/Hello-World/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/Codertocat/Hello-World/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/Codertocat/Hello-World/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/Codertocat/Hello-World/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/Codertocat/Hello-World/contents/{+path}",
"compare_url": "https://api.github.com/repos/Codertocat/Hello-World/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/Codertocat/Hello-World/merges",
"archive_url": "https://api.github.com/repos/Codertocat/Hello-World/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/Codertocat/Hello-World/downloads",
"issues_url": "https://api.github.com/repos/Codertocat/Hello-World/issues{/number}",
"pulls_url": "https://api.github.com/repos/Codertocat/Hello-World/pulls{/number}",
"milestones_url": "https://api.github.com/repos/Codertocat/Hello-World/milestones{/number}",
"notifications_url": "https://api.github.com/repos/Codertocat/Hello-World/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/Codertocat/Hello-World/labels{/name}",
"releases_url": "https://api.github.com/repos/Codertocat/Hello-World/releases{/id}",
"deployments_url": "https://api.github.com/repos/Codertocat/Hello-World/deployments",
"created_at": "2018-05-30T20:18:04Z",
"updated_at": "2018-05-30T20:18:35Z",
"pushed_at": "2018-05-30T20:18:44Z",
"git_url": "git://github.com/Codertocat/Hello-World.git",
"ssh_url": "git@github.com:Codertocat/Hello-World.git",
"clone_url": "https://github.com/Codertocat/Hello-World.git",
"svn_url": "https://github.com/Codertocat/Hello-World",
"homepage": null,
"size": 0,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"open_issues_count": 2,
"license": null,
"forks": 0,
"open_issues": 2,
"watchers": 0,
"default_branch": "master"
},
"sender": {
"login": "Codertocat",
"id": 21031067,
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/Codertocat",
"html_url": "https://github.com/Codertocat",
"followers_url": "https://api.github.com/users/Codertocat/followers",
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
"repos_url": "https://api.github.com/users/Codertocat/repos",
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
"type": "User",
"site_admin": false
}
}
Loading

0 comments on commit 5530085

Please sign in to comment.