-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
139 lines (122 loc) · 4.77 KB
/
config.go
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
127
128
129
130
131
132
133
134
135
136
137
138
139
// maumirror - A GitHub repo mirroring system using webhooks.
// Copyright (C) 2021 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"os"
"sync"
"maunium.net/go/maulogger/v2"
)
type Config struct {
// Cloned repo storage directory.
DataDir string `yaml:"datadir"`
// HTTP server configuration.
Server struct {
// Endpoint for admin API (e.g. dynamically adding webhooks).
AdminEndpoint string `yaml:"admin_endpoint,omitempty"`
// Secret for accessing admin API.
AdminSecret string `yaml:"admin_secret,omitempty"`
// Endpoint for receiving webhooks.
WebhookEndpoint string `yaml:"webhook_endpoint"`
// Public URL where the webhook endpoint is accessible. Used for installing GitHub webhooks automatically.
WebhookPublicURL string `yaml:"webhook_public_url,omitempty"`
// Endpoint for receiving CI status from GitLab.
CIWebhookEndpoint string `yaml:"ci_webhook_endpoint"`
// Public URL where the CI webhook endpoint is accessible. Used for installing GitLab webhooks automatically.
CIWebhookPublicURL string `yaml:"ci_webhook_public_url,omitempty"`
// Whether or not to trust X-Forwarded-For headers for logging.
TrustForwardHeaders bool `yaml:"trust_forward_headers,omitempty"`
// IP and port where the server listens
Address string `yaml:"address"`
} `yaml:"server"`
// GitHub app credentials for mirroring CI status from GitLab back to GitHub using the Checks API.
GitHubApp struct {
// The numeric app ID.
ID int64 `yaml:"id"`
// RSA private key for the app
PrivateKey string `yaml:"private_key"`
} `yaml:"github_app"`
// Shell configuration
Shell struct {
// The command to start shells with
Command string `yaml:"command"`
// The arguments to pass to shells. The script is sent through stdin.
Args []string `yaml:"args"`
// Paths to scripts. If unset, will default to built-in handlers.
Scripts struct {
Push *Script `yaml:"push,omitempty"`
} `yaml:"scripts,omitempty"`
} `yaml:"shell"`
// Repository configuration
Repositories map[string]*Repository `yaml:"repositories"`
// Reverse repository configuration for mirroring CI status back to GitHub.
CIRepositories map[int64]*CIRepository `yaml:"ci_repositories"`
}
type Script struct {
Path string
Data string
}
func (script *Script) UnmarshalYAML(unmarshal func(interface{}) error) error {
if err := unmarshal(&script.Path); err != nil {
return err
}
if len(script.Path) > 0 {
if fileData, err := os.ReadFile(script.Path); err != nil {
return err
} else {
script.Data = string(fileData)
}
}
return nil
}
func (script *Script) MarshalYAML() (interface{}, error) {
if len(script.Path) > 0 {
if err := os.WriteFile(script.Path, []byte(script.Data), 0644); err != nil {
return nil, err
}
}
return script.Path, nil
}
type Repository struct {
// Repository source URL. Optional, defaults to https.
Source string `yaml:"source,omitempty" json:"source"`
// Webhook auth secret. Request signature is not checked if secret is not configured.
Secret string `yaml:"secret,omitempty" json:"secret"`
// Target repo URL. Required.
Target string `yaml:"target" json:"target"`
// Path to SSH key for pushing repo.
PushKey string `yaml:"push_key,omitempty" json:"push_key"`
// Path to SSH key for pulling repo. If set, source repo URL defaults to ssh instead of https.
PullKey string `yaml:"pull_key,omitempty" json:"pull_key"`
// GitLab CI webhook auth secret.
CISecret string `yaml:"ci_secret,omitempty" json:"ci_secret"`
// GitHub installation ID for mirroring CI status
GHInstallationID int `yaml:"gh_installation_id,omitempty" json:"gh_installation_id"`
Name string `yaml:"-" json:"-"`
Log maulogger.Logger `yaml:"-" json:"-"`
}
type CIRepository struct {
// Webhook auth secret.
Secret string `yaml:"secret,omitempty" json:"secret"`
// Target GitHub repo owner and name.
Owner string `yaml:"owner" json:"owner"`
Name string `yaml:"repo" json:"repo"`
// GitHub app installation ID. This will be filled automatically if left empty.
InstallationID int64 `yaml:"installation_id" json:"installation_id"`
plock *PartitionLocker
mapLock sync.RWMutex
checkSuiteIDs map[string]int64
checkRunIDs map[int64]int64
}