Skip to content

Commit

Permalink
Create PR when Application Services makes a release (#8)
Browse files Browse the repository at this point in the history
* Adds automation and github actions
  • Loading branch information
Tarik Eshaq authored Oct 13, 2021
1 parent de4b054 commit b530ffb
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 1 deletion.
89 changes: 89 additions & 0 deletions .github/workflows/update-as.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Create a PR for release with the newest A-S version available

# Controls when the workflow will run
on:
schedule:
# Runs every minute of the 3pm UTC hour of each day
- cron: '* 15 * * *'

jobs:
release-pr:
runs-on: macos-latest
strategy:
max-parallel: 1
matrix:
python-version: [3.7]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
# Checks out the rust-components-swift repository
- uses: actions/checkout@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
submodules: 'recursive'
- name: Setup Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -r ./automation/requirements.txt
# Gets the new application services tag, and stores it in
# the environment variable `AS_VERSION` to be used later
- name: Get new A-S tag
run: |
echo "AS_VERSION=`python automation/read_as_tag.py`" >> $GITHUB_ENV
# Check if a branch was already created on github
# and set an environment variable `ALREADY_CREATED` to
# prevent us from creating multiple PRs/branches
- name: Check if job already ran with this release
run: |
echo "ALREADY_CREATED=$(python automation/is_already_updated.py)" >> $GITHUB_ENV
- name: Setup git information
if: env.ALREADY_CREATED == 'false'
run: |
git config user.email "[email protected]"
git config user.name "Firefox Sync Engineering"
git checkout -b update-as-to-${{ env.AS_VERSION }}
# Update the submodule if we haven't ran those jobs yet
# note that we commit here
- name: Update the submodule to the release application services
if: env.ALREADY_CREATED == 'false'
run: |
cd external/application-services
git fetch --all --tags
git checkout $AS_VERSION
cd ../..
git commit -am "Updates application services submodule to $AS_VERSION"
- name: Update Package.swift with new xcframework URL and checksum
if: env.ALREADY_CREATED == 'false'
run: |
python automation/update_package_swift.py
git commit -am "Updates Package.swift with $AS_VERSION release"
- name: Install Rust for the make_tag script
if: env.ALREADY_CREATED == 'false'
run: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
- name: Install swiftformat to format the uniffi generated bindings
if: env.ALREADY_CREATED == 'false'
run: |
brew install swiftformat
# We strip away the `v` in the AS Version and run the make_tag
# script, which will generate all the code that needs to be generated
# Note that the tag argument isn't useful here, since we will
# have to cut a release with a new tag (possibly with the same name)
# after the PR is merged
- name: Runs make_tag script
if: env.ALREADY_CREATED == 'false'
run: |
./make_tag.sh ${AS_VERSION:1}
- name: Create Pull Request
if: env.ALREADY_CREATED == 'false'
run: |
git push origin update-as-to-${{ env.AS_VERSION }} -f
gh pr create --title "Auto update with latest AS Release ${{ env.AS_VERSION}}" --body "Updates the swift package with the latest Application Services Release" --base "main"
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ let package = Package(
name: "MozillaRustComponents",
//
// For release artifacts, reference the MozillaRustComponents as a URL with checksum.
//
// IMPORTANT: The checksum has to be on the line directly after the `url`
// this is important for our release script so that all values are updated correctly
url: "https://117909-129966583-gh.circle-artifacts.com/0/dist/MozillaRustComponents.xcframework.zip",
checksum: "92a66ee992a3848dae8319be996833120e9a86f2bc08354d181b81197bf79e4e"

Expand Down
45 changes: 45 additions & 0 deletions automation/is_already_updated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import os
from github import Github

GITHUB_REPO = "mozilla/rust-components-swift"
XC_FRAMEWORK_NAME = "MozillaRustComponents.xcframework.zip"
github_access_token = os.getenv("GITHUB_TOKEN")
as_version = os.getenv("AS_VERSION")
PACKAGE_SWIFT = "Package.swift"

def is_already_at_version():
with open(PACKAGE_SWIFT) as fp:
line = fp.readline()
while line:
line = fp.readline()

# If this is the line that has the URL to
# the xcframework
if XC_FRAMEWORK_NAME in line:
return as_version in line
return False

def is_branch_created():
g = Github(github_access_token)
repo = g.get_repo(GITHUB_REPO)

for branch in repo.get_branches():
if f"update-as-to-{as_version}" in branch.name:
return True
return False

def main():
'''
Prints to stdout if rust-components-swift has already
been updated to latest application services or
if a PR has already been created
'''
if not github_access_token or not as_version:
exit(1)
if is_already_at_version() or is_branch_created():
print("true")
else:
print("false")

if __name__ == '__main__':
main()
25 changes: 25 additions & 0 deletions automation/read_as_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
from github import Github

GITHUB_REPO = "mozilla/application-services"
github_access_token = os.getenv("GITHUB_TOKEN")

def get_latest_as_version():
g = Github(github_access_token)
repo = g.get_repo(GITHUB_REPO)

latest_release = repo.get_releases()[0]
return (str(latest_release.tag_name))

def main():
'''
Gets the latest application services
version and prints it to stdout
'''
if not github_access_token:
exit(1)
as_repo_tag = get_latest_as_version()
print(as_repo_tag)

if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions automation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
pygithub
71 changes: 71 additions & 0 deletions automation/update_package_swift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
from github import Github
import hashlib
import requests

GITHUB_REPO = "mozilla/application-services"
XC_FRAMEWORK_NAME = "MozillaRustComponents.xcframework.zip"
PACKAGE_SWIFT = "Package.swift"
github_access_token = os.getenv("GITHUB_TOKEN")
as_version = os.getenv("AS_VERSION")

def get_xcframework_artifact():
g = Github(github_access_token)
repo = g.get_repo(GITHUB_REPO)

release = repo.get_release(as_version)
for asset in release.get_assets():
if asset.name == XC_FRAMEWORK_NAME:
return asset.browser_download_url

def compute_checksum(xc_framework_url):
req = requests.get(xc_framework_url)
return hashlib.sha256(req.content).hexdigest()

def update_package_swift(xc_framework_url, checksum):
old_url = ''
old_checksum = ''
with open(PACKAGE_SWIFT) as fp:
line = fp.readline()
while line:
line = fp.readline()

# If this is the line that has the URL to
# the xcframework
if XC_FRAMEWORK_NAME in line:
url_start = line.find('"') + 1
url_end = line.find('"', url_start)
old_url = line[url_start:url_end]
# NOTE: We assume that the next line is the
# checksum, there is a note in Package.swift
# to make sure we don't change that
line = fp.readline()
checksum_start = line.find('"') + 1
checksum_end = line.find('"', checksum_start)
old_checksum = line[checksum_start:checksum_end]
break
file = open(PACKAGE_SWIFT, "r+")
data = file.read()
data = data.replace(old_url, xc_framework_url)
data = data.replace(old_checksum, checksum)
file.close()
file = open(PACKAGE_SWIFT, "wt")
file.write(data)
file.close()


def main():
'''
Updates `Package.swift` with the latest
xcframework
'''
if not github_access_token or not as_version:
exit(1)
xc_framework_url = get_xcframework_artifact()
checksum = compute_checksum(xc_framework_url)
update_package_swift(xc_framework_url, checksum)



if __name__ == '__main__':
main()

0 comments on commit b530ffb

Please sign in to comment.