Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign commits with GitPython package #7

Merged
merged 30 commits into from
Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ae891f5
[#2] WIP: sign commits with GitPython package
josecelano Nov 19, 2021
d69c5ef
[#2] Rename exmmple to 03
josecelano Nov 19, 2021
df0afbf
[#2] Sign commit using GitPython but with pompt for passphrase
josecelano Nov 23, 2021
3eae028
[#2] Fix sign commit presetting passphrase
josecelano Nov 24, 2021
e857f9b
[#2] Add some links about signing commits
josecelano Nov 24, 2021
0865ae8
[#2] Link article about fingerprints and keygrips
josecelano Nov 24, 2021
5f6169b
[#2] Clean: move examples from code to doc
josecelano Nov 24, 2021
d677ee1
[#2] Clean: extract debug code into docs
josecelano Nov 24, 2021
adc23f5
[#2] Extract no signature example from signed commit example 03
josecelano Nov 24, 2021
7a91e3f
Disable scheduled workflows and enable them for PRs
josecelano Nov 24, 2021
2e22bcf
Add badge for new workflow to the README
josecelano Nov 24, 2021
46bfbf2
Fix workflow for example 05
josecelano Nov 24, 2021
25c928c
[#2] Clean code: extract functions
josecelano Nov 24, 2021
e0ae6ef
Aupdate python-gnupg
josecelano Nov 25, 2021
c4c8538
[#2] Calculate short key from fingerprint and keygrip from gpg command
josecelano Nov 25, 2021
a5c4e63
[#] Prepare example to run it on the workflow
josecelano Nov 25, 2021
2d2f4e0
[#2] Get commit author name and email from GPG key
josecelano Nov 25, 2021
d4cd952
[#2] Fix passing env vars
josecelano Nov 25, 2021
e1b2b54
[#2] Do not throw error if file dir exists
josecelano Nov 25, 2021
4d87441
[#2] Added example 03 explanation
josecelano Nov 26, 2021
a8e7c64
How to create a gpg subkey only for signing
josecelano Nov 26, 2021
87e43c8
Update article: subkeys
josecelano Nov 26, 2021
ced1413
[#] Fix grammar, typos
josecelano Nov 29, 2021
6bc8fe5
[#2] How to remove the sign capability from a primary GPG key
josecelano Nov 29, 2021
a59e3dc
[#2] Fix Python linting
josecelano Nov 29, 2021
b2a0021
[#2] Fix workflows: docker entrypoint changed
josecelano Nov 29, 2021
c0c6fbb
[#2] Add badge for all examples on README
josecelano Nov 29, 2021
cc5b9b5
Example 02: update datetime to Mon Nov 29 11:26:44 UTC 2021
josecelano-bot Nov 29, 2021
9f1d6bd
Merge branch 'main' into issue-2-sign-sing-gitpython
josecelano Nov 29, 2021
671053b
Example 02: update datetime to Mon Nov 29 11:27:22 UTC 2021
josecelano-bot Nov 29, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
INPUT_REPO_TOKEN=XXX
INPUT_REPO_TOKEN=XX
GPG_PRIVATE_KEY=-----BEGIN PGP PRIVATE KEY BLOCK-----\n\nlXX\n-----END PGP PRIVATE KEY BLOCK-----\n
PASSPHRASE=XX
7 changes: 2 additions & 5 deletions .github/workflows/example-01.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Test workflow for example 01

on:
schedule:
- cron: '0 0 * * *'
push:
branches: [ main ]
branches: [ main, issue-* ]

jobs:
import:
Expand All @@ -19,5 +17,4 @@ jobs:
run: |
docker run --rm \
--env INPUT_REPO_TOKEN=${{ secrets.GITHUB_TOKEN }} \
pygithub \
python 01_sign_commit_using_github_api.py
pygithub src/01_sign_commit_using_github_api.py
4 changes: 1 addition & 3 deletions .github/workflows/example-02.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Test workflow for example 02

on:
schedule:
- cron: '0 0 * * *'
push:
branches: [ main ]
branches: [ main, issue-* ]

jobs:
import:
Expand Down
24 changes: 24 additions & 0 deletions .github/workflows/example-03.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Test workflow for example 03

on:
push:
branches: [ main, issue-* ]

jobs:
import:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Build docker image
run: ./bin/build.sh

- name: Run example 03
run: |
docker run --rm \
--env GPG_PRIVATE_KEY="${{ secrets.GPG_PRIVATE_KEY }}" \
--env PASSPHRASE="${{ secrets.PASSPHRASE }}" \
--env MODE="workflow" \
--env REPO_DIR="/app" \
--volume "$(pwd):/app" \
pygithub src/03_sign_commit_using_the_gitpython_package.py
9 changes: 3 additions & 6 deletions .github/workflows/example-04.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
name: Test workflow for example 04

on:
schedule:
- cron: '0 0 * * *'
push:
branches: [ main ]
branches: [ main, issue-* ]

jobs:
import:
Expand All @@ -15,10 +13,9 @@ jobs:
- name: Build docker image
run: ./bin/build.sh

- name: Run example
- name: Run example 04
run: |
docker run --rm \
--env INPUT_REPO_TOKEN=${{ secrets.GITHUB_TOKEN }} \
--env INPUT_BRANCH=main \
pygithub \
python 04_sign_commit_with_multiple_files_using_github_api.py
pygithub src/04_sign_commit_with_multiple_files_using_github_api.py
20 changes: 20 additions & 0 deletions .github/workflows/example-05.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test workflow for example 05

on:
push:
branches: [ main, issue-* ]

jobs:
import:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Build docker image
run: ./bin/build.sh

- name: Run example 05
run: |
docker run --rm \
--volume $(pwd):/app \
pygithub src/05_commit_using_the_gitpython_package_without_signing.py
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.env
.env
private_key.pgp
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ FROM python:3.9
WORKDIR /app
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
COPY ./src /app
ENV GPG_TTY /dev/console
COPY ./gpg-agent.conf /root/.gnupg/
COPY ./src /app/src
RUN rm -rf /app/test
CMD ["python", "/app/src/main.py"]
CMD ["src/main.py"]
ENTRYPOINT ["python"]
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[![Test workflow for example 01](https://github.com/josecelano/pygithub/actions/workflows/example-01.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-01.yml) [![Test workflow for example 02](https://github.com/josecelano/pygithub/actions/workflows/example-02.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-02.yml) [![Test workflow for example 04](https://github.com/josecelano/pygithub/actions/workflows/example-04.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-04.yml)
# Git and GitHub API examples using Python

## Git and GitHub API examples using Python
[![Example 01](https://github.com/josecelano/pygithub/actions/workflows/example-01.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-01.yml) [![Example 02](https://github.com/josecelano/pygithub/actions/workflows/example-02.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-02.yml) [![Example 03](https://github.com/josecelano/pygithub/actions/workflows/example-03.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-03.yml) [![Example 04](https://github.com/josecelano/pygithub/actions/workflows/example-04.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-04.yml) [![Example 05](https://github.com/josecelano/pygithub/actions/workflows/example-05.yml/badge.svg)](https://github.com/josecelano/pygithub/actions/workflows/example-05.yml)

This repo contains some Python examples on using git and GitHub API.

## Articles

1. [Sign automatic commits in GitHub Actions](./docs/how_to_sign_automatic_commits_in_github_actions.md)
2. [How to create a single commit with multiple files using GitHub API](./docs/how_to_create_a_single_commit_with_multiple_files_using_github_api.md)
3. [How to sign commits using the GitPython package](./docs/how_to_sign_commits_using_the_gitpython_package.md)

## Build and run

Expand All @@ -27,6 +28,7 @@ Run examples:

```shell
./bin/run-01.sh
./bin/run-03.sh
./bin/run-04.sh
./bin/run-main.sh
```
Expand Down
2 changes: 1 addition & 1 deletion bin/run-01.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
docker run --rm -it \
--env-file .env \
--volume $(pwd):/app \
pygithub python src/01_sign_commit_using_github_api.py
pygithub src/01_sign_commit_using_github_api.py
8 changes: 8 additions & 0 deletions bin/run-03.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

docker run --rm -it \
--env-file .env \
--env MODE=test \
--env REPO_DIR=/app \
--volume "$(pwd):/app" \
pygithub src/03_sign_commit_using_the_gitpython_package.py
2 changes: 1 addition & 1 deletion bin/run-04.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ docker run --rm -it \
--env-file .env \
--env INPUT_BRANCH=main \
--volume $(pwd):/app \
pygithub python src/04_sign_commit_with_multiple_files_using_github_api.py
pygithub src/04_sign_commit_with_multiple_files_using_github_api.py
5 changes: 5 additions & 0 deletions bin/run-05.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

docker run --rm -it \
--volume $(pwd):/app \
pygithub src/05_commit_using_the_gitpython_package_without_signing.py
7 changes: 7 additions & 0 deletions bin/run-bash.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

docker run --rm -it \
--env-file .env \
--volume $(pwd):/app \
--entrypoint=/bin/bash \
pygithub
2 changes: 1 addition & 1 deletion data/example-02/latest_datetime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Mon Nov 29 11:10:14 UTC 2021
Mon Nov 29 11:27:22 UTC 2021
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ blob1 = remote_repo.create_git_blob(file_to_update_01_content, "utf-8")

We are using a [Python package](https://github.com/PyGithub/PyGithub) which is a wrapper for the GitHub REST API.

The only way to get the data is by using its SHA and we do not have the filename store anywhere yet. The way git stores the filename is by using another object in the database: the `tree`. The simplest `tree` object only contains a reference to one `blob` object. The `tree` is like a directory with the list of files where the contents of the files are the `blob` objects. We need to create a dir (`tree`) also for only one file because Git will add the file metadata there. The same object is used for one an multiple files. The `tree` can also contains another `tree`.
The only way to get the data is by using its SHA and we do not have the filename store anywhere yet. The way git stores the filename is by using another object in the database: the `tree`. The simplest `tree` object only contains a reference to one `blob` object. The `tree` is like a directory with the list of files where the contents of the files are the `blob` objects. We need to create a dir (`tree`) also for only one file because Git will add the file metadata there. The same object is used for one an multiple files. The `tree` can also contain another `tree`.

In order to create the `tree`, we need to take the state of the staging area. You first have to set up an index by staging some files with these commands:

Expand Down
Loading