Skip to content

Commit

Permalink
Get commiter email dynamically from github application for renovate n…
Browse files Browse the repository at this point in the history
…udging PRs

STONEBLD-3194

Signed-off-by: Robert Cerven <[email protected]>
  • Loading branch information
rcerven committed Feb 17, 2025
1 parent 79c8a5d commit 72de6c2
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var _ = Describe("Component nudge controller", func() {
Token: "some_token",
ID: 1,
Repositories: []*gitgithub.Repository{{Name: &repo_name, FullName: &repo_fullname, DefaultBranch: &repo_defaultbranch}},
}, "slug", nil
}, TestGitHubAppName, nil
}
})

Expand Down Expand Up @@ -495,6 +495,10 @@ var _ = Describe("Component nudge controller", func() {
RenovateConfigMapPlatformAutomergeKey: "false",
RenovateConfigMapIgnoreTestsKey: "true",
}
gitHubAppUsername := fmt.Sprintf("%s[bot]", TestGitHubAppName)
gitHubAppGitAuthor := fmt.Sprintf("%s <%d+%[email protected]>", TestGitHubAppName, TestGitHubAppId, gitHubAppUsername)
gitHubAppSignedOff := fmt.Sprintf("Signed-off-by: %s", gitHubAppGitAuthor)
gitHubAppUsed := 0
assertRenovateConfiMap(nil, nil, customConfigMapData, imageBuiltFrom)

renovateConfigMaps := getRenovateConfigMapList()
Expand All @@ -514,10 +518,16 @@ var _ = Describe("Component nudge controller", func() {
Expect(strings.Contains(renovateConfigObj.PackageRules[1].CommitBody, commitBodyValue)).Should(BeTrue())
Expect(renovateConfigObj.CustomManagers[0].FileMatch).Should(Equal(fileMatchValue))
Expect(strings.HasSuffix(key, customConfigType))
if renovateConfigObj.Username == gitHubAppUsername {
gitHubAppUsed++
Expect(renovateConfigObj.GitAuthor).Should(Equal(gitHubAppGitAuthor))
Expect(strings.Contains(renovateConfigObj.PackageRules[1].CommitBody, gitHubAppSignedOff)).Should(BeTrue())
}
}
break
}
}
Expect(gitHubAppUsed).Should(Equal(2))
})

It("Test build performs nudge on success, only component renovate config provided", func() {
Expand Down
27 changes: 25 additions & 2 deletions internal/controller/renovate_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ func (u ComponentDependenciesUpdater) GetUpdateTargetsGithubApp(ctx context.Cont
// Match installed repositories with Components and get custom branch if defined
targetsToUpdate := []updateTarget{}
var slug string
var appBotName string
var appBotId int64
for _, comp := range componentList {
component := comp
if component.Spec.Source.GitSource == nil {
Expand Down Expand Up @@ -287,6 +289,27 @@ func (u ComponentDependenciesUpdater) GetUpdateTargetsGithubApp(ctx context.Cont
continue
}

if appBotId == 0 {
pacConfig := map[string][]byte{PipelinesAsCodeGithubPrivateKey: []byte(privateKey), PipelinesAsCodeGithubAppIdKey: []byte(githubAppIdStr)}
gitClient, err := gitproviderfactory.CreateGitClient(gitproviderfactory.GitClientConfig{
PacSecretData: pacConfig,
GitProvider: gitProvider,
RepoUrl: url,
IsAppInstallationExpected: true,
})
if err != nil {
log.Error(err, "error create git client for component", "ComponentName", component.Name, "RepoUrl", component.Spec.Source.GitSource.URL)
continue
}
appBotName = fmt.Sprintf("%s[bot]", slug)
botID, err := gitClient.GetAppUserID(appBotName)
if err != nil {
log.Error(err, "Failed to get application user info", "userName", appBotName)
continue
}
appBotId = botID
}

branch := gitSource.Revision
if branch == "" {
branch = git.InternalDefaultBranch
Expand Down Expand Up @@ -320,8 +343,8 @@ func (u ComponentDependenciesUpdater) GetUpdateTargetsGithubApp(ctx context.Cont
ComponentName: component.Name,
ComponentCustomRenovateOptions: customRenovateOptions,
GitProvider: gitProvider,
Username: fmt.Sprintf("%s[bot]", slug),
GitAuthor: fmt.Sprintf("%s <126015336+%s[bot]@users.noreply.github.com>", slug, slug),
Username: appBotName,
GitAuthor: fmt.Sprintf("%s <%d+%[email protected]>", slug, appBotId, appBotName),
Token: githubAppInstallation.Token,
Endpoint: git.BuildAPIEndpoint("github").APIEndpoint("github.com"),
Repositories: repositories,
Expand Down
9 changes: 9 additions & 0 deletions internal/controller/suite_util_gitprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
testGitProviderClient = &TestGitProviderClient{}
DefaultBrowseRepository = "https://githost.com/user/repo?rev="
UndoPacMergeRequestURL = "https://githost.com/mr/5678"
TestGitHubAppName = "test-github-app"
TestGitHubAppId = int64(1234567890)

EnsurePaCMergeRequestFunc func(repoUrl string, data *gp.MergeRequestData) (webUrl string, err error)
UndoPaCMergeRequestFunc func(repoUrl string, data *gp.MergeRequestData) (webUrl string, err error)
Expand All @@ -38,6 +40,7 @@ var (
IsFileExistFunc func(repoUrl, branchName, filePath string) (bool, error)
IsRepositoryPublicFunc func(repoUrl string) (bool, error)
GetConfiguredGitAppNameFunc func() (string, string, error)
GetAppUserIDFunc func(userName string) (int64, error)
)

func ResetTestGitProviderClient() {
Expand Down Expand Up @@ -81,6 +84,9 @@ func ResetTestGitProviderClient() {
GetConfiguredGitAppNameFunc = func() (string, string, error) {
return "git-app-name", "slug", nil
}
GetAppUserIDFunc = func(userName string) (int64, error) {
return TestGitHubAppId, nil
}
}

var _ gp.GitProviderClient = (*TestGitProviderClient)(nil)
Expand Down Expand Up @@ -123,3 +129,6 @@ func (*TestGitProviderClient) IsRepositoryPublic(repoUrl string) (bool, error) {
func (*TestGitProviderClient) GetConfiguredGitAppName() (string, string, error) {
return GetConfiguredGitAppNameFunc()
}
func (*TestGitProviderClient) GetAppUserID(userName string) (int64, error) {
return GetAppUserIDFunc(userName)
}
5 changes: 5 additions & 0 deletions pkg/git/github/github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ func (g *GithubClient) GetBrowseRepositoryAtShaLink(repoUrl, sha string) string
return fmt.Sprintf("%s/%s/%s?rev=%s", gitProviderHost, owner, repository, sha)
}

// GetAppUserID get info about application user
func (g *GithubClient) GetAppUserID(userName string) (int64, error) {
return g.getAppUserID(userName)
}

func newGithubClient(accessToken string) *GithubClient {
gh := &GithubClient{}
gh.ctx = context.TODO()
Expand Down
8 changes: 8 additions & 0 deletions pkg/git/github/github_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,11 @@ func (g *GithubClient) getRepositoryInfo(owner, repository string) (*github.Repo
}
return repo, nil
}

func (g *GithubClient) getAppUserID(userName string) (int64, error) {
user, _, err := g.client.Users.Get(g.ctx, userName)
if err != nil {
return 0, err
}
return *user.ID, nil
}
4 changes: 4 additions & 0 deletions pkg/git/gitlab/gitlab_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ func (g *GitlabClient) GetConfiguredGitAppName() (string, string, error) {
return "", "", fmt.Errorf("GitLab does not support applications")
}

func (g *GitlabClient) GetAppUserID(username string) (int64, error) {
return 0, fmt.Errorf("GitLab does not support applications")
}

func newGitlabClient(accessToken, baseUrl string) (*GitlabClient, error) {
glc := &GitlabClient{}
c, err := gitlab.NewClient(accessToken, gitlab.WithBaseURL(baseUrl))
Expand Down
3 changes: 3 additions & 0 deletions pkg/git/gitprovider/gitprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type GitProviderClient interface {
// GetConfiguredGitAppName returns configured git application name and id.
// Not all git providers support applications. Currently only GitHub does.
GetConfiguredGitAppName() (string, string, error)

// GetAppUserID get info about application user
GetAppUserID(userName string) (int64, error)
}

type MergeRequestData struct {
Expand Down

0 comments on commit 72de6c2

Please sign in to comment.