Skip to content

Commit

Permalink
Sammyteillet/dat 30 aau in the explanation bullet point of (#30)
Browse files Browse the repository at this point in the history
* feature: use new version of go-github

* feature: add comment in report

* feature: add comments in process history result
  • Loading branch information
Samox committed May 12, 2023
1 parent ba0572b commit 83c8680
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 24 deletions.
11 changes: 1 addition & 10 deletions debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package debug

import (
"context"
"fmt"
"os"
"strconv"
Expand All @@ -17,7 +16,7 @@ import (
func DebugFunction() {
// Perform debugging operations
fmt.Println("Manual Sync ...")
githubToken := os.Getenv("GITHUB_TOKEN")
// githubToken := os.Getenv("GITHUB_TOKEN")
githubRepoOwner := os.Getenv("GITHUB_REPO_OWNER")
githubRepoName := os.Getenv("GITHUB_REPO_NAME")
githubRepoFilePath := os.Getenv("GITHUB_REPO_FILE_PATH")
Expand All @@ -33,16 +32,8 @@ func DebugFunction() {
_ = notion_database.AssertDatabaseHasDatadriftProperties(notionDatabaseID, notionAPIKey)

client, _ := github.CreateClientFromGithubApp(int64(githubApplicationId))
ctx := context.Background()

config, _ := github.VerifyConfigFile(client, githubRepoOwner, githubRepoName, ctx)
if config.Metrics[0].TimeGrains[0] != common.Day {
return
}

if filepath == "" {

client := github.CreateClientFromGithubToken(githubToken)
newFilepath, err := history.ProcessHistory(client, githubRepoOwner, githubRepoName, common.Metric{
MetricName: "Default metric name",
KPIColumnName: kpiColumn,
Expand Down
2 changes: 1 addition & 1 deletion github/client-factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"

"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/github"
"github.com/google/go-github/v42/github"
"golang.org/x/oauth2"
)

Expand Down
2 changes: 1 addition & 1 deletion github/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func CheckGithubAppConnection() (string, error) {
ctx := context.Background()

// Get the last commit of the repository
commit, _, err := client.Repositories.GetCommit(ctx, "Samox", "copy-libeo-data-history", "main")
commit, _, err := client.Repositories.GetCommit(ctx, "Samox", "copy-libeo-data-history", "main", nil)

if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions github/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/data-drift/kpi-git-history/history"
"github.com/data-drift/kpi-git-history/reports"
"github.com/gin-gonic/gin"
"github.com/google/go-github/github"
"github.com/google/go-github/v42/github"
"github.com/xeipuuv/gojsonschema"
)

Expand Down Expand Up @@ -118,7 +118,7 @@ func processWebhookInTheBackground(config common.Config, c *gin.Context, Install

func VerifyConfigFile(client *github.Client, RepoOwner string, RepoName string, ctx context.Context) (common.Config, error) {

commit, _, _ := client.Repositories.GetCommit(ctx, RepoOwner, RepoName, "main")
commit, _, _ := client.Repositories.GetCommit(ctx, RepoOwner, RepoName, "main", nil)

file, _, _, err := client.Repositories.GetContents(ctx, RepoOwner, RepoName, configFilePath, &github.RepositoryContentGetOptions{
Ref: *commit.SHA,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/data-drift/kpi-git-history
go 1.20

require (
github.com/google/go-github v17.0.0+incompatible
github.com/google/go-github/v42 v42.0.0
github.com/joho/godotenv v1.5.1
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-github v17.0.0+incompatible h1:N0LgJ1j65A7kfXrZnUDaYCs/Sf4rEjNlfyDHW9dolSY=
github.com/google/go-github v17.0.0+incompatible/go.mod h1:zLgOLi98H3fifZn+44m+umXrS52loVEgC2AApnigrVQ=
github.com/google/go-github/v29 v29.0.2 h1:opYN6Wc7DOz7Ku3Oh4l7prmkOMwEcQxpFtxdU8N8Pts=
github.com/google/go-github/v29 v29.0.2/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
github.com/google/go-github/v42 v42.0.0 h1:YNT0FwjPrEysRkLIiKuEfSvBPCGKphW5aS5PxwaoLec=
github.com/google/go-github/v42 v42.0.0/go.mod h1:jgg/jvyI0YlDOM1/ps6XYh04HNQ3vKf0CVko62/EhRg=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
Expand Down
27 changes: 20 additions & 7 deletions history/process-history.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,31 @@ import (
"time"

"github.com/data-drift/kpi-git-history/common"
"github.com/google/go-github/github"
"github.com/google/go-github/v42/github"
"github.com/shopspring/decimal"
)

type CommitSha string
type PeriodId string

type CommitMessage struct {
MessageAuthor string
MessageBody string
}
type PeriodCommitData struct {
Lines int
KPI decimal.Decimal
CommitTimestamp int64
CommitUrl string
CommitMessages []CommitMessage
}

type PeriodData map[PeriodId]map[CommitSha]PeriodCommitData

func ProcessHistory(client *github.Client, repoOwner string, repoName string, metric common.Metric) (string, error) {

ctx := context.Background()

filePath := metric.Filepath
dateColumnName := metric.DateColumnName
KPIColumnName := metric.KPIColumnName
Expand Down Expand Up @@ -61,11 +68,17 @@ func ProcessHistory(client *github.Client, repoOwner string, repoName string, me
// Group the lines of the CSV file by reporting date.
lineCountAndKPIByDateByVersion := make(PeriodData)
for index, commit := range commits {
var commitMessages []CommitMessage
fmt.Printf("\r Commit %d/%d", index, len(commits))

commitSha := CommitSha(*commit.SHA)

commitDate := commit.Commit.Committer.Date
commitComments := GetCommitComments(client, ctx, repoOwner, repoName, *commit.SHA)

for _, comment := range commitComments {
commitMessages = append(commitMessages, CommitMessage{MessageBody: *comment.Body, MessageAuthor: *comment.User.Login})
}
commitTimestamp := commitDate.Unix()
fileContents, err := getFileContentsForCommit(client, repoOwner, repoName, filePath, *commit.SHA)
if err != nil {
Expand Down Expand Up @@ -120,12 +133,12 @@ func ProcessHistory(client *github.Client, repoOwner string, repoName string, me
newLineCount := lineCountAndKPIByDateByVersion[periodKey][commitSha].Lines + 1
newKPI := kpi.Add(lineCountAndKPIByDateByVersion[periodKey][commitSha].KPI)

lineCountAndKPIByDateByVersion[periodKey][commitSha] = struct {
Lines int
KPI decimal.Decimal
CommitTimestamp int64
CommitUrl string
}{Lines: newLineCount, KPI: newKPI, CommitTimestamp: commitTimestamp, CommitUrl: *commit.HTMLURL}
lineCountAndKPIByDateByVersion[periodKey][commitSha] = PeriodCommitData{
Lines: newLineCount,
KPI: newKPI,
CommitTimestamp: commitTimestamp,
CommitUrl: *commit.HTMLURL,
CommitMessages: commitMessages}
}
}

Expand Down
26 changes: 26 additions & 0 deletions history/pull-request-comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package history

import (
"context"

"github.com/google/go-github/v42/github"
)

func GetCommitComments(client *github.Client, ctx context.Context, RepoOwner string, RepoName string, commitSha string) []*github.IssueComment {
pullRequests, _, err := client.PullRequests.ListPullRequestsWithCommit(ctx, RepoOwner, RepoName, commitSha, nil)
if err != nil {
println("[DATADRIFT ERROR] getting pull request:", err.Error())
return []*github.IssueComment{}
}
if len(pullRequests) == 0 {
println("No pull request found")
return []*github.IssueComment{}
}
comments, _, err := client.Issues.ListComments(ctx, RepoOwner, RepoName, pullRequests[0].GetNumber(), nil)
if err != nil {
println("[DATADRIFT ERROR] getting comments:", err.Error())
return []*github.IssueComment{}
}

return comments
}

0 comments on commit 83c8680

Please sign in to comment.