This repository was archived by the owner on Jul 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandlers.go
More file actions
126 lines (107 loc) · 3.98 KB
/
handlers.go
File metadata and controls
126 lines (107 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* Copyright © 2022 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"reflect"
"github.com/atomist-skills/go-skill"
"github.com/atomist-skills/go-skill/util"
"github.com/google/go-github/v45/github"
"golang.org/x/oauth2"
"olympos.io/encoding/edn"
)
// TransactCommitSignature processed incoming Git pushes and transacts the commit signature
// as returned by GitHub
func TransactCommitSignature(ctx context.Context, req skill.RequestContext) skill.Status {
result := req.Event.Context.Subscription.Result[0]
commit := util.Decode[OnCommit](result[0])
gitCommit, err := getCommit(ctx, req, &commit)
if err != nil {
return skill.NewFailedStatus(fmt.Sprintf("Failed to obtain commit signature for %s", commit.Sha))
}
err = transactCommitSignature(ctx, req, commit, gitCommit)
if err != nil {
return skill.NewFailedStatus(fmt.Sprintf("Failed to transact signature for %s", commit.Sha))
}
return skill.NewCompletedStatus(fmt.Sprintf("Successfully transacted commit signature for %d commit", len(req.Event.Context.Subscription.Result)))
}
// LogCommitSignature handles new commit signature entities as they are transacted into
// the database and logs the signature
func LogCommitSignature(_ context.Context, req skill.RequestContext) skill.Status {
result := req.Event.Context.Subscription.Result[0]
commit := util.Decode[OnCommit](result[0])
signature := util.Decode[OnCommitSignature](result[1])
req.Log.Infof("Commit %s is signed and verified by: %s", commit.Sha, signature.Signature)
return skill.NewCompletedStatus("Detected signed and verified commit")
}
// LogWebhookBody logs incoming webhook bodies
func LogWebhookBody(_ context.Context, req skill.RequestContext) skill.Status {
body := req.Event.Context.Webhook.Request.Body
req.Log.Infof("Webhook body: %s", body)
return skill.NewCompletedStatus("Handled incoming webhook event")
}
// transactCommitSignature transact the commit signature facts
func transactCommitSignature(_ context.Context, req skill.RequestContext, commit OnCommit, gitCommit *github.RepositoryCommit) error {
var verified edn.Keyword
if *gitCommit.Commit.Verification.Verified {
verified = Verified
} else {
verified = NotVerified
}
var signature string
verification := *gitCommit.Commit.Verification
if !reflect.ValueOf(verification.Signature).IsNil() {
signature = *verification.Signature
}
err := req.NewTransaction().AddEntities(GitCommitSignatureEntity{
Commit: GitCommitEntity{
Sha: commit.Sha,
Repo: GitRepoEntity{
SourceId: commit.Repo.SourceId,
Url: commit.Repo.Org.Url,
},
Url: commit.Repo.Org.Url,
},
Signature: signature,
Status: verified,
Reason: *gitCommit.Commit.Verification.Reason,
}).Transact()
if err != nil {
return err
}
req.Log.Infof("Transacted commit signature for %s", commit.Sha)
return nil
}
// getCommit obtains commit information from GitHub
func getCommit(ctx context.Context, _ skill.RequestContext, commit *OnCommit) (*github.RepositoryCommit, error) {
var client *github.Client
if commit.Repo.Org.InstallationToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: commit.Repo.Org.InstallationToken},
)
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
} else {
client = github.NewClient(nil)
}
gitCommit, _, err := client.Repositories.GetCommit(ctx, commit.Repo.Org.Name, commit.Repo.Name, commit.Sha, nil)
if err != nil {
fmt.Println(err)
return nil, err
}
return gitCommit, err
}