Merge ~alfonsosanchezbeato/snappy-hwe-snaps/+git/jenkins-jobs:fix-team-permission into ~snappy-hwe-team/snappy-hwe-snaps/+git/jenkins-jobs:master

Proposed by Alfonso Sanchez-Beato
Status: Merged
Approved by: Simon Fels
Approved revision: aca716e8f04d7bbbca728325add1aefc29c843cd
Merged at revision: a68265ee669b59bc0c1acb6dba6538a04c3abbfd
Proposed branch: ~alfonsosanchezbeato/snappy-hwe-snaps/+git/jenkins-jobs:fix-team-permission
Merge into: ~snappy-hwe-team/snappy-hwe-snaps/+git/jenkins-jobs:master
Diff against target: 98 lines (+54/-4)
2 files modified
jobs/snap/snap-trigger-ci.sh (+4/-3)
tools/trigger-ci.py (+50/-1)
Reviewer Review Type Date Requested Status
System Enablement Bot continuous-integration Approve
Simon Fels Approve
Review via email: mp+326063@code.launchpad.net

Description of the change

Check for user belonging to project team

Check for user belonging to project team before triggering build.
Previously we were checking against a fixed team regardless of the
project.

To post a comment you must log in.
Revision history for this message
Simon Fels (morphis) wrote :

LGTM! Thanks for fixing this!

review: Approve
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/jobs/snap/snap-trigger-ci.sh b/jobs/snap/snap-trigger-ci.sh
2index c71d5df..b86724d 100644
3--- a/jobs/snap/snap-trigger-ci.sh
4+++ b/jobs/snap/snap-trigger-ci.sh
5@@ -23,6 +23,7 @@ if [ "$TRIGGER_CI" = False ]; then
6 exit 0
7 fi
8
9-exec $BUILD_SCRIPTS/tools/trigger-ci.py \
10- -p $LAUNCHPAD_PROJECT \
11- -j $SNAP_BUILD_JOB
12+exec "$BUILD_SCRIPTS"/tools/trigger-ci.py \
13+ -p "$LAUNCHPAD_PROJECT" \
14+ -j "$SNAP_BUILD_JOB" \
15+ -t "$LAUNCHPAD_TEAM"
16diff --git a/tools/trigger-ci.py b/tools/trigger-ci.py
17index 45e99af..d50c021 100755
18--- a/tools/trigger-ci.py
19+++ b/tools/trigger-ci.py
20@@ -33,6 +33,8 @@ parser.add_argument('-p', '--project', required=True,
21 help="Launchpad project to check for new merge-proposals")
22 parser.add_argument('-j', '--job', required=True,
23 help="Jenkins job to trigger")
24+parser.add_argument('-t', '--team', required=True,
25+ help="Launchpad team for the project")
26
27 args = vars(parser.parse_args())
28
29@@ -42,6 +44,7 @@ credential_store_path = se_utils.get_config_option('credential_store_path')
30 launchpad = se_utils.get_launchpad(None, credential_store_path, lp_app, lp_env)
31
32 project = launchpad.projects[args['project']]
33+team = args['team']
34
35 proposals = project.getMergeProposals()
36
37@@ -167,6 +170,52 @@ def testing_in_progress(mp, jenkins_job):
38 print('Failed to check if MP is already building')
39 return False
40
41+# Copied over from lp:jenkins-launchpad-plugin
42+def users_in_team(users, team):
43+ """Determine whether any of these users are in the supplied team.
44+
45+ :param users: The users which may be members of the supplied team.
46+ :param team: The team which users may be part of.
47+ :return: True if any of the users are members of the team, otherwise
48+ False.
49+ """
50+ for member in team.participants:
51+ if member in users:
52+ return True
53+ else:
54+ return False
55+
56+# Copied over from lp:jenkins-launchpad-plugin and slightly modified
57+def users_allowed_to_trigger_jobs(lp_users, allowed_people):
58+ """Returns if an of the given users is allowed to run jobs on jenkins.
59+
60+ This is to avoid random people to run jobs on our internal infrastructure.
61+ A user is allowed if they are either directly in the ALLOWED_USERS list or
62+ are member of a team in that list.
63+
64+ :param lp_users: launchpad user handles
65+ """
66+ if len(lp_users) == 0:
67+ return False
68+ for lp_user in lp_users:
69+ if lp_user.name in allowed_people:
70+ return True
71+ lp = lp_users[0]._root
72+ for allowed in allowed_people:
73+ try:
74+ allowed_person = lp.people[allowed]
75+ except KeyError:
76+ logger.warn('User {} from the allowed_users list is not in '
77+ 'launchpad!'.format(allowed))
78+ continue
79+ if not allowed_person.is_team:
80+ continue
81+ if users_in_team(lp_users, allowed_person):
82+ return True
83+ logger.debug('Users "' + ', '.join(u.name for u in lp_users) +
84+ '" not allowed to trigger jobs')
85+ return False
86+
87 project_blacklist = []
88
89 for proposal in proposals:
90@@ -186,7 +235,7 @@ for proposal in proposals:
91 print "Ignoring %s" % proposal.web_link
92 continue
93
94- if not launchpadutils.users_allowed_to_trigger_jobs([proposal.registrant]):
95+ if not users_allowed_to_trigger_jobs([proposal.registrant], [team]):
96 continue
97
98 if latest_candidate_validated(launchpad_user, proposal):

Subscribers

People subscribed via source and target branches