|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/x509" |
| 5 | + "encoding/json" |
| 6 | + "encoding/pem" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/go-resty/resty/v2" |
| 14 | + "github.com/golang-jwt/jwt" |
| 15 | +) |
| 16 | + |
| 17 | +type Installation struct { |
| 18 | + ID int `json:"id"` |
| 19 | + Account struct { |
| 20 | + Login string `json:"login"` |
| 21 | + } `json:"account"` |
| 22 | +} |
| 23 | + |
| 24 | +func main() { |
| 25 | + |
| 26 | + ghAppId := os.Getenv("GITHUB_APP_ID") |
| 27 | + ghAppPkFile := os.Getenv("GITHUB_APP_PK_FILE") |
| 28 | + ghOrg := os.Getenv("GITHUB_ORG") |
| 29 | + |
| 30 | + if ghAppId == "" || ghAppPkFile == "" || ghOrg == "" { |
| 31 | + log.Fatalf("Environment variables GITHUB_APP_ID, GITHUB_APP_PK_FILE and GITHUB_ORG must be passed to this program.") |
| 32 | + } |
| 33 | + |
| 34 | + jwtToken := getJwtToken(ghAppId, ghAppPkFile) |
| 35 | + installationId := getInstallationId(jwtToken, ghOrg) |
| 36 | + accessToken := getAccessToken(jwtToken, installationId) |
| 37 | + |
| 38 | + fmt.Printf("GITHUB_TOKEN=%s\n", accessToken) |
| 39 | +} |
| 40 | + |
| 41 | +func getJwtToken(ghAppId string, ghAppPkFile string) string { |
| 42 | + |
| 43 | + pemContent, _ := ioutil.ReadFile(ghAppPkFile) |
| 44 | + block, _ := pem.Decode(pemContent) |
| 45 | + privateKey, _ := x509.ParsePKCS1PrivateKey(block.Bytes) |
| 46 | + token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{ |
| 47 | + "iat": time.Now().Unix(), |
| 48 | + "exp": time.Now().Add(10 * time.Minute).Unix(), |
| 49 | + "iss": ghAppId, |
| 50 | + }) |
| 51 | + jwtToken, _ := token.SignedString(privateKey) |
| 52 | + |
| 53 | + return jwtToken |
| 54 | +} |
| 55 | + |
| 56 | +func getInstallationId(jwtToken string, ghOrg string) int { |
| 57 | + |
| 58 | + client := resty.New() |
| 59 | + resp, _ := client.R(). |
| 60 | + SetHeader("Authorization", "Bearer "+jwtToken). |
| 61 | + SetHeader("Accept", "application/vnd.github.v3+json"). |
| 62 | + Get("https://api.github.com/app/installations") |
| 63 | + |
| 64 | + var installations []Installation |
| 65 | + json.Unmarshal(resp.Body(), &installations) |
| 66 | + installationId := 0 |
| 67 | + for _, installation := range installations { |
| 68 | + if installation.Account.Login == ghOrg { |
| 69 | + installationId = installation.ID |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return installationId |
| 74 | +} |
| 75 | + |
| 76 | +func getAccessToken(jwtToken string, installationId int) string { |
| 77 | + |
| 78 | + client := resty.New() |
| 79 | + resp, _ := client.R(). |
| 80 | + SetHeader("Authorization", "Bearer "+jwtToken). |
| 81 | + SetHeader("Accept", "application/vnd.github.v3+json"). |
| 82 | + Post(fmt.Sprintf("https://api.github.com/app/installations/%d/access_tokens", installationId)) |
| 83 | + |
| 84 | + var result map[string]interface{} |
| 85 | + json.Unmarshal(resp.Body(), &result) |
| 86 | + |
| 87 | + return result["token"].(string) |
| 88 | +} |
0 commit comments