summaryrefslogtreecommitdiffstats
path: root/src/qtmoduleupdater/main.go
blob: b1058505f2c8d0d506f5755783c3459cd1040bad (plain)
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 (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the repo tools module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

func setupEnvironmentForSubmoduleUpdateBot() (username string, err error) {
	submoduleUpdateBotKeyPath := "submodule_update_bot_key_rsa"
	if _, err = os.Stat(submoduleUpdateBotKeyPath); os.IsNotExist(err) {
		err = fmt.Errorf("cannot locate submodule update bot SSH key file. Please copy it from the coin secrets repo into the current directory")
		return
	}

	os.Setenv("GIT_SSH_COMMAND", fmt.Sprintf("ssh -i %s", submoduleUpdateBotKeyPath))
	os.Setenv("GIT_SSH_USER", "qt_submodule_update_bot")

	os.Setenv("GIT_AUTHOR_NAME", "Qt Submodule Update Bot")
	os.Setenv("GIT_COMMITTER_NAME", "Qt Submodule Update Bot")
	os.Setenv("GIT_AUTHOR_EMAIL", "qt_submodule_update_bot@qt-project.org")
	os.Setenv("GIT_COMMITTER_EMAIL", "qt_submodule_update_bot@qt-project.org")

	username = "qt_submodule_update_bot"
	return
}

func appMain() error {
	var product string
	flag.StringVar(&product, "product", "qt/qt5" /*default*/, "Product repository to use as reference and push completed updates to")
	stageAsBot := false
	flag.BoolVar(&stageAsBot, "stage-as-bot", false /*default*/, "Push changes to Gerrit using the submodule update bot account")
	var branch string
	flag.StringVar(&branch, "branch", "", "Branch to update")
	var productRef string
	flag.StringVar(&productRef, "product-ref", "", "Git ref in qt5 to use as basis for a new round of updates")
	manualStage := false
	flag.BoolVar(&manualStage, "manual-stage", false /*default*/, "Do not stage changes automatically")
	summaryOnly := false
	flag.BoolVar(&summaryOnly, "summarize", false /*default*/, "")
	verbose := false
	flag.BoolVar(&verbose, "verbose", false /*default*/, "Enable verbose logging output")
	autorun := false
	flag.BoolVar(&autorun, "autorun", false, "Run automatically by reading settings from autorun.json")
	flag.Parse()

	if !verbose {
		oldWriter := log.Writer()
		defer log.SetOutput(oldWriter)
		log.SetOutput(ioutil.Discard)
	}

	if autorun {
		stageAsBot = true
	}

	gerrit := &gerritInstance{}
	gerrit.disableStaging = manualStage

	if stageAsBot {
		var err error
		gerrit.pushUserName, err = setupEnvironmentForSubmoduleUpdateBot()
		if err != nil {
			return fmt.Errorf("error preparing environment to work as submodule-update user: %s", err)
		}
	}

	if autorun {
		autorun := &AutoRunSettings{}
		autorun.load()
		autorun.runUpdates(gerrit)
		return nil
	}

	if branch == "" {
		return fmt.Errorf("missing branch. Please specify -branch=<name of branch>")
	}

	batch, err := newModuleUpdateBatch(product, branch, productRef)
	if err != nil {
		return err
	}

	if summaryOnly {
		batch.printSummary()
		return nil
	}

	return batch.runOneIteration(gerrit)
}

func main() {
	err := appMain()
	if err != nil {
		log.Fatalf("Error: %s\n", err)
	}
}