summaryrefslogtreecommitdiffstats
path: root/src/qtmoduleupdater/main.go
blob: 5ef5d0d8c31781f3d08315a11b8820d1ffcb1854 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/****************************************************************************
**
** Copyright (C) 2016 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 fetchRef string
	flag.StringVar(&fetchRef, "fetch-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")
	flag.Parse()

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

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

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

	batch := &ModuleUpdateBatch{
		Product: product,
		Branch:  branch,
	}
	var err error

	err = batch.loadState()
	if os.IsNotExist(err) {
		err = batch.loadTodoList(fetchRef)
		if err != nil {
			return err
		}
	}

	if summaryOnly {
		batch.printSummary()
		return nil
	}

	batch.checkPendingModules()

	if err := batch.scheduleUpdates(pushUserName, manualStage); err != nil {
		return err
	}

	batch.printSummary()

	if !batch.isDone() {
		err = batch.saveState()
		if err != nil {
			return err
		}
	} else {
		os.Remove("state.json")

		if batch.FailedModuleCount == 0 {
			fmt.Println("Preparing qt5 update")
			if err = prepareQt5Update(product, batch.Branch, batch.Done, pushUserName, manualStage); err != nil {
				return fmt.Errorf("error preparing qt5 update: %s", err)
			}
		}
	}

	return nil
}

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