From 5141b8e1db921fe2101c12594c5159b9ffffebc3 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 17 Dec 2020 17:19:42 -0500 Subject: [PATCH] added support for github visibility --- scm/const.go | 25 ++++++ scm/driver/github/repo.go | 29 +++++-- scm/driver/github/testdata/repo.json | 1 + scm/driver/github/testdata/repo.json.golden | 1 + scm/driver/github/testdata/repos.json | 1 + scm/driver/github/testdata/repos.json.golden | 1 + .../github/testdata/webhooks/tag_create.json | 1 + .../testdata/webhooks/tag_create.json.golden | 1 + .../github/testdata/webhooks/tag_delete.json | 1 + .../testdata/webhooks/tag_delete.json.golden | 1 + scm/driver/github/webhook.go | 86 ++++++++++--------- scm/repo.go | 23 ++--- 12 files changed, 113 insertions(+), 58 deletions(-) diff --git a/scm/const.go b/scm/const.go index 061fd3d95..a06db0ca5 100644 --- a/scm/const.go +++ b/scm/const.go @@ -213,3 +213,28 @@ func (k *ContentKind) UnmarshalJSON(data []byte) error { } return nil } + +// Visibility defines repository visibility. +type Visibility int + +// Role values. +const ( + VisibilityUndefined Visibility = iota + VisibilityPublic + VisibilityInternal + VisibilityPrivate +) + +// String returns the string representation of Role. +func (v Visibility) String() (s string) { + switch v { + case VisibilityPublic: + return "public" + case VisibilityInternal: + return "internal" + case VisibilityPrivate: + return "private" + default: + return "unknown" + } +} diff --git a/scm/driver/github/repo.go b/scm/driver/github/repo.go index 6ebb61b97..27cb11794 100644 --- a/scm/driver/github/repo.go +++ b/scm/driver/github/repo.go @@ -24,6 +24,7 @@ type repository struct { FullName string `json:"full_name"` Private bool `json:"private"` Fork bool `json:"fork"` + Visibility string `json:"visibility"` HTMLURL string `json:"html_url"` SSHURL string `json:"ssh_url"` CloneURL string `json:"clone_url"` @@ -202,13 +203,14 @@ func convertRepository(from *repository) *scm.Repository { Pull: from.Permissions.Pull, Admin: from.Permissions.Admin, }, - Link: from.HTMLURL, - Branch: from.DefaultBranch, - Private: from.Private, - Clone: from.CloneURL, - CloneSSH: from.SSHURL, - Created: from.CreatedAt, - Updated: from.UpdatedAt, + Link: from.HTMLURL, + Branch: from.DefaultBranch, + Private: from.Private, + Visibility: convertVisibility(from.Visibility), + Clone: from.CloneURL, + CloneSSH: from.SSHURL, + Created: from.CreatedAt, + Updated: from.UpdatedAt, } } @@ -257,6 +259,19 @@ func convertFromHookEvents(from scm.HookEvents) []string { return events } +func convertVisibility(from string) scm.Visibility { + switch from { + case "public": + return scm.VisibilityPublic + case "private": + return scm.VisibilityPrivate + case "internal": + return scm.VisibilityInternal + default: + return scm.VisibilityUndefined + } +} + type status struct { CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` diff --git a/scm/driver/github/testdata/repo.json b/scm/driver/github/testdata/repo.json index e48e02f90..7a759a19b 100644 --- a/scm/driver/github/testdata/repo.json +++ b/scm/driver/github/testdata/repo.json @@ -24,6 +24,7 @@ "description": "This your first repo!", "private": true, "fork": false, + "visibility": "public", "url": "https://api.github.com/repos/octocat/Hello-World", "html_url": "https://github.com/octocat/Hello-World", "archive_url": "http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", diff --git a/scm/driver/github/testdata/repo.json.golden b/scm/driver/github/testdata/repo.json.golden index 3cfa2e71b..5fa8c4db1 100644 --- a/scm/driver/github/testdata/repo.json.golden +++ b/scm/driver/github/testdata/repo.json.golden @@ -9,6 +9,7 @@ }, "Branch": "master", "Private": true, + "Visibility": 1, "Clone": "https://github.com/octocat/Hello-World.git", "CloneSSH": "git@github.com:octocat/Hello-World.git", "Link": "https://github.com/octocat/Hello-World", diff --git a/scm/driver/github/testdata/repos.json b/scm/driver/github/testdata/repos.json index 4bf6ebe29..eed297f73 100644 --- a/scm/driver/github/testdata/repos.json +++ b/scm/driver/github/testdata/repos.json @@ -25,6 +25,7 @@ "description": "This your first repo!", "private": true, "fork": true, + "visibility": "public", "url": "https://api.github.com/repos/octocat/Hello-World", "html_url": "https://github.com/octocat/Hello-World", "archive_url": "http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", diff --git a/scm/driver/github/testdata/repos.json.golden b/scm/driver/github/testdata/repos.json.golden index a9aa60b24..acb48baf4 100644 --- a/scm/driver/github/testdata/repos.json.golden +++ b/scm/driver/github/testdata/repos.json.golden @@ -10,6 +10,7 @@ }, "Branch": "master", "Private": true, + "Visibility": 1, "Clone": "https://github.com/octocat/Hello-World.git", "CloneSSH": "git@github.com:octocat/Hello-World.git", "Link": "https://github.com/octocat/Hello-World", diff --git a/scm/driver/github/testdata/webhooks/tag_create.json b/scm/driver/github/testdata/webhooks/tag_create.json index af8bfcfd4..05fee28c9 100644 --- a/scm/driver/github/testdata/webhooks/tag_create.json +++ b/scm/driver/github/testdata/webhooks/tag_create.json @@ -30,6 +30,7 @@ "site_admin": false }, "private": true, + "visibility": "private", "html_url": "https://github.com/bradrydzewski/drone-test-go", "description": "test project written in Go", "fork": true, diff --git a/scm/driver/github/testdata/webhooks/tag_create.json.golden b/scm/driver/github/testdata/webhooks/tag_create.json.golden index ad513f682..4b0d059a8 100644 --- a/scm/driver/github/testdata/webhooks/tag_create.json.golden +++ b/scm/driver/github/testdata/webhooks/tag_create.json.golden @@ -10,6 +10,7 @@ "Perm": null, "Branch": "master", "Private": true, + "Visibility": 3, "Clone": "https://github.com/bradrydzewski/drone-test-go.git", "CloneSSH": "git@github.com:bradrydzewski/drone-test-go.git", "Link": "https://github.com/bradrydzewski/drone-test-go", diff --git a/scm/driver/github/testdata/webhooks/tag_delete.json b/scm/driver/github/testdata/webhooks/tag_delete.json index 051ad6c6a..e0862c1b6 100644 --- a/scm/driver/github/testdata/webhooks/tag_delete.json +++ b/scm/driver/github/testdata/webhooks/tag_delete.json @@ -28,6 +28,7 @@ "site_admin": false }, "private": true, + "visibility": "internal", "html_url": "https://github.com/bradrydzewski/drone-test-go", "description": "test project written in Go", "fork": true, diff --git a/scm/driver/github/testdata/webhooks/tag_delete.json.golden b/scm/driver/github/testdata/webhooks/tag_delete.json.golden index 0faf8fdd2..f187b1995 100644 --- a/scm/driver/github/testdata/webhooks/tag_delete.json.golden +++ b/scm/driver/github/testdata/webhooks/tag_delete.json.golden @@ -10,6 +10,7 @@ "Perm": null, "Branch": "master", "Private": true, + "Visibility": 2, "Clone": "https://github.com/bradrydzewski/drone-test-go.git", "CloneSSH": "git@github.com:bradrydzewski/drone-test-go.git", "Link": "https://github.com/bradrydzewski/drone-test-go", diff --git a/scm/driver/github/webhook.go b/scm/driver/github/webhook.go index e848b07f4..556f5a7a1 100644 --- a/scm/driver/github/webhook.go +++ b/scm/driver/github/webhook.go @@ -224,6 +224,7 @@ type ( Name string `json:"name"` FullName string `json:"full_name"` Private bool `json:"private"` + Visibility string `json:"visibility"` Fork bool `json:"fork"` HTMLURL string `json:"html_url"` SSHURL string `json:"ssh_url"` @@ -310,14 +311,15 @@ func convertPushHook(src *pushHook) *scm.PushHook { }, }, Repo: scm.Repository{ - ID: fmt.Sprint(src.Repository.ID), - Namespace: src.Repository.Owner.Login, - Name: src.Repository.Name, - Branch: src.Repository.DefaultBranch, - Private: src.Repository.Private, - Clone: src.Repository.CloneURL, - CloneSSH: src.Repository.SSHURL, - Link: src.Repository.HTMLURL, + ID: fmt.Sprint(src.Repository.ID), + Namespace: src.Repository.Owner.Login, + Name: src.Repository.Name, + Branch: src.Repository.DefaultBranch, + Private: src.Repository.Private, + Visibility: convertVisibility(src.Repository.Visibility), + Clone: src.Repository.CloneURL, + CloneSSH: src.Repository.SSHURL, + Link: src.Repository.HTMLURL, }, Sender: *convertUser(&src.Sender), Commits: commits, @@ -336,14 +338,15 @@ func convertBranchHook(src *createDeleteHook) *scm.BranchHook { Name: src.Ref, }, Repo: scm.Repository{ - ID: fmt.Sprint(src.Repository.ID), - Namespace: src.Repository.Owner.Login, - Name: src.Repository.Name, - Branch: src.Repository.DefaultBranch, - Private: src.Repository.Private, - Clone: src.Repository.CloneURL, - CloneSSH: src.Repository.SSHURL, - Link: src.Repository.HTMLURL, + ID: fmt.Sprint(src.Repository.ID), + Namespace: src.Repository.Owner.Login, + Name: src.Repository.Name, + Branch: src.Repository.DefaultBranch, + Private: src.Repository.Private, + Visibility: convertVisibility(src.Repository.Visibility), + Clone: src.Repository.CloneURL, + CloneSSH: src.Repository.SSHURL, + Link: src.Repository.HTMLURL, }, Sender: *convertUser(&src.Sender), } @@ -355,14 +358,15 @@ func convertTagHook(src *createDeleteHook) *scm.TagHook { Name: src.Ref, }, Repo: scm.Repository{ - ID: fmt.Sprint(src.Repository.ID), - Namespace: src.Repository.Owner.Login, - Name: src.Repository.Name, - Branch: src.Repository.DefaultBranch, - Private: src.Repository.Private, - Clone: src.Repository.CloneURL, - CloneSSH: src.Repository.SSHURL, - Link: src.Repository.HTMLURL, + ID: fmt.Sprint(src.Repository.ID), + Namespace: src.Repository.Owner.Login, + Name: src.Repository.Name, + Branch: src.Repository.DefaultBranch, + Private: src.Repository.Private, + Visibility: convertVisibility(src.Repository.Visibility), + Clone: src.Repository.CloneURL, + CloneSSH: src.Repository.SSHURL, + Link: src.Repository.HTMLURL, }, Sender: *convertUser(&src.Sender), } @@ -372,14 +376,15 @@ func convertPullRequestHook(src *pullRequestHook) *scm.PullRequestHook { return &scm.PullRequestHook{ // Action Action Repo: scm.Repository{ - ID: fmt.Sprint(src.Repository.ID), - Namespace: src.Repository.Owner.Login, - Name: src.Repository.Name, - Branch: src.Repository.DefaultBranch, - Private: src.Repository.Private, - Clone: src.Repository.CloneURL, - CloneSSH: src.Repository.SSHURL, - Link: src.Repository.HTMLURL, + ID: fmt.Sprint(src.Repository.ID), + Namespace: src.Repository.Owner.Login, + Name: src.Repository.Name, + Branch: src.Repository.DefaultBranch, + Private: src.Repository.Private, + Visibility: convertVisibility(src.Repository.Visibility), + Clone: src.Repository.CloneURL, + CloneSSH: src.Repository.SSHURL, + Link: src.Repository.HTMLURL, }, PullRequest: *convertPullRequest(&src.PullRequest), Sender: *convertUser(&src.Sender), @@ -397,14 +402,15 @@ func convertDeploymentHook(src *deploymentHook) *scm.DeployHook { Sha: src.Deployment.Sha.String, }, Repo: scm.Repository{ - ID: fmt.Sprint(src.Repository.ID), - Namespace: src.Repository.Owner.Login, - Name: src.Repository.Name, - Branch: src.Repository.DefaultBranch, - Private: src.Repository.Private, - Clone: src.Repository.CloneURL, - CloneSSH: src.Repository.SSHURL, - Link: src.Repository.HTMLURL, + ID: fmt.Sprint(src.Repository.ID), + Namespace: src.Repository.Owner.Login, + Name: src.Repository.Name, + Branch: src.Repository.DefaultBranch, + Private: src.Repository.Private, + Visibility: convertVisibility(src.Repository.Visibility), + Clone: src.Repository.CloneURL, + CloneSSH: src.Repository.SSHURL, + Link: src.Repository.HTMLURL, }, Sender: *convertUser(&src.Sender), Task: src.Deployment.Task.String, diff --git a/scm/repo.go b/scm/repo.go index 2333a51e0..8bcfa7231 100644 --- a/scm/repo.go +++ b/scm/repo.go @@ -12,17 +12,18 @@ import ( type ( // Repository represents a git repository. Repository struct { - ID string - Namespace string - Name string - Perm *Perm - Branch string - Private bool - Clone string - CloneSSH string - Link string - Created time.Time - Updated time.Time + ID string + Namespace string + Name string + Perm *Perm + Branch string + Private bool + Visibility Visibility + Clone string + CloneSSH string + Link string + Created time.Time + Updated time.Time } // Perm represents a user's repository permissions.