...
1 package ghstatus
2
3 import (
4 "context"
5 "fmt"
6 "os"
7 "strconv"
8 "strings"
9
10 "github.com/google/go-github/v47/github"
11
12 githubclient "edge-infra.dev/pkg/f8n/devinfra/github-client"
13 )
14
15 func Run() error {
16 pkeyFile, found := os.LookupEnv("private_key_file")
17 if !found {
18 return fmt.Errorf("couldnt find app private key")
19 }
20
21 pkey, err := os.ReadFile(pkeyFile)
22 if err != nil {
23 return err
24 }
25
26 state, found := os.LookupEnv("state")
27 if !found {
28 return fmt.Errorf("couldnt find the state")
29 }
30
31 state = sanitizeArgoStatuses(state)
32
33 targetURL, found := os.LookupEnv("target_url")
34 if !found {
35 return fmt.Errorf("couldnt find the target url")
36 }
37
38 description, found := os.LookupEnv("description")
39 if !found {
40 return fmt.Errorf("couldnt find the description")
41 }
42
43 title, found := os.LookupEnv("context")
44 if !found {
45 return fmt.Errorf("couldnt find the context")
46 }
47
48 repo := os.Getenv("repo")
49 org := os.Getenv("org")
50 orgRepo, found := os.LookupEnv("orgrepo")
51 if !found {
52 return fmt.Errorf("couldnt find the repo")
53 }
54
55 orgRepoArr := strings.Split(orgRepo, "/")
56 if len(orgRepoArr) > 1 {
57 org = orgRepoArr[0]
58 repo = orgRepoArr[1]
59 }
60
61
62 if org == "" {
63 return fmt.Errorf("couldnt find the organization")
64 }
65
66 if repo == "" {
67 return fmt.Errorf("couldnt find the repo")
68 }
69
70 sha, found := os.LookupEnv("commit_sha")
71 if !found {
72 return fmt.Errorf("couldnt find the commit sha")
73 }
74
75 appIDSTR, found := os.LookupEnv("app_id")
76 if !found {
77 return fmt.Errorf("couldnt find the appID")
78 }
79
80 appID, err := strconv.ParseInt(appIDSTR, 10, 64)
81 if err != nil {
82 return fmt.Errorf("failed to convert appID from string to int64: %w", err)
83 }
84
85 config := githubclient.Config{
86 Repository: fmt.Sprintf("https://github.com/%s/%s", org, repo),
87 AppID: appID,
88 PrivateKey: pkey,
89 }
90
91 client, err := githubclient.NewClient(config)
92 if err != nil {
93 return err
94 }
95 if client.Client == nil {
96 return fmt.Errorf("the Github client.Client is nil")
97 }
98
99 status := &github.RepoStatus{
100 State: &state,
101 TargetURL: &targetURL,
102 Description: &description,
103 Context: &title,
104 }
105
106 _, resp, err := client.Client.Repositories.CreateStatus(
107 context.Background(),
108 org,
109 repo,
110 sha,
111 status,
112 )
113
114 fmt.Println(resp.StatusCode)
115
116 return err
117 }
118
119
120
121 func sanitizeArgoStatuses(s string) string {
122
123
124 switch s {
125 case "Succeeded":
126 return "success"
127 case "Failed":
128 return "failure"
129 case "Error":
130 return "error"
131 }
132 return s
133 }
134
View as plain text