Last active
December 17, 2021 14:26
-
-
Save syossan27/f8ed6033de68a4354a41c53879b60f33 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 指定した環境を未使用状態に切り替えるgithub actionsを実行し、リポジトリのトップへリダイレクト | |
package p | |
import ( | |
"bytes" | |
"context" | |
"log" | |
"net/http" | |
"os" | |
secretmanager "cloud.google.com/go/secretmanager/apiv1" | |
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1" | |
) | |
func changeUsageStatus(w http.ResponseWriter, r *http.Request) { | |
// 切り替えるアプリケーションを取得(app1~app4) | |
envParam, ok := r.URL.Query()["env"] | |
if !ok || len(envParam) == 0 { | |
log.Fatal("Invalid env param") | |
} | |
env := envParam[0] | |
// 切り替える開発環境を取得(1~3) | |
numParam, ok := r.URL.Query()["num"] | |
if !ok || len(numParam) == 0 { | |
log.Fatal("Invalid env param") | |
} | |
num := numParam[0] | |
// repository_dispatchを通してGitHub Actionsで実行するためのevent_typeを指定したJSON | |
jsonStr := `{"event_type":"change_env_` + num + `_by_button"}` | |
// GitHub Actionsを実行するためのURLと、Cloud Functions実行後のリダイレクト先をアプリケーション毎に設定 | |
var url, redirectUrl string | |
switch env { | |
case "app-1": | |
url = "https://api.github.com/repos/example-owner/app-1/dispatches" | |
redirectUrl = "https://github.com/example-owner/app-1" | |
case "app-2": | |
url = "https://api.github.com/repos/example-owner/app-2/dispatches" | |
redirectUrl = "https://github.com/example-owner/app-2" | |
case "app-3": | |
url = "https://api.github.com/repos/example-owner/app-3/dispatches" | |
redirectUrl = "https://github.com/example-owner/app-3" | |
case "app-4": | |
url = "https://api.github.com/repos/example-owner/app-4/dispatches" | |
redirectUrl = "https://github.com/example-owner/app-4" | |
} | |
if url == "" { | |
log.Fatal("Invalid env") | |
} | |
// GitHub Actionsの実行リクエストの作成 | |
req, err := http.NewRequest( | |
"POST", | |
url, | |
bytes.NewBuffer([]byte(jsonStr)), | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Cloud Functionsに設定した環境変数を使って | |
// Secret Managerから作成したPersonal access tokensを取得します | |
ctx := context.Background() | |
secClient, err := secretmanager.NewClient(ctx) | |
if err != nil { | |
log.Fatal(err) | |
} | |
secReq := &secretmanagerpb.AccessSecretVersionRequest{ | |
Name: os.Getenv("SECRET_RESOURCE_ID"), | |
} | |
result, err := secClient.AccessSecretVersion(ctx, secReq) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// 作成したリクエストに必要なヘッダーを設定 | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Authorization", "token "+string(result.Payload.Data)) | |
req.Header.Set("Accept", "application/vnd.github.everest-preview+json") | |
// GitHub Actionsを実行 | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer resp.Body.Close() | |
// リダイレクト | |
w.Header().Set("Content-Type", "text/html") | |
w.Header().Set("Cache-Control", "no-store") | |
w.Header().Set("location", redirectUrl) | |
w.WriteHeader(http.StatusMovedPermanently) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment