MCPcopy
hub / github.com/ossf/scorecard / scriptHandler

Function scriptHandler

cron/internal/webhook/main.go:42–80  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

40}
41
42func scriptHandler(w http.ResponseWriter, r *http.Request) {
43 switch r.Method {
44 case http.MethodPost:
45 const maxBodySize = 1024 // must be larger than protojson serialized [data.ShardMetadata]
46 r.Body = http.MaxBytesReader(w, r.Body, maxBodySize)
47 jsonBytes, err := io.ReadAll(r.Body)
48 if err != nil {
49 http.Error(w, fmt.Sprintf("unable to read request body: %v", err),
50 http.StatusInternalServerError)
51 return
52 }
53 var metadata data.ShardMetadata
54 if err := protojson.Unmarshal(jsonBytes, &metadata); err != nil {
55 http.Error(w, fmt.Sprintf("unable to parse request as ShardMetadata proto: %v", err),
56 http.StatusBadRequest)
57 return
58 }
59 authn, err := google.NewEnvAuthenticator(r.Context())
60 if err != nil {
61 http.Error(w, fmt.Sprintf("error in NewEnvAuthenticator: %v", err), http.StatusInternalServerError)
62 return
63 }
64 for _, image := range images {
65 if err := crane.Tag(
66 fmt.Sprintf("%s:%s", image, metadata.GetCommitSha()), stableTag,
67 crane.WithAuth(authn)); err != nil {
68 http.Error(w, fmt.Sprintf("crane.Tag: %v", err), http.StatusInternalServerError)
69 return
70 }
71 }
72 w.WriteHeader(http.StatusOK)
73 if _, err := w.Write([]byte("successfully tagged images\n")); err != nil {
74 log.Panic(fmt.Errorf("error during Write: %w", err))
75 }
76 default:
77 http.Error(w, "only POST method is allowed", http.StatusMethodNotAllowed)
78 return
79 }
80}
81
82func main() {
83 http.HandleFunc("/", scriptHandler)

Callers

nothing calls this directly

Calls 3

GetCommitShaMethod · 0.95
WriteMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected