Skip to content

Commit 04dccf9

Browse files
Merge pull request #4969 from linuxfoundation/unicron-fix-40633-prod
Fix handling GitHub PR commits on rebased PRs (prod)
2 parents 56ce6b4 + 4c7a145 commit 04dccf9

7 files changed

Lines changed: 914 additions & 482 deletions

File tree

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright The Linux Foundation and each contributor to CommunityBridge.
2+
// SPDX-License-Identifier: MIT
3+
4+
package main
5+
6+
// cd cla-backend-go
7+
// go run ./cmd/get_pr_commits mochajs mocha 5803
8+
// go run ./cmd/get_pr_commits mochajs mocha 5686
9+
// go run ./cmd/get_pr_commits mlehotskylf-org2 easycla-dev 30
10+
// go run ./cmd/get_pr_commits mlehotskylf-org2 easycla-dev 36
11+
12+
import (
13+
"context"
14+
"fmt"
15+
"os"
16+
"strconv"
17+
"strings"
18+
19+
gh "github.com/google/go-github/v37/github"
20+
easygh "github.com/linuxfoundation/easycla/cla-backend-go/github"
21+
"golang.org/x/oauth2"
22+
)
23+
24+
func die(msg string) {
25+
fmt.Fprintln(os.Stderr, "ERROR:", msg)
26+
os.Exit(1)
27+
}
28+
29+
func firstLine(s string) string {
30+
if idx := strings.IndexByte(s, '\n'); idx >= 0 {
31+
return s[:idx]
32+
}
33+
return s
34+
}
35+
36+
func newGithubOAuthClient(token string) *gh.Client {
37+
src := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
38+
httpClient := oauth2.NewClient(context.Background(), src)
39+
return gh.NewClient(httpClient)
40+
}
41+
42+
func githubTokenFilePath() string {
43+
if path := strings.TrimSpace(os.Getenv("GITHUB_TOKEN_FILE")); path != "" {
44+
return path
45+
}
46+
return "/etc/github/oauth"
47+
}
48+
49+
func loadGithubToken() (string, error) {
50+
tokenFile := githubTokenFilePath()
51+
//nolint:gosec // G304: developer helper intentionally allows explicit local token-file override via GITHUB_TOKEN_FILE.
52+
tokenBytes, err := os.ReadFile(tokenFile)
53+
if err != nil {
54+
return "", fmt.Errorf("unable to read %s: %w", tokenFile, err)
55+
}
56+
token := strings.TrimSpace(string(tokenBytes))
57+
if token == "" {
58+
return "", fmt.Errorf("%s is empty", tokenFile)
59+
}
60+
return token, nil
61+
}
62+
63+
func main() {
64+
if len(os.Args) != 4 {
65+
die(fmt.Sprintf("usage: %s <org> <repo> <pr_number>", os.Args[0]))
66+
}
67+
68+
owner := strings.TrimSpace(os.Args[1])
69+
repo := strings.TrimSpace(os.Args[2])
70+
71+
prNumber, err := strconv.Atoi(os.Args[3])
72+
if err != nil {
73+
die("invalid pr_number: " + os.Args[3])
74+
}
75+
token, err := loadGithubToken()
76+
if err != nil {
77+
die(err.Error())
78+
}
79+
80+
client := newGithubOAuthClient(token)
81+
82+
commits, err := easygh.ListPullRequestCommitsCompare(context.Background(), client, owner, repo, prNumber)
83+
if err != nil {
84+
die(err.Error())
85+
}
86+
87+
fmt.Println("compare results:")
88+
fmt.Printf("count: %d\n", len(commits))
89+
ac := 0
90+
for _, c := range commits {
91+
if c == nil {
92+
continue
93+
}
94+
fmt.Printf(
95+
"%s | author_id: %d | login: %s | name: %s | email: %s | msg: %s\n",
96+
c.GetSHA(),
97+
c.GetAuthor().GetID(),
98+
c.GetAuthor().GetLogin(),
99+
c.GetAuthor().GetName(),
100+
c.GetAuthor().GetEmail(),
101+
firstLine(c.GetCommit().GetMessage()),
102+
)
103+
ac++
104+
}
105+
fmt.Printf("actual count: %d\n", ac)
106+
}

0 commit comments

Comments
 (0)