Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: generate correct commit link for GitHub.com (#5014)
rebelicee580b5535eff
diff --git a/backend/plugin/vcs/github/github.go b/backend/plugin/vcs/github/github.goindex 6b27e056d5..578799e3c3 100644--- a/backend/plugin/vcs/github/github.go+++ b/backend/plugin/vcs/github/github.go@@ -94,6 +94,18 @@ type RepositoryTreeNode struct {Type string `json:"type"`}+// Reference represents a GitHub API response for a reference.+type Reference struct {+ Ref string `json:"ref"`+ NodeID string `json:"node_id"`+ URL string `json:"url"`+ Object struct {+ Type string `json:"type"`+ SHA string `json:"sha"`+ URL string `json:"url"`+ } `json:"object"`+}+// File represents a GitHub API response for a repository file.type File struct {Encoding string `json:"encoding"`@@ -580,7 +592,7 @@ func (p *Provider) CreateFile(ctx context.Context, oauthCtx common.OauthContext,Message: fileCommitCreate.CommitMessage,Content: base64.StdEncoding.EncodeToString([]byte(fileCommitCreate.Content)),Branch: fileCommitCreate.Branch,- SHA: fileCommitCreate.LastCommitID,+ SHA: fileCommitCreate.SHA,}if fileCommitCreate.AuthorName != "" && fileCommitCreate.AuthorEmail != "" {fileCommit.Author = CommitAuthor{@@ -637,7 +649,11 @@ func (p *Provider) OverwriteFile(ctx context.Context, oauthCtx common.OauthConte//// Docs: https://docs.github.com/en/rest/repos/contents#get-repository-contentfunc (p *Provider) ReadFileMeta(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, filePath, ref string) (*vcs.FileMeta, error) {- file, err := p.readFile(ctx, oauthCtx, instanceURL, repositoryID, filePath, ref)+ lastCommitID, err := p.getLastCommitID(ctx, oauthCtx, instanceURL, repositoryID, ref)+ if err != nil {+ return nil, errors.Wrap(err, "get last commit ID")+ }+ file, err := p.readFile(ctx, oauthCtx, instanceURL, repositoryID, filePath, lastCommitID)if err != nil {return nil, errors.Wrap(err, "read file")}@@ -646,10 +662,55 @@ func (p *Provider) ReadFileMeta(ctx context.Context, oauthCtx common.OauthContexName: file.Name,Path: file.Path,Size: file.Size,- LastCommitID: file.SHA,+ SHA: file.SHA,+ LastCommitID: lastCommitID,}, nil}+// getLastCommitID gets the last commit ID of given reference in the repository.+//+// Docs: https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#get-a-reference+func (p *Provider) getLastCommitID(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, ref string) (string, error) {+ url := fmt.Sprintf("%s/repos/%s/git/ref/heads/%s", p.APIURL(instanceURL), repositoryID, ref)++ code, _, body, err := oauth.Get(+ ctx,+ p.client,+ url,+ &oauthCtx.AccessToken,+ tokenRefresher(+ instanceURL,+ oauthContext{+ ClientID: oauthCtx.ClientID,+ ClientSecret: oauthCtx.ClientSecret,+ RefreshToken: oauthCtx.RefreshToken,+ },+ oauthCtx.Refresher,+ ),+ )+ if err != nil {+ return "", errors.Wrapf(err, "GET %s", url)+ }++ if code == http.StatusNotFound {+ return "", common.Errorf(common.NotFound, "failed to get last commit ID from URL %s", url)+ } else if code >= 300 {+ return "",+ errors.Errorf("failed to get last commit ID from URL %s, status code: %d, body: %s",+ url,+ code,+ body,+ )+ }++ var reference Reference+ if err = json.Unmarshal([]byte(body), &reference); err != nil {+ return "", errors.Wrap(err, "unmarshal body")+ }++ return reference.Object.SHA, nil+}+// ReadFileContent reads the content of the given file in the repository.//// Docs: https://docs.github.com/en/rest/repos/contents#get-repository-contentdiff --git a/backend/plugin/vcs/github/github_test.go b/backend/plugin/vcs/github/github_test.goindex f1f443336a..34455a3b8a 100644--- a/backend/plugin/vcs/github/github_test.go+++ b/backend/plugin/vcs/github/github_test.go@@ -530,7 +530,7 @@ func TestProvider_OverwriteFile(t *testing.T) {Branch: "master",Content: "my new file contents",CommitMessage: "update file",- LastCommitID: "7638417db6d59f3c431d3e1f261cc637155684cd",+ SHA: "7638417db6d59f3c431d3e1f261cc637155684cd",},)require.NoError(t, err)@@ -538,11 +538,12 @@ func TestProvider_OverwriteFile(t *testing.T) {func TestProvider_ReadFileMeta(t *testing.T) {p := newMockProvider(func(r *http.Request) (*http.Response, error) {- assert.Equal(t, "/repos/octocat/Hello-World/contents/README.md", r.URL.Path)- return &http.Response{- StatusCode: http.StatusOK,- // Example response derived from https://docs.github.com/en/rest/repos/contents#get-repository-content- Body: io.NopCloser(strings.NewReader(`+ switch r.URL.Path {+ case "/repos/octocat/Hello-World/contents/README.md":+ return &http.Response{+ StatusCode: http.StatusOK,+ // Example response derived from https://docs.github.com/en/rest/repos/contents#get-repository-content+ Body: io.NopCloser(strings.NewReader(`{"type": "file","encoding": "base64",@@ -562,7 +563,28 @@ func TestProvider_ReadFileMeta(t *testing.T) {}}`)),- }, nil+ }, nil+ case "/repos/octocat/Hello-World/git/ref/heads/master":+ return &http.Response{+ StatusCode: http.StatusOK,+ // Example response derived from https://docs.github.com/en/rest/git/refs?apiVersion=2022-11-28#get-a-reference+ Body: io.NopCloser(strings.NewReader(`+{+ "ref": "refs/heads/master",+ "node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlQQ==",+ "url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/master",+ "object": {+ "type": "commit",+ "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",+ "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"+ }+}+ `)),+ }, nil+ default:+ assert.Truef(t, false, "Unsupported API: %s", r.URL.Path)+ return nil, nil+ }},)@@ -574,7 +596,8 @@ func TestProvider_ReadFileMeta(t *testing.T) {Name: "README.md",Path: "README.md",Size: 442,- LastCommitID: "3d21ec53a331a6f037a91c368710b99387d012c1",+ SHA: "3d21ec53a331a6f037a91c368710b99387d012c1",+ LastCommitID: "aa218f56b14c9653891f9e74264a383fa43fefbd",}assert.Equal(t, want, got)}diff --git a/backend/plugin/vcs/vcs.go b/backend/plugin/vcs/vcs.goindex c2d37823c5..e25f2e9a27 100644--- a/backend/plugin/vcs/vcs.go+++ b/backend/plugin/vcs/vcs.go@@ -80,6 +80,7 @@ type FileCommitCreate struct {Content stringCommitMessage stringLastCommitID string+ SHA stringAuthorName stringAuthorEmail string}@@ -90,6 +91,7 @@ type FileMeta struct {Path stringSize int64LastCommitID string+ SHA string}// FileDiffType is the type of file diff.diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.goindex 1cd809cff0..6acd651fc0 100644--- a/backend/runner/taskrun/executor.go+++ b/backend/runner/taskrun/executor.go@@ -632,6 +632,7 @@ func writeBackLatestSchema(ctx context.Context, store *store.Store, repository *)schemaFileCommit.LastCommitID = schemaFileMeta.LastCommitID+ schemaFileCommit.SHA = schemaFileMeta.SHAerr := vcsPlugin.Get(repo2.VCS.Type, vcsPlugin.ProviderConfig{}).OverwriteFile(ctx,common.OauthContext{diff --git a/backend/server/project.go b/backend/server/project.goindex 79dcbe1f69..d716f257bb 100644--- a/backend/server/project.go+++ b/backend/server/project.go@@ -1070,6 +1070,7 @@ func (s *Server) setupVCSSQLReviewBranch(ctx context.Context, repository *api.Refunc (s *Server) setupVCSSQLReviewCIForGitHub(ctx context.Context, repository *api.Repository, branch *vcsPlugin.BranchInfo, sqlReviewEndpoint string) error {sqlReviewConfig := github.SetupSQLReviewCI(sqlReviewEndpoint)fileLastCommitID := ""+ fileSHA := ""fileMeta, err := vcsPlugin.Get(repository.VCS.Type, vcsPlugin.ProviderConfig{}).ReadFileMeta(ctx,@@ -1095,6 +1096,7 @@ func (s *Server) setupVCSSQLReviewCIForGitHub(ctx context.Context, repository *a)} else if fileMeta != nil {fileLastCommitID = fileMeta.LastCommitID+ fileSHA = fileMeta.SHA}return vcsPlugin.Get(repository.VCS.Type, vcsPlugin.ProviderConfig{}).CreateFile(@@ -1114,6 +1116,7 @@ func (s *Server) setupVCSSQLReviewCIForGitHub(ctx context.Context, repository *aCommitMessage: sqlReviewInVCSPRTitle,Content: sqlReviewConfig,LastCommitID: fileLastCommitID,+ SHA: fileSHA,},)}@@ -1223,6 +1226,7 @@ func (s *Server) createOrUpdateVCSSQLReviewFileForGitLab(CommitMessage: sqlReviewInVCSPRTitle,Content: newContent,LastCommitID: fileMeta.LastCommitID,+ SHA: fileMeta.SHA,},)}diff --git a/frontend/src/components/Issue/IssueActivityPanel.vue b/frontend/src/components/Issue/IssueActivityPanel.vueindex 2f9179950b..6b9930a36e 100644--- a/frontend/src/components/Issue/IssueActivityPanel.vue+++ b/frontend/src/components/Issue/IssueActivityPanel.vue@@ -694,6 +694,8 @@ const actionSubject = (activity: Activity): ActionSubject => {const fileCommitActivityUrl = (activity: Activity) => {const payload = activity.payload as ActivityTaskFileCommitPayload;+ if (payload.vcsInstanceUrl.includes("https://github.com"))+ return `${payload.vcsInstanceUrl}/${payload.repositoryFullPath}/commit/${payload.commitId}`;return `${payload.vcsInstanceUrl}/${payload.repositoryFullPath}/-/commit/${payload.commitId}`;};