Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto configure new repo (only with GH app) #890

Merged
merged 2 commits into from
Oct 28, 2022

Conversation

sm43
Copy link
Contributor

@sm43 sm43 commented Oct 11, 2022

if auto-configure-new-github-repo is enabled in pac configmap
then PAC will auto configure newly created GitHub Repositories

it will create Repository CR and a namespace
By default if the github repository is pac/test-repo
then it will create namespace with name test-repo-pipelines and repository with name test-repo

User can provide template for generating name for namespace using auto-configure-repo-namespace-template

  # Whether to auto configure newly created repositories, this will create a new namespace
  # and repository CR, supported only with GitHub App
  auto-configure-new-github-repo: "false"

  # add a template to generate name for namespace for your auto configured github repo
  # supported fields are repo_owner, repo_name
  # eg. if defined as `{{repo_owner}}-{{repo_name}}-ci`, then namespace generated for repository
  # https://github.com/owner/repo will be `owner-repo-ci`
  auto-configure-repo-namespace-template: ""

Changes

Submitter Checklist

  • ♽ Run make test lint before submitting a PR (ie: with pre-commit, no need to waste CPU cycle on CI
  • 📖 If you are adding a user facing feature or make a change of the behavior, please verify that you have documented it
  • 🧪 100% coverage is not a target but most of the time we would rather have a unit test if you make a code change.
  • 🎁 If that's something that is possible to do please ensure to check if we can add a e2e test.
  • 🔎 If there is a flakiness in the CI tests then don't necessary ignore it, better get the flakyness fixed before merging or if that's not possible there is a good reason to bypass it. (token rate limitation may be a good reason to skip).

@sm43 sm43 changed the title [DND] Auto configure new repo [DND] Auto configure new repo (only with GH app) Oct 11, 2022
@sm43 sm43 changed the title [DND] Auto configure new repo (only with GH app) [DNM] Auto configure new repo (only with GH app) Oct 11, 2022
@codecov-commenter
Copy link

codecov-commenter commented Oct 11, 2022

Codecov Report

Merging #890 (3596e9b) into main (e4dfebd) will decrease coverage by 0.14%.
The diff coverage is 61.42%.

@@            Coverage Diff             @@
##             main     #890      +/-   ##
==========================================
- Coverage   63.93%   63.79%   -0.15%     
==========================================
  Files          80       81       +1     
  Lines        5182     5264      +82     
==========================================
+ Hits         3313     3358      +45     
- Misses       1560     1587      +27     
- Partials      309      319      +10     
Impacted Files Coverage Δ
pkg/adapter/adapter.go 50.00% <0.00%> (-5.00%) ⬇️
pkg/provider/github/github.go 76.12% <ø> (ø)
pkg/provider/github/repository.go 74.13% <74.13%> (ø)
pkg/matcher/repo_runinfo_matcher.go 30.76% <0.00%> (-6.74%) ⬇️
pkg/hub/get.go 82.85% <0.00%> (-0.48%) ⬇️
pkg/random/random.go 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@sm43 sm43 force-pushed the auto-configure-new-repo branch from 76384c3 to 245b69c Compare October 19, 2022 03:31
@sm43 sm43 changed the title [DNM] Auto configure new repo (only with GH app) Auto configure new repo (only with GH app) Oct 19, 2022
Copy link
Member

@chmouel chmouel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One caveat with this implementation is that it only works with newly created repo, but I would imagine this would be more used when existing repos are there and the github apps is installed and then they use PAC...

- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "create"]
- apiGroups: ["pipelinesascode.tekton.dev"]
resources: ["repositories"]
verbs: ["list"]
verbs: ["create", "list"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit annoying thing to give! I wish we could separate those capaibilities depending of feature being enabled or not...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it is possible to do through operator, if enabled then update the controller role.
not possible directly with PAC I think 🤔
but gets complicated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it complicated with operator or when doing it with PAC,

I wonder if we should just disable it by default, document it and let the operator handles it?

I don't think everyone would be using this feature....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with operator it will be easy actually depending on what user configure we an update the role

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/content/docs/install/settings.md Outdated Show resolved Hide resolved
docs/content/docs/install/settings.md Outdated Show resolved Hide resolved
docs/content/docs/install/settings.md Outdated Show resolved Hide resolved
return
}
l.logger.Infof("repository configuration detected: configuring: %v, err: %v", configuring, err)
l.writeResponse(response, http.StatusOK, "rejected")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not understanding this code, is the info string wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if its a repository event -> detected is returned as true
then if the action is created -> configuring is returned as true

here if action is other than created then we return the response as ok but saying rejected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I think I messed it up here.. the status code too
its not a async execution..

  • will return statusCreated is we configure it
  • or just okay if we don't do anything

wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status created sounds fine, did you look in the github documentation if they expect something back?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on github we do see a response code, if pac is down, this will show unreachable here
image

pkg/params/run.go Show resolved Hide resolved
pkg/provider/github/repository.go Show resolved Hide resolved
pkg/provider/github/repository_test.go Show resolved Hide resolved
pkg/provider/github/repository.go Show resolved Hide resolved
@sm43
Copy link
Contributor Author

sm43 commented Oct 20, 2022

One caveat with this implementation is that it only works with newly created repo, but I would imagine this would be more used when existing repos are there and the github apps is installed and then they use PAC...

yeah. I was looking into in if we install github app on all repositories does the old repo emit any event 🤔 but nope.

we have 2 ways now...

  • if auto configure is enabled then list all repos which are attached to github app.. (I guess there should be some api) and then create repository Cr for it.
  • or when we get event from that repo, and we don't find a any repository cr for it. we create one. (not secure but we will have to check where the event is coming from using some regex/org name)

@chmouel
Copy link
Member

chmouel commented Oct 20, 2022

yeah. I was looking into in if we install github app on all repositories does the old repo emit any event thinking but nope.

(about importing existing repo since this PR handles only newly created repos).

maybe having a import flag in the CLI could solve this, it will :

tkn pac import organisation org

will go over every repos in the org organisation and create a namespace/repo using a template.

this could be used as well for other git providers too,

but we need to check with @siamaksade and koustav if that's a customer use case

@sm43
Copy link
Contributor Author

sm43 commented Oct 20, 2022

yeah. I was looking into in if we install github app on all repositories does the old repo emit any event thinking but nope.

(about importing existing repo since this PR handles only newly created repos).

maybe having a import flag in the CLI could solve this, it will :

tkn pac import organisation org

will go over every repos in the org organisation and create a namespace/repo using a template.

this could be used as well for other git providers too,

but we need to check with @siamaksade and koustav if that's a customer use case

yeah but here user will need to provide token, CLI could be an option

@sm43
Copy link
Contributor Author

sm43 commented Oct 20, 2022

yeah. I was looking into in if we install github app on all repositories does the old repo emit any event thinking but nope.

(about importing existing repo since this PR handles only newly created repos).

maybe having a import flag in the CLI could solve this, it will :

tkn pac import organisation org

will go over every repos in the org organisation and create a namespace/repo using a template.

this could be used as well for other git providers too,

but we need to check with @siamaksade and koustav if that's a customer use case

#934

pkg/adapter/adapter.go Outdated Show resolved Hide resolved
}

func generateNamespaceName(nsTemplate string, gitEvent *github.RepositoryEvent) (string, error) {
repoOwner, repoName, err := formatting.GetRepoOwnerSplitted(gitEvent.Repo.GetHTMLURL())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetRepoOwnerSplitter try to handle weird edge case for gitlab or other vcs naming, perhaps you can just use splitGithubURL which should be more robust for github

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetRepoOwnerSplitter is doing a lot more things .. and it error out on normal git url like https://github.com/openshift-pipelines/pipelines-as-code GetRepoOwnerSplitted just split and return owner and repo which is fine I think here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errors out here

return "", "", "", "", fmt.Errorf("URL %s does not seem to be a proper provider url: %w", uri, err)

not sure why that condition is <= 🤔 any reason to have =

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's at least org/repo/path, == would be no subpath too

@sm43 sm43 force-pushed the auto-configure-new-repo branch from c3c0396 to a1fd135 Compare October 21, 2022 07:04
@sm43 sm43 requested a review from chmouel October 21, 2022 07:09
pkg/adapter/adapter.go Outdated Show resolved Hide resolved
l.writeResponse(response, http.StatusOK, "failed to configure")
return
}
l.writeResponse(response, http.StatusOK, "skipped event")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would look weird in log can you write which event has been skipped?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't log this here, this is api response
we log here

logger.Infof("github: repository event \"%v\" is not supported", repoEvent.GetAction())

for skipped repo events

supports template for generating namespace for repos

Signed-off-by: Shivam Mukhade <[email protected]>
@sm43 sm43 force-pushed the auto-configure-new-repo branch from 3596e9b to 08fecbd Compare October 26, 2022 03:33
@chmouel chmouel merged commit 76de58f into openshift-pipelines:main Oct 28, 2022
@sm43 sm43 deleted the auto-configure-new-repo branch October 28, 2022 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants