package ghstatus import ( "context" "fmt" "os" "strconv" "strings" "github.com/google/go-github/v47/github" githubclient "edge-infra.dev/pkg/f8n/devinfra/github-client" ) func Run() error { pkeyFile, found := os.LookupEnv("private_key_file") if !found { return fmt.Errorf("couldnt find app private key") } pkey, err := os.ReadFile(pkeyFile) if err != nil { return err } state, found := os.LookupEnv("state") if !found { return fmt.Errorf("couldnt find the state") } // sanitize argo statuses state = sanitizeArgoStatuses(state) targetURL, found := os.LookupEnv("target_url") if !found { return fmt.Errorf("couldnt find the target url") } description, found := os.LookupEnv("description") if !found { return fmt.Errorf("couldnt find the description") } title, found := os.LookupEnv("context") if !found { return fmt.Errorf("couldnt find the context") } repo := os.Getenv("repo") org := os.Getenv("org") orgRepo, found := os.LookupEnv("orgrepo") if !found { return fmt.Errorf("couldnt find the repo") } orgRepoArr := strings.Split(orgRepo, "/") if len(orgRepoArr) > 1 { org = orgRepoArr[0] repo = orgRepoArr[1] } // make sure org and repo were set at some point if org == "" { return fmt.Errorf("couldnt find the organization") } if repo == "" { return fmt.Errorf("couldnt find the repo") } sha, found := os.LookupEnv("commit_sha") if !found { return fmt.Errorf("couldnt find the commit sha") } appIDSTR, found := os.LookupEnv("app_id") if !found { return fmt.Errorf("couldnt find the appID") } appID, err := strconv.ParseInt(appIDSTR, 10, 64) if err != nil { return fmt.Errorf("failed to convert appID from string to int64: %w", err) } config := githubclient.Config{ Repository: fmt.Sprintf("https://github.com/%s/%s", org, repo), AppID: appID, PrivateKey: pkey, } client, err := githubclient.NewClient(config) if err != nil { return err } if client.Client == nil { return fmt.Errorf("the Github client.Client is nil") } status := &github.RepoStatus{ State: &state, // pending, success, error, or failure TargetURL: &targetURL, Description: &description, Context: &title, } _, resp, err := client.Client.Repositories.CreateStatus( context.Background(), org, repo, sha, status, ) fmt.Println(resp.StatusCode) return err } // sanitizeArgoStatuses maps argos statuses to gh statuses // anything not matched will fallthrough func sanitizeArgoStatuses(s string) string { // argo statuses: Succeeded, Failed, Error // GH commit statuses: pending, success, error, or failure. switch s { case "Succeeded": return "success" case "Failed": return "failure" case "Error": return "error" } return s }