-
Notifications
You must be signed in to change notification settings - Fork 12
/
run.py
60 lines (51 loc) · 2.16 KB
/
run.py
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
import argparse
import base64
import os
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Generate .npmrc file for Azure DevOps")
parser.add_argument("--organisation", dest="organisation", required=True, help="Your Azure organisation")
parser.add_argument("--project", dest="project", required=False, help="Your Azure project")
parser.add_argument("--registry", dest="registry", required=True, help="Your Azure registry")
parser.add_argument("--user", dest="user", required=True, help="Your Azure user")
parser.add_argument("--password", dest="password", required=True, help="Your Azure password")
parser.add_argument("--encode_password", dest="encode_password", default=False, help="Encode the given password to base64")
parser.add_argument("--email", dest="email", required=True, help="Your Azure email")
parser.add_argument("--scope", dest="scope", required=False, help="Your package scope")
return parser.parse_args()
def generate_url(args: argparse.Namespace):
if args.project is not None:
return f"pkgs.dev.azure.com/{args.organisation}/{args.project}/_packaging/{args.registry}/npm"
return f"pkgs.dev.azure.com/{args.organisation}/_packaging/{args.registry}/npm"
def generate_registry(args):
scope = ""
if args.scope is not None:
scope = f"@{args.scope}:"
return f"""{scope}registry=https://{generate_url(args)}/registry/
always-auth=true
"""
def encode_password(args):
if args.encode_password:
return base64.b64encode(args.password.encode('utf-8')).decode("utf-8")
return args.password
def generate_credentials(args):
url = generate_url(args)
password = encode_password(args)
return f"""; begin auth token
//{url}/registry/:username={args.user}
//{url}/registry/:_password="{password}"
//{url}/registry/:email={args.email}
//{url}/:username={args.user}
//{url}/:_password="{password}"
//{url}/:email={args.email}
; end auth token"""
def write_file(content):
path = os.path.join(os.environ["GITHUB_WORKSPACE"], ".npmrc")
with open(path, 'w') as f:
f.write(content)
def main():
args = parse_args()
content = generate_registry(args)
content += generate_credentials(args)
write_file(content)
if __name__ == "__main__":
main()