GitHub: ファイル削除

GitHub: ファイル削除

GitHub: Delete File

この工程は、GitHub の指定したリポジトリのファイルを削除します。

Basic Configs
工程名
メモ
Auto Step icon
Configs for this Auto Step
conf_Auth
C1: 個人用アクセストークン(fine-grained) *
conf_RepositoryUrl
C2: リポジトリの URL (https://github.com/{owner}/{repository}) *
conf_Path
C3: ファイルパス *#{EL}
conf_Branch
C4: ブランチ(空白の場合、デフォルトのブランチ)
conf_Hash
C5: 削除するファイルの SHA *
conf_Message
C6: コミットメッセージ *#{EL}

Notes

  • 個人用アクセストークン(fine-grained)の作成方法については、GitHub のドキュメントを参照してください
    • ファイルの削除には、Repository permissions > Contents > Read and Write のアクセス許可が必要です
  • ファイルの削除には、ファイルの SHA が必要になります

Capture

See Also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • github-file-delete.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます


const API_VERSION = '2022-11-28';
const API_VERSION_HEADER = 'X-GitHub-Api-Version';

const main = () => {
    ////// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const auth = configs.getObject('conf_Auth');
    const ownerAndRepo = retrieveOwnerAndRepo();
    const path = retrieveConfTextFieldValue("conf_Path", "File Path");
    const branch = retrieveSelectValue("conf_Branch");
    const hash = retrieveSelectValue("conf_Hash", "SHA of the file");
    const message = retrieveConfTextFieldValue("conf_Message", "Commit Message");

    ////// == 演算 / Calculating ==
    deleteFile(auth, ownerAndRepo, path, branch, hash, message);
}

/**
  * config からリポジトリの URL を読み出し、API に必要な文字列を取り出す
  * @return {String} ownerAndRepo
  */
const retrieveOwnerAndRepo = () => {
    const repositoryUrl = retrieveSelectValue("conf_RepositoryUrl");

    if (repositoryUrl === null) {
        throw new Error("Repository's URL is blank.");
    }

    const regExp = /^https:\/\/github\.com\/([a-zA-Z0-9-_]+\/[a-zA-Z0-9-_]+)$/;
    const found = repositoryUrl.match(regExp);
    if (found === null) {
        throw new Error("Invalid repository URL.");
    }
    const ownerAndRepo = found[1];
    return ownerAndRepo;
}

/**
 * config からform-type="TEXTFIELD" "TEXTAREA" の値を読み出す
 * 空の場合はエラー
 * @param {String} confName config 名
 * @param {String} label エラーメッセージ用のラベル
 * @return {String}
 */
const retrieveConfTextFieldValue = (configName, label) => {
    const value = configs.get(configName);
    if (value === '') {
        throw new Error(`${label} is blank.`);
    }
    return value;
};

/**
  * form-type="SELECT" 
  * の設定値を読み出す
  * conf_Hash の値が空の場合はエラー
  * @param {String} confName 設定名
  * @param {String} label エラー出力用ラベル
  * @return {String} value 設定値
  */
function retrieveSelectValue(confName, label) {
    let value = '';
    const dataDef = configs.getObject(confName);
    if (dataDef === null) {
        value = configs.get(confName);
    } else {
        value = engine.findData(dataDef);
    }
    if ((confName === "conf_Hash") && (value === '' || value === null)) {
        throw new Error(`${label} is blank.`);
    }
    return value;
}

/**
 * ファイルを 削除する
 * @param {AuthSettingWrapper} auth HTTP 認証設定
 * @param {String} ownerAndRepo {owner}/{repo}
 * @param {String} path 削除するファイルのパス
 * @param {String} branch ブランチ
 * @param {String} hash 削除するファイルのハッシュ値
 * @param {String} message コミットメッセージ
 */
const deleteFile = (auth, ownerAndRepo, path, branch, hash, message) => {

    const url = `https://api.github.com/repos/${ownerAndRepo}/contents/${path}`;

    let request = httpClient.begin()
        .header(API_VERSION_HEADER, API_VERSION)
        .header("accept", "application/vnd.github+json")
        .authSetting(auth)
        // DELETE メソッドにリクエストボディをセットできないため、クエリパラメータで代用
        .queryParam("sha", `${hash}`)
        .queryParam("message", `${message}`);

    if (branch !== null && branch !== '') {
        request = request.queryParam("branch", branch);
    }
    const response = request.delete(url);

    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();

    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to delete a file. status: ${status}`);
    }
    engine.log(`Successfully deleted a file. File Path: ${path}`);

};

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む