about summary refs log tree commit diff
path: root/ops/sync-gcsr/main.go
blob: 4841b62619342ca4f8b167eb40ee8da54efc6b3e (plain) (blame)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
//
// sync-gcsr implements a small utility that periodically mirrors a
// remote Google Cloud Source Repository to a local file path.
//
// This utility is also responsible for triggering depot builds on
// builds.sr.ht if a change is detected on the master branch.
package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"os"
	"time"
	"bytes"

	git "gopkg.in/src-d/go-git.v4"
	"gopkg.in/src-d/go-git.v4/plumbing"
	githttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)

// Path to the build manifest, added by Nix at compile time.
var BuildManifest string

// Represents a builds.sr.ht build object as described on
// https://man.sr.ht/builds.sr.ht/api.md
type Build struct {
	Manifest string   `json:"manifest"`
	Note     string   `json:"note"`
	Tags     []string `json:"tags"`
}

func EnvOr(key, def string) string {
	v := os.Getenv(key)
	if v == "" {
		return def
	}

	return v
}

// Trigger a build of master on builds.sr.ht
func triggerBuild(commit string) {
	manifest, err := ioutil.ReadFile(BuildManifest)
	if err != nil {
		log.Fatalln("[ERROR] failed to read sr.ht build manifest:", err)
	}

	build := Build{
		Manifest: string(manifest),
		Note:     fmt.Sprintf("Build of 'master' at '%s'", commit),
		Tags: []string{
			"tazjins-depot", "master",
		},
	}

	body, _ := json.Marshal(build)
	reader := ioutil.NopCloser(bytes.NewReader(body))

	req, err := http.NewRequest("POST", "https://builds.sr.ht/api/jobs", reader)
	if err != nil {
		log.Fatalln("[ERROR] failed to create an HTTP request:", err)
	}

	req.Header.Add("Authorization", fmt.Sprintf("token %s", os.Getenv("SRHT_TOKEN")))
	req.Header.Add("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		// This might indicate a temporary error on the SourceHut side, do
		// not fail the whole program.
		log.Println("failed to send builds.sr.ht request:", err)
		return
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		respBody, err := ioutil.ReadAll(resp.Body)
		log.Printf("received non-success response from builds.sr.ht: %s (%v)[%s]", respBody, resp.Status, err)
	}
}

// ensure that all remote branches exist locally & are up to date.
func updateBranches(auth *githttp.BasicAuth, repo *git.Repository) error {
	origin, err := repo.Remote("origin")
	if err != nil {
		return err
	}

	refs, err := origin.List(&git.ListOptions{
		Auth: auth,
	})
	if err != nil {
		return err
	}

	for _, ref := range refs {
		if !ref.Name().IsBranch() || ref.Type() != plumbing.HashReference {
			continue
		}

		name := plumbing.NewBranchReferenceName(ref.Name().Short())

		if current, err := repo.Storer.Reference(name); err == nil {
			// Determine whether the reference has changed to skip
			// unnecessary modifications.
			if current.Hash() == ref.Hash() {
				continue
			}
		}

		branch := plumbing.NewHashReference(name, ref.Hash())

		err := repo.Storer.SetReference(branch)
		if err != nil {
			return err
		}

		if ref.Name().Short() == "master" {
			go triggerBuild(ref.Hash().String())
		}

		log.Println("Updated branch", ref.Name().String())
	}

	return nil
}

func updateRepo(auth *githttp.BasicAuth, repo *git.Repository, opts *git.FetchOptions) error {
	err := repo.Fetch(opts)

	if err == git.NoErrAlreadyUpToDate {
		// nothing to do ...
		return nil
	} else if err != nil {
		return err
	}

	log.Println("Fetched updates from remote, updating local branches")
	return updateBranches(auth, repo)
}

func cloneRepo(dest, project, repo string, auth *githttp.BasicAuth) (*git.Repository, error) {
	var cloneOpts = git.CloneOptions{
		Auth: auth,
		URL:  fmt.Sprintf("https://source.developers.google.com/p/%s/r/%s", project, repo),
	}

	handle, err := git.PlainClone(dest, true, &cloneOpts)

	if err == git.ErrRepositoryAlreadyExists {
		handle, err = git.PlainOpen(dest)
	}

	return handle, updateBranches(auth, handle)
}

func main() {
	dest := EnvOr("SYNC_DEST", "/git/depot")
	project := EnvOr("SYNC_PROJECT", "tazjins-infrastructure")
	repo := EnvOr("SYNC_REPO", "depot")
	user := os.Getenv("SYNC_USER")
	pass := os.Getenv("SYNC_PASS")

	log.Printf("Syncing repository '%s/%s' to destination '%s'", project, repo, dest)

	var auth *githttp.BasicAuth
	if user != "" && pass != "" {
		auth = &githttp.BasicAuth{
			Username: user,
			Password: pass,
		}
		log.Println("Enabling basic authentication as user", user)
	}

	handle, err := cloneRepo(dest, project, repo, auth)

	if err != nil {
		log.Fatalf("Failed to clone repository: %s", err)
	} else {
		log.Println("Initiating update loop")
	}

	fetchOpts := git.FetchOptions{
		Auth:  auth,
		Force: true,
	}

	for {
		if err = updateRepo(auth, handle, &fetchOpts); err != nil {
			log.Fatalf("Failed to pull updated repository: %s", err)
		}

		time.Sleep(10 * time.Second)
	}
}