github.com/decred/politeia@v1.4.0/politeiawww/legacy/codetracker/github/convert.go (about) 1 // Copyright (c) 2020 The Decred developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package github 6 7 import ( 8 "strconv" 9 "time" 10 11 "github.com/decred/politeia/politeiawww/legacy/codetracker" 12 "github.com/decred/politeia/politeiawww/legacy/codetracker/github/api" 13 "github.com/decred/politeia/politeiawww/legacy/codetracker/github/database" 14 ) 15 16 const githubPullURL = "https://github.com" 17 18 func convertAPICommitsToDbComits(apiCommits []*api.PullRequestCommit, org, repoName string) []*database.Commit { 19 dbCommits := make([]*database.Commit, 0, len(apiCommits)) 20 for _, commit := range apiCommits { 21 parentSHA := "" 22 parentURL := "" 23 if len(commit.Parents) > 0 { 24 parentSHA = commit.Parents[0].SHA 25 parentURL = commit.Parents[0].URL 26 } 27 dbCommits = append(dbCommits, &database.Commit{ 28 SHA: commit.SHA, 29 URL: commit.URL, 30 Message: commit.Commit.Message, 31 Author: commit.Author.Login, 32 Committer: commit.Committer.Login, 33 Date: parseTime(commit.Commit.Author.Date).Unix(), 34 Additions: commit.Stats.Additions, 35 Deletions: commit.Stats.Deletions, 36 ParentSHA: parentSHA, 37 ParentURL: parentURL, 38 Repo: repoName, 39 Organization: org, 40 }) 41 } 42 return dbCommits 43 } 44 func convertAPIPullRequestToDbPullRequest(apiPR *api.PullRequest, repoName, org string) (*database.PullRequest, error) { 45 url := githubPullURL + "/" + org + "/" + repoName + "/pull/" + 46 strconv.Itoa(apiPR.Number) 47 dbPR := &database.PullRequest{ 48 ID: url + apiPR.UpdatedAt, 49 Repo: repoName, 50 Organization: org, 51 User: apiPR.User.Login, 52 URL: url, 53 Number: apiPR.Number, 54 State: apiPR.State, 55 Additions: apiPR.Additions, 56 Deletions: apiPR.Deletions, 57 } 58 if apiPR.MergedAt != "" { 59 mergedAt, err := time.Parse(time.RFC3339, apiPR.MergedAt) 60 if err != nil { 61 return nil, err 62 } 63 dbPR.MergedAt = mergedAt.Unix() 64 } 65 if apiPR.UpdatedAt != "" { 66 updatedAt, err := time.Parse(time.RFC3339, apiPR.UpdatedAt) 67 if err != nil { 68 return nil, err 69 } 70 dbPR.UpdatedAt = updatedAt.Unix() 71 } 72 return dbPR, nil 73 } 74 75 func convertAPIReviewsToDbReviews(apiReviews []api.PullRequestReview, repo string, prNumber int, url string) []database.PullRequestReview { 76 dbReviews := make([]database.PullRequestReview, 0, len(apiReviews)) 77 for _, review := range apiReviews { 78 dbReview := convertAPIReviewToDbReview(review) 79 dbReview.Repo = repo 80 dbReview.Number = prNumber 81 dbReview.PullRequestURL = url 82 dbReviews = append(dbReviews, dbReview) 83 } 84 return dbReviews 85 } 86 87 func convertAPIReviewToDbReview(apiReview api.PullRequestReview) database.PullRequestReview { 88 dbReview := database.PullRequestReview{ 89 ID: apiReview.ID, 90 Author: apiReview.User.Login, 91 State: apiReview.State, 92 SubmittedAt: parseTime(apiReview.SubmittedAt).Unix(), 93 CommitID: apiReview.CommitID, 94 } 95 return dbReview 96 } 97 98 func convertDBPullRequestsToPullRequests(dbPRs []*database.PullRequest) []codetracker.PullRequestInformation { 99 prInfo := make([]codetracker.PullRequestInformation, 0, len(dbPRs)) 100 for _, dbPR := range dbPRs { 101 prInfo = append(prInfo, codetracker.PullRequestInformation{ 102 URL: dbPR.URL, 103 Repository: dbPR.Repo, 104 Additions: int64(dbPR.Additions), 105 Deletions: int64(dbPR.Deletions), 106 Date: time.Unix(dbPR.UpdatedAt, 0).Format(time.RFC1123), 107 Number: dbPR.Number, 108 State: dbPR.State, 109 }) 110 } 111 return prInfo 112 } 113 114 func convertDBPullRequestReviewsToReviews(dbReviews []database.PullRequestReview) []codetracker.ReviewInformation { 115 reviewInfo := make([]codetracker.ReviewInformation, 0, len(dbReviews)) 116 for _, dbReview := range dbReviews { 117 reviewInfo = append(reviewInfo, codetracker.ReviewInformation{ 118 URL: dbReview.PullRequestURL, 119 State: dbReview.State, 120 Number: dbReview.Number, 121 Repository: dbReview.Repo, 122 Additions: dbReview.Additions, 123 Deletions: dbReview.Deletions, 124 }) 125 } 126 return reviewInfo 127 } 128 129 func convertDBCommitsToCommits(dbCommits []database.Commit) []codetracker.CommitInformation { 130 commitInfo := make([]codetracker.CommitInformation, 0, len(dbCommits)) 131 for _, dbCommit := range dbCommits { 132 commitInfo = append(commitInfo, codetracker.CommitInformation{ 133 SHA: dbCommit.SHA, 134 URL: dbCommit.URL, 135 Repository: dbCommit.Repo, 136 Additions: dbCommit.Additions, 137 Deletions: dbCommit.Deletions, 138 Date: dbCommit.Date, 139 }) 140 } 141 return commitInfo 142 }