Skip to content

Commit

Permalink
Merge branch 'atarax:main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
iul1an authored Jan 18, 2024
2 parents 31edc66 + c4a63b1 commit 3306787
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 14 deletions.
28 changes: 28 additions & 0 deletions apis/organizations/v1alpha1/repository_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type RepositoryParameters struct {
Description string `json:"description,omitempty"`
Permissions RepositoryPermissions `json:"permissions,omitempty"`

Webhooks []RepositoryWebhook `json:"webhooks,omitempty"`

// Org is the Organization for the Membership
// +immutable
// +crossplane:generate:reference:type=Organization
Expand Down Expand Up @@ -94,6 +96,32 @@ type RepositoryTeam struct {
Role string `json:"role"`
}

// Repository webhook
// https://docs.github.com/en/webhooks/types-of-webhooks#repository-webhooks
type RepositoryWebhook struct {
// The URL to which the payloads will be delivered.
Url string `json:"url"`

// Determines whether the SSL certificate of the host for url will be verified when delivering payloads.
// Supported values include "0" (verification is performed) and "1" (verification is not performed).
// We strongly recommend not setting this to "1" as you are subject to man-in-the-middle and other attacks.
// +optional
// +kubebuilder:default=false
InsecureSsl bool `json:"insecureSsl,omitempty"`

// The media type used to serialize the payloads. Supported values include json and form.
// +kubebuilder:validation:Enum=json;form
ContentType string `json:"contentType"`

// Determines what events the hook is triggered for. See https://docs.github.com/en/webhooks/webhook-events-and-payloads
Events []string `json:"events"`

// Determines if notifications are sent when the webhook is triggered. Default: true
// +optional
// +kubebuilder:default=true
Active bool `json:"active,omitempty"`
}

// RepositoryObservation are the observable fields of a Repository.
type RepositoryObservation struct {
ObservableField string `json:"observableField,omitempty"`
Expand Down
27 changes: 27 additions & 0 deletions apis/organizations/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type RepositoriesClient interface {
AddCollaborator(ctx context.Context, owner, repo, user string, opts *github.RepositoryAddCollaboratorOptions) (*github.CollaboratorInvitation, *github.Response, error)
RemoveCollaborator(ctx context.Context, owner, repo, user string) (*github.Response, error)
Delete(ctx context.Context, owner, repo string) (*github.Response, error)
CreateHook(ctx context.Context, owner, repo string, hook *github.Hook) (*github.Hook, *github.Response, error)
EditHook(ctx context.Context, owner, repo string, id int64, hook *github.Hook) (*github.Hook, *github.Response, error)
DeleteHook(ctx context.Context, owner, repo string, id int64) (*github.Response, error)
ListHooks(ctx context.Context, owner, repo string, opts *github.ListOptions) ([]*github.Hook, *github.Response, error)
}

// NewClient creates a new client.
Expand Down
20 changes: 20 additions & 0 deletions internal/clients/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type MockRepositoriesClient struct {
MockAddCollaborator func(ctx context.Context, owner, repo, user string, opts *github.RepositoryAddCollaboratorOptions) (*github.CollaboratorInvitation, *github.Response, error)
MockRemoveCollaborator func(ctx context.Context, owner, repo, user string) (*github.Response, error)
MockDelete func(ctx context.Context, owner, repo string) (*github.Response, error)
MockCreateHook func(ctx context.Context, owner, repo string, hook *github.Hook) (*github.Hook, *github.Response, error)
MockEditHook func(ctx context.Context, owner, repo string, id int64, hook *github.Hook) (*github.Hook, *github.Response, error)
MockDeleteHook func(ctx context.Context, owner, repo string, id int64) (*github.Response, error)
MockListHooks func(ctx context.Context, owner, repo string, opts *github.ListOptions) ([]*github.Hook, *github.Response, error)
}

func (m *MockRepositoriesClient) Get(ctx context.Context, owner, repo string) (*github.Repository, *github.Response, error) {
Expand Down Expand Up @@ -109,6 +113,22 @@ func (m *MockRepositoriesClient) RemoveCollaborator(ctx context.Context, owner,
return m.MockRemoveCollaborator(ctx, owner, repo, user)
}

func (m *MockRepositoriesClient) CreateHook(ctx context.Context, owner, repo string, hook *github.Hook) (*github.Hook, *github.Response, error) {
return m.MockCreateHook(ctx, owner, repo, hook)
}

func (m *MockRepositoriesClient) EditHook(ctx context.Context, owner, repo string, id int64, hook *github.Hook) (*github.Hook, *github.Response, error) {
return m.MockEditHook(ctx, owner, repo, id, hook)
}

func (m *MockRepositoriesClient) DeleteHook(ctx context.Context, owner, repo string, id int64) (*github.Response, error) {
return m.MockDeleteHook(ctx, owner, repo, id)
}

func (m *MockRepositoriesClient) ListHooks(ctx context.Context, owner, repo string, opts *github.ListOptions) ([]*github.Hook, *github.Response, error) {
return m.MockListHooks(ctx, owner, repo, opts)
}

type MockTeamsClient struct {
MockGetTeamBySlug func(ctx context.Context, org, slug string) (*github.Team, *github.Response, error)
MockListTeamMembersBySlug func(ctx context.Context, org, slug string, opts *github.TeamListTeamMembersOptions) ([]*github.User, *github.Response, error)
Expand Down
29 changes: 15 additions & 14 deletions internal/controller/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,28 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, err
}

// To use this function, the organization permission policy for enabled_repositories must be configured to selected, otherwise you get error 409 Conflict
aResp, _, err := c.github.Actions.ListEnabledReposInOrg(ctx, name, &github.ListOptions{PerPage: 100})

if err != nil {
return managed.ExternalObservation{}, err
}

notUpToDate := managed.ExternalObservation{
ResourceExists: true,
ResourceUpToDate: false,
}

aRepos := getSortedRepoNames(aResp.Repositories)
// To use this function, the organization permission policy for enabled_repositories must be configured to selected, otherwise you get error 409 Conflict
if cr.Spec.ForProvider.Actions.EnabledRepos != nil {
aResp, _, err := c.github.Actions.ListEnabledReposInOrg(ctx, name, &github.ListOptions{PerPage: 100})

crARepos := getSortedEnabledReposFromCr(cr.Spec.ForProvider.Actions.EnabledRepos)
if err != nil {
return managed.ExternalObservation{}, err
}

if err != nil {
return managed.ExternalObservation{}, err
}
if !reflect.DeepEqual(aRepos, crARepos) {
return notUpToDate, nil
crARepos := getSortedEnabledReposFromCr(cr.Spec.ForProvider.Actions.EnabledRepos)
aRepos := getSortedRepoNames(aResp.Repositories)

if err != nil {
return managed.ExternalObservation{}, err
}
if !reflect.DeepEqual(aRepos, crARepos) {
return notUpToDate, nil
}
}

if cr.Spec.ForProvider.Description != pointer.StringDeref(org.Description, "") {
Expand Down
Loading

0 comments on commit 3306787

Please sign in to comment.